forked from pothosware/SoapyBladeRF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bladeRF_Streaming.cpp
494 lines (425 loc) · 14.1 KB
/
bladeRF_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
/*
* This file is part of the bladeRF project:
* http://www.github.com/nuand/bladeRF
*
* Copyright (C) 2015-2016 Josh Blum
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "bladeRF_SoapySDR.hpp"
#include <SoapySDR/Logger.hpp>
#include <stdexcept>
//cross platform usleep()
#ifdef _MSC_VER
#include <windows.h>
#define usleep(t) Sleep((t)/1000)
#else
#include <unistd.h>
#endif
#define DEF_NUM_BUFFS 32
#define DEF_BUFF_LEN 4096
#define STRINGIFY_(x) #x
#define STRINGIFY(x) STRINGIFY_(x)
std::vector<std::string> bladeRF_SoapySDR::getStreamFormats(const int, const size_t) const
{
std::vector<std::string> formats;
formats.push_back("CS16");
formats.push_back("CF32");
return formats;
}
std::string bladeRF_SoapySDR::getNativeStreamFormat(const int, const size_t, double &fullScale) const
{
fullScale = 2048;
return "CS16";
}
SoapySDR::ArgInfoList bladeRF_SoapySDR::getStreamArgsInfo(const int, const size_t) const
{
SoapySDR::ArgInfoList streamArgs;
SoapySDR::ArgInfo buffersArg;
buffersArg.key = "buffers";
buffersArg.value = STRINGIFY(DEF_NUM_BUFFS);
buffersArg.name = "Buffer Count";
buffersArg.description = "Number of async USB buffers.";
buffersArg.units = "buffers";
buffersArg.type = SoapySDR::ArgInfo::INT;
streamArgs.push_back(buffersArg);
SoapySDR::ArgInfo lengthArg;
lengthArg.key = "buflen";
lengthArg.value = STRINGIFY(DEF_BUFF_LEN);
lengthArg.name = "Buffer Length";
lengthArg.description = "Number of bytes per USB buffer, the number must be a multiple of 1024.";
lengthArg.units = "bytes";
lengthArg.type = SoapySDR::ArgInfo::INT;
streamArgs.push_back(lengthArg);
SoapySDR::ArgInfo xfersArg;
xfersArg.key = "transfers";
xfersArg.value = "0";
xfersArg.name = "Num Transfers";
xfersArg.description = "Number of async USB transfers. Use 0 for automatic";
xfersArg.units = "bytes";
xfersArg.type = SoapySDR::ArgInfo::INT;
xfersArg.range = SoapySDR::Range(0, 32);
streamArgs.push_back(xfersArg);
return streamArgs;
}
SoapySDR::Stream *bladeRF_SoapySDR::setupStream(
const int direction,
const std::string &format,
const std::vector<size_t> &channels,
const SoapySDR::Kwargs &args)
{
//check the channel configuration
if (channels.size() > 1 or (channels.size() > 0 and channels.at(0) != 0))
{
throw std::runtime_error("setupStream invalid channel selection");
}
//check the format
if (format == "CF32") {}
else if (format == "CS16") {}
else throw std::runtime_error("setupStream invalid format " + format);
//determine the number of buffers to allocate
int numBuffs = (args.count("buffers") == 0)? 0 : atoi(args.at("buffers").c_str());
if (numBuffs == 0) numBuffs = DEF_NUM_BUFFS;
if (numBuffs == 1) numBuffs++;
//determine the size of each buffer in samples
int bufSize = (args.count("buflen") == 0)? 0 : atoi(args.at("buflen").c_str());
if (bufSize == 0) bufSize = DEF_BUFF_LEN;
if ((bufSize % 1024) != 0) bufSize = ((bufSize/1024) + 1) * 1024;
//determine the number of active transfers
int numXfers = (args.count("transfers") == 0)? 0 : atoi(args.at("transfers").c_str());
if (numXfers == 0) numXfers = numBuffs/2;
if (numXfers > numBuffs) numXfers = numBuffs; //cant have more than available buffers
if (numXfers > 32) numXfers = 32; //libusb limit
//setup the stream for sync tx/rx calls
int ret = bladerf_sync_config(
_dev,
_dir2mod(direction),
BLADERF_FORMAT_SC16_Q11_META,
numBuffs,
bufSize,
numXfers,
1000); //1 second timeout
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_sync_config() returned %d", ret);
throw std::runtime_error("setupStream() " + _err2str(ret));
}
//activate the stream here -- only call once
ret = bladerf_enable_module(_dev, _dir2mod(direction), true);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_enable_module(true) returned %d", ret);
throw std::runtime_error("setupStream() " + _err2str(ret));
}
if (direction == SOAPY_SDR_RX)
{
_rxOverflow = false;
_rxFloats = (format == "CF32");
_rxConvBuff = new int16_t[bufSize*2];
_rxBuffSize = bufSize;
this->updateRxMinTimeoutMs();
}
if (direction == SOAPY_SDR_TX)
{
_txFloats = (format == "CF32");
_txConvBuff = new int16_t[bufSize*2];
_txBuffSize = bufSize;
}
return (SoapySDR::Stream *)(new int(direction));
}
void bladeRF_SoapySDR::closeStream(SoapySDR::Stream *stream)
{
const int direction = *reinterpret_cast<int *>(stream);
//deactivate the stream here -- only call once
const int ret = bladerf_enable_module(_dev, _dir2mod(direction), false);
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_enable_module(false) returned %s", _err2str(ret).c_str());
throw std::runtime_error("closeStream() " + _err2str(ret));
}
//cleanup stream convert buffers
if (direction == SOAPY_SDR_RX)
{
delete [] _rxConvBuff;
}
if (direction == SOAPY_SDR_TX)
{
delete [] _txConvBuff;
}
delete reinterpret_cast<int *>(stream);
}
size_t bladeRF_SoapySDR::getStreamMTU(SoapySDR::Stream *stream) const
{
const int direction = *reinterpret_cast<int *>(stream);
return (direction == SOAPY_SDR_RX)?_rxBuffSize:_txBuffSize;
}
int bladeRF_SoapySDR::activateStream(
SoapySDR::Stream *stream,
const int flags,
const long long timeNs,
const size_t numElems)
{
const int direction = *reinterpret_cast<int *>(stream);
if (direction == SOAPY_SDR_RX)
{
StreamMetadata cmd;
cmd.flags = flags;
cmd.timeNs = timeNs;
cmd.numElems = numElems;
_rxCmds.push(cmd);
}
if (direction == SOAPY_SDR_TX)
{
if (flags != 0) return SOAPY_SDR_NOT_SUPPORTED;
}
return 0;
}
int bladeRF_SoapySDR::deactivateStream(
SoapySDR::Stream *stream,
const int flags,
const long long)
{
const int direction = *reinterpret_cast<int *>(stream);
if (flags != 0) return SOAPY_SDR_NOT_SUPPORTED;
if (direction == SOAPY_SDR_RX)
{
//clear all commands when deactivating
while (not _rxCmds.empty()) _rxCmds.pop();
}
if (direction == SOAPY_SDR_TX)
{
//in a burst -> end it
if (_inTxBurst)
{
//initialize metadata
bladerf_metadata md;
md.timestamp = 0;
md.flags = BLADERF_META_FLAG_TX_BURST_END;
md.status = 0;
//send the tx samples
_txConvBuff[0] = 0;
_txConvBuff[1] = 0;
bladerf_sync_tx(_dev, _txConvBuff, 1, &md, 100/*ms*/);
}
_inTxBurst = false;
}
return 0;
}
int bladeRF_SoapySDR::readStream(
SoapySDR::Stream *,
void * const *buffs,
size_t numElems,
int &flags,
long long &timeNs,
const long timeoutUs)
{
//clip to the available conversion buffer size
numElems = std::min(numElems, _rxBuffSize);
//extract the front-most command
//no command, this is a timeout...
if (_rxCmds.empty()) return SOAPY_SDR_TIMEOUT;
StreamMetadata &cmd = _rxCmds.front();
//clear output metadata
flags = 0;
timeNs = 0;
//return overflow status indicator
if (_rxOverflow)
{
_rxOverflow = false;
flags |= SOAPY_SDR_HAS_TIME;
timeNs = _rxTicksToTimeNs(_rxNextTicks);
return SOAPY_SDR_OVERFLOW;
}
//initialize metadata
bladerf_metadata md;
md.timestamp = 0;
md.flags = 0;
md.status = 0;
//without a soapy sdr time flag, set the blade rf now flag
if ((cmd.flags & SOAPY_SDR_HAS_TIME) == 0) md.flags |= BLADERF_META_FLAG_RX_NOW;
md.timestamp = _timeNsToRxTicks(cmd.timeNs);
if (cmd.numElems > 0) numElems = std::min(cmd.numElems, numElems);
cmd.flags = 0; //clear flags for subsequent calls
//prepare buffers
void *samples = (void *)buffs[0];
if (_rxFloats) samples = _rxConvBuff;
//recv the rx samples
const long timeoutMs = std::max(_rxMinTimeoutMs, timeoutUs/1000);
int ret = bladerf_sync_rx(_dev, samples, numElems, &md, timeoutMs);
if (ret == BLADERF_ERR_TIMEOUT) return SOAPY_SDR_TIMEOUT;
if (ret == BLADERF_ERR_TIME_PAST) return SOAPY_SDR_TIME_ERROR;
if (ret != 0)
{
//any error when this is a finite burst causes the command to be removed
if (cmd.numElems > 0) _rxCmds.pop();
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_sync_rx() returned %s", _err2str(ret).c_str());
return SOAPY_SDR_STREAM_ERROR;
}
//perform the int16 to float conversion
if (_rxFloats)
{
float *output = (float *)buffs[0];
for (size_t i = 0; i < 2 * md.actual_count; i++)
{
output[i] = float(_rxConvBuff[i])/2048;
}
}
//unpack the metadata
flags |= SOAPY_SDR_HAS_TIME;
timeNs = _rxTicksToTimeNs(md.timestamp);
//parse the status
if ((md.status & BLADERF_META_STATUS_OVERRUN) != 0)
{
SoapySDR::log(SOAPY_SDR_SSI, "0");
_rxOverflow = true;
}
//consume from the command if this is a finite burst
if (cmd.numElems > 0)
{
cmd.numElems -= md.actual_count;
if (cmd.numElems == 0) _rxCmds.pop();
}
_rxNextTicks = md.timestamp + md.actual_count;
return md.actual_count;
}
int bladeRF_SoapySDR::writeStream(
SoapySDR::Stream *,
const void * const *buffs,
size_t numElems,
int &flags,
const long long timeNs,
const long timeoutUs)
{
//clear EOB when the last sample will not be transmitted
if (numElems > _txBuffSize) flags &= ~(SOAPY_SDR_END_BURST);
//clip to the available conversion buffer size
numElems = std::min(numElems, _txBuffSize);
//initialize metadata
bladerf_metadata md;
md.timestamp = 0;
md.flags = 0;
md.status = 0;
//time and burst start
if (_inTxBurst)
{
if ((flags & SOAPY_SDR_HAS_TIME) != 0)
{
md.timestamp = _timeNsToTxTicks(timeNs);
md.flags |= BLADERF_META_FLAG_TX_UPDATE_TIMESTAMP;
_txNextTicks = md.timestamp;
}
}
else
{
md.flags |= BLADERF_META_FLAG_TX_BURST_START;
if ((flags & SOAPY_SDR_HAS_TIME) != 0)
{
md.timestamp = _timeNsToTxTicks(timeNs);
}
else
{
md.flags |= BLADERF_META_FLAG_TX_NOW;
bladerf_get_timestamp(_dev, BLADERF_MODULE_TX, &md.timestamp);
}
_txNextTicks = md.timestamp;
}
//end of burst
if ((flags & SOAPY_SDR_END_BURST) != 0)
{
md.flags |= BLADERF_META_FLAG_TX_BURST_END;
}
//prepare buffers
void *samples = (void *)buffs[0];
if (_txFloats) samples = _txConvBuff;
//perform the float to int16 conversion
if (_txFloats)
{
float *input = (float *)buffs[0];
for (size_t i = 0; i < 2 * numElems; i++)
{
_txConvBuff[i] = int16_t(input[i]*2048);
}
}
//send the tx samples
int ret = bladerf_sync_tx(_dev, samples, numElems, &md, timeoutUs/1000);
if (ret == BLADERF_ERR_TIMEOUT) return SOAPY_SDR_TIMEOUT;
if (ret == BLADERF_ERR_TIME_PAST) return SOAPY_SDR_TIME_ERROR;
if (ret != 0)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "bladerf_sync_tx() returned %s", _err2str(ret).c_str());
return SOAPY_SDR_STREAM_ERROR;
}
_txNextTicks += numElems;
//always in a burst after successful tx
_inTxBurst = true;
//parse the status
if ((md.status & BLADERF_META_STATUS_UNDERRUN) != 0)
{
SoapySDR::log(SOAPY_SDR_SSI, "U");
StreamMetadata resp;
resp.flags = 0;
resp.code = SOAPY_SDR_UNDERFLOW;
_txResps.push(resp);
}
//end burst status message
if ((flags & SOAPY_SDR_END_BURST) != 0)
{
StreamMetadata resp;
resp.flags = SOAPY_SDR_END_BURST | SOAPY_SDR_HAS_TIME;
resp.timeNs = this->_txTicksToTimeNs(_txNextTicks);
resp.code = 0;
_txResps.push(resp);
_inTxBurst = false;
}
return numElems;
}
int bladeRF_SoapySDR::readStreamStatus(
SoapySDR::Stream *stream,
size_t &,
int &flags,
long long &timeNs,
const long timeoutUs
)
{
const int direction = *reinterpret_cast<int *>(stream);
if (direction == SOAPY_SDR_RX) return SOAPY_SDR_NOT_SUPPORTED;
//wait for an event to be ready considering the timeout and time
//this is an emulation by polling and waiting on the hardware time
long long timeNowNs = this->getHardwareTime();
const long long exitTimeNs = timeNowNs + (timeoutUs*1000);
while (true)
{
//no status to report, sleep for a bit
if (_txResps.empty()) goto pollSleep;
//no time on the current status, done waiting...
if ((_txResps.front().flags & SOAPY_SDR_HAS_TIME) == 0) break;
//current status time expired, done waiting...
if (_txResps.front().timeNs < timeNowNs) break;
//sleep a bit, never more than time remaining
pollSleep:
usleep(std::min<long>(1000, (exitTimeNs-timeNowNs)/1000));
//check for timeout expired
timeNowNs = this->getHardwareTime();
if (exitTimeNs < timeNowNs) return SOAPY_SDR_TIMEOUT;
}
//extract the most recent status event
if (_txResps.empty()) return SOAPY_SDR_TIMEOUT;
StreamMetadata resp = _txResps.front();
_txResps.pop();
//load the output from the response
flags = resp.flags;
timeNs = resp.timeNs;
return resp.code;
}