1use core::marker::PhantomData;
4
5mod config;
6mod error;
7mod serial;
8
9pub use config::*;
10pub use error::*;
11pub use serial::*;
12
13pub const APB0: usize = 50_000_000;
15pub const CLK_OSC: usize = 24_000_000;
17
18pub const TIMEOUT_US: u64 = 1_000_000;
20
21pub struct UartTx<T: Serial> {
25 _serial: PhantomData<T>,
26}
27
28impl<T: Serial> UartTx<T> {
29 fn new_inner() -> Self {
30 Self {
31 _serial: PhantomData,
32 }
33 }
34
35 pub fn write_bytes(&mut self, data: &[u8]) -> Result<usize> {
44 let count = data.len();
45
46 data.iter()
47 .try_for_each(|&c| nb::block!(self.write_byte(c)))?;
48
49 Ok(count)
50 }
51
52 fn write_byte(&mut self, byte: u8) -> nb::Result<(), Error> {
53 T::write_byte(byte)
54 }
55
56 fn flush(&mut self) -> nb::Result<(), Error> {
57 T::flush()
58 }
59}
60
61pub struct UartRx<T: Serial> {
65 _serial: PhantomData<T>,
66}
67
68impl<T: Serial> UartRx<T> {
69 fn new_inner() -> Self {
70 Self {
71 _serial: PhantomData,
72 }
73 }
74
75 pub fn read_bytes(&mut self, buf: &mut [u8]) -> Result<usize> {
79 let mut count = 0usize;
80
81 for byte in buf.iter_mut() {
82 match self.read_byte() {
83 Ok(b) => {
84 *byte = b;
85 count = count.saturating_add(1);
86 }
87 Err(nb::Error::WouldBlock) => {
88 if count > 0 {
90 break;
91 }
92 }
93 Err(nb::Error::Other(e)) => return Err(e),
94 }
95 }
96
97 Ok(count)
98 }
99
100 fn read_byte(&mut self) -> nb::Result<u8, Error> {
101 T::read_byte()
102 }
103}
104
105#[repr(C)]
109pub struct Uart<UART: Serial> {
110 tx: UartTx<UART>,
111 rx: UartRx<UART>,
112 timeout: u64,
113 config: Config,
114}
115
116impl<UART: Serial> Uart<UART> {
117 pub fn new(uart: UART) -> Self {
131 Self::new_with_config(uart, TIMEOUT_US, Config::new())
132 }
133
134 pub fn new_with_config(mut uart: UART, timeout: u64, config: Config) -> Self {
162 uart.setup(config).ok();
163
164 Self {
165 tx: UartTx::new_inner(),
166 rx: UartRx::new_inner(),
167 timeout,
168 config,
169 }
170 }
171
172 pub fn split(self) -> (UartTx<UART>, UartRx<UART>) {
174 (self.tx, self.rx)
175 }
176
177 pub fn read_byte(&mut self) -> Result<u8> {
179 Ok(self.rx.read_byte()?)
180 }
181
182 pub fn write_byte(&mut self, byte: u8) -> Result<()> {
184 Ok(self.tx.write_byte(byte)?)
185 }
186
187 pub const fn timeout(&self) -> u64 {
189 self.timeout
190 }
191
192 pub fn set_timeout(&mut self, timeout: u64) {
196 if timeout > 0 {
197 self.timeout = timeout;
198 }
199 }
200
201 pub fn with_timeout(mut self, timeout: u64) -> Self {
205 self.set_timeout(timeout);
206 self
207 }
208
209 pub const fn config(&self) -> Config {
211 self.config
212 }
213
214 pub fn set_config(&mut self, config: Config) {
216 self.config = config;
217 }
218
219 pub fn with_config(mut self, config: Config) -> Self {
221 self.set_config(config);
222 self
223 }
224}
225
226impl<UART: Serial> io::ErrorType for Uart<UART> {
227 type Error = Error;
228}
229
230impl<UART: Serial> io::ErrorType for UartRx<UART> {
231 type Error = Error;
232}
233
234impl<UART: Serial> io::ErrorType for UartTx<UART> {
235 type Error = Error;
236}
237
238impl<UART: Serial> embedded_hal_nb::serial::ErrorType for Uart<UART> {
239 type Error = Error;
240}
241
242impl<UART: Serial> embedded_hal_nb::serial::ErrorType for UartRx<UART> {
243 type Error = Error;
244}
245
246impl<UART: Serial> embedded_hal_nb::serial::ErrorType for UartTx<UART> {
247 type Error = Error;
248}
249
250impl<UART: Serial> io::Read for Uart<UART> {
251 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
252 self.rx.read_bytes(buf)
253 }
254}
255
256impl<UART: Serial> io::Read for UartRx<UART> {
257 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
258 self.read_bytes(buf)
259 }
260}
261
262impl<UART: Serial> io::Write for Uart<UART> {
263 fn write(&mut self, buf: &[u8]) -> Result<usize> {
264 self.tx.write_bytes(buf)
265 }
266
267 fn flush(&mut self) -> Result<()> {
268 self.tx.flush()?;
269 Ok(())
270 }
271}
272
273impl<UART: Serial> io::Write for UartTx<UART> {
274 fn write(&mut self, buf: &[u8]) -> Result<usize> {
275 self.write_bytes(buf)
276 }
277
278 fn flush(&mut self) -> Result<()> {
279 self.flush()?;
280 Ok(())
281 }
282}
283
284impl<UART: Serial> embedded_hal_nb::serial::Read for Uart<UART> {
285 fn read(&mut self) -> nb::Result<u8, Self::Error> {
286 self.rx.read_byte()
287 }
288}
289
290impl<UART: Serial> embedded_hal_nb::serial::Read for UartRx<UART> {
291 fn read(&mut self) -> nb::Result<u8, Self::Error> {
292 self.read_byte()
293 }
294}
295
296impl<UART: Serial> embedded_hal_nb::serial::Write for Uart<UART> {
297 fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> {
298 self.tx.write_byte(val)
299 }
300
301 fn flush(&mut self) -> nb::Result<(), Self::Error> {
302 self.tx.flush()
303 }
304}
305
306impl<UART: Serial> embedded_hal_nb::serial::Write for UartTx<UART> {
307 fn write(&mut self, val: u8) -> nb::Result<(), Self::Error> {
308 self.write_byte(val)
309 }
310
311 fn flush(&mut self) -> nb::Result<(), Self::Error> {
312 self.flush()
313 }
314}