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
use embedded_hal_one::serial::ErrorType;

impl<USART, PINS, WORD> ErrorType for super::Serial<USART, PINS, WORD> {
    type Error = super::Error;
}

impl<USART, WORD> ErrorType for super::Rx<USART, WORD> {
    type Error = super::Error;
}

impl<USART, WORD> ErrorType for super::Tx<USART, WORD> {
    type Error = super::Error;
}

mod nb {
    use super::super::{Error, Instance, Rx, Serial, Tx};
    use embedded_hal_one::serial::{
        nb::{Read, Write},
        ErrorType,
    };

    impl<USART, PINS, WORD: Copy> Read<WORD> for Serial<USART, PINS, WORD>
    where
        USART: Instance,
        Rx<USART, WORD>: Read<WORD> + ErrorType<Error = Self::Error>,
    {
        fn read(&mut self) -> nb::Result<WORD, Error> {
            self.rx.read()
        }
    }

    impl<USART: Instance> Read<u8> for Rx<USART, u8> {
        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> {
        fn read(&mut self) -> nb::Result<u16, Self::Error> {
            self.read()
        }
    }

    impl<USART, PINS, WORD: Copy> Write<WORD> for Serial<USART, PINS, WORD>
    where
        USART: Instance,
        Tx<USART, WORD>: Write<WORD> + ErrorType<Error = Self::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> {
        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> {
        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::{Instance, Serial, Tx};
    use super::ErrorType;
    use embedded_hal_one::serial::blocking::Write;

    impl<USART, PINS, WORD: Copy> Write<WORD> for Serial<USART, PINS, WORD>
    where
        USART: Instance,
        Tx<USART, WORD>: Write<WORD> + ErrorType<Error = Self::Error>,
    {
        fn write(&mut self, slice: &[WORD]) -> Result<(), Self::Error> {
            self.tx.write(slice)
        }

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

    impl<USART: Instance> Write<u8> for Tx<USART, u8> {
        fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
            self.bwrite_all(bytes)
        }

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

    impl<USART: Instance> Write<u16> for Tx<USART, u16> {
        fn write(&mut self, slice: &[u16]) -> Result<(), Self::Error> {
            self.bwrite_all(slice)
        }

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