nrf-rs / nrf51-hal

A Rust embedded-hal impl crate for the Nordic nrf51 series of microcontrollers
BSD Zero Clause License
19 stars 13 forks source link

Fix: UART hangs waiting for TXDRDY event #18

Closed guncha closed 5 years ago

guncha commented 5 years ago

I'm using this crate on the MicroBit and this issue is something that started happening after I enabled the SoftDevice. UART would print half of the message and then hang there indefinitely. It's obvious what's happening in retrospect, but it sure didn't seem that way before hours of debugging.

What's happening is that the SoftDevice is triggering quite a lot of interrupts and doing its work while the main thread is paused. If it just so happens that the interrupt is triggered after the Send byte, but before the Reset TXD ready line, it's possible that the UART hardware will be done with writing the byte by the time the interrupt returns and already set the TXD ready flag, and upon return, it will be cleared and never set again. The fix is trivial - just change the order of those two operations so the TXD ready flag is cleared before the next write.

I'm kind of extrapolating that the same issue happens with the RX part though I'm not using it and didn't experience it.