soft_uart: fix pulling a small number of Tx bits

Change-Id: I454c8786697a6f2389d56b350e6e20ca953fe859
Related: OS#4396
This commit is contained in:
Vadim Yanitskiy 2023-11-21 02:25:32 +07:00
parent dab6629f1c
commit ffdd99779d
2 changed files with 40 additions and 73 deletions

View File

@ -217,7 +217,7 @@ static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart, struct msgb
switch (suart->tx.flow_state) {
case SUART_FLOW_ST_IDLE:
if (msgb_length(msg) > 0) { /* if we have pending data */
if (msg && msgb_length(msg) > 0) { /* if we have pending data */
suart->tx.shift_reg = msgb_pull_u8(msg);
suart->tx.flow_state = SUART_FLOW_ST_DATA;
suart->tx.bit_count = 0;
@ -283,21 +283,31 @@ static inline ubit_t osmo_uart_tx_bit(struct osmo_soft_uart *suart, struct msgb
int osmo_soft_uart_tx_ubits(struct osmo_soft_uart *suart, ubit_t *ubits, size_t n_ubits)
{
const struct osmo_soft_uart_cfg *cfg = &suart->cfg;
size_t n_frame_bits;
struct msgb *msg;
size_t n_frame_bits, n_chars;
struct msgb *msg = NULL;
/* calculate UART frame size for the effective config */
n_frame_bits = 1 + cfg->num_data_bits + cfg->num_stop_bits;
if (cfg->parity_mode != OSMO_SUART_PARITY_NONE)
n_frame_bits += 1;
/* allocate a Tx buffer msgb */
msg = msgb_alloc_c(suart, n_ubits / n_frame_bits, "soft_uart_tx");
OSMO_ASSERT(msg != NULL);
/* calculate the number of characters we can fit into n_ubits */
n_chars = n_ubits / n_frame_bits;
if (n_chars == 0) {
/* we can transmit at least one character */
if (suart->tx.flow_state == SUART_FLOW_ST_IDLE)
n_chars = 1;
}
/* call the .tx_cb() to populate the Tx buffer */
OSMO_ASSERT(cfg->tx_cb != NULL);
suart->cfg.tx_cb(cfg->priv, msg);
if (n_chars > 0) {
/* allocate a Tx buffer msgb */
msg = msgb_alloc_c(suart, n_chars, "soft_uart_tx");
OSMO_ASSERT(msg != NULL);
/* call the .tx_cb() to populate the Tx buffer */
OSMO_ASSERT(cfg->tx_cb != NULL);
suart->cfg.tx_cb(cfg->priv, msg);
}
for (size_t i = 0; i < n_ubits; i++)
ubits[i] = osmo_uart_tx_bit(suart, msg);

View File

@ -172,74 +172,31 @@ test_tx_rx_exec_one(n_bits_total=32): 00000101 00000011 00000001 00000001
suart_tx_cb(len=0/4):
test_tx_rx_exec_one(n_bits_total=32): 11111111 11111111 11111111 11111111
======== test_tx_rx_pull_n(): pulling 32 bits (1 at a time)
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
11111111111111111111111111111111
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
01010101010101010101010101010101
======== test_tx_rx_pull_n(): feeding 32 bits into the receiver
suart_rx_cb(flags=00): 55 55 55
======== test_tx_rx_pull_n(): pulling 32 bits (2 at a time)
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
11111111111111111111111111111111
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
01010101010101010101010101010101
======== test_tx_rx_pull_n(): feeding 32 bits into the receiver
suart_rx_cb(flags=00): 55 55 55
======== test_tx_rx_pull_n(): pulling 32 bits (4 at a time)
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
11111111111111111111111111111111
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
01010101011101010101011101010101
======== test_tx_rx_pull_n(): feeding 32 bits into the receiver
suart_rx_cb(flags=00): 55 55
======== test_tx_rx_pull_n(): pulling 32 bits (8 at a time)
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
suart_tx_cb(len=0/0):
11111111111111111111111111111111
suart_tx_cb(len=1/1): 55
suart_tx_cb(len=1/1): 55
01010101011111110101010101111111
======== test_tx_rx_pull_n(): feeding 32 bits into the receiver
suart_rx_cb(flags=00): 55 55