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

serial::Write implementation is blocking #4

Closed Nemo157 closed 6 years ago

Nemo157 commented 6 years ago

Opening this issue to avoid polluting https://github.com/japaric/nb/issues/13 with too much device specific discussion.


The current impl embedded_hal::serial::Write for Tx<UART0> is blocking, the easy implementation of

fn write(&mut self, byte: u8) -> nb::Result<(), !> {
    let uart = unsafe { &*UART0::ptr() };
    if uart.events_txdrdy.read().bits() == 1 {
         uart.events_txdrdy.reset();
         uart.txd.write(|w| unsafe { w.bits(u32::from(byte)) });
         Ok(())
    } else {
        Err(nb::Error::WouldBlock)
    }
}

doesn't work because events_txdrdy is only set after a byte is transmitted, not when the peripheral is first enabled. It doesn't appear to be possible to manually set event registers as well.

therealprof commented 6 years ago

Thanks for the ticket. I'll have a look.

therealprof commented 6 years ago

Turns out it was rather easy to address by just pumping in a \0 byte during initialisation even before enabling the UART.

Nemo157 commented 6 years ago

I’m surprised that works, but happy to hear it :)