-
Notifications
You must be signed in to change notification settings - Fork 4
/
PlutoSDR_Streaming.cpp
783 lines (593 loc) · 19.5 KB
/
PlutoSDR_Streaming.cpp
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
#include "SoapyPlutoSDR.hpp"
#include <memory>
#include <iostream>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <algorithm>
#include <chrono>
/* static scratch mem for strings */
static char tmpstr[64];
//TODO: Need to be a power of 2 for maximum efficiency ?
# define DEFAULT_RX_BUFFER_SIZE (1 << 16)
/* helper function generating channel names */
static char* get_ch_name(const char* type, int id)
{
snprintf(tmpstr, sizeof(tmpstr), "%s%d", type, id);
return tmpstr;
}
std::vector<std::string> SoapyPlutoSDR::getStreamFormats(const int direction, const size_t channel) const
{
std::vector<std::string> formats;
formats.push_back(SOAPY_SDR_CS8);
formats.push_back(SOAPY_SDR_CS12);
formats.push_back(SOAPY_SDR_CS16);
formats.push_back(SOAPY_SDR_CF32);
return formats;
}
std::string SoapyPlutoSDR::getNativeStreamFormat(const int direction, const size_t channel, double &fullScale) const
{
if (direction == SOAPY_SDR_RX) {
fullScale = 2048; // RX expects 12 bit samples LSB aligned
}
else if (direction == SOAPY_SDR_TX) {
fullScale = 32768; // TX expects 12 bit samples MSB aligned
}
return SOAPY_SDR_CS16;
}
SoapySDR::ArgInfoList SoapyPlutoSDR::getStreamArgsInfo(const int direction, const size_t channel) const
{
SoapySDR::ArgInfoList streamArgs;
return streamArgs;
}
bool SoapyPlutoSDR::IsValidRxStreamHandle(SoapySDR::Stream* handle) const
{
if (handle == nullptr) {
return false;
}
//handle is an opaque pointer hiding either rx_stream or tx_streamer:
//check that the handle matches one of them, consistently with direction:
if (rx_stream) {
//test if these handles really belong to us:
if (reinterpret_cast<rx_streamer*>(handle) == rx_stream.get()) {
return true;
}
}
return false;
}
bool SoapyPlutoSDR::IsValidTxStreamHandle(SoapySDR::Stream* handle)
{
if (handle == nullptr) {
return false;
}
//handle is an opaque pointer hiding either rx_stream or tx_streamer:
//check that the handle matches one of them, consistently with direction:
if (tx_stream) {
//test if these handles really belong to us:
if (reinterpret_cast<tx_streamer*>(handle) == tx_stream.get()) {
return true;
}
}
return false;
}
SoapySDR::Stream *SoapyPlutoSDR::setupStream(
const int direction,
const std::string &format,
const std::vector<size_t> &channels,
const SoapySDR::Kwargs &args )
{
//check the format
plutosdrStreamFormat streamFormat;
if (format == SOAPY_SDR_CF32) {
SoapySDR_log(SOAPY_SDR_INFO, "Using format CF32.");
streamFormat = PLUTO_SDR_CF32;
}
else if (format == SOAPY_SDR_CS16) {
SoapySDR_log(SOAPY_SDR_INFO, "Using format CS16.");
streamFormat = PLUTO_SDR_CS16;
}
else if (format == SOAPY_SDR_CS12) {
SoapySDR_log(SOAPY_SDR_INFO, "Using format CS12.");
streamFormat = PLUTO_SDR_CS12;
}
else if (format == SOAPY_SDR_CS8) {
SoapySDR_log(SOAPY_SDR_INFO, "Using format CS8.");
streamFormat = PLUTO_SDR_CS8;
}
else {
throw std::runtime_error(
"setupStream invalid format '" + format + "' -- Only CS8, CS12, CS16 and CF32 are supported by SoapyPlutoSDR module.");
}
if(direction == SOAPY_SDR_RX){
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
this->rx_stream = std::unique_ptr<rx_streamer>(new rx_streamer (rx_dev, streamFormat, channels, args));
return reinterpret_cast<SoapySDR::Stream*>(this->rx_stream.get());
}
else if (direction == SOAPY_SDR_TX) {
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
this->tx_stream = std::unique_ptr<tx_streamer>(new tx_streamer (tx_dev, streamFormat, channels, args));
return reinterpret_cast<SoapySDR::Stream*>(this->tx_stream.get());
}
return nullptr;
}
void SoapyPlutoSDR::closeStream( SoapySDR::Stream *handle)
{
//scope lock:
{
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if (IsValidRxStreamHandle(handle)) {
this->rx_stream.reset();
}
}
//scope lock :
{
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
if (IsValidTxStreamHandle(handle)) {
this->tx_stream.reset();
}
}
}
size_t SoapyPlutoSDR::getStreamMTU( SoapySDR::Stream *handle) const
{
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if (IsValidRxStreamHandle(handle)) {
return this->rx_stream->get_mtu_size();
}
return 0;
}
int SoapyPlutoSDR::activateStream(
SoapySDR::Stream *handle,
const int flags,
const long long timeNs,
const size_t numElems )
{
if (flags & ~SOAPY_SDR_END_BURST)
return SOAPY_SDR_NOT_SUPPORTED;
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if (IsValidRxStreamHandle(handle)) {
return this->rx_stream->start(flags, timeNs, numElems);
}
return 0;
}
int SoapyPlutoSDR::deactivateStream(
SoapySDR::Stream *handle,
const int flags,
const long long timeNs )
{
//scope lock:
{
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if (IsValidRxStreamHandle(handle)) {
return this->rx_stream->stop(flags, timeNs);
}
}
//scope lock :
{
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
if (IsValidTxStreamHandle(handle)) {
this->tx_stream->flush();
return 0;
}
}
return 0;
}
int SoapyPlutoSDR::readStream(
SoapySDR::Stream *handle,
void * const *buffs,
const size_t numElems,
int &flags,
long long &timeNs,
const long timeoutUs )
{
//the spin_mutex is especially very useful here for minimum overhead !
std::lock_guard<pluto_spin_mutex> lock(rx_device_mutex);
if (IsValidRxStreamHandle(handle)) {
return int(this->rx_stream->recv(buffs, numElems, flags, timeNs, timeoutUs));
} else {
return SOAPY_SDR_NOT_SUPPORTED;
}
}
int SoapyPlutoSDR::writeStream(
SoapySDR::Stream *handle,
const void * const *buffs,
const size_t numElems,
int &flags,
const long long timeNs,
const long timeoutUs )
{
std::lock_guard<pluto_spin_mutex> lock(tx_device_mutex);
if (IsValidTxStreamHandle(handle)) {
return this->tx_stream->send(buffs, numElems, flags, timeNs, timeoutUs);;
} else {
return SOAPY_SDR_NOT_SUPPORTED;
}
}
int SoapyPlutoSDR::readStreamStatus(
SoapySDR::Stream *stream,
size_t &chanMask,
int &flags,
long long &timeNs,
const long timeoutUs)
{
return SOAPY_SDR_NOT_SUPPORTED;
}
void rx_streamer::set_buffer_size_by_samplerate(const size_t samplerate) {
//Adapt buffer size (= MTU) as a tradeoff to minimize readStream overhead but at
//the same time allow realtime applications. Keep it a power of 2 which seems to be better.
//so try to target very roughly 60fps [30 .. 100] readStream calls / s for realtime applications.
int rounded_nb_samples_per_call = (int)::round(samplerate / 60.0);
int power_of_2_nb_samples = 0;
while (rounded_nb_samples_per_call > (1 << power_of_2_nb_samples)) {
power_of_2_nb_samples++;
}
this->set_buffer_size(1 << power_of_2_nb_samples);
SoapySDR_logf(SOAPY_SDR_INFO, "Auto setting Buffer Size: %lu", (unsigned long)buffer_size);
//Recompute MTU from buffer size change.
//We always set MTU size = Buffer Size.
//On buffer size adjustment to sample rate,
//MTU can be changed accordingly safely here.
set_mtu_size(this->buffer_size);
}
void rx_streamer::set_mtu_size(const size_t mtu_size) {
this->mtu_size = mtu_size;
SoapySDR_logf(SOAPY_SDR_INFO, "Set MTU Size: %lu", (unsigned long)mtu_size);
}
rx_streamer::rx_streamer(const iio_device *_dev, const plutosdrStreamFormat _format, const std::vector<size_t> &channels, const SoapySDR::Kwargs &args):
dev(_dev), buffer_size(DEFAULT_RX_BUFFER_SIZE), mtu_size(DEFAULT_RX_BUFFER_SIZE), buf(nullptr), format(_format)
{
if (dev == nullptr) {
SoapySDR_logf(SOAPY_SDR_ERROR, "cf-ad9361-lpc not found!");
throw std::runtime_error("cf-ad9361-lpc not found!");
}
unsigned int nb_channels = iio_device_get_channels_count(dev), i;
for (i = 0; i < nb_channels; i++)
iio_channel_disable(iio_device_get_channel(dev, i));
//default to channel 0, if none were specified
const std::vector<size_t> &channelIDs = channels.empty() ? std::vector<size_t>{0} : channels;
for (i = 0; i < channelIDs.size() * 2; i++) {
struct iio_channel *chn = iio_device_get_channel(dev, i);
iio_channel_enable(chn);
channel_list.push_back(chn);
}
if ( args.count( "bufflen" ) != 0 ){
try
{
size_t bufferLength = std::stoi(args.at("bufflen"));
if (bufferLength > 0)
this->set_buffer_size(bufferLength);
}
catch (const std::invalid_argument &){}
}else{
long long samplerate;
iio_channel_attr_read_longlong(iio_device_find_channel(dev, "voltage0", false),"sampling_frequency",&samplerate);
this->set_buffer_size_by_samplerate(samplerate);
}
}
rx_streamer::~rx_streamer()
{
if (buf) {
iio_buffer_cancel(buf);
iio_buffer_destroy(buf);
}
for (unsigned int i = 0; i < channel_list.size(); ++i) {
iio_channel_disable(channel_list[i]);
}
}
size_t rx_streamer::recv(void * const *buffs,
const size_t numElems,
int &flags,
long long &timeNs,
const long timeoutUs)
{
//
if (items_in_buffer <= 0) {
// auto before = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
if (!buf) {
return 0;
}
ssize_t ret = iio_buffer_refill(buf);
// auto after = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
if (ret < 0)
return SOAPY_SDR_TIMEOUT;
items_in_buffer = (unsigned long)ret / iio_buffer_step(buf);
// SoapySDR_logf(SOAPY_SDR_INFO, "iio_buffer_refill took %d ms to refill %d items", (int)(after - before), items_in_buffer);
byte_offset = 0;
}
size_t items = std::min(items_in_buffer,numElems);
ptrdiff_t buf_step = iio_buffer_step(buf);
if (direct_copy) {
// optimize for single RX, 2 channel (I/Q), same endianess direct copy
// note that RX is 12 bits LSB aligned, i.e. fullscale 2048
uint8_t *src = (uint8_t *)iio_buffer_start(buf) + byte_offset;
int16_t const *src_ptr = (int16_t *)src;
if (format == PLUTO_SDR_CS16) {
::memcpy(buffs[0], src_ptr, 2 * sizeof(int16_t) * items);
}
else if (format == PLUTO_SDR_CF32) {
float *dst_cf32 = (float *)buffs[0];
for (size_t index = 0; index < items * 2; ++index) {
*dst_cf32 = float(*src_ptr) / 2048.0f;
src_ptr++;
dst_cf32++;
}
}
else if (format == PLUTO_SDR_CS12) {
int8_t *dst_cs12 = (int8_t *)buffs[0];
for (size_t index = 0; index < items; ++index) {
int16_t i = *src_ptr++;
int16_t q = *src_ptr++;
// produce 24 bit (iiqIQQ), note the input is LSB aligned, scale=2048
// note: byte0 = i[7:0]; byte1 = {q[3:0], i[11:8]}; byte2 = q[11:4];
*dst_cs12++ = uint8_t(i);
*dst_cs12++ = uint8_t((q << 4) | ((i >> 8) & 0x0f));
*dst_cs12++ = uint8_t(q >> 4);
}
}
else if (format == PLUTO_SDR_CS8) {
int8_t *dst_cs8 = (int8_t *)buffs[0];
for (size_t index = 0; index < items * 2; index++) {
*dst_cs8 = int8_t(*src_ptr >> 4);
src_ptr++;
dst_cs8++;
}
}
}
else {
int16_t conv = 0, *conv_ptr = &conv;
for (unsigned int i = 0; i < channel_list.size(); i++) {
iio_channel *chn = channel_list[i];
unsigned int index = i / 2;
uint8_t *src = (uint8_t *)iio_buffer_first(buf, chn) + byte_offset;
int16_t const *src_ptr = (int16_t *)src;
if (format == PLUTO_SDR_CS16) {
int16_t *dst_cs16 = (int16_t *)buffs[index];
for (size_t j = 0; j < items; ++j) {
iio_channel_convert(chn, conv_ptr, src_ptr);
src_ptr += buf_step;
dst_cs16[j * 2 + i] = conv;
}
}
else if (format == PLUTO_SDR_CF32) {
float *dst_cf32 = (float *)buffs[index];
for (size_t j = 0; j < items; ++j) {
iio_channel_convert(chn, conv_ptr, src_ptr);
src_ptr += buf_step;
dst_cf32[j * 2 + i] = float(conv) / 2048.0f;
}
}
else if (format == PLUTO_SDR_CS8) {
int8_t *dst_cs8 = (int8_t *)buffs[index];
for (size_t j = 0; j < items; ++j) {
iio_channel_convert(chn, conv_ptr, src_ptr);
src_ptr += buf_step;
dst_cs8[j * 2 + i] = int8_t(conv >> 4);
}
}
}
}
items_in_buffer -= items;
byte_offset += items * iio_buffer_step(buf);
return(items);
}
int rx_streamer::start(const int flags,
const long long timeNs,
const size_t numElems)
{
//force proper stop before
stop(flags, timeNs);
// re-create buffer
buf = iio_device_create_buffer(dev, buffer_size, false);
if (!buf) {
SoapySDR_logf(SOAPY_SDR_ERROR, "Unable to create buffer!");
throw std::runtime_error("Unable to create buffer!\n");
}
direct_copy = has_direct_copy();
SoapySDR_logf(SOAPY_SDR_INFO, "Has direct RX copy: %d", (int)direct_copy);
return 0;
}
int rx_streamer::stop(const int flags,
const long long timeNs)
{
//cancel first
if (buf) {
iio_buffer_cancel(buf);
}
//then destroy
if (buf) {
iio_buffer_destroy(buf);
buf = nullptr;
}
items_in_buffer = 0;
byte_offset = 0;
return 0;
}
void rx_streamer::set_buffer_size(const size_t _buffer_size){
if (!buf || this->buffer_size != _buffer_size) {
//cancel first
if (buf) {
iio_buffer_cancel(buf);
}
//then destroy
if (buf) {
iio_buffer_destroy(buf);
}
items_in_buffer = 0;
byte_offset = 0;
buf = iio_device_create_buffer(dev, _buffer_size, false);
if (!buf) {
SoapySDR_logf(SOAPY_SDR_ERROR, "Unable to create buffer!");
throw std::runtime_error("Unable to create buffer!\n");
}
}
this->buffer_size=_buffer_size;
}
size_t rx_streamer::get_mtu_size() {
return this->mtu_size;
}
// return wether can we optimize for single RX, 2 channel (I/Q), same endianess direct copy
bool rx_streamer::has_direct_copy()
{
if (channel_list.size() != 2) // one RX with I + Q
return false;
ptrdiff_t buf_step = iio_buffer_step(buf);
if (buf_step != 2 * sizeof(int16_t))
return false;
if (iio_buffer_start(buf) != iio_buffer_first(buf, channel_list[0]))
return false;
int16_t test_dst, test_src = 0x1234;
iio_channel_convert(channel_list[0], &test_dst, (const void *)&test_src);
return test_src == test_dst;
}
tx_streamer::tx_streamer(const iio_device *_dev, const plutosdrStreamFormat _format, const std::vector<size_t> &channels, const SoapySDR::Kwargs &args) :
dev(_dev), format(_format), buf(nullptr)
{
if (dev == nullptr) {
SoapySDR_logf(SOAPY_SDR_ERROR, "cf-ad9361-dds-core-lpc not found!");
throw std::runtime_error("cf-ad9361-dds-core-lpc not found!");
}
unsigned int nb_channels = iio_device_get_channels_count(dev), i;
for (i = 0; i < nb_channels; i++)
iio_channel_disable(iio_device_get_channel(dev, i));
//default to channel 0, if none were specified
const std::vector<size_t> &channelIDs = channels.empty() ? std::vector<size_t>{0} : channels;
for (i = 0; i < channelIDs.size() * 2; i++) {
iio_channel *chn = iio_device_get_channel(dev, i);
iio_channel_enable(chn);
channel_list.push_back(chn);
}
buf_size = 4096;
items_in_buf = 0;
buf = iio_device_create_buffer(dev, buf_size, false);
if (!buf) {
SoapySDR_logf(SOAPY_SDR_ERROR, "Unable to create buffer!");
throw std::runtime_error("Unable to create buffer!");
}
direct_copy = has_direct_copy();
SoapySDR_logf(SOAPY_SDR_INFO, "Has direct TX copy: %d", (int)direct_copy);
}
tx_streamer::~tx_streamer(){
if (buf) { iio_buffer_destroy(buf); }
for(unsigned int i=0;i<channel_list.size(); ++i)
iio_channel_disable(channel_list[i]);
}
int tx_streamer::send( const void * const *buffs,
const size_t numElems,
int &flags,
const long long timeNs,
const long timeoutUs )
{
if (!buf) {
return 0;
}
size_t items = std::min(buf_size - items_in_buf, numElems);
int16_t src = 0;
int16_t const *src_ptr = &src;
uint8_t *dst_ptr;
ptrdiff_t buf_step = iio_buffer_step(buf);
if (direct_copy && format == PLUTO_SDR_CS16) {
// optimize for single TX, 2 channel (I/Q), same endianess direct copy
dst_ptr = (uint8_t *)iio_buffer_start(buf) + items_in_buf * 2 * sizeof(int16_t);
memcpy(dst_ptr, buffs[0], 2 * sizeof(int16_t) * items);
}
else if (direct_copy && format == PLUTO_SDR_CS12) {
dst_ptr = (uint8_t *)iio_buffer_start(buf) + items_in_buf * 2 * sizeof(int16_t);
int8_t *samples_cs12 = (int8_t *)buffs[0];
for (size_t index = 0; index < items; ++index) {
// consume 24 bit (iiqIQQ)
uint16_t src0 = uint16_t(*(samples_cs12++));
uint16_t src1 = uint16_t(*(samples_cs12++));
uint16_t src2 = uint16_t(*(samples_cs12++));
// produce 2x 16 bit, note the output is MSB aligned, scale=32768
// note: byte0 = i[11:4]; byte1 = {q[7:4], i[15:12]}; byte2 = q[15:8];
*dst_ptr = int16_t((src1 << 12) | (src0 << 4));
dst_ptr++;
*dst_ptr = int16_t((src2 << 8) | (src1 & 0xf0));
dst_ptr++;
}
}
else if (format == PLUTO_SDR_CS12) {
SoapySDR_logf(SOAPY_SDR_ERROR, "CS12 not available with this endianess or channel layout");
throw std::runtime_error("CS12 not available with this endianess or channel layout");
}
else
for (unsigned int k = 0; k < channel_list.size(); k++) {
iio_channel *chn = channel_list[k];
unsigned int index = k / 2;
dst_ptr = (uint8_t *)iio_buffer_first(buf, chn) + items_in_buf * buf_step;
// note that TX expects samples MSB aligned, unlike RX which is LSB aligned
if (format == PLUTO_SDR_CS16) {
int16_t *samples_cs16 = (int16_t *)buffs[index];
for (size_t j = 0; j < items; ++j) {
src = samples_cs16[j*2+k];
iio_channel_convert_inverse(chn, dst_ptr, src_ptr);
dst_ptr += buf_step;
}
}
else if (format == PLUTO_SDR_CF32) {
float *samples_cf32 = (float *)buffs[index];
for (size_t j = 0; j < items; ++j) {
src = (int16_t)(samples_cf32[j*2+k] * 32767.999f); // 32767.999f (0x46ffffff) will ensure better distribution
iio_channel_convert_inverse(chn, dst_ptr, src_ptr);
dst_ptr += buf_step;
}
}
else if (format == PLUTO_SDR_CS8) {
int8_t *samples_cs8 = (int8_t *)buffs[index];
for (size_t j = 0; j < items; ++j) {
src = (int16_t)(samples_cs8[j*2+k] << 8);
iio_channel_convert_inverse(chn, dst_ptr, src_ptr);
dst_ptr += buf_step;
}
}
}
items_in_buf += items;
if (items_in_buf == buf_size || (flags & SOAPY_SDR_END_BURST && numElems == items)) {
int ret = send_buf();
if (ret < 0) {
return SOAPY_SDR_ERROR;
}
if ((size_t)ret != buf_size) {
return SOAPY_SDR_ERROR;
}
}
return items;
}
int tx_streamer::flush()
{
return send_buf();
}
int tx_streamer::send_buf()
{
if (!buf) {
return 0;
}
if (items_in_buf > 0) {
if (items_in_buf < buf_size) {
ptrdiff_t buf_step = iio_buffer_step(buf);
uint8_t *buf_ptr = (uint8_t *)iio_buffer_start(buf) + items_in_buf * buf_step;
uint8_t *buf_end = (uint8_t *)iio_buffer_end(buf);
memset(buf_ptr, 0, buf_end - buf_ptr);
}
ssize_t ret = iio_buffer_push(buf);
items_in_buf = 0;
if (ret < 0) {
return ret;
}
return int(ret / iio_buffer_step(buf));
}
return 0;
}
// return wether can we optimize for single TX, 2 channel (I/Q), same endianess direct copy
bool tx_streamer::has_direct_copy()
{
if (channel_list.size() != 2) // one TX with I/Q
return false;
ptrdiff_t buf_step = iio_buffer_step(buf);
if (buf_step != 2 * sizeof(int16_t))
return false;
if (iio_buffer_start(buf) != iio_buffer_first(buf, channel_list[0]))
return false;
int16_t test_dst, test_src = 0x1234;
iio_channel_convert_inverse(channel_list[0], &test_dst, (const void *)&test_src);
return test_src == test_dst;
}