2021-03-20 21:53:44 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <spdlog/spdlog.h>
|
|
|
|
#include <module.h>
|
|
|
|
#include <gui/gui.h>
|
|
|
|
#include <signal_path/signal_path.h>
|
|
|
|
#include <core.h>
|
|
|
|
#include <gui/style.h>
|
|
|
|
#include <config.h>
|
|
|
|
#include <options.h>
|
|
|
|
#include <gui/widgets/stepped_slider.h>
|
|
|
|
#include <libbladeRF.h>
|
|
|
|
#include <dsp/processing.h>
|
|
|
|
|
|
|
|
#define CONCAT(a, b) ((std::string(a) + b).c_str())
|
|
|
|
|
|
|
|
#define NUM_BUFFERS 128
|
2021-05-02 02:23:10 +02:00
|
|
|
#define NUM_TRANSFERS 1
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
SDRPP_MOD_INFO {
|
|
|
|
/* Name: */ "bladerf_source",
|
|
|
|
/* Description: */ "BladeRF source module for SDR++",
|
|
|
|
/* Author: */ "Ryzerth",
|
|
|
|
/* Version: */ 0, 1, 0,
|
|
|
|
/* Max instances */ 1
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ConfigManager config;
|
|
|
|
|
|
|
|
class BladeRFSourceModule : public ModuleManager::Instance {
|
|
|
|
public:
|
|
|
|
BladeRFSourceModule(std::string name) {
|
|
|
|
this->name = name;
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
sampleRate = 1000000.0;
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
handler.ctx = this;
|
|
|
|
handler.selectHandler = menuSelected;
|
|
|
|
handler.deselectHandler = menuDeselected;
|
|
|
|
handler.menuHandler = menuHandler;
|
|
|
|
handler.startHandler = start;
|
|
|
|
handler.stopHandler = stop;
|
|
|
|
handler.tuneHandler = tune;
|
|
|
|
handler.stream = &stream;
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
selectFirst();
|
|
|
|
|
|
|
|
// Select device here
|
|
|
|
core::setInputSampleRate(sampleRate);
|
|
|
|
|
|
|
|
sigpath::sourceManager.registerSource("BladeRF", &handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
~BladeRFSourceModule() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable() {
|
|
|
|
enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh() {
|
|
|
|
devListTxt = "";
|
|
|
|
|
|
|
|
if (devInfoList != NULL) {
|
|
|
|
bladerf_free_device_list(devInfoList);
|
|
|
|
}
|
|
|
|
|
|
|
|
devCount = bladerf_get_device_list(&devInfoList);
|
|
|
|
if (devCount < 0) {
|
|
|
|
spdlog::error("Could not list devices");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < devCount; i++) {
|
2021-05-03 04:20:48 +02:00
|
|
|
// Keep only the first 32 character of the serial number for display
|
2021-05-03 18:10:52 +02:00
|
|
|
devListTxt += std::string(devInfoList[i].serial).substr(0, 16);
|
2021-03-20 21:53:44 +01:00
|
|
|
devListTxt += '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectFirst() {
|
|
|
|
if (devCount > 0) { selectByInfo(&devInfoList[0]); }
|
|
|
|
}
|
|
|
|
|
|
|
|
void selectByInfo(bladerf_devinfo* info) {
|
|
|
|
int ret = bladerf_open_with_devinfo(&openDev, info);
|
|
|
|
if (ret != 0) {
|
|
|
|
spdlog::error("Could not open device {0}", info->serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Gather info about the BladeRF's ranges
|
2021-03-20 21:53:44 +01:00
|
|
|
channelCount = bladerf_get_channel_count(openDev, BLADERF_RX);
|
2021-05-02 02:23:10 +02:00
|
|
|
bladerf_get_sample_rate_range(openDev, BLADERF_CHANNEL_RX(0), &srRange);
|
|
|
|
bladerf_get_bandwidth_range(openDev, BLADERF_CHANNEL_RX(0), &bwRange);
|
|
|
|
bladerf_get_gain_range(openDev, BLADERF_CHANNEL_RX(0), &gainRange);
|
|
|
|
|
2021-05-03 18:10:52 +02:00
|
|
|
// Generate sampleRate and Bandwidth lists
|
|
|
|
sampleRates.clear();
|
|
|
|
sampleRatesTxt = "";
|
|
|
|
sampleRates.push_back(srRange->min);
|
|
|
|
sampleRatesTxt += getBandwdithScaled(srRange->min);
|
|
|
|
sampleRatesTxt += '\0';
|
|
|
|
for (int i = 2000000; i < srRange->max; i += 2000000) {
|
|
|
|
sampleRates.push_back(i);
|
|
|
|
sampleRatesTxt += getBandwdithScaled(i);
|
|
|
|
sampleRatesTxt += '\0';
|
|
|
|
}
|
|
|
|
sampleRates.push_back(srRange->max);
|
|
|
|
sampleRatesTxt += getBandwdithScaled(srRange->max);
|
|
|
|
sampleRatesTxt += '\0';
|
|
|
|
|
|
|
|
// Generate bandwidth list
|
|
|
|
bandwidths.clear();
|
|
|
|
bandwidthsTxt = "";
|
|
|
|
bandwidths.push_back(bwRange->min);
|
|
|
|
bandwidthsTxt += getBandwdithScaled(bwRange->min);
|
|
|
|
bandwidthsTxt += '\0';
|
|
|
|
for (int i = 2000000; i < bwRange->max; i += 2000000) {
|
|
|
|
bandwidths.push_back(i);
|
|
|
|
bandwidthsTxt += getBandwdithScaled(i);
|
|
|
|
bandwidthsTxt += '\0';
|
|
|
|
}
|
|
|
|
bandwidths.push_back(bwRange->max);
|
|
|
|
bandwidthsTxt += getBandwdithScaled(bwRange->max);
|
|
|
|
bandwidthsTxt += '\0';
|
2021-05-03 20:21:30 +02:00
|
|
|
bandwidthsTxt += "Auto";
|
|
|
|
bandwidthsTxt += '\0';
|
2021-05-03 18:10:52 +02:00
|
|
|
|
|
|
|
srId = 0;
|
|
|
|
sampleRate = sampleRates[0];
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-05-03 20:21:30 +02:00
|
|
|
// Set bandwidth to auto as default
|
|
|
|
bwId = bandwidths.size();
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
bladerf_close(openDev);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string getBandwdithScaled(double bw) {
|
|
|
|
char buf[1024];
|
|
|
|
if (bw >= 1000000.0) {
|
|
|
|
sprintf(buf, "%.1lfMHz", bw / 1000000.0);
|
|
|
|
}
|
|
|
|
else if (bw >= 1000.0) {
|
|
|
|
sprintf(buf, "%.1lfKHz", bw / 1000.0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, "%.1lfHz", bw);
|
|
|
|
}
|
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menuSelected(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Menu Select!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menuDeselected(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Menu Deselect!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
if (_this->running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_this->devCount == 0) { return; }
|
|
|
|
|
|
|
|
// Open device
|
|
|
|
bladerf_devinfo info = _this->devInfoList[_this->devId];
|
|
|
|
int ret = bladerf_open_with_devinfo(&_this->openDev, &info);
|
|
|
|
if (ret != 0) {
|
|
|
|
spdlog::error("Could not open device {0}", info.serial);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Calculate buffer size, must be a multiple of 1024
|
2021-05-02 02:23:10 +02:00
|
|
|
_this->bufferSize = _this->sampleRate / 200.0;
|
|
|
|
_this->bufferSize /= 1024;
|
|
|
|
_this->bufferSize *= 1024;
|
2021-05-03 02:08:49 +02:00
|
|
|
if (_this->bufferSize < 1024) { _this->bufferSize = 1024; }
|
2021-05-02 02:23:10 +02:00
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Setup device parameters
|
|
|
|
bladerf_set_sample_rate(_this->openDev, BLADERF_CHANNEL_RX(0), _this->sampleRate, NULL);
|
2021-03-20 21:53:44 +01:00
|
|
|
bladerf_set_frequency(_this->openDev, BLADERF_CHANNEL_RX(0), _this->freq);
|
2021-05-04 20:41:23 +02:00
|
|
|
bladerf_set_bandwidth(_this->openDev, BLADERF_CHANNEL_RX(0), (_this->bwId == _this->bandwidths.size()) ?
|
|
|
|
std::clamp<uint64_t>(_this->sampleRate, _this->bwRange->min, _this->bwRange->max) : _this->bandwidths[_this->bwId], NULL);
|
2021-05-02 02:23:10 +02:00
|
|
|
bladerf_set_gain_mode(_this->openDev, BLADERF_CHANNEL_RX(0), BLADERF_GAIN_MANUAL);
|
|
|
|
bladerf_set_gain(_this->openDev, BLADERF_CHANNEL_RX(0), _this->testGain);
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
// Setup syncronous transfer
|
|
|
|
bladerf_sync_config(_this->openDev, BLADERF_RX_X1, BLADERF_FORMAT_SC16_Q11, 16, _this->bufferSize, 8, 3500);
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Enable streaming
|
2021-03-20 21:53:44 +01:00
|
|
|
bladerf_enable_module(_this->openDev, BLADERF_CHANNEL_RX(0), true);
|
|
|
|
|
|
|
|
_this->running = true;
|
|
|
|
_this->workerThread = std::thread(&BladeRFSourceModule::worker, _this);
|
|
|
|
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Start!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stop(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
if (!_this->running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_this->running = false;
|
|
|
|
_this->stream.stopWriter();
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Disable streaming
|
2021-03-20 21:53:44 +01:00
|
|
|
bladerf_enable_module(_this->openDev, BLADERF_CHANNEL_RX(0), false);
|
2021-05-03 04:20:48 +02:00
|
|
|
|
|
|
|
// Wait for read worker to terminate
|
2021-03-20 21:53:44 +01:00
|
|
|
if (_this->workerThread.joinable()) {
|
|
|
|
_this->workerThread.join();
|
|
|
|
}
|
|
|
|
|
2021-05-03 04:20:48 +02:00
|
|
|
// Close device
|
2021-03-20 21:53:44 +01:00
|
|
|
bladerf_close(_this->openDev);
|
|
|
|
|
|
|
|
_this->stream.clearWriteStop();
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Stop!", _this->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tune(double freq, void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
_this->freq = freq;
|
|
|
|
if (_this->running) {
|
|
|
|
bladerf_set_frequency(_this->openDev, BLADERF_CHANNEL_RX(0), _this->freq);
|
|
|
|
}
|
|
|
|
spdlog::info("BladeRFSourceModule '{0}': Tune: {1}!", _this->name, freq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void menuHandler(void* ctx) {
|
|
|
|
BladeRFSourceModule* _this = (BladeRFSourceModule*)ctx;
|
|
|
|
float menuWidth = ImGui::GetContentRegionAvailWidth();
|
|
|
|
|
|
|
|
if (_this->running) { style::beginDisabled(); }
|
|
|
|
|
|
|
|
ImGui::SetNextItemWidth(menuWidth);
|
2021-05-03 02:08:49 +02:00
|
|
|
if (ImGui::Combo(CONCAT("##_balderf_dev_sel_", _this->name), &_this->devId, _this->devListTxt.c_str())) {
|
2021-03-20 21:53:44 +01:00
|
|
|
// Select device
|
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
|
|
|
// Save config
|
|
|
|
}
|
|
|
|
|
2021-05-03 18:10:52 +02:00
|
|
|
if (ImGui::Combo(CONCAT("##_balderf_sr_sel_", _this->name), &_this->srId, _this->sampleRatesTxt.c_str())) {
|
|
|
|
_this->sampleRate = _this->sampleRates[_this->srId];
|
2021-03-20 21:53:44 +01:00
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
|
|
|
// Save config
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
float refreshBtnWdith = menuWidth - ImGui::GetCursorPosX();
|
2021-05-03 02:08:49 +02:00
|
|
|
if (ImGui::Button(CONCAT("Refresh##_balderf_refr_", _this->name), ImVec2(refreshBtnWdith, 0))) {
|
2021-03-20 21:53:44 +01:00
|
|
|
_this->refresh();
|
|
|
|
config.aquire();
|
|
|
|
std::string devSerial = config.conf["device"];
|
|
|
|
config.release();
|
|
|
|
// Reselect device
|
|
|
|
core::setInputSampleRate(_this->sampleRate);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_this->running) { style::endDisabled(); }
|
|
|
|
|
2021-05-03 20:21:30 +02:00
|
|
|
ImGui::Text("Bandwidth");
|
|
|
|
ImGui::SameLine();
|
|
|
|
ImGui::SetNextItemWidth(menuWidth - ImGui::GetCursorPosX());
|
2021-05-03 18:10:52 +02:00
|
|
|
if (ImGui::Combo(CONCAT("##_balderf_bw_sel_", _this->name), &_this->bwId, _this->bandwidthsTxt.c_str())) {
|
|
|
|
if (_this->running) {
|
2021-05-04 20:41:23 +02:00
|
|
|
bladerf_set_bandwidth(_this->openDev, BLADERF_CHANNEL_RX(0), (_this->bwId == _this->bandwidths.size()) ?
|
|
|
|
std::clamp<uint64_t>(_this->sampleRate, _this->bwRange->min, _this->bwRange->max) : _this->bandwidths[_this->bwId], NULL);
|
2021-05-03 18:10:52 +02:00
|
|
|
}
|
|
|
|
// Save config
|
|
|
|
}
|
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
// General config BS
|
2021-05-03 02:08:49 +02:00
|
|
|
if (ImGui::SliderInt("Test Gain", &_this->testGain, (_this->gainRange != NULL) ? _this->gainRange->min : 0, (_this->gainRange != NULL) ? _this->gainRange->max : 60)) {
|
2021-05-02 02:23:10 +02:00
|
|
|
if (_this->running) {
|
|
|
|
spdlog::info("Setting gain to {0}", _this->testGain);
|
|
|
|
bladerf_set_gain(_this->openDev, BLADERF_CHANNEL_RX(0), _this->testGain);
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 21:53:44 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void worker() {
|
2021-05-02 02:23:10 +02:00
|
|
|
int16_t* buffer = new int16_t[bufferSize * 2];
|
|
|
|
bladerf_metadata meta;
|
2021-05-03 04:20:48 +02:00
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
while (true) {
|
2021-05-03 04:20:48 +02:00
|
|
|
// Receive from the stream and break on error
|
2021-05-02 02:23:10 +02:00
|
|
|
int ret = bladerf_sync_rx(openDev, buffer, bufferSize, &meta, 3500);
|
2021-05-03 04:20:48 +02:00
|
|
|
if (ret != 0) { break; }
|
|
|
|
|
|
|
|
// Convert to complex float and swap buffers
|
2021-05-03 02:08:49 +02:00
|
|
|
volk_16i_s32f_convert_32f((float*)stream.writeBuf, buffer, 32768.0f, bufferSize * 2);
|
2021-05-02 02:23:10 +02:00
|
|
|
if (!stream.swap(bufferSize)) { break; }
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
2021-05-03 04:20:48 +02:00
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
delete[] buffer;
|
2021-03-20 21:53:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
bladerf* openDev;
|
|
|
|
bool enabled = true;
|
|
|
|
dsp::stream<dsp::complex_t> stream;
|
|
|
|
double sampleRate;
|
|
|
|
SourceManager::SourceHandler handler;
|
|
|
|
bool running = false;
|
|
|
|
double freq;
|
|
|
|
int devId = 0;
|
|
|
|
int srId = 0;
|
2021-05-03 18:10:52 +02:00
|
|
|
int bwId = 0;
|
2021-05-02 02:23:10 +02:00
|
|
|
int chanId = 0;
|
2021-03-20 21:53:44 +01:00
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
int channelCount;
|
|
|
|
|
2021-05-03 02:08:49 +02:00
|
|
|
const bladerf_range* srRange = NULL;
|
|
|
|
const bladerf_range* bwRange = NULL;
|
|
|
|
const bladerf_range* gainRange = NULL;
|
2021-05-02 02:23:10 +02:00
|
|
|
|
2021-05-03 18:10:52 +02:00
|
|
|
std::vector<uint64_t> sampleRates;
|
|
|
|
std::string sampleRatesTxt;
|
|
|
|
std::vector<uint64_t> bandwidths;
|
|
|
|
std::string bandwidthsTxt;
|
|
|
|
|
2021-05-02 02:23:10 +02:00
|
|
|
int bufferSize;
|
2021-03-20 21:53:44 +01:00
|
|
|
struct bladerf_stream* rxStream;
|
|
|
|
|
2021-05-03 02:08:49 +02:00
|
|
|
int testGain = 0;
|
2021-05-02 02:23:10 +02:00
|
|
|
|
2021-03-20 21:53:44 +01:00
|
|
|
std::thread workerThread;
|
|
|
|
|
|
|
|
int devCount = 0;
|
|
|
|
bladerf_devinfo* devInfoList = NULL;
|
|
|
|
std::string devListTxt;
|
|
|
|
};
|
|
|
|
|
|
|
|
MOD_EXPORT void _INIT_() {
|
|
|
|
json def = json({});
|
|
|
|
def["devices"] = json({});
|
|
|
|
def["device"] = "";
|
|
|
|
config.setPath(options::opts.root + "/bladerf_config.json");
|
|
|
|
config.load(def);
|
|
|
|
config.enableAutoSave();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT ModuleManager::Instance* _CREATE_INSTANCE_(std::string name) {
|
|
|
|
return new BladeRFSourceModule(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _DELETE_INSTANCE_(ModuleManager::Instance* instance) {
|
|
|
|
delete (BladeRFSourceModule*)instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOD_EXPORT void _END_() {
|
|
|
|
config.disableAutoSave();
|
|
|
|
config.save();
|
|
|
|
}
|