Skip to content
This repository was archived by the owner on May 15, 2021. It is now read-only.

Commit 440261f

Browse files
committed
Made serial hal implementation non-blocking. Fixes #4.
Signed-off-by: Daniel Egger <[email protected]>
1 parent 26bcfb8 commit 440261f

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ keywords = [
1616
license = "0BSD"
1717
name = "nrf51-hal"
1818
repository = "https://github.com/therealprof/nrf51-hal"
19-
version = "0.4.2"
19+
version = "0.4.3"
2020

2121
[dependencies]
2222
bare-metal = "0.1.1"

src/serial.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@ impl Serial<UART0> {
3737
rxpin: PIN<Input<Floating>>,
3838
speed: BAUDRATEW,
3939
) -> Self {
40-
/* Tell UART which pins to use for sending and receiving */
40+
// Fill register with dummy data to trigger txd event
41+
uart.txd.write(|w| unsafe { w.bits(0) });
42+
43+
// Set output TXD and RXD pins
4144
uart.pseltxd
4245
.write(|w| unsafe { w.bits(txpin.get_id().into()) });
4346
uart.pselrxd
4447
.write(|w| unsafe { w.bits(rxpin.get_id().into()) });
4548

46-
/* Set baud rate */
49+
// Set baud rate
4750
uart.baudrate.write(|w| w.baudrate().variant(speed));
4851

49-
/* Enable UART function */
52+
// Enable UART function
5053
uart.enable.write(|w| w.enable().enabled());
5154

52-
/* Fire up transmitting and receiving task */
55+
// Fire up transmitting and receiving task
5356
uart.tasks_starttx.write(|w| unsafe { w.bits(1) });
5457
uart.tasks_startrx.write(|w| unsafe { w.bits(1) });
5558

@@ -68,16 +71,18 @@ impl Serial<UART0> {
6871
impl hal::serial::Read<u8> for Rx<UART0> {
6972
type Error = Error;
7073

71-
fn read(&mut self) -> nb::Result<u8, Error> {
72-
match unsafe { (*UART0::ptr()).events_rxdrdy.read().bits() } {
74+
fn read(&mut self) -> nb::Result<u8, Self::Error> {
75+
let uart = unsafe { &*UART0::ptr() };
76+
match uart.events_rxdrdy.read().bits() {
7377
0 => Err(nb::Error::WouldBlock),
7478
_ => {
75-
/* We're going to pick up the data soon, let's signal the buffer is already waiting for
76-
* more data */
77-
unsafe { (*UART0::ptr()).events_rxdrdy.write(|w| w.bits(0)) };
79+
// Read one 8bit value
80+
let byte = uart.rxd.read().bits() as u8;
81+
82+
// Reset ready for receive event
83+
unsafe { uart.events_rxdrdy.write(|w| w.bits(0)) };
7884

79-
/* Read one 8bit value */
80-
Ok(unsafe { (*UART0::ptr()).rxd.read().bits() } as u8)
85+
Ok(byte)
8186
}
8287
}
8388
}
@@ -86,19 +91,24 @@ impl hal::serial::Read<u8> for Rx<UART0> {
8691
impl hal::serial::Write<u8> for Tx<UART0> {
8792
type Error = !;
8893

89-
fn flush(&mut self) -> nb::Result<(), !> {
94+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
9095
Ok(())
9196
}
9297

93-
fn write(&mut self, byte: u8) -> nb::Result<(), !> {
94-
/* Write one 8bit value */
95-
unsafe { (*UART0::ptr()).txd.write(|w| w.bits(u32::from(byte))) }
98+
fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
99+
let uart = unsafe { &*UART0::ptr() };
100+
// Are we ready for sending out next byte?
101+
if uart.events_txdrdy.read().bits() == 1 {
102+
// Send byte
103+
uart.txd.write(|w| unsafe { w.bits(u32::from(byte)) });
96104

97-
/* Wait until written ... */
98-
while unsafe { (*UART0::ptr()).events_txdrdy.read().bits() } == 0 {}
105+
// Reset ready for transmit event
106+
uart.events_txdrdy.reset();
99107

100-
/* ... and clear read bit, there's no other way this will work */
101-
unsafe { (*UART0::ptr()).events_txdrdy.write(|w| w.bits(0)) };
102-
Ok(())
108+
Ok(())
109+
} else {
110+
// We're not ready, tell application to try again
111+
Err(nb::Error::WouldBlock)
112+
}
103113
}
104114
}

0 commit comments

Comments
 (0)