sdr: release buffered data; set sampling rate

This commit is contained in:
Thomas Kolb 2024-01-02 23:25:14 +01:00
parent 2b741acd8a
commit d55e3ab902

View file

@ -16,7 +16,7 @@
#define CHECK_HACKRF_RESULT(result, call) do { \
if(result != HACKRF_SUCCESS) { \
fprintf(stderr, call "() failed: %s (%d)", hackrf_error_name(result), result); \
fprintf(stderr, call "() failed: %s (%d)\n", hackrf_error_name(result), result); \
return ERR_SDR; \
} \
} while(0);
@ -39,7 +39,7 @@ static int rx_callback(hackrf_transfer *transfer)
int result = cbuffercf_push(sdr_ctx->rx_buf, sample);
if(result != LIQUID_OK) {
fprintf(stderr, "cbuffercf_push failed: %d. Samples are lost.\n", result);
fprintf(stderr, "cbuffercf_push failed on sample %zu of %zu: %d. Samples are lost.\n", i, nsamples, result);
break;
}
}
@ -93,6 +93,18 @@ static int tx_callback(hackrf_transfer *transfer)
transfer->buffer[2*i + 1] = clamp_float2int8(cimag(samples[i]));
}
if(samples_read < samples_requested) {
// fill the rest with zeros
memset(&transfer->buffer[2*samples_read], 0, 2*(samples_requested-samples_read));
}
result = cbuffercf_release(sdr_ctx->tx_buf, samples_read);
if(result != LIQUID_OK) {
fprintf(stderr, "cbuffercf_release failed: %d.\n", result);
sem_post(&sdr_ctx->buf_sem);
return ERR_LIQUID;
}
if(sem_post(&sdr_ctx->buf_sem) < 0) {
perror("sem_post");
return HACKRF_ERROR_OTHER;
@ -127,6 +139,8 @@ static result_t stop_streaming(sdr_ctx_t *ctx)
nanosleep(&ts_1ms, NULL);
}
ctx->status = SDR_STATUS_IDLE;
return OK;
}
@ -191,6 +205,9 @@ result_t sdr_start_rx(sdr_ctx_t *ctx)
result = hackrf_set_freq(ctx->hackrf, SDR_RX_FREQ - SDR_RX_IF_SHIFT);
CHECK_HACKRF_RESULT(result, "hackrf_set_freq");
result = hackrf_set_sample_rate(ctx->hackrf, SDR_RX_SAMPLING_RATE);
CHECK_HACKRF_RESULT(result, "hackrf_set_sample_rate");
result = hackrf_set_lna_gain(ctx->hackrf, SDR_GAIN_RX_LNA);
CHECK_HACKRF_RESULT(result, "hackrf_set_lna_gain");
@ -203,6 +220,8 @@ result_t sdr_start_rx(sdr_ctx_t *ctx)
result = hackrf_start_rx(ctx->hackrf, rx_callback, ctx);
CHECK_HACKRF_RESULT(result, "hackrf_start_rx");
ctx->status = SDR_STATUS_RX;
return OK;
}
@ -216,6 +235,9 @@ result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size)
result = hackrf_set_freq(ctx->hackrf, SDR_TX_FREQ);
CHECK_HACKRF_RESULT(result, "hackrf_set_freq");
result = hackrf_set_sample_rate(ctx->hackrf, SDR_TX_SAMPLING_RATE);
CHECK_HACKRF_RESULT(result, "hackrf_set_sample_rate");
result = hackrf_set_amp_enable(ctx->hackrf, SDR_GAIN_TX_AMP > 0);
CHECK_HACKRF_RESULT(result, "hackrf_set_amp_enable");
@ -225,6 +247,8 @@ result_t sdr_start_tx(sdr_ctx_t *ctx, size_t burst_size)
result = hackrf_start_tx(ctx->hackrf, tx_callback, ctx);
CHECK_HACKRF_RESULT(result, "hackrf_start_tx");
ctx->status = SDR_STATUS_TX;
return OK;
}
@ -252,7 +276,7 @@ result_t sdr_transmit(sdr_ctx_t *ctx, const float complex *samples, size_t nsamp
int result = cbuffercf_write(ctx->tx_buf, (float complex*)samples, nsamples);
if(result != LIQUID_OK) {
fprintf(stderr, "cbuffercf_push failed: %d. Samples are lost.\n", result);
fprintf(stderr, "cbuffercf_write failed: %d. Samples are lost.\n", result);
sem_post(&ctx->buf_sem);
return ERR_LIQUID;
}
@ -280,7 +304,7 @@ result_t sdr_receive(sdr_ctx_t *ctx, float complex *samples, size_t *nsamples, l
unsigned int samples_read;
float complex *buf_samples;
result = cbuffercf_read(ctx->tx_buf, *nsamples, &buf_samples, &samples_read);
result = cbuffercf_read(ctx->rx_buf, *nsamples, &buf_samples, &samples_read);
if(result != LIQUID_OK) {
fprintf(stderr, "cbuffercf_read failed: %d. Samples are lost.\n", result);
sem_post(&ctx->buf_sem);
@ -289,6 +313,13 @@ result_t sdr_receive(sdr_ctx_t *ctx, float complex *samples, size_t *nsamples, l
memcpy(samples, buf_samples, samples_read * sizeof(samples[0]));
result = cbuffercf_release(ctx->rx_buf, samples_read);
if(result != LIQUID_OK) {
fprintf(stderr, "cbuffercf_release failed: %d.\n", result);
sem_post(&ctx->buf_sem);
return ERR_LIQUID;
}
if(sem_post(&ctx->buf_sem) < 0) {
perror("sem_post");
return ERR_SDR;