Skip to content

Commit 916d27e

Browse files
committed
libiio v1: FMComms5 ported, multichip sync in M-code; emu tests for ADRV9009, AD9371, ADRV9002, FMComms5
adi.FMComms5.Rx/Tx no longer call libiio or libad9361 through calllib. The device of the second AD9361 is looked up with getDev, both AD9361 get their sampling rate with setBBRate (ad9361_set_bb_rate), and ad9361_fmcomms5_multichip_sync of libad9361-iio is re-implemented in adi.FMComms5.Base with the same attribute and register accesses in the same order. In adi.FMComms5.Tx the attenuations of the second AD9361 are written to that AD9361 at setup (the device was passed in the place of the tolerance), and RFPortSelectChipB sets its own property and the port of the transmitter. adi.ADRV9002: the stream file of a custom profile is written to stream_config as bytes. As text the write is refused by the bindings (the file has zero bytes). A failed read of ensm_mode is reported with its status. Tests on the emu backend, 84 to 121 test points: frames in and out for every receiver and transmitter class, the attributes that setup writes and the tunable ones for ADRV9009, AD9371, ADRV9002 and FMComms5, profile loading, the ADRV9002 with one DMA per channel, and the access sequence of the multichip sync against the C source of libad9361-iio (test/emu/ad9361_mcs). Context files from pyadi-iio; adrv9009.xml is put together from adrv9008-1.xml and adrv9008-2.xml (test/emu/make_adrv9009_xml.py). Signed-off-by: Travis F. Collins <travis.collins@analog.com>
1 parent d45e938 commit 916d27e

27 files changed

Lines changed: 1970 additions & 56 deletions

‎+adi/+ADRV9002/Base.m‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,23 @@ function writeProfileFile(obj)
301301
% Wrap update in read writes since once profiles are loaded
302302
% some attributes get lost
303303
% state = savePartState(obj);
304-
stream_data_str = fileread(obj.CustomStreamFileName);
305-
obj.setDeviceAttributeRAW('stream_config',stream_data_str);
304+
% The stream file is a binary image. Its bytes are written
305+
% as they are: as text it would end at its first zero byte,
306+
% which is the third byte of such a file.
307+
fid = fopen(obj.CustomStreamFileName,'r');
308+
if fid < 0
309+
error('adi:ADRV9002:noStreamFile', ...
310+
'Cannot open the stream file %s', obj.CustomStreamFileName);
311+
end
312+
stream_data = fread(fid,[1 Inf],'*uint8');
313+
fclose(fid);
314+
phydev = getDev(obj, obj.phyDevName);
315+
bytes = iio_device_attr_write_raw(obj,phydev,'stream_config',stream_data);
316+
if bytes ~= numel(stream_data)
317+
cstatus(obj,min(bytes,-1),sprintf( ...
318+
'Attribute write failed for : stream_config (%d bytes of %d written)', ...
319+
max(bytes,0),numel(stream_data)));
320+
end
306321
profle_data_str = fileread(obj.CustomProfileFileName);
307322
obj.setDeviceAttributeRAW('profile_config',profle_data_str);
308323
% returnPartState(obj,state)

‎+adi/+ADRV9002/Rx.m‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,11 @@ function setupInit(obj)
573573
if status ~= 0
574574
error("Cannot find channel voltage1")
575575
end
576-
[status, rValue] = iio_channel_attr_read(obj,chanPtr,'ensm_mode',1024);
576+
status = iio_channel_attr_read(obj,chanPtr,'ensm_mode',1024);
577577
if status == -19
578578
channelsAval = 1;
579579
elseif status < 0
580-
cstatus(obj,rValue,'Attribute read failed for : ensm_mode');
580+
cstatus(obj,status,'Attribute read failed for : ensm_mode');
581581
else
582582
channelsAval = 2;
583583
end

‎+adi/+ADRV9002/Tx.m‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,11 @@ function setupInit(obj)
367367
if status ~= 0
368368
error("Cannot find channel voltage1")
369369
end
370-
[status, rValue] = iio_channel_attr_read(obj,chanPtr,'ensm_mode',1024);
370+
status = iio_channel_attr_read(obj,chanPtr,'ensm_mode',1024);
371371
if status == -19
372372
channelsAval = 1;
373373
elseif status < 0
374-
cstatus(obj,rValue,'Attribute read failed for : ensm_mode');
374+
cstatus(obj,status,'Attribute read failed for : ensm_mode');
375375
else
376376
channelsAval = 2;
377377
end

‎+adi/+FMComms5/Base.m‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,134 @@ function writeFilterFileFMComms5ChipB(obj)
3535
obj.setAttributeRAW('voltage0','filter_fir_en','1',false,obj.iioDevPHYChipB);
3636
end
3737

38+
function setupChipB(obj)
39+
% Device handle of the second AD9361. The first one is
40+
% looked up by setupLibad9361 of adi.AD9361.Base.
41+
obj.iioDevPHYChipB = getDev(obj,'ad9361-phy-B');
42+
end
43+
44+
function setBBRateChipB(obj, rate)
45+
% setBBRate of adi.AD9361.Base for the second AD9361:
46+
% ad9361_set_bb_rate of libad9361-iio. A step that fails is
47+
% an error.
48+
status = ad9361SetBBRate(obj, obj.iioDevPHYChipB, rate);
49+
cstatus(obj, status, sprintf( ...
50+
'ad9361_set_bb_rate failed on ad9361-phy-B for a sampling rate of %d', rate));
51+
end
52+
53+
function multichipSync(obj, flags)
54+
% Synchronise the two AD9361: ad9361_fmcomms5_multichip_sync
55+
% of libad9361-iio. A device that is missing is an error (the
56+
% classes ignored the return value of the library).
57+
status = ad9361FMComms5MultichipSync(obj, flags);
58+
cstatus(obj, status, 'ad9361_fmcomms5_multichip_sync failed');
59+
end
60+
61+
function status = ad9361FMComms5MultichipSync(obj, flags)
62+
% ad9361_fmcomms5_multichip_sync of libad9361-iio
63+
% (ad9361_multichip_sync.c): ad9361-phy is the master and
64+
% ad9361-phy-B its one slave
65+
master = iio_context_find_device(obj, obj.iioCtx, 'ad9361-phy');
66+
slave = iio_context_find_device(obj, obj.iioCtx, 'ad9361-phy-B');
67+
if isempty(master) || isempty(slave)
68+
status = -19; % ENODEV
69+
return
70+
end
71+
status = ad9361MultichipSync(obj, master, {slave}, flags);
72+
end
73+
74+
function status = ad9361MultichipSync(obj, master, slaves, flags)
75+
% ad9361_multichip_sync of libad9361-iio
76+
% (ad9361_multichip_sync.c) on the libiio v1 bindings: the
77+
% same attribute and register accesses in the same order,
78+
% and its return value (0, or a negative errno). MASTER is an
79+
% iio.Device, SLAVES a cell array of one to three of them.
80+
% FLAGS is a sum of
81+
% 1 FIXUP_INTERFACE_TIMING: the registers 0x6 and 0x7 of
82+
% the master are copied to the slaves
83+
% 2 CHECK_SAMPLE_RATES: a slave whose sampling rate
84+
% differs from that of the master gets that rate
85+
% As in the library, the result of no access is checked.
86+
% Where the library would go on with a value that it did not
87+
% get (a read that failed leaves its variable as it was),
88+
% nothing is written here. The library reports a sampling
89+
% rate that it updates on stderr; nothing is printed here.
90+
maxSyncDevs = 4;
91+
numSlaves = numel(slaves);
92+
mcsIsDebugAttr = isempty(master.find_attr('multichip_sync'));
93+
94+
if numSlaves >= maxSyncDevs || numSlaves < 1
95+
status = -22; % EINVAL
96+
return
97+
end
98+
99+
if bitand(flags, 2) ~= 0 % CHECK_SAMPLE_RATES
100+
txSampleMaster = iio_device_find_channel(obj, master, 'voltage0', true);
101+
[stsMaster, masterFreq] = iio_channel_attr_read_longlong(obj, txSampleMaster, 'sampling_frequency');
102+
for i = 1:numSlaves
103+
txSampleSlave = iio_device_find_channel(obj, slaves{i}, 'voltage0', true);
104+
if isempty(txSampleSlave)
105+
status = -19; % ENODEV
106+
return
107+
end
108+
[stsSlave, slaveFreq] = iio_channel_attr_read_longlong(obj, txSampleSlave, 'sampling_frequency');
109+
if stsMaster >= 0 && stsSlave >= 0 && masterFreq ~= slaveFreq
110+
iio_channel_attr_write_longlong(obj, txSampleSlave, 'sampling_frequency', masterFreq);
111+
end
112+
end
113+
end
114+
115+
if bitand(flags, 1) ~= 0 % FIXUP_INTERFACE_TIMING
116+
[sts6, reg6] = iio_device_reg_read(obj, master, 6);
117+
[sts7, reg7] = iio_device_reg_read(obj, master, 7);
118+
for i = 1:numSlaves
119+
if sts6 >= 0
120+
iio_device_reg_write(obj, slaves{i}, 6, reg6);
121+
end
122+
if sts7 >= 0
123+
iio_device_reg_write(obj, slaves{i}, 7, reg7);
124+
end
125+
end
126+
end
127+
128+
% Move the parts into ALERT for MCS
129+
ensmMode = cell(1, numSlaves + 1);
130+
ensmRead = zeros(1, numSlaves + 1);
131+
[ensmRead(1), ensmMode{1}] = iio_device_attr_read(obj, master, 'ensm_mode', 80);
132+
iio_device_attr_write(obj, master, 'ensm_mode', 'alert');
133+
for i = 1:numSlaves
134+
[ensmRead(i + 1), ensmMode{i + 1}] = iio_device_attr_read(obj, slaves{i}, 'ensm_mode', 80);
135+
iio_device_attr_write(obj, slaves{i}, 'ensm_mode', 'alert');
136+
end
137+
138+
for step = 0:5
139+
for i = 1:numSlaves
140+
if mcsIsDebugAttr
141+
iio_device_debug_attr_write_longlong(obj, slaves{i}, 'multichip_sync', step);
142+
else
143+
iio_device_attr_write_longlong(obj, slaves{i}, 'multichip_sync', step);
144+
end
145+
end
146+
% The master controls the SYNC GPIO
147+
if mcsIsDebugAttr
148+
iio_device_debug_attr_write_longlong(obj, master, 'multichip_sync', step);
149+
else
150+
iio_device_attr_write_longlong(obj, master, 'multichip_sync', step);
151+
end
152+
pause(0.001);
153+
end
154+
155+
if ensmRead(1) >= 0
156+
iio_device_attr_write(obj, master, 'ensm_mode', ensmMode{1});
157+
end
158+
for i = 1:numSlaves
159+
if ensmRead(i + 1) >= 0
160+
iio_device_attr_write(obj, slaves{i}, 'ensm_mode', ensmMode{i + 1});
161+
end
162+
end
163+
status = 0;
164+
end
165+
38166
end
39167
end
40168

‎+adi/+FMComms5/Rx.m‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function setupInit(obj)
233233
% Write all attributes to device once connected through set
234234
% methods
235235
setupLibad9361(obj);
236-
obj.iioDevPHYChipB = calllib('libiio', 'iio_context_find_device',obj.iioCtx,'ad9361-phy-B');
236+
setupChipB(obj);
237237
% Do writes directly to hardware without using set methods.
238238
% This is required sine Simulink support doesn't support
239239
% modification to nontunable variables at SetupImpl
@@ -279,22 +279,22 @@ function setupInit(obj)
279279

280280
% Sample rates and RF bandwidth
281281
if ~obj.EnableCustomFilter
282-
if libisloaded('libad9361')
283-
calllib('libad9361','ad9361_set_bb_rate',obj.iioDevPHY,int32(obj.SamplingRate));
284-
calllib('libad9361','ad9361_set_bb_rate',obj.iioDevPHYChipB,int32(obj.SamplingRate));
285-
else
286-
obj.setAttributeLongLong('voltage0','sampling_frequency',obj.SamplingRate,true,4);
287-
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidth,strcmp(obj.Type,'Tx'));
288-
obj.setAttributeLongLong('voltage0','sampling_frequency',obj.SamplingRate,true,4,obj.iioDevPHYChipB);
289-
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidthChipB,strcmp(obj.Type,'Tx'),0,obj.iioDevPHYChipB);
290-
end
282+
% The rate with its FIR filter on both AD9361
283+
% (ad9361_set_bb_rate of libad9361-iio), then the
284+
% bandwidths. With libad9361 loaded the classes did not
285+
% write the bandwidths here.
286+
setBBRate(obj, obj.SamplingRate);
287+
setBBRateChipB(obj, obj.SamplingRate);
288+
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidth,strcmp(obj.Type,'Tx'));
289+
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidthChipB,strcmp(obj.Type,'Tx'),0,obj.iioDevPHYChipB);
291290
else
292291
writeFilterFile(obj);
293292
writeFilterFileFMComms5ChipB(obj);
294293
end
295294
obj.setAttributeRAW('voltage0','rf_port_select',obj.RFPortSelect,false);
296295
obj.setAttributeRAW('voltage0','rf_port_select',obj.RFPortSelectChipB,false,obj.iioDevPHYChipB);
297-
calllib('libad9361','ad9361_fmcomms5_multichip_sync',obj.iioCtx,uint32(3));
296+
% FIXUP_INTERFACE_TIMING and CHECK_SAMPLE_RATES
297+
multichipSync(obj, 3);
298298

299299
if (obj.CustomAGC)
300300
% Initialize hardware to reflect debug attribute changes

‎+adi/+FMComms5/Tx.m‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@
102102
end
103103
% Check RFPortSelect
104104
function set.RFPortSelectChipB(obj, value)
105-
obj.RFPortSelect = value;
105+
obj.RFPortSelectChipB = value;
106106
if obj.ConnectedToDevice
107-
obj.setAttributeRAW('voltage0','rf_port_select',value,false,obj.iioDevPHYChipB); %#ok<MCSUP>
107+
obj.setAttributeRAW('voltage0','rf_port_select',value,true,obj.iioDevPHYChipB); %#ok<MCSUP>
108108
end
109109
end
110110
% Check Attenuation
@@ -290,7 +290,7 @@ function setupInit(obj)
290290
% Write all attributes to device once connected through set
291291
% methods
292292
setupLibad9361(obj);
293-
obj.iioDevPHYChipB = calllib('libiio', 'iio_context_find_device',obj.iioCtx,'ad9361-phy-B');
293+
setupChipB(obj);
294294

295295
% Do writes directly to hardware without using set methods.
296296
% This is required sine Simulink support doesn't support
@@ -299,15 +299,14 @@ function setupInit(obj)
299299
obj.setAttributeLongLong(id,'frequency',obj.CenterFrequency,true,8);
300300
obj.setAttributeLongLong(id,'frequency',obj.CenterFrequencyChipB,true,8,obj.iioDevPHYChipB);
301301
if ~obj.EnableCustomFilter
302-
if libisloaded('libad9361')
303-
calllib('libad9361','ad9361_set_bb_rate',obj.iioDevPHY,int32(obj.SamplingRate));
304-
calllib('libad9361','ad9361_set_bb_rate',obj.iioDevPHYChipB,int32(obj.SamplingRate));
305-
else
306-
obj.setAttributeLongLong('voltage0','sampling_frequency',obj.SamplingRate,true,4);
307-
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidth,strcmp(obj.Type,'Tx'));
308-
obj.setAttributeLongLong('voltage0','sampling_frequency',obj.SamplingRate,true,4,obj.iioDevPHYChipB);
309-
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidthChipB,strcmp(obj.Type,'Tx'),[],obj.iioDevPHYChipB);
310-
end
302+
% The rate with its FIR filter on both AD9361
303+
% (ad9361_set_bb_rate of libad9361-iio), then the
304+
% bandwidths. With libad9361 loaded the classes did not
305+
% write the bandwidths here.
306+
setBBRate(obj, obj.SamplingRate);
307+
setBBRateChipB(obj, obj.SamplingRate);
308+
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidth,strcmp(obj.Type,'Tx'));
309+
obj.setAttributeLongLong('voltage0','rf_bandwidth',obj.RFBandwidthChipB,strcmp(obj.Type,'Tx'),[],obj.iioDevPHYChipB);
311310
else
312311
writeFilterFile(obj);
313312
writeFilterFileFMComms5ChipB(obj);
@@ -320,10 +319,10 @@ function setupInit(obj)
320319
obj.setAttributeDouble('voltage1','hardwaregain',obj.AttenuationChannel1,true);
321320
end
322321
if (any(obj.EnabledChannels == 3))
323-
obj.setAttributeDouble('voltage0','hardwaregain',obj.AttenuationChannel0ChipB,true,obj.iioDevPHYChipB);
322+
obj.setAttributeDouble('voltage0','hardwaregain',obj.AttenuationChannel0ChipB,true,0,obj.iioDevPHYChipB);
324323
end
325324
if (any(obj.EnabledChannels == 4))
326-
obj.setAttributeDouble('voltage1','hardwaregain',obj.AttenuationChannel1ChipB,true,obj.iioDevPHYChipB);
325+
obj.setAttributeDouble('voltage1','hardwaregain',obj.AttenuationChannel1ChipB,true,0,obj.iioDevPHYChipB);
327326
end
328327
obj.ToggleDDS(strcmp(obj.DataSource,'DDS'));
329328
if strcmp(obj.DataSource,'DDS')

‎.github/workflows/libiio-v1.yml‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name: libiio v1 bindings
22

33
# Hardware-free tests of the device classes on the libiio v1 MATLAB bindings
4-
# (github.com/tfcollins/libiio-conv, package "iio"): adi.AD9361.Rx/Tx and
5-
# adi.Pluto.Rx/Tx, and adi.common.JESD204, against libiio's emulation
6-
# backend, see test/IIOv1EmuTests.m. Hosted runner only: no board, no self-hosted runner.
4+
# (github.com/tfcollins/libiio-conv, package "iio"): adi.AD9361.Rx/Tx,
5+
# adi.Pluto.Rx/Tx, adi.FMComms5.Rx/Tx, adi.ADRV9009.Rx/ORx/Tx,
6+
# adi.AD9371.Rx/ORx/Tx and adi.ADRV9002.Rx/Tx, and adi.common.JESD204, against
7+
# libiio's emulation backend, see test/IIOv1EmuTests.m. Hosted runner only: no
8+
# board, no self-hosted runner.
79

810
on:
911
push:
@@ -23,7 +25,7 @@ env:
2325

2426
jobs:
2527
emu:
26-
name: AD9361 family on emu (R2025b)
28+
name: Device classes on emu (R2025b)
2729
runs-on: ubuntu-24.04
2830
timeout-minutes: 30
2931
steps:
@@ -64,6 +66,20 @@ jobs:
6466
test/emu/ad9361_bb_rate/generate.sh libiio-conv/deps/install "$RUNNER_TEMP/bb_rate_expected"
6567
diff -r test/emu/ad9361_bb_rate/expected "$RUNNER_TEMP/bb_rate_expected"
6668
69+
# The same for the multichip sync of the FMComms5
70+
# (ad9361_fmcomms5_multichip_sync).
71+
- name: Check the expected files of the multichip sync tests
72+
run: |
73+
test/emu/ad9361_mcs/generate.sh libiio-conv/deps/install "$RUNNER_TEMP/mcs_expected"
74+
diff -r test/emu/ad9361_mcs/expected "$RUNNER_TEMP/mcs_expected"
75+
76+
# adrv9009.xml is put together from the two ADRV9008 context files. This
77+
# makes it again and fails when the committed one differs.
78+
- name: Check the ADRV9009 context file
79+
run: |
80+
python3 test/emu/make_adrv9009_xml.py test/emu/adrv9008-1.xml test/emu/adrv9008-2.xml "$RUNNER_TEMP/adrv9009.xml"
81+
cmp test/emu/adrv9009.xml "$RUNNER_TEMP/adrv9009.xml"
82+
6783
# MATLAB alone: the device classes need matlab.System and nothing else.
6884
- name: Set up MATLAB
6985
id: setup-matlab
@@ -102,12 +118,13 @@ jobs:
102118

103119
# A green test step says only that no test failed. This also fails the job
104120
# when tests did not run or were filtered. --min-tests is the number of
105-
# test points of IIOv1EmuTests (84: 21 methods for 3 boards, one for 6
106-
# cases, and 15);
121+
# test points of IIOv1EmuTests (121: 21 methods for 3 boards, two for 6
122+
# cases each, one for 8 receivers, one for 6 transmitters, two for 2
123+
# files each, and 28);
107124
# raise it with every test that is added.
108125
- name: Check the test results (count, skipped, duration)
109126
if: always()
110-
run: python3 libiio-conv/matlab/tools/check_junit.py test-results/emu.xml --min-tests 84 --max-seconds 60 --max-skipped 0
127+
run: python3 libiio-conv/matlab/tools/check_junit.py test-results/emu.xml --min-tests 121 --max-seconds 60 --max-skipped 0
111128

112129
- name: Collect crash dumps
113130
if: always()

‎test/IIOv1ADRV9002Recorder.m‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
classdef IIOv1ADRV9002Recorder < adi.ADRV9002.Rx
2+
% IIOv1ADRV9002Recorder adi.ADRV9002.Rx that keeps its device attribute writes
3+
% For test/IIOv1EmuTests.m only. Every write of a device attribute
4+
% as text (iio_device_attr_write) or as bytes
5+
% (iio_device_attr_write_raw) through the members of
6+
% adi.common.IIOBase is added to Writes with what was handed to the
7+
% bindings. The emulation keeps a value only up to its first zero
8+
% byte, so the bytes of a stream file cannot be read back from it.
9+
10+
properties (Hidden)
11+
%Writes One row per write: 'string' or 'raw', the name of the
12+
% attribute, the value (char or uint8), the result of the member
13+
Writes = cell(0, 4);
14+
end
15+
16+
methods
17+
function obj = IIOv1ADRV9002Recorder(varargin)
18+
obj = obj@adi.ADRV9002.Rx(varargin{:});
19+
end
20+
end
21+
22+
methods (Hidden)
23+
24+
function bytes = iio_device_attr_write(obj, devPtr, attr, value)
25+
bytes = iio_device_attr_write@adi.ADRV9002.Rx(obj, devPtr, attr, value);
26+
obj.Writes(end + 1, :) = {'string', attr, value, bytes};
27+
end
28+
29+
function bytes = iio_device_attr_write_raw(obj, devPtr, attr, value)
30+
bytes = iio_device_attr_write_raw@adi.ADRV9002.Rx(obj, devPtr, attr, value);
31+
obj.Writes(end + 1, :) = {'raw', attr, value, bytes};
32+
end
33+
34+
end
35+
36+
end

0 commit comments

Comments
 (0)