Skip to content

Commit 4cda158

Browse files
committed
Fix uart race condition dropping bytes on NRF52
In nordic_nrf5_uart_event_handler if the events NRF_UARTE_EVENT_ENDRX and NRF_UARTE_EVENT_RXSTARTED become pending after the check for NRF_UARTE_EVENT_ENDRX but before the check for NRF_UARTE_EVENT_RXSTARTED the RX DMA buffers will be setup incorrectly by nordic_nrf5_uart_event_handler_rxstarted because active_bank hasn't been updated. This cause dropped and incorrect data. This patch fixes that problem by adding a second check for NRF_UARTE_EVENT_ENDRX after checking for NRF_UARTE_EVENT_RXSTARTED and skipping processing if NRF_UARTE_EVENT_ENDRX is set. The subsequent interrupt will process both in the correct order. This ensures that these events cannot be handled out of order and thus fixes the corruption.
1 parent d643034 commit 4cda158

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/serial_api.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,14 @@ static void nordic_nrf5_uart_event_handler(int instance)
727727
}
728728
}
729729

730-
/* Rx DMA buffer has been armed. */
731-
if (nrf_uarte_event_check(nordic_nrf5_uart_register[instance], NRF_UARTE_EVENT_RXSTARTED))
730+
/* Rx DMA buffer has been armed.
731+
*
732+
* Warning - Do not process NRF_UARTE_EVENT_RXSTARTED if NRF_UARTE_EVENT_ENDRX is pending.
733+
* NRF_UARTE_EVENT_RXSTARTED must be processed first or nordic_nrf5_uart_event_handler_rxstarted
734+
* will setup the wrong DMA buffer and cause data to be lost.
735+
*/
736+
if (nrf_uarte_event_check(nordic_nrf5_uart_register[instance], NRF_UARTE_EVENT_RXSTARTED) &&
737+
!nrf_uarte_event_check(nordic_nrf5_uart_register[instance], NRF_UARTE_EVENT_ENDRX))
732738
{
733739
nrf_uarte_event_clear(nordic_nrf5_uart_register[instance], NRF_UARTE_EVENT_RXSTARTED);
734740

0 commit comments

Comments
 (0)