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.
This commit is contained in:
Thomas Kolb 2024-01-03 17:30:20 +01:00
parent 8689ed5b27
commit ac087634dd
2 changed files with 14 additions and 0 deletions

View file

@ -69,6 +69,8 @@ static int tx_callback(hackrf_transfer *transfer)
int result; int result;
int retcode = 0; int retcode = 0;
fprintf(stderr, "tx_callback()\n");
sdr_ctx_t *sdr_ctx = (sdr_ctx_t*)transfer->tx_ctx; sdr_ctx_t *sdr_ctx = (sdr_ctx_t*)transfer->tx_ctx;
if(transfer->valid_length % 2 != 0) { if(transfer->valid_length % 2 != 0) {
@ -96,11 +98,20 @@ static int tx_callback(hackrf_transfer *transfer)
sdr_ctx->tx_done = true; 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++) { for(size_t i = 0; i < samples_read; i++) {
transfer->buffer[2*i + 0] = clamp_float2int8(creal(samples[i])); transfer->buffer[2*i + 0] = clamp_float2int8(creal(samples[i]));
transfer->buffer[2*i + 1] = clamp_float2int8(cimag(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) { if(samples_read < samples_requested) {
// fill the rest with zeros // fill the rest with zeros
memset(&transfer->buffer[2*samples_read], 0, 2*(samples_requested-samples_read)); 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"); CHECK_HACKRF_RESULT(result, "hackrf_start_tx");
ctx->status = SDR_STATUS_TX; ctx->status = SDR_STATUS_TX;
ctx->tx_start_time = 0.0f; // will be updated by tx_callback
return OK; return OK;
} }

View file

@ -32,6 +32,8 @@ typedef struct {
sdr_status_t status; sdr_status_t status;
bool tx_done; bool tx_done;
double tx_duration;
double tx_start_time; // time when tx_callback() was first called
} sdr_ctx_t; } sdr_ctx_t;
result_t sdr_init(sdr_ctx_t *ctx); result_t sdr_init(sdr_ctx_t *ctx);