-
Notifications
You must be signed in to change notification settings - Fork 18
/
libiio_if.m
697 lines (615 loc) · 29.5 KB
/
libiio_if.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
% Copyright 2014(c) Analog Devices, Inc.
%
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without modification,
% are permitted provided that the following conditions are met:
% - Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% - Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the
% distribution.
% - Neither the name of Analog Devices, Inc. nor the names of its
% contributors may be used to endorse or promote products derived
% from this software without specific prior written permission.
% - The use of this software may or may not infringe the patent rights
% of one or more patent holders. This license does not release you
% from the requirement that you obtain separate licenses from these
% patent holders to use this software.
% - Use of the software either in source or binary form or filter designs
% resulting from the use of this software, must be connected to, run
% on or loaded to an Analog Devices Inc. component.
%
% THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
% INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
% PARTICULAR PURPOSE ARE DISCLAIMED.
%
% IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
% EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, INTELLECTUAL PROPERTY
% RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
% BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
% STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
% THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
classdef libiio_if < handle
% libiio_if Interface object for for IIO devices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Protected properties
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
properties (Access = protected)
libname = 'libiio';
hname = 'iio.h';
dev_name = '';
data_ch_no = 0;
data_ch_size = 0;
dev_type = '';
iio_ctx = {};
iio_dev = {};
iio_buffer = {};
iio_channel = {};
iio_buf_size = 8192;
iio_scan_elm_no = 0;
if_initialized = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Static private methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods (Static, Access = private)
function out = modInstanceCnt(val)
% Manages the number of object instances to handle proper DLL unloading
persistent instance_cnt;
if isempty(instance_cnt)
instance_cnt = 0;
end
instance_cnt = instance_cnt + val;
out = instance_cnt;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Protected methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods (Access = protected)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Creates the network context
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, err_msg, msg_log] = createNetworkContext(obj, ip_address)
% Initialize the return values
ret = -1;
err_msg = '';
msg_log = [];
% Create the network context
obj.iio_ctx = calllib(obj.libname, 'iio_create_network_context', ip_address);
% Check if the network context is valid
if (isNull(obj.iio_ctx))
obj.iio_ctx = {};
err_msg = 'Could not connect to the IIO server!';
return;
end
% Increase the object's instance count
libiio_if.modInstanceCnt(1);
msg_log = [msg_log sprintf('%s: Connected to IP %s\n', class(obj), ip_address)];
% Set the return code to success
ret = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Releases the network context and unload the libiio library
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function releaseContext(obj)
calllib(obj.libname, 'iio_context_destroy', obj.iio_ctx);
obj.iio_ctx = {};
instCnt = libiio_if.modInstanceCnt(-1);
if(instCnt == 0)
unloadlibrary(obj.libname);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Checks the compatibility of the different software modules.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, err_msg, msg_log] = checkVersions(obj)
% Initialize the return values
ret = -1;
err_msg = '';
msg_log = [];
% Create a set of pointers to read the iiod and dll versions
data = zeros(1, 10);
remote_pMajor = libpointer('uint32Ptr', data(1));
remote_pMinor = libpointer('uint32Ptr', data(2));
remote_pGitTag = libpointer('int8Ptr', [int8(data(3:end)) 0]);
local_pMajor = libpointer('uint32Ptr', data(1));
local_pMinor = libpointer('uint32Ptr', data(2));
local_pGitTag = libpointer('int8Ptr', [int8(data(3:end)) 0]);
% get remote libiio version
calllib(obj.libname, 'iio_context_get_version', obj.iio_ctx, remote_pMajor, remote_pMinor, remote_pGitTag);
% extract git hash without trailing null char
remote_githash = remote_pGitTag.Value(1:7);
remote_version_str = sprintf('Remote libiio version: %d.%d, (git-%s)', remote_pMajor.Value, remote_pMinor.Value, remote_githash);
msg_log = [msg_log sprintf('%s: %s\n', class(obj), remote_version_str)];
% get local libiio version
calllib(obj.libname, 'iio_library_get_version', local_pMajor, local_pMinor, local_pGitTag);
local_githash = local_pGitTag.Value(1:7);
local_version_str = sprintf('Local libiio version: %d.%d, (git-%s)', local_pMajor.Value, local_pMinor.Value, local_githash);
msg_log = [msg_log sprintf('%s: %s\n', class(obj), local_version_str)];
if(remote_pMajor.Value < local_pMajor.Value)
err_msg = ['The libiio version running on the device is outdated! ' ...
'Run the adi_update_tools.sh script to get libiio up to date.'];
return;
elseif(remote_pMajor.Value > local_pMajor.Value)
err_msg = ['The libiio version on the local host is outdated! ' ...
'On Windows, reinstall the dll using the latest installer ' ...
'from the Analog Devices wiki.'];
return;
end
% Set the return code to success
ret = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Detect if the specified device is present in the system
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, err_msg, msg_log] = initDevice(obj, dev_name)
% Initialize the return values
ret = -1;
err_msg = '';
msg_log = [];
% Store the device name
obj.dev_name = dev_name;
% Get the number of devices
nb_devices = calllib(obj.libname, 'iio_context_get_devices_count', obj.iio_ctx);
% If no devices are present return with error
if(nb_devices == 0)
err_msg = 'No devices were detected in the system!';
return;
end
msg_log = [msg_log sprintf('%s: Found %d devices in the system\n', class(obj), nb_devices)];
% Detect if the targeted device is installed
dev_found = 0;
for i = 0 : nb_devices - 1
dev = calllib(obj.libname, 'iio_context_get_device', obj.iio_ctx, i);
name = calllib(obj.libname, 'iio_device_get_name', dev);
if(strcmp(name, dev_name))
obj.iio_dev = dev;
dev_found = 1;
break;
end
clear dev;
end
% Check if the target device was detected
if(dev_found == 0)
err_msg = 'Could not find target configuration device!';
return;
end
msg_log = [msg_log sprintf('%s: %s was found in the system\n', class(obj), obj.dev_name)];
% Set the return code to success
ret = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initializes the output data channels
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, err_msg, msg_log] = initOutputDataChannels(obj, ch_no, ch_size)
% Initialize the return values
ret = -1;
err_msg = '';
msg_log = [];
% Save the number of channels and size
obj.data_ch_no = ch_no;
obj.data_ch_size = ch_size;
% Get the number of channels that the device has
nb_channels = calllib(obj.libname, 'iio_device_get_channels_count', obj.iio_dev);
if(nb_channels == 0)
err_msg = 'The selected device does not have any channels!';
return;
end
% Enable the data channels
if(ch_no ~= 0)
% Check if the device has output channels. The
% logic here assumes that a device can have
% only input or only output channels
obj.iio_channel{1} = calllib(obj.libname, 'iio_device_get_channel', obj.iio_dev, 0);
is_output = calllib(obj.libname, 'iio_channel_is_output', obj.iio_channel{1});
if(is_output == 0)
err_msg = 'The selected device does not have output channels!';
return;
end
% Enable all the channels
for j = 0 : nb_channels - 1
obj.iio_channel{j+1} = calllib(obj.libname, 'iio_device_get_channel', obj.iio_dev, j);
calllib(obj.libname, 'iio_channel_enable', obj.iio_channel{j+1});
is_scan_element = calllib(obj.libname, 'iio_channel_is_scan_element', obj.iio_channel{j+1});
if(is_scan_element == 1)
obj.iio_scan_elm_no = obj.iio_scan_elm_no + 1;
end
end
msg_log = [msg_log sprintf('%s: Found %d output channels for the device %s\n', class(obj), obj.iio_scan_elm_no, obj.dev_name)];
% Check if the number of channels in the device
% is greater or equal to the system object
% input channels
if(obj.iio_scan_elm_no < ch_no)
obj.iio_channel = {};
err_msg = 'The selected device does not have enough output channels!';
return;
end
% Enable the DAC buffer output
obj.if_initialized = 1;
ret = writeAttributeString(obj, 'altvoltage0*raw', '0');
obj.if_initialized = 0;
if(ret < 0)
obj.iio_channel = {};
err_msg = 'Could not enable the DAC buffer output!';
return;
end
% Create the IIO buffer used to write data
obj.iio_buf_size = obj.data_ch_size * obj.iio_scan_elm_no;
obj.iio_buffer = calllib(obj.libname, 'iio_device_create_buffer', obj.iio_dev,...
obj.data_ch_size, 1);
end
msg_log = [msg_log sprintf('%s: %s output data channels successfully initialized\n', class(obj), obj.dev_name)];
% Set the return code to success
ret = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initializes the input data channels
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, err_msg, msg_log] = initInputDataChannels(obj, ch_no, ch_size)
% Initialize the return values
ret = -1;
err_msg = '';
msg_log = [];
% Save the number of channels and size
obj.data_ch_no = ch_no;
obj.data_ch_size = ch_size;
% Get the number of channels that the device has
nb_channels = calllib(obj.libname, 'iio_device_get_channels_count', obj.iio_dev);
if(nb_channels == 0)
err_msg = 'The selected device does not have any channels!';
return;
end
% Enable the system object output channels
if(ch_no ~= 0)
% Check if the device has input channels. The
% logic here assumes that a device can have
% only input or only output channels
obj.iio_channel{1} = calllib(obj.libname, 'iio_device_get_channel', obj.iio_dev, 0);
is_output = calllib(obj.libname, 'iio_channel_is_output', obj.iio_channel{1});
if(is_output == 1)
err_msg = 'The selected device does not have input channels!';
return;
end
msg_log = [msg_log sprintf('%s: Found %d input channels for the device %s\n', class(obj), nb_channels, obj.dev_name)];
% Check if the number of channels in the device
% is greater or equal to the system object
% output channels
if(nb_channels < ch_no)
obj.iio_channel = {};
err_msg = 'The selected device does not have enough input channels!';
return;
end
% Enable the channels
for j = 0 : ch_no - 1
obj.iio_channel{j+1} = calllib(obj.libname, 'iio_device_get_channel', obj.iio_dev, j);
calllib(obj.libname, 'iio_channel_enable', obj.iio_channel{j+1});
end
for j = ch_no : nb_channels - 1
obj.iio_channel{j+1} = calllib(obj.libname, 'iio_device_get_channel', obj.iio_dev, j);
calllib(obj.libname, 'iio_channel_disable', obj.iio_channel{j+1});
end
% Create the IIO buffer used to read data
obj.iio_buf_size = obj.data_ch_size * obj.data_ch_no;
obj.iio_buffer = calllib(obj.libname, 'iio_device_create_buffer', obj.iio_dev, obj.iio_buf_size, 0);
end
msg_log = [msg_log sprintf('%s: %s input data channels successfully initialized\n', class(obj), obj.dev_name)];
% Set the return code to success
ret = 0;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Public methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Constructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function obj = libiio_if()
% Constructor
obj.if_initialized = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Destructor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function delete(obj)
% Release any resources used by the system object.
if((obj.if_initialized == 1) && libisloaded(obj.libname))
if(~isempty(obj.iio_buffer))
calllib(obj.libname, 'iio_buffer_destroy', obj.iio_buffer);
end
if(~isempty(obj.iio_ctx))
calllib(obj.libname, 'iio_context_destroy', obj.iio_ctx);
end
obj.iio_buffer = {};
obj.iio_channel = {};
obj.iio_dev = {};
obj.iio_ctx = {};
instCnt = libiio_if.modInstanceCnt(-1);
if(instCnt == 0)
unloadlibrary(obj.libname);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Initializes the libiio interface
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, err_msg, msg_log] = init(obj, ip_address, ...
dev_name, dev_type, ...
data_ch_no, data_ch_size)
% Initialize the return values
ret = -1;
err_msg = '';
msg_log = [];
% Save the device type
obj.dev_type = dev_type;
% Set the initialization status to fail
obj.if_initialized = 0;
% Load the libiio library
if(~libisloaded(obj.libname))
try
[notfound, warnings] = loadlibrary(obj.libname, obj.hname);
catch exception
err_msg = exception.message;
return;
end
end
if(~libisloaded(obj.libname))
err_msg = 'Could not load the libiio library!';
return;
end
% Create the network context
[ret, err_msg, msg_log] = createNetworkContext(obj, ip_address);
if(ret < 0)
return;
end
% Check the software versions
[ret, err_msg, msg_log_new] = checkVersions(obj);
msg_log = [msg_log msg_log_new];
if(ret < 0)
releaseContext(obj);
return;
end
% Initialize the device
[ret, err_msg, msg_log_new] = initDevice(obj, dev_name);
msg_log = [msg_log msg_log_new];
if(ret < 0)
releaseContext(obj);
return;
end
% Initialize the output data channels
if(strcmp(dev_type, 'OUT'))
[ret, err_msg, msg_log_new] = initOutputDataChannels(obj, data_ch_no, data_ch_size);
msg_log = [msg_log msg_log_new];
if(ret < 0)
releaseContext(obj);
return;
end
end
% Initialize the input data channels
if(strcmp(dev_type, 'IN'))
[ret, err_msg, msg_log_new] = initInputDataChannels(obj, data_ch_no, data_ch_size);
msg_log = [msg_log msg_log_new];
if(ret < 0)
releaseContext(obj);
return;
end
end
% Set the initialization status to success
obj.if_initialized = 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Implement the data capture flow
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, data] = readData(obj)
% Initialize the return values
ret = -1;
data = cell(1, obj.data_ch_no);
for i = 1 : obj.data_ch_no
data{i} = zeros(obj.data_ch_size, 1);
end
% Check if the interface is initialized
if(obj.if_initialized == 0)
return;
end
% Check if the device type is output
if(~strcmp(obj.dev_type, 'IN'))
return;
end
% Read the data
calllib(obj.libname, 'iio_buffer_refill', obj.iio_buffer);
buffer = calllib(obj.libname, 'iio_buffer_first', obj.iio_buffer, obj.iio_channel{1});
setdatatype(buffer, 'int16Ptr', obj.iio_buf_size);
for i = 1 : obj.data_ch_no
data{i} = double(buffer.Value(i:obj.data_ch_no:end));
end
% Set the return code to success
ret = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Implement the data transmit flow
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ret = writeData(obj, data)
% Initialize the return values
ret = -1;
% Check if the interface is initialized
if(obj.if_initialized == 0)
return;
end
% Check if the device type is input
if(~strcmp(obj.dev_type, 'OUT'))
return;
end
% Destroy the buffer
calllib(obj.libname, 'iio_buffer_destroy', obj.iio_buffer);
obj.iio_buffer = {};
% Enable the DAC buffer output
ret = writeAttributeString(obj, 'altvoltage0*raw', '0');
if(ret < 0)
obj.iio_channel = {};
err_msg = 'Could not enable the DAC buffer output!';
return;
end
% Create the IIO buffer used to write data
obj.iio_buf_size = obj.data_ch_size * obj.iio_scan_elm_no;
obj.iio_buffer = calllib(obj.libname, 'iio_device_create_buffer', obj.iio_dev,...
obj.data_ch_size, 1);
% Transmit the data
buffer = calllib(obj.libname, 'iio_buffer_start', obj.iio_buffer);
setdatatype(buffer, 'int16Ptr', obj.iio_buf_size);
for i = 1 : obj.data_ch_no
buffer.Value(i : obj.iio_scan_elm_no : obj.iio_buf_size) = int16(data{i});
end
for i = obj.data_ch_no + 1 : obj.iio_scan_elm_no
buffer.Value(i : obj.iio_scan_elm_no : obj.iio_buf_size) = 0;
end
calllib(obj.libname, 'iio_buffer_push', obj.iio_buffer);
% Set the return code to success
ret = 0;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Find an attribute based on the name. The name can contain wildcard '*' characters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, ch, attr] = findAttribute(obj, attr_name)
% Initialize the return values
ret = -1;
ch = 0;
attr = '';
% Check if the interface is initialized
if(obj.if_initialized == 0)
return;
end
% Check if this is a device attribute
name = calllib(obj.libname, 'iio_device_find_attr', obj.iio_dev, attr_name);
if(~isempty(name))
ret = 0;
return;
end
% This is a channel attribute, search for the corresponding channel
chn_no = calllib(obj.libname, 'iio_device_get_channels_count', obj.iio_dev);
for k = 0 : chn_no - 1
ch = calllib(obj.libname, 'iio_device_get_channel', obj.iio_dev, k);
attr_no = calllib(obj.libname, 'iio_channel_get_attrs_count', ch);
attr_found = 0;
for l = 0 : attr_no - 1
attr = calllib(obj.libname, 'iio_channel_get_attr', ch, l);
name = calllib(obj.libname, 'iio_channel_attr_get_filename', ch, attr);
% The attribute to find can contain wildcard '*' characters,
% search for all the substrings in the attribute name
str_find = strsplit(attr_name, '*');
str_find = str_find(find(~strcmp(str_find, '')));
has_wildcard = ~isempty(strfind(attr_name, '*'));
attr_found = 1;
for i = 1 : length(str_find)
if(has_wildcard == 0)
ret = strcmp(name, str_find{i});
if(ret == 0)
ret = [];
end
else
ret = strfind(name, str_find{i});
end
if(isempty(ret))
attr_found = 0;
break;
end
end
if(attr_found == 1)
break;
end
clear attr;
end
% Check if the attribute was found
if(attr_found == 0)
clear ch;
else
ret = 1;
break;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Read an attribute as a double value
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, val] = readAttributeDouble(obj, attr_name)
% Find the attribute
[ret, ch, attr] = findAttribute(obj, attr_name);
if(ret < 0)
val = 0;
return;
end
% Create a double pointer to be used for data read
data = zeros(1, 10);
pData = libpointer('doublePtr', data(1));
% Read the attribute value
if(ret > 0)
calllib(obj.libname, 'iio_channel_attr_read_double', ch, attr, pData);
clear ch;
clear attr;
else
calllib(obj.libname, 'iio_device_attr_read_double', obj.iio_dev, attr_name, pData);
end
val = pData.Value;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Read an attribute as a string value
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ret, val] = readAttributeString(obj, attr_name)
% Find the attribute
[ret, ch, attr] = findAttribute(obj, attr_name);
if(ret < 0)
val = '';
return;
end
% Create a pointer to be used for data read
data = char(ones(1, 512));
pData = libpointer('stringPtr', data);
% Read the attribute value
if(ret > 0)
[~, ~, ~, val] = calllib(obj.libname, 'iio_channel_attr_read', ch, attr, pData, 512);
clear ch;
clear attr;
else
[~, ~, ~, val] = calllib(obj.libname, 'iio_device_attr_read', obj.iio_dev, attr_name, pData, 512);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Write a string double value
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ret = writeAttributeDouble(obj, attr_name, val)
% Find the attribute
[ret, ch, attr] = findAttribute(obj, attr_name);
if(ret < 0)
return;
end
% Write the attribute
if(ret > 0)
calllib(obj.libname, 'iio_channel_attr_write_double', ch, attr, val);
clear ch;
clear attr;
else
calllib(obj.libname, 'iio_device_attr_write_double', obj.iio_dev, attr_name, val);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Write a string attribute value
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ret = writeAttributeString(obj, attr_name, str)
% Find the attribute
[ret, ch, attr] = findAttribute(obj, attr_name);
if(ret < 0)
return;
end
% Write the attribute
if(ret > 0)
calllib(obj.libname, 'iio_channel_attr_write', ch, attr, str);
clear ch;
clear attr;
else
calllib(obj.libname, 'iio_device_attr_write', obj.iio_dev, attr_name, str);
end
end
end
end