Skip to content

Commit 5c52cc0

Browse files
committed
Abstract the DMA initialization a bit away.
1 parent ba414ab commit 5c52cc0

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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"

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

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

66
use core::fmt::Write;
7-
use microbit::hal::prelude::*;
87

98
#[cfg(feature = "v1")]
109
use microbit::{
1110
hal::uart,
1211
hal::uart::{Baudrate, Parity},
12+
hal::prelude::*,
1313
};
14+
1415
#[cfg(feature = "v2")]
1516
use microbit::{
1617
hal::uarte,
1718
hal::uarte::{Baudrate, Parity},
19+
hal::prelude::*,
1820
};
1921

2022
use cortex_m_rt::entry;
2123

24+
#[cfg(feature = "v2")]
25+
mod serial_setup;
26+
#[cfg(feature = "v2")]
27+
use serial_setup::UartePort;
28+
29+
2230
#[entry]
2331
fn main() -> ! {
2432
let board = microbit::Board::take().unwrap();
@@ -35,22 +43,18 @@ fn main() -> ! {
3543

3644
#[cfg(feature = "v2")]
3745
let mut serial = {
38-
uarte::Uarte::new(
46+
let serial = uarte::Uarte::new(
3947
board.UARTE0,
4048
board.uart.into(),
4149
Parity::EXCLUDED,
4250
Baudrate::BAUD115200,
43-
)
51+
);
52+
UartePort::new(serial)
4453
};
4554

46-
/* Print a nice hello message */
47-
write!(serial, "Please type characters to echo:\r\n").unwrap();
48-
49-
/* Endless loop */
5055
loop {
51-
/* Read and echo back */
52-
if let Ok(c) = nb::block!(serial.read()) {
53-
let _ = nb::block!(serial.write(c));
54-
}
56+
write!(serial, "Hello World:\r\n").unwrap();
57+
let input = nb::block!(serial.read()).unwrap();
58+
write!(serial, "You said: {}\r\n", input as char).unwrap();
5559
}
5660
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use microbit::hal::uarte::{Uarte, UarteTx, UarteRx, Instance, Error};
2+
use embedded_hal::serial;
3+
use embedded_hal::blocking::serial as bserial;
4+
use core::fmt;
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.split(
14+
unsafe { &mut TX_BUF },
15+
unsafe { &mut RX_BUF }
16+
).unwrap();
17+
UartePort(tx, rx)
18+
}
19+
}
20+
21+
impl<T: Instance> fmt::Write for UartePort<T> {
22+
fn write_str(&mut self, s: &str) -> fmt::Result {
23+
self.0.write_str(s)
24+
}
25+
}
26+
27+
impl<T: Instance> serial::Write<u8> for UartePort<T> {
28+
type Error = Error;
29+
30+
fn write(&mut self, b: u8) -> nb::Result<(), Self::Error> {
31+
self.0.write(b)
32+
}
33+
34+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
35+
self.0.flush()
36+
}
37+
}
38+
39+
impl<T: Instance> bserial::write::Default<u8> for UartePort<T> {}
40+
41+
impl<T: Instance> serial::Read<u8> for UartePort<T> {
42+
type Error = Error;
43+
44+
fn read(&mut self) -> nb::Result<u8, Self::Error> {
45+
self.1.read()
46+
}
47+
}

0 commit comments

Comments
 (0)