Skip to content
Snippets Groups Projects
Commit b2e79959 authored by jfbortolotti's avatar jfbortolotti Committed by Kaspar Schleiser
Browse files

cpu: nRF52: move clearing of TXRDY flag

... before writing the data to avoid an infinite loop in case of multiple thread accessing the UART. (#6029) 
parent 364874f7
No related branches found
No related tags found
No related merge requests found
...@@ -140,12 +140,22 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len) ...@@ -140,12 +140,22 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len)
{ {
if (uart == 0) { if (uart == 0) {
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
/* This section of the function is not thread safe:
- another thread may mess up with the uart at the same time.
In order to avoid an infinite loop in the interrupted thread,
the TXRDY flag must be cleared before writing the data to be
sent and not after. This way, the higher priority thread will
exit this function with the TXRDY flag set, then the interrupted
thread may have not transmitted his data but will still exit the
while loop.
*/
/* reset ready flag */
NRF_UART0->EVENTS_TXDRDY = 0;
/* write data into transmit register */ /* write data into transmit register */
NRF_UART0->TXD = data[i]; NRF_UART0->TXD = data[i];
/* wait for any transmission to be done */ /* wait for any transmission to be done */
while (NRF_UART0->EVENTS_TXDRDY == 0) {} while (NRF_UART0->EVENTS_TXDRDY == 0) {}
/* reset ready flag */
NRF_UART0->EVENTS_TXDRDY = 0;
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment