1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#![cfg(feature = "semver-exempt")]
pub unsafe trait UartExt:
core::ops::Deref<Target = rp2040_pac::uart0::RegisterBlock> + Sized
{
fn global() -> Self {
assert_eq!(core::mem::size_of::<Self>(), 0);
unsafe { core::mem::zeroed() }
}
fn configure_pins(&self, io_bank0: &rp2040_pac::io_bank0::RegisterBlock);
fn reset(&self, resets: &rp2040_pac::resets::RegisterBlock);
#[inline]
fn configure_uart(&self, baud_rate: u32) {
let clk_peri = 48_000_000u32;
let baud_rate_div = clk_peri.checked_mul(8).unwrap() / baud_rate;
let baud_ibrd = baud_rate_div >> 7;
let baud_fbrd = ((baud_rate_div & 0x7f) + 1) / 2;
assert!(baud_ibrd > 0 && baud_ibrd < 65536);
self.uartibrd.write(|b| unsafe { b.bits(baud_ibrd) });
if baud_ibrd == 65535 {
self.uartfbrd.write(|b| unsafe { b.bits(0) });
} else {
self.uartfbrd.write(|b| unsafe { b.bits(baud_fbrd) });
}
self.uartlcr_h.modify(|_, w| w);
self.uartcr.write(|b| b.uarten().set_bit().txe().set_bit());
self.uartlcr_h
.write(|b| unsafe { b.wlen().bits(0b11).fen().set_bit() });
}
fn into_nb_writer(self) -> NbWriter<Self> {
NbWriter(self)
}
}
unsafe impl UartExt for rp2040_pac::UART0 {
#[inline]
fn configure_pins(&self, io_bank0: &rp2040_pac::io_bank0::RegisterBlock) {
io_bank0.gpio[0]
.gpio_ctrl
.write(|b| unsafe { b.funcsel().bits(2) });
io_bank0.gpio[1]
.gpio_ctrl
.write(|b| unsafe { b.funcsel().bits(2) });
}
fn reset(&self, resets: &rp2040_pac::resets::RegisterBlock) {
resets.reset.modify(|_, w| w.uart0().set_bit());
resets.reset.modify(|_, w| w.uart0().clear_bit());
while resets.reset_done.read().uart0().bit_is_clear() {}
}
}
pub struct NbWriter<T>(T);
unsafe impl<T> Sync for NbWriter<T> {}
impl<T: UartExt> embedded_hal::serial::Write<u8> for NbWriter<T> {
type Error = core::convert::Infallible;
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
let uart = &*self.0;
if uart.uartfr.read().txff().bit_is_set() {
Err(nb::Error::WouldBlock)
} else {
uart.uartdr.write(|w| unsafe { w.data().bits(word) });
Ok(())
}
}
fn flush(&mut self) -> nb::Result<(), Self::Error> {
if self.0.uartfr.read().txfe().bit_is_set() {
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
}