Improved logging and soapy stream handling

This commit is contained in:
Thomas Kolb 2023-05-20 22:01:26 +02:00
parent 2b078a4147
commit 112a8a1215
4 changed files with 76 additions and 32 deletions

View file

@ -22,7 +22,7 @@
#define MAX_COARSE_FREQ_OFFSET 0.20f
#define SHOW_DEBUG_LOG 1
#define SHOW_DEBUG_LOG 0
#if SHOW_DEBUG_LOG
# define DEBUG_LOG(...) fprintf(stderr, "DBG: " __VA_ARGS__)
@ -150,7 +150,7 @@ result_t layer1_rx_process(layer1_rx_t *rx, const float complex *samples, size_t
float complex filtered_samples[sample_count];
firfilt_crcf_execute_block(rx->channel_filter, (float complex *)samples, sample_count, filtered_samples);
fprintf(stderr, "\nagc: %f\n", agc_crcf_get_gain(rx->agc));
DEBUG_LOG("\nagc: %f\n", agc_crcf_get_gain(rx->agc));
for(unsigned int i = 0; i < sample_count; i++) {
rx_state_t last_state = rx->state;

View file

@ -165,7 +165,7 @@ static result_t transmit_in_chunks(sdr_ctx_t *sdr, const float complex *samples,
}
}
write(debug_fd, rf_samples, to_transmit_rf*sizeof(rf_samples[0]));
//write(debug_fd, rf_samples, to_transmit_rf*sizeof(rf_samples[0]));
fprintf(stderr, "t");
@ -220,6 +220,7 @@ int main(void)
double old = get_hires_time();
size_t total_samples = 0;
double next_rate_print_time = old + 0.5;
while(m_running) {
double now = get_hires_time();
@ -256,18 +257,21 @@ int main(void)
dump_array_cf(whole_burst, burst_len, 1.0f, "/tmp/tx.cpx");
if(!on_air) {
//RESULT_CHECK(sdr_stop_rx(&sdr));
RESULT_CHECK(sdr_start_tx(&sdr, burst_len * SDR_OVERSAMPLING));
RESULT_CHECK(sdr_flush_tx_buffer(&sdr));
// hopefully no longer needed with latest libhackrf
//RESULT_CHECK(sdr_flush_tx_buffer(&sdr));
}
RESULT_CHECK(transmit_in_chunks(&sdr, whole_burst, burst_len));
on_air = true;
} else if(on_air) { // ret == 0
RESULT_CHECK(sdr_flush_tx_buffer(&sdr));
//RESULT_CHECK(sdr_flush_tx_buffer(&sdr));
RESULT_CHECK(sdr_start_rx(&sdr));
//RESULT_CHECK(sdr_stop_tx(&sdr));
//RESULT_CHECK(sdr_start_rx(&sdr));
on_air = false;
block_tx_for(TX_SWITCH_BACKOFF_AFTER_RX_ON);
@ -293,12 +297,17 @@ int main(void)
}
}
double new = get_hires_time();
total_samples += n_rf_samples;
double rate = total_samples / (new - old);
fprintf(stderr, "\nEstimated rate: %.3f MS/s\n", rate / 1e6);
double new = get_hires_time();
if(new >= next_rate_print_time) {
double rate = total_samples / (new - old);
fprintf(stderr, "\nEstimated rate: %.3f MS/s\n", rate / 1e6);
next_rate_print_time += 0.5;
total_samples = 0;
old = new;
}
rx_retries = 0;

View file

@ -1,5 +1,6 @@
#include <SoapySDR/Device.h>
#include <SoapySDR/Formats.h>
#include <SoapySDR/Logger.h>
#include <liquid/liquid.h>
#include <stdio.h> //printf
#include <stdlib.h> //free
@ -11,6 +12,11 @@
#include "sdr.h"
void soapy_log_handler(const SoapySDRLogLevel logLevel, const char *message)
{
fprintf(stderr, "soapy [%d]: %s\n", logLevel, message);
}
static void close_streams(sdr_ctx_t *ctx)
{
if(ctx->rx_stream) {
@ -42,6 +48,9 @@ result_t sdr_init(sdr_ctx_t *ctx)
nco_crcf_set_frequency(ctx->tx_nco, 2 * M_PI * SDR_TX_IF_SHIFT / SDR_TX_SAMPLING_RATE);
nco_crcf_set_frequency(ctx->rx_nco, 2 * M_PI * SDR_RX_IF_SHIFT / SDR_RX_SAMPLING_RATE);
// set up logging
SoapySDR_registerLogHandler(soapy_log_handler);
//enumerate devices
SoapySDRKwargs *results = SoapySDRDevice_enumerate(NULL, &length);
for (size_t i = 0; i < length; i++)
@ -88,6 +97,21 @@ result_t sdr_init(sdr_ctx_t *ctx)
fprintf(stderr, "\n");
free(ranges);
//setup streams
ctx->rx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL);
if(ctx->rx_stream == NULL) {
fprintf(stderr, "sdr: setupStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
ctx->tx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL);
if(ctx->tx_stream == NULL) {
fprintf(stderr, "sdr: setupStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
//apply settings
if (SoapySDRDevice_setSampleRate(ctx->sdr, SOAPY_SDR_RX, 0, SDR_RX_SAMPLING_RATE) != 0) {
fprintf(stderr, "sdr: setSampleRate fail: %s\n", SoapySDRDevice_lastError());
@ -107,22 +131,6 @@ result_t sdr_init(sdr_ctx_t *ctx)
return ERR_SOAPY;
}
//setup streams
ctx->rx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_RX, SOAPY_SDR_CF32, NULL, 0, NULL);
if(ctx->rx_stream == NULL) {
fprintf(stderr, "sdr: setupStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
ctx->tx_stream = SoapySDRDevice_setupStream(ctx->sdr, SOAPY_SDR_TX, SOAPY_SDR_CF32, NULL, 0, NULL);
if(ctx->tx_stream == NULL) {
fprintf(stderr, "sdr: setupStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
return OK;
}
@ -149,6 +157,7 @@ result_t sdr_start_rx(sdr_ctx_t *ctx)
{
if(SoapySDRDevice_activateStream(ctx->sdr, ctx->rx_stream, 0, 0, 0) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
// set gains
@ -167,17 +176,12 @@ result_t sdr_start_rx(sdr_ctx_t *ctx)
return ERR_SOAPY;
}
return OK;
}
result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size)
{
if(SoapySDRDevice_activateStream(ctx->sdr, ctx->tx_stream, 0, 0, burst_size) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError());
}
// set gain
if(SoapySDRDevice_setGainElement(ctx->sdr, SOAPY_SDR_TX, 0, "AMP", SDR_GAIN_TX_AMP) != 0) {
fprintf(stderr, "sdr: setGainElement fail: %s\n", SoapySDRDevice_lastError());
@ -189,6 +193,32 @@ result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size)
return ERR_SOAPY;
}
if(SoapySDRDevice_activateStream(ctx->sdr, ctx->tx_stream, 0, 0, burst_size) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
return OK;
}
result_t sdr_stop_rx(sdr_ctx_t *ctx)
{
if(SoapySDRDevice_deactivateStream(ctx->sdr, ctx->rx_stream, 0, 0) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
return OK;
}
result_t sdr_stop_tx(sdr_ctx_t *ctx)
{
if(SoapySDRDevice_deactivateStream(ctx->sdr, ctx->tx_stream, 0, 0) != 0) {
fprintf(stderr, "sdr: activateStream fail: %s\n", SoapySDRDevice_lastError());
return ERR_SOAPY;
}
return OK;
}
@ -260,7 +290,8 @@ result_t sdr_rf_to_baseband(sdr_ctx_t *ctx,
const float complex *rf_samples, size_t nrf,
float complex *bb_samples, size_t nbb)
{
if((nbb * SDR_OVERSAMPLING) > nrf) {
if((nbb * SDR_OVERSAMPLING) < nrf) {
fprintf(stderr, "sdr_rf_to_baseband: result would not fit in output: %zd * %d < %zd\n", nbb, SDR_OVERSAMPLING, nrf);
return ERR_SIZE;
}
@ -284,6 +315,7 @@ result_t sdr_baseband_to_rf(sdr_ctx_t *ctx,
float complex *rf_samples, size_t nrf)
{
if((nbb * SDR_OVERSAMPLING) > nrf) {
fprintf(stderr, "sdr_baseband_to_rf: result would not fit in output: %zd * %d > %zd\n", nbb, SDR_OVERSAMPLING, nrf);
return ERR_SIZE;
}

View file

@ -28,6 +28,9 @@ result_t sdr_destroy(sdr_ctx_t *ctx);
result_t sdr_start_rx(sdr_ctx_t *ctx);
result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size);
result_t sdr_stop_rx(sdr_ctx_t *ctx);
result_t sdr_stop_tx(sdr_ctx_t *ctx);
result_t sdr_transmit(sdr_ctx_t *ctx, const float complex *samples, size_t nsamples, long timeout_us);
result_t sdr_receive(sdr_ctx_t *ctx, float complex *samples, size_t *nsamples, long timeout_us);