Skip to content

[BUG] UART ZEPHYR Transport will (most likely) stop to work after a few transactions when using USB-CDC #420

Closed
@ghost

Description

Describe the bug

Problem is the way how ring buffer is used.

static void TransferCallback(const struct device *dev, void *user_data)
{
uint8_t c;
uint32_t size;
uint32_t rx_size;
uint32_t tx_size;
uint8_t *data;
int err;
if (!uart_irq_update(dev))
{
return;
}
if (uart_irq_rx_ready(dev))
{
size = ring_buf_put_claim(&uart_receive_buf, &data, UART_BUFFER_SIZE);
/* read until FIFO empty */
rx_size = uart_fifo_read(dev, data, size);
if (rx_size < 0)
{
/* Error */
}
if (rx_size == size)

A call to ring_buf_put_claim() does not return the free space in the buffer, but the number of bytes that can be written into the also returned buffer pointer until the wrap around point it hit.

image

In my case the ERPC packets are 26 bytes long, the default buffer size appears to be 256 bytes.
When sending the 1st packet ring_buf_put_claim() will return 256. When the 2nd packet is received however it will only return 230. On the next packet 204 and so on.

The problem now is that when the 10th packet arrives ring_buf_put_claim() will only return 22 and the current implementation thus does not empty the receive fifo.

The problematic condition is actually even tested for, but the 'Error' is not handled

if (rx_size == size)
{
if (uart_fifo_read(dev, &c, 1) == 1)
{
/* Error - more data in fifo */
}
}

As a consequence the erpc server on the device will stop responding - with the given example of 26 byte sized service requests this will happen at the 10th invocation.

With a 'regular' UART this current implementation should normally not be a problem as you get an Interrupt per single byte received, but of course that depends on actual HW used.

To Reproduce

Create an erpc server on MCU running ZephyrRTOS, create a service that results in packets with size

Expected behavior

Client should be able to invoke the service on the device indefinltely as many times as desired

Screenshots

n/a

Desktop (please complete the following information)

Steps you didn't forgot to do

  • [ x] I checked if there is no related issue opened/closed.
  • [ x] I checked that there doesn't exist opened PR which is solving this issue.

Additional context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions