Skip to content

Commit

Permalink
bladeRF-cli: fix tx buffer malloc for 8b sample format
Browse files Browse the repository at this point in the history
closes #936
  • Loading branch information
Aleix Galan authored and rthomp10 committed Aug 14, 2024
1 parent fe3304d commit 3db52ac
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions host/utilities/bladeRF-cli/src/cmd/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
{
int status = 0;
unsigned int samples_per_buffer;
int16_t *tx_buffer;
void *tx_buffer;
struct tx_params *tx_params = tx->params;
unsigned int repeats_remaining;
unsigned int delay_us;
Expand All @@ -55,6 +55,9 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
bladerf_sample_rate sample_rate = 0;
int i;

unsigned int size_of_format =
(s->bit_mode_8bit) ? sizeof(int8_t) : sizeof(int16_t);

enum state { INIT, READ_FILE, DELAY, PAD_TRAILING, DONE };
enum state state = INIT;

Expand Down Expand Up @@ -93,7 +96,7 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
delay_samples_remaining = delay_samples;

/* Allocate a buffer to hold each block of samples to transmit */
tx_buffer = (int16_t *)malloc(samples_per_buffer * 2 * sizeof(int16_t));
tx_buffer = malloc(samples_per_buffer * 2 * size_of_format);
if (tx_buffer == NULL) {
status = CLI_RET_MEM;
set_last_error(&tx->last_error, ETYPE_ERRNO,
Expand All @@ -105,7 +108,7 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
while (state != DONE && status == 0) {
unsigned char requests;
unsigned int buffer_samples_remaining = samples_per_buffer;
int16_t *tx_buffer_current = tx_buffer;
void *tx_buffer_current = tx_buffer;

/* Stop stream on STOP or SHUTDOWN, but only clear STOP. This will keep
* the SHUTDOWN request around so we can read it when determining
Expand All @@ -127,7 +130,7 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)

/* Read from the input file */
samples_populated =
fread(tx_buffer_current, 2 * sizeof(int16_t),
fread(tx_buffer_current, 2 * size_of_format,
buffer_samples_remaining, tx->file_mgmt.file);

assert(samples_populated <= UINT_MAX);
Expand Down Expand Up @@ -168,7 +171,7 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
delay_samples_remaining);

memset(tx_buffer_current, 0,
samples_populated * 2 * sizeof(uint16_t));
samples_populated * 2 * size_of_format);

delay_samples_remaining -= (unsigned int)samples_populated;

Expand All @@ -180,7 +183,7 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
case PAD_TRAILING:
/* Populate the remainder of the buffer with zeros */
memset(tx_buffer_current, 0,
buffer_samples_remaining * 2 * sizeof(uint16_t));
buffer_samples_remaining * 2 * size_of_format);

state = DONE;
break;
Expand All @@ -193,7 +196,9 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
/* Advance the buffer pointer.
* Remember, two int16_t's make up 1 sample in the SC16Q11 format */
buffer_samples_remaining -= (unsigned int)samples_populated;
tx_buffer_current += (2 * samples_populated);
/* GCC allows for void pointer arithmetic, but not the C standard */
tx_buffer_current = (int8_t*) tx_buffer_current
+ (2 * samples_populated * size_of_format);
}

/* If there were no errors, transmit the data buffer */
Expand All @@ -211,7 +216,7 @@ static int tx_task_exec_running(struct rxtx_data *tx, struct cli_state *s)
const unsigned int num_buffers = tx->data_mgmt.num_buffers;
unsigned int i;

memset(tx_buffer, 0, samples_per_buffer * 2 * sizeof(int16_t));
memset(tx_buffer, 0, samples_per_buffer * 2 * size_of_format);
for (i = 0; i < (num_buffers + 1) && status == 0; i++) {
status = bladerf_sync_tx(s->dev, tx_buffer, samples_per_buffer,
NULL, timeout_ms);
Expand Down

0 comments on commit 3db52ac

Please sign in to comment.