From 4a13041c38c848118181907e912878a42d16e36f Mon Sep 17 00:00:00 2001 From: Mieszko Mierunski Date: Fri, 9 Aug 2019 08:57:51 +0200 Subject: [PATCH] drivers: uart: Fix incorrect len and offset in nrf UARTE async API. Due to longer than expected user callback handling, rx byte counting got out of sync with real values. It leads to incorrect values reported to user. This fix adds sync point at the end of buffer. When using hardware rx counting this issue should not occur. Signed-off-by: Mieszko Mierunski --- drivers/serial/uart_nrfx_uarte.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/serial/uart_nrfx_uarte.c b/drivers/serial/uart_nrfx_uarte.c index 46f856b5b7b..d368c450e4e 100644 --- a/drivers/serial/uart_nrfx_uarte.c +++ b/drivers/serial/uart_nrfx_uarte.c @@ -674,6 +674,18 @@ static void endrx_isr(struct device *dev) size_t rx_len = nrf_uarte_rx_amount_get(uarte) - data->async->rx_offset; + + data->async->rx_total_user_byte_cnt += rx_len; + + if (!hw_rx_counting_enabled(data)) { + /* Prevent too low value of rx_cnt.cnt which may occur due to + * latencies in handling of the RXRDY interrupt. Because whole + * buffer was filled we can be sure that rx_total_user_byte_cnt + * is current total number of received bytes. + */ + data->async->rx_cnt.cnt = data->async->rx_total_user_byte_cnt; + } + struct uart_event evt = { .type = UART_RX_RDY, .data.rx.buf = data->async->rx_buf, @@ -690,7 +702,6 @@ static void endrx_isr(struct device *dev) data->async->rx_buf = data->async->rx_next_buf; data->async->rx_next_buf = NULL; - data->async->rx_total_user_byte_cnt += rx_len; data->async->rx_offset = 0; } else { data->async->rx_buf = NULL;