-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbladeRF_MIMO_XCVR.m
718 lines (603 loc) · 28.2 KB
/
bladeRF_MIMO_XCVR.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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
% bladeRF RX/TX control and configuration.
%
% This is a submodule of the bladeRF object. It is not intended to be
% accessed directly, but through the top-level bladeRF object.
%
%
% Copyright (c) 2015 Nuand LLC
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
%
%% Control and configuration of transceiver properties
classdef bladeRF_MIMO_XCVR < handle
properties
config % Stream configuration. See bladeRF_StreamConfig.
corrections % IQ corrections. See bladeRF_IQCorr.
end
properties(Dependent = true)
agc % Enable automatic gain control ['AUTO', 'MANUAL', 'FAST', 'SLOW']
gain % Universal gain setting. This value is standardized to output 0dBm when set to 60.
biastee % Control biastee
samplerate % Samplerate. Must be within 160 kHz and 40 MHz. A 2-3 MHz minimum is suggested unless external filters are being used.
frequency % Frequency. Must be within [237.5 MHz, 3.8 GHz] when no XB-200 is attached, or [0 GHz, 3.8 GHz] when an XB-200 is attached.
bandwidth % LPF bandwidth seting. This is rounded to the nearest of the available discrete settings. It is recommended to set this and read back the actual value.
vga1 % VGA1 gain. RX range: [5, 30], TX Range: [-35, -4]
vga2 % VGA2 gain. RX range: [0, 30], TX range: [0, 25]
lna % RX LNA gain. Values: { 'BYPASS', 'MID', 'MAX' }
xb200_filter % XB200 Filter selection. Only valid when an XB200 is attached. Options are: '50M', '144M', '222M', 'AUTO_1DB', 'AUTO_3DB', 'CUSTOM'
mux % FPGA sample FIFO mux mode. Only valid for RX, with options 'BASEBAND_LMS', '12BIT_COUNTER', '32BIT_COUNTER', 'DIGITAL_LOOPBACK'
channel % Channel number
end
properties(SetAccess = immutable, Hidden=true)
bladerf % Associated bladeRF device handle
module % Module specifier (as a libbladeRF enum)
direction % Module direction: { 'RX', 'TX' }
min_frequency % Lower frequency tuning limit
max_frequency % Higher frequency tuning limit
min_sampling % Lower sampling rate tuning limit
max_sampling % Higher sampling rate tuning limit
xb200_attached; % Modify behavior due to XB200 being attached
end
properties(SetAccess = private, Hidden=true)
current_channel % Current active channel
end
properties(SetAccess = private)
running % Denotes whether or not the module is enabled to stream samples.
timestamp % Provides a coarse readback of the timestamp counter.
end
properties(Access={?bladeRF_MIMO})
sob = true % TX Start of Burst (Applicable to TX only.)
eob = false % TX end-of-burst flag (Applicable to TX only.)
end
methods
%% Property handling
% Samplerate
function set.samplerate(obj, val)
% Create the holding structures
rate = libstruct('bladerf_rational_rate');
actual = libstruct('bladerf_rational_rate');
% Requested rate
rate.integer = floor(val);
[rate.num, rate.den] = rat(mod(val,1));
% Set the samplerate
for i = 1:2
[status, ~, ~, actual] = calllib('libbladeRF', 'bladerf_set_rational_sample_rate', obj.bladerf.device, obj.module{i}, rate, rate);
bladeRF.check_status('bladerf_set_rational_sample_rate', status);
fprintf('Set %s%d samplerate. Requested: %d + %d/%d, Actual: %d + %d/%d\n', ...
obj.direction, ...
i, ...
rate.integer, rate.num, rate.den, ...
actual.integer, actual.num, actual.den);
end
end
function samplerate_val = get.samplerate(obj)
rate = libstruct('bladerf_rational_rate');
rate.integer = 0;
rate.num = 0;
rate.den = 1;
% Get the sample rate from the hardware
[status, ~, rate] = calllib('libbladeRF', 'bladerf_get_rational_sample_rate', obj.bladerf.device, obj.module{1}, rate);
bladeRF.check_status('bladerf_get_rational_sample_rate', status);
%fprintf('Read %s samplerate: %d + %d/%d\n', ...
% obj.direction, rate.integer, rate.num, rate.den);
samplerate_val = rate.integer + rate.num / rate.den;
end
% Frequency
function set.frequency(obj, val)
[status, ~] = calllib('libbladeRF', 'bladerf_set_frequency', obj.bladerf.device, obj.module{1}, val);
bladeRF.check_status('bladerf_set_frequency', status);
actual = obj.frequency;
fprintf('Set %s frequency. Requested: %d, Actual: %d\n', ...
obj.direction, val, actual);
end
function freq_val = get.frequency(obj)
freq_val = uint32(0);
[status, ~, freq_val] = calllib('libbladeRF', 'bladerf_get_frequency', obj.bladerf.device, obj.module{1}, freq_val);
bladeRF.check_status('bladerf_get_frequency', status);
%fprintf('Read %s frequency: %f\n', obj.direction, freq_val);
end
% Configures the LPF bandwidth on the associated module
function set.bandwidth(obj, val)
for i = 1:2
actual = uint32(0);
[status, ~, actual] = calllib('libbladeRF', 'bladerf_set_bandwidth', obj.bladerf.device, obj.module{i}, val, actual);
bladeRF.check_status('bladerf_set_bandwidth', status);
fprintf('Set %s%d bandwidth. Requested: %f, Actual: %f\n', ...
obj.direction, i, val, actual)
end
end
% Reads the LPF bandwidth configuration on the associated module
function bw_val = get.bandwidth(obj)
bw_val = uint32(0);
[status, ~, bw_val] = calllib('libbladeRF', 'bladerf_get_bandwidth', obj.bladerf.device, obj.module{1}, bw_val);
bladeRF.check_status('bladerf_get_bandwidth', status);
%fprintf('Read %s bandwidth: %f\n', obj.direction, bw_val);
end
% Configures the automatic gain control setting
function set.agc(obj, val)
if strcmpi(obj.direction,'RX') == true
ch = 'BLADERF_CHANNEL_RX1';
else
ch = 'BLADERF_CHANNEL_TX1';
end
ch = obj.channel;
switch lower(val)
case 'auto'
agc_val = 'BLADERF_GAIN_DEFAULT';
case 'manual'
agc_val = 'BLADERF_GAIN_MGC';
case 'fast'
agc_val = 'BLADERF_GAIN_FASTATTACK_AGC';
case 'enable'
agc_val = 'BLADERF_GAIN_SLOWATTACK_AGC';
case 'slow'
agc_val = 'BLADERF_GAIN_SLOWATTACK_AGC';
case 'hybrid'
agc_val = 'BLADERF_GAIN_HYBRID_AGC';
otherwise
error(strcat('Invalid AGC setting: ', val));
end
for i = 1:2
[status, ~] = calllib('libbladeRF', 'bladerf_set_gain_mode', obj.bladerf.device, ch{i}, agc_val);
if status == -8
if obj.bladerf.info.gen == 1
disp('Cannot enable AGC. AGC DC LUT file is missing, run `cal table agc rx'' in bladeRF-cli.')
end
else
bladeRF.check_status('bladerf_set_gain_mode', status);
end
end
fprintf('Set AGC. Requested: %s, Actual: %s\n', ...
val, obj.agc)
end
% Reads the current automatic gain control setting
function val = get.agc(obj)
val = int32(0);
if strcmpi(obj.direction,'RX') == true
ch = 'BLADERF_CHANNEL_RX1';
else
ch = 'BLADERF_CHANNEL_TX1';
end
ch = obj.channel;
tmp = int32(0);
[status, ~, mode] = calllib('libbladeRF', 'bladerf_get_gain_mode', obj.bladerf.device, ch{1}, tmp);
bladeRF.check_status('bladerf_get_gain_mode', status);
switch mode
case 'BLADERF_GAIN_DEFAULT'
val = 'auto';
case 'BLADERF_GAIN_MGC'
val = 'manual';
case 'BLADERF_GAIN_FASTATTACK_AGC'
val = 'fast';
case 'BLADERF_GAIN_SLOWATTACK_AGC'
val = 'slow';
case 'BLADERF_GAIN_HYBRID_AGC'
val = 'hybrid';
otherwise
error(strcat('Invalid AGC setting: ', val));
end
%fprintf('Read %s gain: %d\n', obj.direction, val);
end
% Configures active channel setting
function set.channel(obj, val)
if (strcmpi(val,'RX') == true || strcmpi(val, 'RX1') || strcmp(val, 'BLADERF_CHANNEL_RX1'))
channel = {'BLADERF_CHANNEL_RX1', 'BLADERF_CHANNEL_RX2'};
% elseif (strcmpi(val, 'RX2') || strcmp(val, 'BLADERF_CHANNEL_RX2'))
% channel = 'BLADERF_CHANNEL_RX2';
elseif (strcmpi(val,'TX') == true || strcmpi(val, 'TX1') || strcmp(val, 'BLADERF_CHANNEL_TX1'))
channel = {'BLADERF_CHANNEL_TX1', 'BLADERF_CHANNEL_TX2'};
% elseif (strcmpi(val, 'TX2') || strcmp(val, 'BLADERF_CHANNEL_TX2'))
% channel = 'BLADERF_CHANNEL_TX2';
end
obj.current_channel = channel;
% if obj.running
% [status, ~] = calllib('libbladeRF', 'bladerf_enable_module', ...
% obj.bladerf.device, ...
% obj.current_channel, ...
% false);
%
% bladeRF.check_status('bladerf_enable_module', status);
%
%
% [status, ~] = calllib('libbladeRF', 'bladerf_enable_module', ...
% obj.bladerf.device, ...
% channel, ...
% true);
%
% bladeRF.check_status('bladerf_enable_module', status);
% end
end
% Reads the current active channel setting
function val = get.channel(obj)
val = obj.current_channel;
end
% Configures the universal gain
function set.gain(obj, val)
% TODO: Check Gain assigment. Only the first value is being
% writen
assert(length(val) == 2, "Input must be a 2 element array. i.e. [60, 40]")
if strcmpi(obj.direction,'RX') == true && strcmpi(obj.agc,'manual') == 0
warning(['Cannot set ' obj.direction ' gain when AGC is in ' obj.agc ' mode'])
end
% if strcmpi(obj.direction,'RX') == true
% ch = 'BLADERF_CHANNEL_RX1';
% else
% ch = 'BLADERF_CHANNEL_TX1';
% end
for i = 1:2
[status, ~] = calllib('libbladeRF', 'bladerf_set_gain', obj.bladerf.device, obj.channel{i}, val(i));
bladeRF.check_status('bladerf_set_gain', status);
end
fprintf('Set %s%d bandwidth. Requested: [%d, %d], Actual: [%d, %d]\n', ...
obj.direction, i, val, obj.gain)
end
% Reads the current universal gain configuration
function vals = get.gain(obj)
val = int32(0);
if strcmpi(obj.direction,'RX') == true
ch = 'BLADERF_CHANNEL_RX1';
else
ch = 'BLADERF_CHANNEL_TX1';
end
ch = obj.channel;
tmp = int32(0);
vals = zeros(1,2);
for i = 1:2
[status, ~, val] = calllib('libbladeRF', 'bladerf_get_gain', obj.bladerf.device, ch{i}, tmp);
bladeRF.check_status('bladerf_get_gain', status);
vals(i) = val;
end
%fprintf('Read %s gain: %d\n', obj.direction, val);
end
% Configures the gain of VGA1
function set.vga1(obj, val)
if strcmpi(obj.direction,'RX') == true
[status, ~] = calllib('libbladeRF', 'bladerf_set_rxvga1', obj.bladerf.device, val);
else
[status, ~] = calllib('libbladeRF', 'bladerf_set_txvga1', obj.bladerf.device, val);
end
bladeRF.check_status('bladerf_set_vga1', status);
%fprintf('Set %s VGA1: %d\n', obj.direction, val);
end
% Reads the current VGA1 gain configuration
function val = get.vga1(obj)
if obj.bladerf.info.gen ~= 1
val = 0;
return
end
val = int32(0);
if strcmpi(obj.direction,'RX') == true
[status, ~, val] = calllib('libbladeRF', 'bladerf_get_rxvga1', obj.bladerf.device, val);
else
[status, ~, val] = calllib('libbladeRF', 'bladerf_get_txvga1', obj.bladerf.device, val);
end
bladeRF.check_status('bladerf_get_vga1', status);
%fprintf('Read %s VGA1: %d\n', obj.direction, val);
end
% Configures the gain of VGA2
function set.vga2(obj, val)
if strcmpi(obj.direction,'RX') == true
[status, ~] = calllib('libbladeRF', 'bladerf_set_rxvga2', obj.bladerf.device, val);
else
[status, ~] = calllib('libbladeRF', 'bladerf_set_txvga2', obj.bladerf.device, val);
end
bladeRF.check_status('bladerf_set_vga2', status);
%fprintf('Set %s VGA2: %d\n', obj.direction, obj.vga2);
end
% Reads the current VGA2 configuration
function val = get.vga2(obj)
if obj.bladerf.info.gen ~= 1
val = 0;
return
end
val = int32(0);
if strcmpi(obj.direction,'RX') == true
[status, ~, val] = calllib('libbladeRF', 'bladerf_get_rxvga2', obj.bladerf.device, val);
else
[status, ~, val] = calllib('libbladeRF', 'bladerf_get_txvga2', obj.bladerf.device, val);
end
bladeRF.check_status('bladerf_get_vga2', status);
%fprintf('Read %s VGA2: %d\n', obj.direction, val);
end
% Configure the RX LNA gain
function set.lna(obj, val)
if strcmpi(obj.direction,'TX') == true
error('LNA gain is not applicable to the TX path');
end
valid_value = true;
if isnumeric(val)
switch val
case 0
lna_val = 'BLADERF_LNA_GAIN_BYPASS';
case 3
lna_val = 'BLADERF_LNA_GAIN_MID';
case 6
lna_val = 'BLADERF_LNA_GAIN_MAX';
otherwise
valid_value = false;
end
else
if strcmpi(val,'bypass') == true
lna_val = 'BLADERF_LNA_GAIN_BYPASS';
elseif strcmpi(val, 'mid') == true
lna_val = 'BLADERF_LNA_GAIN_MID';
elseif strcmpi(val, 'max') == true
lna_val = 'BLADERF_LNA_GAIN_MAX';
else
valid_value = false;
end
end
if valid_value ~= true
error('Valid LNA values are [''BYPASS'', ''MID'', ''MAX''] or [0, 3, 6], respectively.');
else
[status, ~] = calllib('libbladeRF', 'bladerf_set_lna_gain', obj.bladerf.device, lna_val);
bladeRF.check_status('bladerf_set_lna_gain', status);
%fprintf('Set RX LNA gain to: %s\n', lna_val);
end
end
% Read current RX LNA gain setting
function val = get.lna(obj)
if obj.bladerf.info.gen ~= 1
val = 0;
return
end
if strcmpi(obj.direction,'TX') == true
error('LNA gain is not applicable to the TX path');
end
val = 0;
[status, ~, lna] = calllib('libbladeRF', 'bladerf_get_lna_gain', obj.bladerf.device, val);
bladeRF.check_status('bladerf_get_lna_gain', status);
if strcmpi(lna, 'BLADERF_LNA_GAIN_BYPASS') == true
val = 'BYPASS';
elseif strcmpi(lna, 'BLADERF_LNA_GAIN_MID') == true
val = 'MID';
elseif strcmpi(lna, 'BLADERF_LNA_GAIN_MAX') == true
val = 'MAX';
else
val = 'UNKNOWN';
end
%fprintf('Got RX LNA gain: %s\n', val);
end
% Configures the bias tee
function set.biastee(obj, val)
if strcmpi(obj.direction,'RX') == true
ch = 'BLADERF_CHANNEL_RX1';
else
ch = 'BLADERF_CHANNEL_TX1';
end
[status, ~] = calllib('libbladeRF', 'bladerf_set_bias_tee', obj.bladerf.device, ch, val);
%fprintf('Set %s biastee: %d\n', obj.direction, obj.vga2);
end
% Reads the current bias tee configuration
function val = get.biastee(obj)
if strcmpi(obj.direction,'RX') == true
ch = 'BLADERF_CHANNEL_RX1';
else
ch = 'BLADERF_CHANNEL_TX1';
end
tmp = int32(0);
[status, ~, val] = calllib('libbladeRF', 'bladerf_get_bias_tee', obj.bladerf.device, ch, tmp);
%fprintf('Get %s biastee: %d\n', obj.direction, val);
end
% Read the timestamp counter from the associated module
function val = get.timestamp(obj)
val = uint64(0);
actualDirection = '';
if strcmpi(obj.current_channel{1}, 'BLADERF_CHANNEL_TX1') == true || strcmpi(obj.current_channel{2}, 'BLADERF_CHANNEL_TX2') == true
actualDirection = 'BLADERF_TX';
else
actualDirection = 'BLADERF_RX';
end
[status, ~, val] = calllib('libbladeRF', 'bladerf_get_timestamp', obj.bladerf.device, actualDirection, val);
%[status, ~, val] = calllib('libbladeRF', 'bladerf_get_timestamp', obj.bladerf.device, obj.module, val);
bladeRF.check_status('bladerf_get_timestamp', status);
end
% Set the current XB200 filter
function set.xb200_filter(obj, filter)
if obj.xb200_attached == false
error('Cannot set XB200 filter because the handle was not initialized for use with the XB200.');
end
filter = upper(filter);
switch filter
case '50M'
case '144M'
case '222M'
case 'AUTO_1DB'
case 'AUTO_3DB'
case 'CUSTOM'
otherwise
error(['Invalid XB200 filter: ' filter]);
end
filter_val = ['BLADERF_XB200_' filter ];
for i = 1:2
status = calllib('libbladeRF', 'bladerf_xb200_set_filterbank', obj.bladerf.device, obj.module{i}, filter_val);
bladeRF.check_status('bladerf_xb200_set_filterbank', status);
end
end
% Get the current XB200 filter
function filter_val = get.xb200_filter(obj)
if obj.xb200_attached == false
filter_val = 'N/A';
return;
end
filter_val = 0;
[status, ~, filter_val] = calllib('libbladeRF', 'bladerf_xb200_get_filterbank', obj.bladerf.device, obj.module{1}, filter_val);
bladeRF.check_status('bladerf_xb200_get_filterbank', status);
filter_val = strrep(filter_val, 'BLADERF_XB200_', '');
end
% Set the RX mux mode setting
function set.mux(obj, mode)
if strcmpi(obj.direction, 'TX') == true
error('FPGA sample mux mode configuration is only applicable to the RX module.');
end
mode = upper(mode);
switch mode
case { 'BASEBAND_LMS', '12BIT_COUNTER', '32BIT_COUNTER', 'DIGITAL_LOOPBACK' }
mode = ['BLADERF_RX_MUX_' mode ];
otherwise
error(['Invalid RX mux mode: ' mode]);
end
status = calllib('libbladeRF', 'bladerf_set_rx_mux', obj.bladerf.device, mode);
bladeRF.check_status('bladerf_set_rx_mux', status);
end
% Get the current RX mux mode setting
function mode = get.mux(obj)
if strcmpi(obj.direction, 'TX') == true
error('FPGA sample mux mode configuration is only applicable to the RX module.');
end
mode = 'BLADERF_RX_MUX_INVALID';
[status, ~, mode] = calllib('libbladeRF', 'bladerf_get_rx_mux', obj.bladerf.device, mode);
bladeRF.check_status('bladerf_get_rx_mux', status);
mode = strrep(mode, 'BLADERF_RX_MUX_', '');
end
% Constructor
function obj = bladeRF_MIMO_XCVR(dev, dir, xb)
if strcmpi(dir,'RX') == false && strcmpi(dir,'TX') == false
error('Invalid direction specified');
end
% Set the direction of the transceiver
obj.direction = dir;
obj.bladerf = dev;
obj.channel = dir;
obj.module = obj.current_channel;
if strcmpi(xb, 'XB200') == true
obj.min_frequency = 0;
obj.xb200_attached = true;
obj.xb200_filter = 'AUTO_3DB';
else
obj.min_frequency = 237.5e6;
obj.xb200_attached = false;
end
% Setup defaults
fprintf('Initializing %s with default parameters\n', obj.direction)
obj.config = bladeRF_StreamConfig;
obj.samplerate = 1e6;
obj.frequency = 890e6;
obj.bandwidth = 1e6;
freqrange = libstruct('bladerf_range');
status = calllib('libbladeRF', 'bladerf_get_frequency_range', obj.bladerf.device, obj.module{1}, freqrange);
bladeRF.check_status('bladerf_get_frequency_range', status);
obj.min_frequency = freqrange.min;
obj.max_frequency = freqrange.max;
samplerange = libstruct('bladerf_range');
status = calllib('libbladeRF', 'bladerf_get_sample_rate_range', obj.bladerf.device, obj.module{1}, samplerange);
bladeRF.check_status('bladerf_get_frequency_range', status);
obj.min_sampling = samplerange.min;
obj.max_sampling = samplerange.max;
if strcmpi(dir,'RX') == true
for i = 1:2
status = calllib('libbladeRF', 'bladerf_set_gain_mode', obj.bladerf.device, obj.module{i}, 'BLADERF_GAIN_DEFAULT');
if status == -8
if obj.bladerf.info.gen == 1
disp('Cannot enable AGC. AGC DC LUT file is missing, run `cal table agc rx'' in bladeRF-cli.')
end
else
bladeRF.check_status('bladerf_set_gain_mode', status);
end
gainmode = int32(0);
[status, ~, gainmode] = calllib('libbladeRF', 'bladerf_get_gain_mode', obj.bladerf.device, obj.module{i}, gainmode);
end
end
if dev.info.gen == 1
if strcmpi(dir, 'RX') == true
obj.vga1 = 30;
obj.vga2 = 0;
obj.lna = 'MAX';
else
obj.vga1 = -8;
obj.vga2 = 16;
end
end
obj.corrections = bladeRF_IQCorr(dev, obj.module{1}, 0, 0, 0, 0);
obj.running = false;
end
function start(obj)
% Apply stream configuration parameters and enable the module.
%
% bladeRF.rx.start() or bladeRF.tx.start().
%
%fprintf('Starting %s stream.\n', obj.direction);
obj.running = true;
obj.config.lock();
% If we're starting up a TX module, reset our cached EOB/SOB
% flags so that we can internally take care of these if the
% user doesn't want to worry about them.
if strcmpi(obj.direction,'TX') == true
obj.sob = true;
obj.eob = false;
layout = 3; %'BLADERF_TX_X2';
else
layout = 2; %'BLADERF_RX_X2';
end
% Configure the sync config
[status, ~] = calllib('libbladeRF', 'bladerf_sync_config', ...
obj.bladerf.device, ...
layout, ... %obj.module, ...
'BLADERF_FORMAT_SC16_Q11_META', ...
obj.config.num_buffers, ...
obj.config.buffer_size, ...
obj.config.num_transfers, ...
obj.config.timeout_ms);
bladeRF.check_status('bladerf_sync_config', status);
% Enable the module
for i = 1:2
[status, ~] = calllib('libbladeRF', 'bladerf_enable_module', ...
obj.bladerf.device, ...
obj.current_channel{i}, ...
true);
bladeRF.check_status('bladerf_enable_module', status);
end
end
function stop(obj)
% Stop streaming and disable the module
%
% bladeRF.rx.stop() or bladeRF.tx.stop().
%
%fprintf('Stopping %s module.\n', obj.direction);
% If the user is trying top stop "mid-burst", we'll want to
% end the burst to ensure the TX DAC is reset to 0+0j
if strcmpi(obj.direction,'TX') == true
if obj.sob == false && obj.eob == false
obj.eob = true;
obj.bladerf.transmit([0 0], 0, obj.sob, obj.eob);
% Ensure these zeros are transmitted by waiting for
% any remaining data in buffers to flush
max_buffered = obj.bladerf.tx.config.num_buffers * ...
obj.bladerf.tx.config.buffer_size;
target_time = obj.bladerf.tx.timestamp + ...
max_buffered;
while obj.bladerf.tx.timestamp <= target_time
pause(1e-3);
end
end
end
% Disable the module
for i = 1:2
[status, ~] = calllib('libbladeRF', 'bladerf_enable_module', ...
obj.bladerf.device, ...
obj.current_channel{i}, ...
false);
bladeRF.check_status('bladerf_enable_module', status);
end
% Unlock the configuration for changing
obj.config.unlock();
obj.running = false;
end
end
end