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
use super::{DataReg, Error, Flags, Receive, Transmit, Uart, ValidConfig};
use embedded_hal::{
blocking,
serial::{Read, Write},
};
use nb::Error::WouldBlock;
use num_traits::AsPrimitive;
impl<C, D> Read<C::Word> for Uart<C, D>
where
C: ValidConfig,
D: Receive,
DataReg: AsPrimitive<C::Word>,
{
type Error = Error;
#[inline]
fn read(&mut self) -> nb::Result<C::Word, Error> {
let flags = self.read_flags_errors()?;
if flags.contains(Flags::RXC) {
unsafe { Ok(self.read_data().as_()) }
} else {
Err(WouldBlock)
}
}
}
impl<C, D> Write<C::Word> for Uart<C, D>
where
C: ValidConfig,
D: Transmit,
{
type Error = core::convert::Infallible;
#[inline]
fn write(&mut self, word: C::Word) -> nb::Result<(), Self::Error> {
if self.read_flags().contains(Flags::DRE) {
unsafe { self.write_data(word.as_()) };
Ok(())
} else {
Err(WouldBlock)
}
}
#[inline]
fn flush(&mut self) -> nb::Result<(), Self::Error> {
if self.read_flags().contains(Flags::TXC) {
self.clear_flags(Flags::TXC);
Ok(())
} else {
Err(WouldBlock)
}
}
}
impl<C, D> blocking::serial::write::Default<C::Word> for Uart<C, D>
where
C: ValidConfig,
D: Transmit,
Uart<C, D>: Write<C::Word>,
{
}