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)
This commit is contained in:
parent
364874f7e1
commit
b2e79959b1
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user