|
9 | 9 | use core::convert::Infallible;
|
10 | 10 | use core::mem;
|
11 | 11 |
|
12 |
| -use embedded_hal::serial; |
13 |
| - |
14 | 12 | use crate::clock::Clocks;
|
15 | 13 | use crate::pac::{uart1, UART1, UART2, UART3, UARTHS};
|
16 | 14 | use crate::time::Bps;
|
@@ -98,44 +96,48 @@ impl Serial<UARTHS> {
|
98 | 96 | }
|
99 | 97 | }
|
100 | 98 |
|
101 |
| -impl serial::Read<u8> for Rx<UARTHS> { |
102 |
| - type Error = Infallible; |
103 |
| - |
104 |
| - fn read(&mut self) -> nb::Result<u8, Infallible> { |
105 |
| - let rxdata = self.uart.rxdata.read(); |
| 99 | +impl embedded_io::ErrorType for Rx<UARTHS> { |
| 100 | + type Error = core::convert::Infallible; |
| 101 | +} |
106 | 102 |
|
107 |
| - if rxdata.empty().bit_is_set() { |
108 |
| - Err(nb::Error::WouldBlock) |
109 |
| - } else { |
110 |
| - Ok(rxdata.data().bits() as u8) |
| 103 | +impl embedded_io::Read for Rx<UARTHS> { |
| 104 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Infallible> { |
| 105 | + while self.uart.rxdata.read().empty().bit_is_set() { |
| 106 | + // Block until rxdata available. |
| 107 | + core::hint::spin_loop() |
| 108 | + } |
| 109 | + let len = buf.len(); |
| 110 | + for slot in buf { |
| 111 | + *slot = self.uart.rxdata.read().data().bits(); |
111 | 112 | }
|
| 113 | + Ok(len) |
112 | 114 | }
|
113 | 115 | }
|
114 | 116 |
|
115 |
| -impl serial::Write<u8> for Tx<UARTHS> { |
116 |
| - type Error = Infallible; |
117 |
| - |
118 |
| - fn write(&mut self, byte: u8) -> nb::Result<(), Infallible> { |
119 |
| - let txdata = self.uart.txdata.read(); |
| 117 | +impl embedded_io::ErrorType for Tx<UARTHS> { |
| 118 | + type Error = core::convert::Infallible; |
| 119 | +} |
120 | 120 |
|
121 |
| - if txdata.full().bit_is_set() { |
122 |
| - Err(nb::Error::WouldBlock) |
123 |
| - } else { |
| 121 | +impl embedded_io::Write for Tx<UARTHS> { |
| 122 | + fn write(&mut self, bytes: &[u8]) -> Result<usize, Infallible> { |
| 123 | + while self.uart.txdata.read().full().bit_is_set() { |
| 124 | + // Block until txdata available. |
| 125 | + core::hint::spin_loop() |
| 126 | + } |
| 127 | + for byte in bytes { |
124 | 128 | unsafe {
|
125 |
| - (*UARTHS::ptr()).txdata.write(|w| w.data().bits(byte)); |
| 129 | + self.uart.txdata.write(|w| w.data().bits(*byte)); |
126 | 130 | }
|
127 |
| - Ok(()) |
128 | 131 | }
|
| 132 | + Ok(bytes.len()) |
129 | 133 | }
|
130 | 134 |
|
131 |
| - fn flush(&mut self) -> nb::Result<(), Infallible> { |
132 |
| - let txdata = self.uart.txdata.read(); |
133 |
| - |
134 |
| - if txdata.full().bit_is_set() { |
135 |
| - Err(nb::Error::WouldBlock) |
136 |
| - } else { |
137 |
| - Ok(()) |
| 135 | + fn flush(&mut self) -> Result<(), Infallible> { |
| 136 | + while self.uart.txdata.read().full().bit_is_set() { |
| 137 | + // Block until flush complete. If you don't want a block, use embedded_io_async traits instead. |
| 138 | + core::hint::spin_loop() |
138 | 139 | }
|
| 140 | + Ok(()) |
139 | 141 | }
|
140 | 142 | }
|
141 | 143 |
|
@@ -207,40 +209,43 @@ impl<UART: UartX> Serial<UART> {
|
207 | 209 | }
|
208 | 210 | }
|
209 | 211 |
|
210 |
| -impl<UART: UartX> serial::Read<u8> for Rx<UART> { |
211 |
| - type Error = Infallible; |
212 |
| - |
213 |
| - fn read(&mut self) -> nb::Result<u8, Infallible> { |
214 |
| - let lsr = self.uart.lsr.read(); |
| 212 | +impl<UART: UartX> embedded_io::ErrorType for Rx<UART> { |
| 213 | + type Error = core::convert::Infallible; |
| 214 | +} |
215 | 215 |
|
216 |
| - if (lsr.bits() & (1 << 0)) == 0 { |
| 216 | +impl<UART: UartX> embedded_io::Read for Rx<UART> { |
| 217 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Infallible> { |
| 218 | + while (self.uart.lsr.read().bits() & (1 << 0)) == 0 { |
217 | 219 | // Data Ready bit
|
218 |
| - Err(nb::Error::WouldBlock) |
219 |
| - } else { |
220 |
| - let rbr = self.uart.rbr_dll_thr.read(); |
221 |
| - Ok((rbr.bits() & 0xff) as u8) |
| 220 | + core::hint::spin_loop() |
| 221 | + } |
| 222 | + let len = buf.len(); |
| 223 | + for slot in buf { |
| 224 | + *slot = (self.uart.rbr_dll_thr.read().bits() & 0xff) as u8; |
222 | 225 | }
|
| 226 | + Ok(len) |
223 | 227 | }
|
224 | 228 | }
|
225 | 229 |
|
226 |
| -impl<UART: UartX> serial::Write<u8> for Tx<UART> { |
227 |
| - type Error = Infallible; |
228 |
| - |
229 |
| - fn write(&mut self, byte: u8) -> nb::Result<(), Infallible> { |
230 |
| - let lsr = self.uart.lsr.read(); |
| 230 | +impl<UART: UartX> embedded_io::ErrorType for Tx<UART> { |
| 231 | + type Error = core::convert::Infallible; |
| 232 | +} |
231 | 233 |
|
232 |
| - if (lsr.bits() & (1 << 5)) != 0 { |
| 234 | +impl<UART: UartX> embedded_io::Write for Tx<UART> { |
| 235 | + fn write(&mut self, bytes: &[u8]) -> Result<usize, Infallible> { |
| 236 | + while (self.uart.lsr.read().bits() & (1 << 5)) != 0 { |
233 | 237 | // Transmit Holding Register Empty bit
|
234 |
| - Err(nb::Error::WouldBlock) |
235 |
| - } else { |
| 238 | + core::hint::spin_loop(); |
| 239 | + } |
| 240 | + for byte in bytes { |
236 | 241 | unsafe {
|
237 |
| - self.uart.rbr_dll_thr.write(|w| w.bits(byte.into())); |
| 242 | + self.uart.rbr_dll_thr.write(|w| w.bits(*byte as u32)); |
238 | 243 | }
|
239 |
| - Ok(()) |
240 | 244 | }
|
| 245 | + Ok(bytes.len()) |
241 | 246 | }
|
242 | 247 |
|
243 |
| - fn flush(&mut self) -> nb::Result<(), Infallible> { |
| 248 | + fn flush(&mut self) -> Result<(), Infallible> { |
244 | 249 | // TODO
|
245 | 250 | Ok(())
|
246 | 251 | }
|
|
0 commit comments