use bladeRF

This commit is contained in:
Sebastien Bourdeauducq 2023-09-07 20:17:02 +08:00
parent 7fced717e6
commit 9eb13ee140
3 changed files with 43 additions and 50 deletions

View File

@ -6,11 +6,10 @@ OBJS = $(addsuffix .o, $(basename $(notdir $(SOURCES))))
CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(POCKETFFT_DIR) CXXFLAGS = -std=c++14 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(POCKETFFT_DIR)
CXXFLAGS += -g -Wall -Wformat -O2 CXXFLAGS += -g -Wall -Wformat -O2
LIBS = -L$(TINYALSA_DIR)/lib
LIBS += -lGL `pkg-config --static --libs glfw3 alsa` LIBS += -lGL `pkg-config --static --libs glfw3 libbladeRF`
CXXFLAGS += `pkg-config --cflags glfw3 alsa` CXXFLAGS += `pkg-config --cflags glfw3 libbladeRF`
CFLAGS = $(CXXFLAGS) CFLAGS = $(CXXFLAGS)
##--------------------------------------------------------------------- ##---------------------------------------------------------------------

View File

@ -17,7 +17,7 @@
name = "fastsa"; name = "fastsa";
src = self; src = self;
nativeBuildInputs = [ pkgs.pkg-config ]; nativeBuildInputs = [ pkgs.pkg-config ];
propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi pkgs.alsa-lib ]; propagatedBuildInputs = [ pkgs.wayland pkgs.glfw-wayland pkgs.libffi pkgs.libbladeRF ];
preBuild = '' preBuild = ''
export IMGUI_DIR=${imgui_dir} export IMGUI_DIR=${imgui_dir}
export POCKETFFT_DIR=${pocketfft} export POCKETFFT_DIR=${pocketfft}

View File

@ -14,14 +14,14 @@
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h> #include <imgui_impl_opengl3.h>
#include <alsa/asoundlib.h> #include <libbladeRF.h>
#include <pocketfft_hdronly.h> #include <pocketfft_hdronly.h>
static std::atomic<bool> terminate_dsp; static std::atomic<bool> terminate_dsp;
static snd_pcm_t* pcm; struct bladerf* bladerf_dev;
static std::mutex waterfall_data_mutex; static std::mutex waterfall_data_mutex;
static int waterfall_width = 1600; static int waterfall_width = 1600;
@ -33,10 +33,10 @@ static std::atomic<int> fps;
static void dsp_thread() { static void dsp_thread() {
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
size_t len = 6400; size_t len = 8192;
std::vector<short int> frames(len); std::vector<std::complex<int16_t>> frames(len);
std::vector<float> frames_f(len); std::vector<std::complex<float>> frames_f(len);
pocketfft::shape_t shape{len}; pocketfft::shape_t shape{len};
pocketfft::stride_t stride(shape.size()); pocketfft::stride_t stride(shape.size());
size_t tmp=sizeof(float); size_t tmp=sizeof(float);
@ -51,19 +51,19 @@ static void dsp_thread() {
int iterations = 0; int iterations = 0;
auto last_second = std::chrono::steady_clock::now(); auto last_second = std::chrono::steady_clock::now();
while(!terminate_dsp) { while(!terminate_dsp) {
int read_count = snd_pcm_readi(pcm, frames.data(), len); int status;
if(read_count < 0) { if((status = bladerf_sync_rx(bladerf_dev, frames.data(), len, NULL, 0)) != 0) {
std::cerr << "read from audio interface failed: " << snd_strerror(read_count) << std::endl; std::cerr << "failed to receive samples from bladeRF: " << bladerf_strerror(status) << std::endl;
break; break;
} }
for(size_t i=0;i<len;i++) for(size_t i=0;i<len;i++)
frames_f[i] = frames[i]; frames_f[i] = frames[i];
pocketfft::r2c(shape, stride, stride, axes, true, frames_f.data(), frames_ft.data(), 1.0f); pocketfft::c2c(shape, stride, stride, axes, true, frames_f.data(), frames_ft.data(), 1.0f);
{ {
std::lock_guard<std::mutex> guard(waterfall_data_mutex); std::lock_guard<std::mutex> guard(waterfall_data_mutex);
std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], sizeof(int)*waterfall_width*(waterfall_height - 1)); std::memmove(&waterfall_data[waterfall_width], &waterfall_data[0], sizeof(int)*waterfall_width*(waterfall_height - 1));
for(int i=0;i<waterfall_width;i++) { for(int i=0;i<waterfall_width;i++) {
waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(abs(frames_ft[i*len/(4*waterfall_width)])/10000.), 255); waterfall_data[i] = 0xff000000 | 0x010101*std::min(int(abs(frames_ft[i*len/(2*waterfall_width)])/300.), 255);
} }
} }
iterations++; iterations++;
@ -118,55 +118,49 @@ int main(int argc, char* argv[])
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int err; int status;
if ((err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_CAPTURE, 0)) < 0) { struct bladerf_devinfo dev_info;
std::cerr << "cannot open ALSA device: " << snd_strerror(err) << std::endl; bladerf_init_devinfo(&dev_info);
if((status = bladerf_open_with_devinfo(&bladerf_dev, &dev_info)) != 0) {
std::cerr << "cannot open bladeRF device: " << bladerf_strerror(status) << std::endl;
return 1; return 1;
} }
static auto PCMClose = []() { static auto BladeRFClose = []() {
snd_pcm_close(pcm); bladerf_close(bladerf_dev);
}; };
std::atexit(PCMClose); std::atexit(BladeRFClose);
snd_pcm_hw_params_t* pcm_hw_params; if((status = bladerf_set_frequency(bladerf_dev, BLADERF_CHANNEL_RX(0), 1'650'000'000)) != 0) {
if((err = snd_pcm_hw_params_malloc(&pcm_hw_params)) < 0) { std::cerr << "failed to set bladeRF frequency: " << bladerf_strerror(status) << std::endl;
std::cerr << "cannot allocate ALSA hardware parameter structure: " << snd_strerror(err) << std::endl;
return 1; return 1;
} }
if((err = snd_pcm_hw_params_any(pcm, pcm_hw_params)) < 0) { if((status = bladerf_set_sample_rate(bladerf_dev, BLADERF_CHANNEL_RX(0), 40'000'000, NULL)) != 0) {
std::cerr << "cannot initialize ALSA hardware parameter structure: " << snd_strerror (err) << std::endl; std::cerr << "failed to set bladeRF sample rate: " << bladerf_strerror(status) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1; return 1;
} }
if((err = snd_pcm_hw_params_set_access(pcm, pcm_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) { if((status = bladerf_set_bandwidth(bladerf_dev, BLADERF_CHANNEL_RX(0), 35'000'000, NULL)) != 0) {
std::cerr << "cannot set ALSA access type: " << snd_strerror (err) << std::endl; std::cerr << "failed to set bladeRF bandwidth: " << bladerf_strerror(status) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1; return 1;
} }
if((err = snd_pcm_hw_params_set_format(pcm, pcm_hw_params, SND_PCM_FORMAT_S16_LE)) < 0) { if((status = bladerf_set_gain(bladerf_dev, BLADERF_CHANNEL_RX(0), 20)) != 0) {
std::cerr << "cannot set sample format: " << snd_strerror (err) << std::endl; std::cerr << "failed to set bladeRF gain: " << bladerf_strerror(status) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1; return 1;
} }
if((err = snd_pcm_hw_params_set_rate(pcm, pcm_hw_params, 192000, 0)) < 0) { if((status = bladerf_sync_config(bladerf_dev, BLADERF_RX_X1, BLADERF_FORMAT_SC16_Q11,
std::cerr << "cannot set ALSA sample rate: " << snd_strerror (err) << std::endl; 16, /* num_buffers */
snd_pcm_hw_params_free(pcm_hw_params); 8192, /* buffer_size */
8, /* num_transfers */
3500 /* timeout_ms */)) != 0) {
std::cerr << "failed to set bladeRF sync settings: " << bladerf_strerror(status) << std::endl;
return 1; return 1;
} }
if((err = snd_pcm_hw_params_set_channels(pcm, pcm_hw_params, 1)) < 0) { if((status = bladerf_enable_module(bladerf_dev, BLADERF_RX, true) != 0)) {
std::cerr << "cannot set ALSA channel count: " << snd_strerror (err) << std::endl; std::cerr << "failed to enable bladeRF RX: " << bladerf_strerror(status) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
if((err = snd_pcm_hw_params(pcm, pcm_hw_params)) < 0) {
std::cerr << "cannot set ALSA parameters: " << snd_strerror (err) << std::endl;
snd_pcm_hw_params_free(pcm_hw_params);
return 1;
}
snd_pcm_hw_params_free(pcm_hw_params);
if((err = snd_pcm_prepare(pcm)) < 0) {
std::cerr << "cannot prepare ALSA interface: " << snd_strerror (err) << std::endl;
return 1; return 1;
} }
static auto BladeRFDisableRX = []() {
bladerf_enable_module(bladerf_dev, BLADERF_RX, false);
};
std::atexit(BladeRFDisableRX);
terminate_dsp = false; terminate_dsp = false;
static std::thread dsp_thread_h = std::thread(dsp_thread); static std::thread dsp_thread_h = std::thread(dsp_thread);