sdr: stop reading packets if the TX buffer is too full

This commit is contained in:
Thomas Kolb 2024-01-05 14:13:11 +01:00
parent e807f0617b
commit 3a4be428b5
4 changed files with 33 additions and 1 deletions

View file

@ -1,5 +1,4 @@
#include <liquid/liquid.h> #include <liquid/liquid.h>
#include <math.h>
#include <assert.h> #include <assert.h>

View file

@ -262,6 +262,18 @@ int main(void)
break; break;
} else if(ret > 0) { } else if(ret > 0) {
// there is a packet to be read. // there is a packet to be read.
// check free buffer space (50 ms required corresponding to 5000 baseband symbols)
size_t buffer_free_space_samples = sdr_get_tx_buffer_free_space(&sdr);
fprintf(stderr, "TX buffer free: %zu\n", buffer_free_space_samples);
if(buffer_free_space_samples < 100000) { // sample count for 50 ms at 2 MHz
// try again after a short delay
fsleep(10e-3);
continue;
}
uint8_t packetbuf[2048]; uint8_t packetbuf[2048];
ret = read(m_tunfd, packetbuf, sizeof(packetbuf)); ret = read(m_tunfd, packetbuf, sizeof(packetbuf));
if(ret < 0) { if(ret < 0) {

View file

@ -366,6 +366,26 @@ result_t sdr_flush_tx_buffer(sdr_ctx_t *ctx)
} }
size_t sdr_get_tx_buffer_free_space(sdr_ctx_t *ctx)
{
size_t free_samples = 0;
if(sem_wait(&ctx->buf_sem) < 0) {
perror("sem_wait");
return 0;
}
free_samples = cbuffercf_space_available(ctx->tx_buf);
if(sem_post(&ctx->buf_sem) < 0) {
perror("sem_post");
return 0;
}
return free_samples;
}
result_t sdr_rf_to_baseband(sdr_ctx_t *ctx, result_t sdr_rf_to_baseband(sdr_ctx_t *ctx,
const float complex *rf_samples, size_t nrf, const float complex *rf_samples, size_t nrf,
float complex *bb_samples, size_t *nbb) float complex *bb_samples, size_t *nbb)

View file

@ -49,6 +49,7 @@ result_t sdr_transmit(sdr_ctx_t *ctx, const float complex *samples, size_t nsamp
result_t sdr_receive(sdr_ctx_t *ctx, 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);
result_t sdr_flush_tx_buffer(sdr_ctx_t *ctx); result_t sdr_flush_tx_buffer(sdr_ctx_t *ctx);
size_t sdr_get_tx_buffer_free_space(sdr_ctx_t *ctx);
/*! /*!
* \brief Convert and resample a received signal to baseband. * \brief Convert and resample a received signal to baseband.