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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
mod nb {
    use super::super::{Error, Instance, Rx, Serial, Tx};
    use embedded_hal::serial::{Read, Write};

    impl<USART, PINS, WORD> Read<WORD> for Serial<USART, PINS, WORD>
    where
        USART: Instance,
        Rx<USART, WORD>: Read<WORD, Error = Error>,
    {
        type Error = Error;

        fn read(&mut self) -> nb::Result<WORD, Self::Error> {
            self.rx.read()
        }
    }

    impl<USART: Instance> Read<u8> for Rx<USART, u8> {
        type Error = Error;

        fn read(&mut self) -> nb::Result<u8, Self::Error> {
            self.read()
        }
    }

    /// Reads 9-bit words from the UART/USART
    ///
    /// If the UART/USART was configured with `WordLength::DataBits9`, the returned value will contain
    /// 9 received data bits and all other bits set to zero. Otherwise, the returned value will contain
    /// 8 received data bits and all other bits set to zero.
    impl<USART: Instance> Read<u16> for Rx<USART, u16> {
        type Error = Error;

        fn read(&mut self) -> nb::Result<u16, Self::Error> {
            self.read()
        }
    }

    impl<USART, PINS, WORD> Write<WORD> for Serial<USART, PINS, WORD>
    where
        USART: Instance,
        Tx<USART, WORD>: Write<WORD, Error = Error>,
    {
        type Error = Error;

        fn flush(&mut self) -> nb::Result<(), Self::Error> {
            self.tx.flush()
        }

        fn write(&mut self, byte: WORD) -> nb::Result<(), Self::Error> {
            self.tx.write(byte)
        }
    }

    impl<USART: Instance> Write<u8> for Tx<USART, u8> {
        type Error = Error;

        fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
            self.write(word)
        }

        fn flush(&mut self) -> nb::Result<(), Self::Error> {
            self.flush()
        }
    }

    /// Writes 9-bit words to the UART/USART
    ///
    /// If the UART/USART was configured with `WordLength::DataBits9`, the 9 least significant bits will
    /// be transmitted and the other 7 bits will be ignored. Otherwise, the 8 least significant bits
    /// will be transmitted and the other 8 bits will be ignored.
    impl<USART: Instance> Write<u16> for Tx<USART, u16> {
        type Error = Error;

        fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> {
            self.write(word)
        }

        fn flush(&mut self) -> nb::Result<(), Self::Error> {
            self.flush()
        }
    }
}

mod blocking {
    use super::super::{Error, Instance, Serial, Tx};
    use embedded_hal::blocking::serial::Write;

    impl<USART: Instance> Write<u8> for Tx<USART, u8> {
        type Error = Error;

        fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
            self.bwrite_all(bytes)
        }

        fn bflush(&mut self) -> Result<(), Self::Error> {
            self.bflush()
        }
    }

    impl<USART: Instance, PINS> Write<u8> for Serial<USART, PINS, u8> {
        type Error = Error;

        fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
            self.tx.bwrite_all(bytes)
        }

        fn bflush(&mut self) -> Result<(), Self::Error> {
            self.tx.bflush()
        }
    }

    impl<USART: Instance> Write<u16> for Tx<USART, u16> {
        type Error = Error;

        fn bwrite_all(&mut self, slice: &[u16]) -> Result<(), Self::Error> {
            self.bwrite_all(slice)
        }

        fn bflush(&mut self) -> Result<(), Self::Error> {
            self.bflush()
        }
    }

    impl<USART: Instance, PINS> Write<u16> for Serial<USART, PINS, u16> {
        type Error = Error;

        fn bwrite_all(&mut self, slice: &[u16]) -> Result<(), Self::Error> {
            self.tx.bwrite_all(slice)
        }

        fn bflush(&mut self) -> Result<(), Self::Error> {
            self.tx.bflush()
        }
    }
}