Skip to content

Commit d29705d

Browse files
bors[bot]hargoniX
andauthored
Merge #63
63: Initial UART(E) implementation r=robyoung a=hargoniX This implements an abstraction for UARTE, very similar to the general I2C abstracttion. At the moment the example doesn't compile for nrf52833 yet, once nrf-rs/nrf-hal#281 is finally merged that should be fairly trivial to do though. Co-authored-by: Henrik Böving <[email protected]>
2 parents 458d705 + deaf0a8 commit d29705d

File tree

11 files changed

+177
-60
lines changed

11 files changed

+177
-60
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2121
- Add Board struct following the pattern used in other nrf board support crates.
2222
- Add magnetometer example.
2323
- LEDs on the micro:bit V1 are now turned off per default
24+
- UART(E) is now exposed in the same way as I2C
2425

2526
## [0.10.1] - 2021-05-25
2627

examples/serial-hal-blocking-echo/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ cortex-m-rt = "0.6.12"
88
panic-halt = "0.2.0"
99
defmt-rtt = "0.2.0"
1010
nb = "0.1.2"
11+
embedded-hal = "0.2.6"
1112

1213
[dependencies.microbit]
1314
path = "../../microbit"
15+
optional = true
16+
17+
[dependencies.microbit-v2]
18+
path = "../../microbit-v2"
19+
optional = true
20+
21+
[features]
22+
v1 = ["microbit"]
23+
v2 = ["microbit-v2"]

examples/serial-hal-blocking-echo/src/main.rs

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,56 @@
44
use panic_halt as _;
55

66
use core::fmt::Write;
7-
use microbit::hal;
8-
use microbit::hal::prelude::*;
9-
use microbit::hal::uart::Baudrate;
7+
8+
#[cfg(feature = "v1")]
9+
use microbit::{
10+
hal::prelude::*,
11+
hal::uart,
12+
hal::uart::{Baudrate, Parity},
13+
};
14+
15+
#[cfg(feature = "v2")]
16+
use microbit::{
17+
hal::prelude::*,
18+
hal::uarte,
19+
hal::uarte::{Baudrate, Parity},
20+
};
1021

1122
use cortex_m_rt::entry;
1223

24+
#[cfg(feature = "v2")]
25+
mod serial_setup;
26+
#[cfg(feature = "v2")]
27+
use serial_setup::UartePort;
28+
1329
#[entry]
1430
fn main() -> ! {
15-
if let Some(p) = microbit::Peripherals::take() {
16-
let gpio = hal::gpio::p0::Parts::new(p.GPIO);
17-
18-
/* Initialise serial port on the micro:bit */
19-
let mut serial = microbit::serial_port!(gpio, p.UART0, Baudrate::BAUD115200);
20-
21-
/* Print a nice hello message */
22-
write!(serial, "Please type characters to echo:\r\n");
23-
24-
/* Endless loop */
25-
loop {
26-
/* Read and echo back */
27-
if let Ok(c) = nb::block!(serial.read()) {
28-
let _ = nb::block!(serial.write(c));
29-
}
30-
}
31-
}
31+
let board = microbit::Board::take().unwrap();
32+
33+
#[cfg(feature = "v1")]
34+
let mut serial = {
35+
uart::Uart::new(
36+
board.UART0,
37+
board.uart.into(),
38+
Parity::EXCLUDED,
39+
Baudrate::BAUD115200,
40+
)
41+
};
42+
43+
#[cfg(feature = "v2")]
44+
let mut serial = {
45+
let serial = uarte::Uarte::new(
46+
board.UARTE0,
47+
board.uart.into(),
48+
Parity::EXCLUDED,
49+
Baudrate::BAUD115200,
50+
);
51+
UartePort::new(serial)
52+
};
3253

3354
loop {
34-
continue;
55+
write!(serial, "Hello World:\r\n").unwrap();
56+
let input = nb::block!(serial.read()).unwrap();
57+
write!(serial, "You said: {}\r\n", input as char).unwrap();
3558
}
3659
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use core::fmt;
2+
use embedded_hal::blocking::serial as bserial;
3+
use embedded_hal::serial;
4+
use microbit::hal::uarte::{Error, Instance, Uarte, UarteRx, UarteTx};
5+
6+
static mut TX_BUF: [u8; 1] = [0; 1];
7+
static mut RX_BUF: [u8; 1] = [0; 1];
8+
9+
pub struct UartePort<T: Instance>(UarteTx<T>, UarteRx<T>);
10+
11+
impl<T: Instance> UartePort<T> {
12+
pub fn new(serial: Uarte<T>) -> UartePort<T> {
13+
let (tx, rx) = serial
14+
.split(unsafe { &mut TX_BUF }, unsafe { &mut RX_BUF })
15+
.unwrap();
16+
UartePort(tx, rx)
17+
}
18+
}
19+
20+
impl<T: Instance> fmt::Write for UartePort<T> {
21+
fn write_str(&mut self, s: &str) -> fmt::Result {
22+
self.0.write_str(s)
23+
}
24+
}
25+
26+
impl<T: Instance> serial::Write<u8> for UartePort<T> {
27+
type Error = Error;
28+
29+
fn write(&mut self, b: u8) -> nb::Result<(), Self::Error> {
30+
self.0.write(b)
31+
}
32+
33+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
34+
self.0.flush()
35+
}
36+
}
37+
38+
impl<T: Instance> bserial::write::Default<u8> for UartePort<T> {}
39+
40+
impl<T: Instance> serial::Read<u8> for UartePort<T> {
41+
type Error = Error;
42+
43+
fn read(&mut self) -> nb::Result<u8, Self::Error> {
44+
self.1.read()
45+
}
46+
}

examples/v2-speaker/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn main() -> ! {
4848
let speaker = pwm::Pwm::new(board.PWM0);
4949
speaker
5050
// output the waveform on the speaker pin
51-
.set_output_pin(pwm::Channel::C0, &speaker_pin.degrade())
51+
.set_output_pin(pwm::Channel::C0, speaker_pin.degrade())
5252
// Use prescale by 16 to achive darker sounds
5353
.set_prescaler(pwm::Prescaler::Div16)
5454
// Initial frequency

microbit-common/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ embedded-hal = "0.2.4"
3030

3131
[dependencies.nrf51-hal]
3232
optional = true
33-
version = "0.12.1"
33+
version = "0.13.0"
3434

3535
[dependencies.nrf52833-hal]
3636
optional = true
37-
version = "0.12.1"
37+
version = "0.13.0"
38+
git = "https://github.com/nrf-rs/nrf-hal"
3839

3940
[features]
4041
doc = []

microbit-common/src/display/blocking.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ impl Display {
7272
/// to create [`DisplayPins`].
7373
pub fn new(pins: DisplayPins) -> Self {
7474
let (cols, rows) = pins.degrade();
75-
let mut retval = Display {
75+
Display {
7676
delay_ms: DEFAULT_DELAY_MS,
7777
rows,
7878
cols,
79-
};
80-
retval
79+
}
8180
}
8281

8382
/// Clear the display

microbit-common/src/v1/board.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::gpio::{DisplayPins, BTN_A, BTN_B, SCL, SDA};
1+
use super::gpio::{DisplayPins, BTN_A, BTN_B, SCL, SDA, UART_RX, UART_TX};
22
use crate::{
33
hal::{
44
gpio::{p0, Disconnected, Level},
5-
twi,
5+
twi, uart,
66
},
77
pac,
88
};
@@ -22,6 +22,9 @@ pub struct Board {
2222
/// I2C shared internal and external bus pins
2323
pub i2c: I2CPins,
2424

25+
/// UART to debugger pins
26+
pub uart: UartPins,
27+
2528
/// Core peripheral: Cache and branch predictor maintenance operations
2629
pub CBP: pac::CBP,
2730

@@ -84,6 +87,9 @@ pub struct Board {
8487

8588
/// nRF51 peripheral: TWI0
8689
pub TWI0: pac::TWI0,
90+
91+
/// nrf51 peripheral: UART0
92+
pub UART0: pac::UART0,
8793
}
8894

8995
impl Board {
@@ -112,8 +118,6 @@ impl Board {
112118
p0_21: p0parts.p0_21,
113119
p0_22: p0parts.p0_22,
114120
p0_23: p0parts.p0_23,
115-
p0_24: p0parts.p0_24,
116-
p0_25: p0parts.p0_25,
117121
p0_27: p0parts.p0_27,
118122
p0_28: p0parts.p0_28,
119123
p0_29: p0parts.p0_29,
@@ -140,6 +144,10 @@ impl Board {
140144
scl: p0parts.p0_00.into_floating_input(),
141145
sda: p0parts.p0_30.into_floating_input(),
142146
},
147+
uart: UartPins {
148+
tx: p0parts.p0_24.into_push_pull_output(Level::Low),
149+
rx: p0parts.p0_25.into_floating_input(),
150+
},
143151

144152
// Core peripherals
145153
CBP: cp.CBP,
@@ -165,6 +173,7 @@ impl Board {
165173
TIMER1: p.TIMER1,
166174
TIMER2: p.TIMER2,
167175
TWI0: p.TWI0,
176+
UART0: p.UART0,
168177
}
169178
}
170179
}
@@ -196,8 +205,8 @@ pub struct Pins {
196205
pub p0_21: p0::P0_21<Disconnected>,
197206
pub p0_22: p0::P0_22<Disconnected>,
198207
pub p0_23: p0::P0_23<Disconnected>,
199-
pub p0_24: p0::P0_24<Disconnected>,
200-
pub p0_25: p0::P0_25<Disconnected>,
208+
// pub p0_24: p0::P0_24<Disconnected>, // UART TX
209+
// pub p0_25: p0::P0_25<Disconnected>, // UART RX
201210
// pub p0_26: p0::P0_26<Disconnected>, // BTN_B
202211
pub p0_27: p0::P0_27<Disconnected>,
203212
pub p0_28: p0::P0_28<Disconnected>,
@@ -227,3 +236,20 @@ impl Into<twi::Pins> for I2CPins {
227236
}
228237
}
229238
}
239+
240+
/// UART to debugger pins
241+
pub struct UartPins {
242+
tx: UART_TX,
243+
rx: UART_RX,
244+
}
245+
246+
impl Into<uart::Pins> for UartPins {
247+
fn into(self) -> uart::Pins {
248+
uart::Pins {
249+
txd: self.tx.degrade(),
250+
rxd: self.rx.degrade(),
251+
cts: None,
252+
rts: None,
253+
}
254+
}
255+
}

microbit-common/src/v2/board.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::gpio::{DisplayPins, BTN_A, BTN_B, INT_SCL, INT_SDA, SCL, SDA};
1+
use super::gpio::{DisplayPins, BTN_A, BTN_B, INT_SCL, INT_SDA, SCL, SDA, UART_RX, UART_TX};
22
use crate::{
33
hal::{
44
gpio::{p0, p1, Disconnected, Level},
5-
twim, twis,
5+
twim, twis, uarte,
66
},
77
pac,
88
};
@@ -28,6 +28,9 @@ pub struct Board {
2828
/// I2C external bus pins
2929
pub i2c_external: I2CExternalPins,
3030

31+
/// UART to debugger pins
32+
pub uart: UartPins,
33+
3134
/// Core peripheral: Cache and branch predictor maintenance operations
3235
pub CBP: pac::CBP,
3336

@@ -114,6 +117,12 @@ pub struct Board {
114117

115118
/// nRF52 peripheral: TWIS0
116119
pub TWIS0: pac::TWIS0,
120+
121+
/// nRF52 peripheral: UARTE0
122+
pub UARTE0: pac::UARTE0,
123+
124+
/// nRF52 peripheral: UARTE1
125+
pub UARTE1: pac::UARTE1,
117126
}
118127

119128
impl Board {
@@ -138,7 +147,6 @@ impl Board {
138147
p0_03: p0parts.p0_03,
139148
p0_04: p0parts.p0_04,
140149
p0_05: p0parts.p0_05,
141-
p0_06: p0parts.p0_06,
142150
p0_07: p0parts.p0_07,
143151
p0_09: p0parts.p0_09,
144152
p0_10: p0parts.p0_10,
@@ -156,7 +164,6 @@ impl Board {
156164
p1_04: p1parts.p1_04,
157165
p1_06: p1parts.p1_06,
158166
p1_07: p1parts.p1_07,
159-
p1_08: p1parts.p1_08,
160167
p1_09: p1parts.p1_09,
161168
},
162169
display_pins: DisplayPins {
@@ -184,6 +191,10 @@ impl Board {
184191
scl: p0parts.p0_26.into_floating_input(),
185192
sda: p1parts.p1_00.into_floating_input(),
186193
},
194+
uart: UartPins {
195+
tx: p0parts.p0_06.into_push_pull_output(Level::High),
196+
rx: p1parts.p1_08.into_floating_input(),
197+
},
187198

188199
// Core peripherals
189200
CBP: cp.CBP,
@@ -217,6 +228,8 @@ impl Board {
217228
TIMER4: p.TIMER4,
218229
TWIM0: p.TWIM0,
219230
TWIS0: p.TWIS0,
231+
UARTE0: p.UARTE0,
232+
UARTE1: p.UARTE1,
220233
}
221234
}
222235
}
@@ -230,7 +243,7 @@ pub struct Pins {
230243
pub p0_03: p0::P0_03<Disconnected>,
231244
pub p0_04: p0::P0_04<Disconnected>,
232245
pub p0_05: p0::P0_05<Disconnected>,
233-
pub p0_06: p0::P0_06<Disconnected>,
246+
// pub p0_06: p0::P0_06<Disconnected>, // UART RX
234247
pub p0_07: p0::P0_07<Disconnected>,
235248
// pub p0_08: p0::P0_08<Disconnected>, // INT_SCL
236249
pub p0_09: p0::P0_09<Disconnected>,
@@ -264,7 +277,7 @@ pub struct Pins {
264277
// pub p1_05: p1::P1_05<Disconnected>, // LEDs
265278
pub p1_06: p1::P1_06<Disconnected>,
266279
pub p1_07: p1::P1_07<Disconnected>,
267-
pub p1_08: p1::P1_08<Disconnected>,
280+
// pub p1_08: p1::P1_08<Disconnected>, // UART TX
268281
pub p1_09: p1::P1_09<Disconnected>,
269282
}
270283

@@ -323,3 +336,20 @@ impl Into<twis::Pins> for I2CExternalPins {
323336
}
324337
}
325338
}
339+
340+
/// UART to debugger pins
341+
pub struct UartPins {
342+
tx: UART_TX,
343+
rx: UART_RX,
344+
}
345+
346+
impl Into<uarte::Pins> for UartPins {
347+
fn into(self) -> uarte::Pins {
348+
uarte::Pins {
349+
txd: self.tx.degrade(),
350+
rxd: self.rx.degrade(),
351+
cts: None,
352+
rts: None,
353+
}
354+
}
355+
}

microbit-common/src/v2/gpio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ pub type SCL = p0::P0_26<Input<Floating>>;
114114
pub type SDA = p1::P1_00<Input<Floating>>;
115115

116116
/* uart */
117-
pub type UART_TX = p1::P1_08<Output<PushPull>>;
118-
pub type UART_RX = p0::P0_06<Input<Floating>>;
117+
pub type UART_TX = p0::P0_06<Output<PushPull>>;
118+
pub type UART_RX = p1::P1_08<Input<Floating>>;
119119

120120
/* speaker */
121121
pub type SPEAKER = p0::P0_00<Output<PushPull>>;

0 commit comments

Comments
 (0)