From ac087634ddd5bc61213a6863a870a07d85a87bbb Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Wed, 3 Jan 2024 17:30:20 +0100 Subject: [PATCH] sdr: ensure that all samples were transmitted before TX is stopped This is done by tracking the time that the transmitter must stay on based on the number of samples to be transmitted and the sampling rate. --- impl/src/sdr/sdr.c | 12 ++++++++++++ impl/src/sdr/sdr.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/impl/src/sdr/sdr.c b/impl/src/sdr/sdr.c index 00156ab..6c53af7 100644 --- a/impl/src/sdr/sdr.c +++ b/impl/src/sdr/sdr.c @@ -69,6 +69,8 @@ static int tx_callback(hackrf_transfer *transfer) int result; int retcode = 0; + fprintf(stderr, "tx_callback()\n"); + sdr_ctx_t *sdr_ctx = (sdr_ctx_t*)transfer->tx_ctx; if(transfer->valid_length % 2 != 0) { @@ -96,11 +98,20 @@ static int tx_callback(hackrf_transfer *transfer) sdr_ctx->tx_done = true; } + if(sdr_ctx->tx_start_time == 0.0) { + sdr_ctx->tx_start_time = get_hires_time(); + sdr_ctx->tx_duration = 0.0f; + } + for(size_t i = 0; i < samples_read; i++) { transfer->buffer[2*i + 0] = clamp_float2int8(creal(samples[i])); transfer->buffer[2*i + 1] = clamp_float2int8(cimag(samples[i])); } + sdr_ctx->tx_duration += (double)samples_read / SDR_TX_SAMPLING_RATE; + + fprintf(stderr, "copied %u samples to HackRF.\n", samples_read); + if(samples_read < samples_requested) { // fill the rest with zeros memset(&transfer->buffer[2*samples_read], 0, 2*(samples_requested-samples_read)); @@ -255,6 +266,7 @@ result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size) CHECK_HACKRF_RESULT(result, "hackrf_start_tx"); ctx->status = SDR_STATUS_TX; + ctx->tx_start_time = 0.0f; // will be updated by tx_callback return OK; } diff --git a/impl/src/sdr/sdr.h b/impl/src/sdr/sdr.h index 6a29228..b6debc2 100644 --- a/impl/src/sdr/sdr.h +++ b/impl/src/sdr/sdr.h @@ -32,6 +32,8 @@ typedef struct { sdr_status_t status; bool tx_done; + double tx_duration; + double tx_start_time; // time when tx_callback() was first called } sdr_ctx_t; result_t sdr_init(sdr_ctx_t *ctx);