Skip to content

Commit 1b29247

Browse files
committed
Initial UART(E) implementation
1 parent 458d705 commit 1b29247

File tree

6 files changed

+103
-49
lines changed

6 files changed

+103
-49
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ nb = "0.1.2"
1111

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

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

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

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

1120
use cortex_m_rt::entry;
1221

22+
1323
#[entry]
1424
fn main() -> ! {
15-
if let Some(p) = microbit::Peripherals::take() {
16-
let gpio = hal::gpio::p0::Parts::new(p.GPIO);
25+
let board = microbit::Board::take().unwrap();
1726

18-
/* Initialise serial port on the micro:bit */
19-
let mut serial = microbit::serial_port!(gpio, p.UART0, Baudrate::BAUD115200);
27+
#[cfg(feature = "v1")]
28+
let mut serial = { uart::Uart::new(board.UART0, board.uart.into(), Parity::EXCLUDED, Baudrate::BAUD115200) };
2029

21-
/* Print a nice hello message */
22-
write!(serial, "Please type characters to echo:\r\n");
30+
#[cfg(feature = "v2")]
31+
let mut serial = { uarte::Uarte::new(board.UARTE0, board.uart.into(), Parity::EXCLUDED, Baudrate::BAUD115200) };
2332

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-
}
33+
/* Print a nice hello message */
34+
write!(serial, "Please type characters to echo:\r\n").unwrap();
3235

36+
/* Endless loop */
3337
loop {
34-
continue;
38+
/* Read and echo back */
39+
if let Ok(c) = nb::block!(serial.read()) {
40+
let _ = nb::block!(serial.write(c));
41+
}
3542
}
3643
}

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use super::gpio::{DisplayPins, BTN_A, BTN_B, SCL, SDA};
1+
use super::gpio::{DisplayPins, BTN_A, BTN_B, SCL, SDA, UART_TX, UART_RX};
22
use crate::{
33
hal::{
44
gpio::{p0, Disconnected, Level},
55
twi,
6+
uart,
67
},
78
pac,
89
};
@@ -22,6 +23,9 @@ pub struct Board {
2223
/// I2C shared internal and external bus pins
2324
pub i2c: I2CPins,
2425

26+
/// UART to debugger pins
27+
pub uart: UartPins,
28+
2529
/// Core peripheral: Cache and branch predictor maintenance operations
2630
pub CBP: pac::CBP,
2731

@@ -84,6 +88,9 @@ pub struct Board {
8488

8589
/// nRF51 peripheral: TWI0
8690
pub TWI0: pac::TWI0,
91+
92+
/// nrf51 peripheral: UART0
93+
pub UART0: pac::UART0,
8794
}
8895

8996
impl Board {
@@ -112,8 +119,6 @@ impl Board {
112119
p0_21: p0parts.p0_21,
113120
p0_22: p0parts.p0_22,
114121
p0_23: p0parts.p0_23,
115-
p0_24: p0parts.p0_24,
116-
p0_25: p0parts.p0_25,
117122
p0_27: p0parts.p0_27,
118123
p0_28: p0parts.p0_28,
119124
p0_29: p0parts.p0_29,
@@ -140,6 +145,10 @@ impl Board {
140145
scl: p0parts.p0_00.into_floating_input(),
141146
sda: p0parts.p0_30.into_floating_input(),
142147
},
148+
uart: UartPins {
149+
tx: p0parts.p0_24.into_push_pull_output(Level::Low),
150+
rx: p0parts.p0_25.into_floating_input(),
151+
},
143152

144153
// Core peripherals
145154
CBP: cp.CBP,
@@ -165,6 +174,7 @@ impl Board {
165174
TIMER1: p.TIMER1,
166175
TIMER2: p.TIMER2,
167176
TWI0: p.TWI0,
177+
UART0: p.UART0,
168178
}
169179
}
170180
}
@@ -196,8 +206,8 @@ pub struct Pins {
196206
pub p0_21: p0::P0_21<Disconnected>,
197207
pub p0_22: p0::P0_22<Disconnected>,
198208
pub p0_23: p0::P0_23<Disconnected>,
199-
pub p0_24: p0::P0_24<Disconnected>,
200-
pub p0_25: p0::P0_25<Disconnected>,
209+
// pub p0_24: p0::P0_24<Disconnected>, // UART TX
210+
// pub p0_25: p0::P0_25<Disconnected>, // UART RX
201211
// pub p0_26: p0::P0_26<Disconnected>, // BTN_B
202212
pub p0_27: p0::P0_27<Disconnected>,
203213
pub p0_28: p0::P0_28<Disconnected>,
@@ -227,3 +237,20 @@ impl Into<twi::Pins> for I2CPins {
227237
}
228238
}
229239
}
240+
241+
/// UART to debugger pins
242+
pub struct UartPins {
243+
tx: UART_TX,
244+
rx: UART_RX,
245+
}
246+
247+
impl Into<uart::Pins> for UartPins {
248+
fn into(self) -> uart::Pins {
249+
uart::Pins {
250+
txd: self.tx.degrade(),
251+
rxd: self.rx.degrade(),
252+
cts: None,
253+
rts: None,
254+
}
255+
}
256+
}

microbit-common/src/v2/board.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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_TX, UART_RX};
22
use crate::{
33
hal::{
44
gpio::{p0, p1, Disconnected, Level},
55
twim, twis,
6+
uarte,
67
},
78
pac,
89
};
@@ -28,6 +29,9 @@ pub struct Board {
2829
/// I2C external bus pins
2930
pub i2c_external: I2CExternalPins,
3031

32+
/// UART to debugger pins
33+
pub uart: UartPins,
34+
3135
/// Core peripheral: Cache and branch predictor maintenance operations
3236
pub CBP: pac::CBP,
3337

@@ -114,6 +118,12 @@ pub struct Board {
114118

115119
/// nRF52 peripheral: TWIS0
116120
pub TWIS0: pac::TWIS0,
121+
122+
/// nRF52 peripheral: UARTE0
123+
pub UARTE0: pac::UARTE0,
124+
125+
/// nRF52 peripheral: UARTE1
126+
pub UARTE1: pac::UARTE1,
117127
}
118128

119129
impl Board {
@@ -138,7 +148,6 @@ impl Board {
138148
p0_03: p0parts.p0_03,
139149
p0_04: p0parts.p0_04,
140150
p0_05: p0parts.p0_05,
141-
p0_06: p0parts.p0_06,
142151
p0_07: p0parts.p0_07,
143152
p0_09: p0parts.p0_09,
144153
p0_10: p0parts.p0_10,
@@ -156,7 +165,6 @@ impl Board {
156165
p1_04: p1parts.p1_04,
157166
p1_06: p1parts.p1_06,
158167
p1_07: p1parts.p1_07,
159-
p1_08: p1parts.p1_08,
160168
p1_09: p1parts.p1_09,
161169
},
162170
display_pins: DisplayPins {
@@ -184,6 +192,10 @@ impl Board {
184192
scl: p0parts.p0_26.into_floating_input(),
185193
sda: p1parts.p1_00.into_floating_input(),
186194
},
195+
uart: UartPins {
196+
tx: p1parts.p1_08.into_push_pull_output(Level::Low),
197+
rx: p0parts.p0_06.into_floating_input(),
198+
},
187199

188200
// Core peripherals
189201
CBP: cp.CBP,
@@ -217,6 +229,8 @@ impl Board {
217229
TIMER4: p.TIMER4,
218230
TWIM0: p.TWIM0,
219231
TWIS0: p.TWIS0,
232+
UARTE0: p.UARTE0,
233+
UARTE1: p.UARTE1,
220234
}
221235
}
222236
}
@@ -230,7 +244,7 @@ pub struct Pins {
230244
pub p0_03: p0::P0_03<Disconnected>,
231245
pub p0_04: p0::P0_04<Disconnected>,
232246
pub p0_05: p0::P0_05<Disconnected>,
233-
pub p0_06: p0::P0_06<Disconnected>,
247+
// pub p0_06: p0::P0_06<Disconnected>, // UART RX
234248
pub p0_07: p0::P0_07<Disconnected>,
235249
// pub p0_08: p0::P0_08<Disconnected>, // INT_SCL
236250
pub p0_09: p0::P0_09<Disconnected>,
@@ -264,7 +278,7 @@ pub struct Pins {
264278
// pub p1_05: p1::P1_05<Disconnected>, // LEDs
265279
pub p1_06: p1::P1_06<Disconnected>,
266280
pub p1_07: p1::P1_07<Disconnected>,
267-
pub p1_08: p1::P1_08<Disconnected>,
281+
// pub p1_08: p1::P1_08<Disconnected>, // UART TX
268282
pub p1_09: p1::P1_09<Disconnected>,
269283
}
270284

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

microbit/src/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,3 @@
1414
#![allow(non_camel_case_types)]
1515

1616
pub use microbit_common::*;
17-
18-
/// Create a [Uart](hal::uart::Uart) client with the default pins
19-
#[macro_export]
20-
macro_rules! serial_port {
21-
( $gpio:expr, $uart:expr, $speed:expr ) => {{
22-
use microbit::hal::{gpio::Level, uart};
23-
24-
/* Configure RX and TX pins accordingly */
25-
let pins = uart::Pins {
26-
rxd: $gpio.p0_25.into_floating_input().degrade(),
27-
txd: $gpio.p0_24.into_push_pull_output(Level::Low).degrade(),
28-
cts: None,
29-
rts: None,
30-
};
31-
32-
/* Set up serial port using the prepared pins */
33-
uart::Uart::new($uart, pins, uart::Parity::EXCLUDED, $speed)
34-
}};
35-
}

0 commit comments

Comments
 (0)