diff options
| author | Mathias <[email protected]> | 2023-03-31 10:43:30 +0200 |
|---|---|---|
| committer | Mathias <[email protected]> | 2023-03-31 10:43:30 +0200 |
| commit | cfbe93c2805598652d85e21322b343032586b398 (patch) | |
| tree | 6514b8fa8cbd7ce8a724f67e5fa39c321a080843 | |
| parent | 7a841b58d127cc6d22c8895197d3f4d4c0974ad7 (diff) | |
Rework bufferedUart to get rid of PeripheralMutex in a similar fashion as nrf & rp. Also adds embedded-hal traits to bufferedUart
| -rw-r--r-- | embassy-stm32/src/usart/buffered.rs | 728 | ||||
| -rw-r--r-- | embassy-stm32/src/usart/mod.rs | 9 |
2 files changed, 479 insertions, 258 deletions
diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index cd7d72f91..6721c44a1 100644 --- a/embassy-stm32/src/usart/buffered.rs +++ b/embassy-stm32/src/usart/buffered.rs | |||
| @@ -1,51 +1,47 @@ | |||
| 1 | use core::cell::RefCell; | ||
| 2 | use core::future::poll_fn; | 1 | use core::future::poll_fn; |
| 3 | use core::sync::atomic::{compiler_fence, Ordering}; | 2 | use core::slice; |
| 4 | use core::task::Poll; | 3 | use core::task::Poll; |
| 5 | 4 | ||
| 6 | use embassy_cortex_m::peripheral::{PeripheralMutex, PeripheralState, StateStorage}; | 5 | use embassy_cortex_m::interrupt::Interrupt; |
| 7 | use embassy_hal_common::ring_buffer::RingBuffer; | 6 | use embassy_hal_common::atomic_ring_buffer::RingBuffer; |
| 8 | use embassy_sync::waitqueue::WakerRegistration; | 7 | use embassy_sync::waitqueue::AtomicWaker; |
| 9 | 8 | ||
| 10 | use super::*; | 9 | use super::*; |
| 11 | 10 | ||
| 12 | pub struct State<'d, T: BasicInstance>(StateStorage<StateInner<'d, T>>); | 11 | pub struct State { |
| 13 | impl<'d, T: BasicInstance> State<'d, T> { | 12 | rx_waker: AtomicWaker, |
| 14 | pub const fn new() -> Self { | 13 | rx_buf: RingBuffer, |
| 15 | Self(StateStorage::new()) | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | struct StateInner<'d, T: BasicInstance> { | ||
| 20 | phantom: PhantomData<&'d mut T>, | ||
| 21 | |||
| 22 | rx_waker: WakerRegistration, | ||
| 23 | rx: RingBuffer<'d>, | ||
| 24 | 14 | ||
| 25 | tx_waker: WakerRegistration, | 15 | tx_waker: AtomicWaker, |
| 26 | tx: RingBuffer<'d>, | 16 | tx_buf: RingBuffer, |
| 27 | } | 17 | } |
| 28 | 18 | ||
| 29 | unsafe impl<'d, T: BasicInstance> Send for StateInner<'d, T> {} | 19 | impl State { |
| 30 | unsafe impl<'d, T: BasicInstance> Sync for StateInner<'d, T> {} | 20 | pub const fn new() -> Self { |
| 21 | Self { | ||
| 22 | rx_buf: RingBuffer::new(), | ||
| 23 | tx_buf: RingBuffer::new(), | ||
| 24 | rx_waker: AtomicWaker::new(), | ||
| 25 | tx_waker: AtomicWaker::new(), | ||
| 26 | } | ||
| 27 | } | ||
| 28 | } | ||
| 31 | 29 | ||
| 32 | pub struct BufferedUart<'d, T: BasicInstance> { | 30 | pub struct BufferedUart<'d, T: BasicInstance> { |
| 33 | inner: RefCell<PeripheralMutex<'d, StateInner<'d, T>>>, | 31 | rx: BufferedUartRx<'d, T>, |
| 32 | tx: BufferedUartTx<'d, T>, | ||
| 34 | } | 33 | } |
| 35 | 34 | ||
| 36 | pub struct BufferedUartTx<'u, 'd, T: BasicInstance> { | 35 | pub struct BufferedUartTx<'d, T: BasicInstance> { |
| 37 | inner: &'u BufferedUart<'d, T>, | 36 | phantom: PhantomData<&'d mut T>, |
| 38 | } | 37 | } |
| 39 | 38 | ||
| 40 | pub struct BufferedUartRx<'u, 'd, T: BasicInstance> { | 39 | pub struct BufferedUartRx<'d, T: BasicInstance> { |
| 41 | inner: &'u BufferedUart<'d, T>, | 40 | phantom: PhantomData<&'d mut T>, |
| 42 | } | 41 | } |
| 43 | 42 | ||
| 44 | impl<'d, T: BasicInstance> Unpin for BufferedUart<'d, T> {} | ||
| 45 | |||
| 46 | impl<'d, T: BasicInstance> BufferedUart<'d, T> { | 43 | impl<'d, T: BasicInstance> BufferedUart<'d, T> { |
| 47 | pub fn new( | 44 | pub fn new( |
| 48 | state: &'d mut State<'d, T>, | ||
| 49 | peri: impl Peripheral<P = T> + 'd, | 45 | peri: impl Peripheral<P = T> + 'd, |
| 50 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 46 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 51 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 47 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| @@ -57,11 +53,10 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 57 | T::enable(); | 53 | T::enable(); |
| 58 | T::reset(); | 54 | T::reset(); |
| 59 | 55 | ||
| 60 | Self::new_inner(state, peri, rx, tx, irq, tx_buffer, rx_buffer, config) | 56 | Self::new_inner(peri, rx, tx, irq, tx_buffer, rx_buffer, config) |
| 61 | } | 57 | } |
| 62 | 58 | ||
| 63 | pub fn new_with_rtscts( | 59 | pub fn new_with_rtscts( |
| 64 | state: &'d mut State<'d, T>, | ||
| 65 | peri: impl Peripheral<P = T> + 'd, | 60 | peri: impl Peripheral<P = T> + 'd, |
| 66 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 61 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 67 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 62 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| @@ -86,12 +81,11 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 86 | }); | 81 | }); |
| 87 | } | 82 | } |
| 88 | 83 | ||
| 89 | Self::new_inner(state, peri, rx, tx, irq, tx_buffer, rx_buffer, config) | 84 | Self::new_inner(peri, rx, tx, irq, tx_buffer, rx_buffer, config) |
| 90 | } | 85 | } |
| 91 | 86 | ||
| 92 | #[cfg(not(usart_v1))] | 87 | #[cfg(not(usart_v1))] |
| 93 | pub fn new_with_de( | 88 | pub fn new_with_de( |
| 94 | state: &'d mut State<'d, T>, | ||
| 95 | peri: impl Peripheral<P = T> + 'd, | 89 | peri: impl Peripheral<P = T> + 'd, |
| 96 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 90 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 97 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 91 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| @@ -113,11 +107,10 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 113 | }); | 107 | }); |
| 114 | } | 108 | } |
| 115 | 109 | ||
| 116 | Self::new_inner(state, peri, rx, tx, irq, tx_buffer, rx_buffer, config) | 110 | Self::new_inner(peri, rx, tx, irq, tx_buffer, rx_buffer, config) |
| 117 | } | 111 | } |
| 118 | 112 | ||
| 119 | fn new_inner( | 113 | fn new_inner( |
| 120 | state: &'d mut State<'d, T>, | ||
| 121 | _peri: impl Peripheral<P = T> + 'd, | 114 | _peri: impl Peripheral<P = T> + 'd, |
| 122 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 115 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 123 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 116 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| @@ -128,8 +121,13 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 128 | ) -> BufferedUart<'d, T> { | 121 | ) -> BufferedUart<'d, T> { |
| 129 | into_ref!(_peri, rx, tx, irq); | 122 | into_ref!(_peri, rx, tx, irq); |
| 130 | 123 | ||
| 131 | let r = T::regs(); | 124 | let state = T::buffered_state(); |
| 125 | let len = tx_buffer.len(); | ||
| 126 | unsafe { state.tx_buf.init(tx_buffer.as_mut_ptr(), len) }; | ||
| 127 | let len = rx_buffer.len(); | ||
| 128 | unsafe { state.rx_buf.init(rx_buffer.as_mut_ptr(), len) }; | ||
| 132 | 129 | ||
| 130 | let r = T::regs(); | ||
| 133 | unsafe { | 131 | unsafe { |
| 134 | rx.set_as_af(rx.af_num(), AFType::Input); | 132 | rx.set_as_af(rx.af_num(), AFType::Input); |
| 135 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); | 133 | tx.set_as_af(tx.af_num(), AFType::OutputPushPull); |
| @@ -147,273 +145,297 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 147 | }); | 145 | }); |
| 148 | } | 146 | } |
| 149 | 147 | ||
| 150 | Self { | 148 | irq.set_handler(on_interrupt::<T>); |
| 151 | inner: RefCell::new(PeripheralMutex::new(irq, &mut state.0, move || StateInner { | 149 | irq.unpend(); |
| 152 | phantom: PhantomData, | 150 | irq.enable(); |
| 153 | tx: RingBuffer::new(tx_buffer), | ||
| 154 | tx_waker: WakerRegistration::new(), | ||
| 155 | 151 | ||
| 156 | rx: RingBuffer::new(rx_buffer), | 152 | Self { |
| 157 | rx_waker: WakerRegistration::new(), | 153 | rx: BufferedUartRx { phantom: PhantomData }, |
| 158 | })), | 154 | tx: BufferedUartTx { phantom: PhantomData }, |
| 159 | } | 155 | } |
| 160 | } | 156 | } |
| 161 | 157 | ||
| 162 | pub fn split<'u>(&'u mut self) -> (BufferedUartRx<'u, 'd, T>, BufferedUartTx<'u, 'd, T>) { | 158 | pub fn split(self) -> (BufferedUartRx<'d, T>, BufferedUartTx<'d, T>) { |
| 163 | (BufferedUartRx { inner: self }, BufferedUartTx { inner: self }) | 159 | (self.rx, self.tx) |
| 164 | } | 160 | } |
| 161 | } | ||
| 165 | 162 | ||
| 166 | async fn inner_read<'a>(&'a self, buf: &'a mut [u8]) -> Result<usize, Error> { | 163 | impl<'d, T: BasicInstance> BufferedUartRx<'d, T> { |
| 164 | async fn read(&self, buf: &mut [u8]) -> Result<usize, Error> { | ||
| 167 | poll_fn(move |cx| { | 165 | poll_fn(move |cx| { |
| 168 | let mut do_pend = false; | 166 | let state = T::buffered_state(); |
| 169 | let mut inner = self.inner.borrow_mut(); | 167 | let mut rx_reader = unsafe { state.rx_buf.reader() }; |
| 170 | let res = inner.with(|state| { | 168 | let n = rx_reader.pop(|data| { |
| 171 | compiler_fence(Ordering::SeqCst); | 169 | let n = data.len().min(buf.len()); |
| 172 | 170 | buf[..n].copy_from_slice(&data[..n]); | |
| 173 | // We have data ready in buffer? Return it. | 171 | n |
| 174 | let data = state.rx.pop_buf(); | ||
| 175 | if !data.is_empty() { | ||
| 176 | let len = data.len().min(buf.len()); | ||
| 177 | buf[..len].copy_from_slice(&data[..len]); | ||
| 178 | |||
| 179 | if state.rx.is_full() { | ||
| 180 | do_pend = true; | ||
| 181 | } | ||
| 182 | state.rx.pop(len); | ||
| 183 | |||
| 184 | return Poll::Ready(Ok(len)); | ||
| 185 | } | ||
| 186 | |||
| 187 | state.rx_waker.register(cx.waker()); | ||
| 188 | Poll::Pending | ||
| 189 | }); | 172 | }); |
| 190 | 173 | if n == 0 { | |
| 191 | if do_pend { | 174 | state.rx_waker.register(cx.waker()); |
| 192 | inner.pend(); | 175 | return Poll::Pending; |
| 193 | } | 176 | } |
| 194 | 177 | ||
| 195 | res | 178 | // FIXME: |
| 179 | // (Re-)Enable the interrupt to receive more data in case it was | ||
| 180 | // disabled because the buffer was full. | ||
| 181 | // let regs = T::regs(); | ||
| 182 | // unsafe { | ||
| 183 | // regs.uartimsc().write_set(|w| { | ||
| 184 | // w.set_rxim(true); | ||
| 185 | // w.set_rtim(true); | ||
| 186 | // }); | ||
| 187 | // } | ||
| 188 | |||
| 189 | Poll::Ready(Ok(n)) | ||
| 196 | }) | 190 | }) |
| 197 | .await | 191 | .await |
| 192 | |||
| 193 | // poll_fn(move |cx| { | ||
| 194 | // let state = T::buffered_state(); | ||
| 195 | |||
| 196 | // let mut do_pend = false; | ||
| 197 | // compiler_fence(Ordering::SeqCst); | ||
| 198 | |||
| 199 | // // We have data ready in buffer? Return it. | ||
| 200 | // let data = state.rx_buf.pop_buf(); | ||
| 201 | // if !data.is_empty() { | ||
| 202 | // let len = data.len().min(buf.len()); | ||
| 203 | // buf[..len].copy_from_slice(&data[..len]); | ||
| 204 | |||
| 205 | // if state.rx_buf.is_full() { | ||
| 206 | // do_pend = true; | ||
| 207 | // } | ||
| 208 | // state.rx_buf.pop(len); | ||
| 209 | |||
| 210 | // return Poll::Ready(Ok(len)); | ||
| 211 | // } | ||
| 212 | |||
| 213 | // state.rx_waker.register(cx.waker()); | ||
| 214 | |||
| 215 | // if do_pend { | ||
| 216 | // inner.pend(); | ||
| 217 | // } | ||
| 218 | |||
| 219 | // Poll::Pending | ||
| 220 | // }) | ||
| 221 | // .await | ||
| 198 | } | 222 | } |
| 199 | 223 | ||
| 200 | fn inner_blocking_read(&self, buf: &mut [u8]) -> Result<usize, Error> { | 224 | fn blocking_read(&self, buf: &mut [u8]) -> Result<usize, Error> { |
| 201 | loop { | 225 | loop { |
| 202 | let mut do_pend = false; | 226 | let state = T::buffered_state(); |
| 203 | let mut inner = self.inner.borrow_mut(); | 227 | let mut rx_reader = unsafe { state.rx_buf.reader() }; |
| 204 | let n = inner.with(|state| { | 228 | let n = rx_reader.pop(|data| { |
| 205 | compiler_fence(Ordering::SeqCst); | 229 | let n = data.len().min(buf.len()); |
| 206 | 230 | buf[..n].copy_from_slice(&data[..n]); | |
| 207 | // We have data ready in buffer? Return it. | 231 | n |
| 208 | let data = state.rx.pop_buf(); | ||
| 209 | if !data.is_empty() { | ||
| 210 | let len = data.len().min(buf.len()); | ||
| 211 | buf[..len].copy_from_slice(&data[..len]); | ||
| 212 | |||
| 213 | if state.rx.is_full() { | ||
| 214 | do_pend = true; | ||
| 215 | } | ||
| 216 | state.rx.pop(len); | ||
| 217 | |||
| 218 | return len; | ||
| 219 | } | ||
| 220 | |||
| 221 | 0 | ||
| 222 | }); | 232 | }); |
| 223 | 233 | ||
| 224 | if do_pend { | ||
| 225 | inner.pend(); | ||
| 226 | } | ||
| 227 | |||
| 228 | if n > 0 { | 234 | if n > 0 { |
| 235 | // FIXME: | ||
| 236 | // (Re-)Enable the interrupt to receive more data in case it was | ||
| 237 | // disabled because the buffer was full. | ||
| 238 | // let regs = T::regs(); | ||
| 239 | // unsafe { | ||
| 240 | // regs.uartimsc().write_set(|w| { | ||
| 241 | // w.set_rxim(true); | ||
| 242 | // w.set_rtim(true); | ||
| 243 | // }); | ||
| 244 | // } | ||
| 245 | |||
| 229 | return Ok(n); | 246 | return Ok(n); |
| 230 | } | 247 | } |
| 231 | } | 248 | } |
| 232 | } | 249 | } |
| 233 | 250 | ||
| 234 | async fn inner_write<'a>(&'a self, buf: &'a [u8]) -> Result<usize, Error> { | 251 | async fn fill_buf(&self) -> Result<&[u8], Error> { |
| 235 | poll_fn(move |cx| { | 252 | poll_fn(move |cx| { |
| 236 | let mut inner = self.inner.borrow_mut(); | 253 | let state = T::buffered_state(); |
| 237 | let (poll, empty) = inner.with(|state| { | 254 | let mut rx_reader = unsafe { state.rx_buf.reader() }; |
| 238 | let empty = state.tx.is_empty(); | 255 | let (p, n) = rx_reader.pop_buf(); |
| 239 | let tx_buf = state.tx.push_buf(); | 256 | if n == 0 { |
| 240 | if tx_buf.is_empty() { | 257 | state.rx_waker.register(cx.waker()); |
| 241 | state.tx_waker.register(cx.waker()); | 258 | return Poll::Pending; |
| 242 | return (Poll::Pending, empty); | 259 | } |
| 243 | } | 260 | |
| 261 | let buf = unsafe { slice::from_raw_parts(p, n) }; | ||
| 262 | Poll::Ready(Ok(buf)) | ||
| 263 | }) | ||
| 264 | .await | ||
| 265 | } | ||
| 244 | 266 | ||
| 245 | let n = core::cmp::min(tx_buf.len(), buf.len()); | 267 | fn consume(&self, amt: usize) { |
| 246 | tx_buf[..n].copy_from_slice(&buf[..n]); | 268 | let state = T::buffered_state(); |
| 247 | state.tx.push(n); | 269 | let mut rx_reader = unsafe { state.rx_buf.reader() }; |
| 270 | let full = state.rx_buf.is_full(); | ||
| 271 | rx_reader.pop_done(amt); | ||
| 272 | if full { | ||
| 273 | unsafe { T::Interrupt::steal().pend() }; | ||
| 274 | } | ||
| 275 | } | ||
| 276 | } | ||
| 248 | 277 | ||
| 249 | (Poll::Ready(Ok(n)), empty) | 278 | impl<'d, T: BasicInstance> BufferedUartTx<'d, T> { |
| 279 | async fn write(&self, buf: &[u8]) -> Result<usize, Error> { | ||
| 280 | poll_fn(move |cx| { | ||
| 281 | let state = T::buffered_state(); | ||
| 282 | let mut tx_writer = unsafe { state.tx_buf.writer() }; | ||
| 283 | let n = tx_writer.push(|data| { | ||
| 284 | let n = data.len().min(buf.len()); | ||
| 285 | data[..n].copy_from_slice(&buf[..n]); | ||
| 286 | n | ||
| 250 | }); | 287 | }); |
| 251 | if empty { | 288 | if n == 0 { |
| 252 | inner.pend(); | 289 | state.tx_waker.register(cx.waker()); |
| 290 | return Poll::Pending; | ||
| 253 | } | 291 | } |
| 254 | poll | 292 | |
| 293 | // The TX interrupt only triggers when the there was data in the | ||
| 294 | // FIFO and the number of bytes drops below a threshold. When the | ||
| 295 | // FIFO was empty we have to manually pend the interrupt to shovel | ||
| 296 | // TX data from the buffer into the FIFO. | ||
| 297 | unsafe { T::Interrupt::steal() }.pend(); | ||
| 298 | Poll::Ready(Ok(n)) | ||
| 255 | }) | 299 | }) |
| 256 | .await | 300 | .await |
| 257 | } | 301 | } |
| 258 | 302 | ||
| 259 | async fn inner_flush<'a>(&'a self) -> Result<(), Error> { | 303 | async fn flush(&self) -> Result<(), Error> { |
| 260 | poll_fn(move |cx| { | 304 | poll_fn(move |cx| { |
| 261 | self.inner.borrow_mut().with(|state| { | 305 | let state = T::buffered_state(); |
| 262 | if !state.tx.is_empty() { | 306 | if !state.tx_buf.is_empty() { |
| 263 | state.tx_waker.register(cx.waker()); | 307 | state.tx_waker.register(cx.waker()); |
| 264 | return Poll::Pending; | 308 | return Poll::Pending; |
| 265 | } | 309 | } |
| 266 | 310 | ||
| 267 | Poll::Ready(Ok(())) | 311 | Poll::Ready(Ok(())) |
| 268 | }) | ||
| 269 | }) | 312 | }) |
| 270 | .await | 313 | .await |
| 271 | } | 314 | } |
| 272 | 315 | ||
| 273 | fn inner_blocking_write(&self, buf: &[u8]) -> Result<usize, Error> { | 316 | fn blocking_write(&self, buf: &[u8]) -> Result<usize, Error> { |
| 274 | loop { | 317 | loop { |
| 275 | let mut inner = self.inner.borrow_mut(); | 318 | let state = T::buffered_state(); |
| 276 | let (n, empty) = inner.with(|state| { | 319 | let mut tx_writer = unsafe { state.tx_buf.writer() }; |
| 277 | let empty = state.tx.is_empty(); | 320 | let n = tx_writer.push(|data| { |
| 278 | let tx_buf = state.tx.push_buf(); | 321 | let n = data.len().min(buf.len()); |
| 279 | if tx_buf.is_empty() { | 322 | data[..n].copy_from_slice(&buf[..n]); |
| 280 | return (0, empty); | 323 | n |
| 281 | } | ||
| 282 | |||
| 283 | let n = core::cmp::min(tx_buf.len(), buf.len()); | ||
| 284 | tx_buf[..n].copy_from_slice(&buf[..n]); | ||
| 285 | state.tx.push(n); | ||
| 286 | |||
| 287 | (n, empty) | ||
| 288 | }); | 324 | }); |
| 289 | if empty { | 325 | |
| 290 | inner.pend(); | ||
| 291 | } | ||
| 292 | if n != 0 { | 326 | if n != 0 { |
| 327 | // The TX interrupt only triggers when the there was data in the | ||
| 328 | // FIFO and the number of bytes drops below a threshold. When the | ||
| 329 | // FIFO was empty we have to manually pend the interrupt to shovel | ||
| 330 | // TX data from the buffer into the FIFO. | ||
| 331 | unsafe { T::Interrupt::steal() }.pend(); | ||
| 293 | return Ok(n); | 332 | return Ok(n); |
| 294 | } | 333 | } |
| 295 | } | 334 | } |
| 296 | } | 335 | } |
| 297 | 336 | ||
| 298 | fn inner_blocking_flush(&self) -> Result<(), Error> { | 337 | fn blocking_flush(&self) -> Result<(), Error> { |
| 299 | loop { | 338 | loop { |
| 300 | if !self.inner.borrow_mut().with(|state| state.tx.is_empty()) { | 339 | let state = T::buffered_state(); |
| 340 | if state.tx_buf.is_empty() { | ||
| 301 | return Ok(()); | 341 | return Ok(()); |
| 302 | } | 342 | } |
| 303 | } | 343 | } |
| 304 | } | 344 | } |
| 345 | } | ||
| 305 | 346 | ||
| 306 | async fn inner_fill_buf<'a>(&'a self) -> Result<&'a [u8], Error> { | 347 | impl<'d, T: BasicInstance> Drop for BufferedUartRx<'d, T> { |
| 307 | poll_fn(move |cx| { | 348 | fn drop(&mut self) { |
| 308 | self.inner.borrow_mut().with(|state| { | 349 | let state = T::buffered_state(); |
| 309 | compiler_fence(Ordering::SeqCst); | 350 | unsafe { |
| 310 | 351 | state.rx_buf.deinit(); | |
| 311 | // We have data ready in buffer? Return it. | ||
| 312 | let buf = state.rx.pop_buf(); | ||
| 313 | if !buf.is_empty() { | ||
| 314 | let buf: &[u8] = buf; | ||
| 315 | // Safety: buffer lives as long as uart | ||
| 316 | let buf: &[u8] = unsafe { core::mem::transmute(buf) }; | ||
| 317 | return Poll::Ready(Ok(buf)); | ||
| 318 | } | ||
| 319 | |||
| 320 | state.rx_waker.register(cx.waker()); | ||
| 321 | Poll::<Result<&[u8], Error>>::Pending | ||
| 322 | }) | ||
| 323 | }) | ||
| 324 | .await | ||
| 325 | } | ||
| 326 | 352 | ||
| 327 | fn inner_consume(&self, amt: usize) { | 353 | // TX is inactive if the the buffer is not available. |
| 328 | let mut inner = self.inner.borrow_mut(); | 354 | // We can now unregister the interrupt handler |
| 329 | let signal = inner.with(|state| { | 355 | if state.tx_buf.len() == 0 { |
| 330 | let full = state.rx.is_full(); | 356 | T::Interrupt::steal().disable(); |
| 331 | state.rx.pop(amt); | 357 | } |
| 332 | full | ||
| 333 | }); | ||
| 334 | if signal { | ||
| 335 | inner.pend(); | ||
| 336 | } | 358 | } |
| 337 | } | 359 | } |
| 338 | } | 360 | } |
| 339 | 361 | ||
| 340 | impl<'d, T: BasicInstance> StateInner<'d, T> | 362 | impl<'d, T: BasicInstance> Drop for BufferedUartTx<'d, T> { |
| 341 | where | 363 | fn drop(&mut self) { |
| 342 | Self: 'd, | 364 | let state = T::buffered_state(); |
| 343 | { | ||
| 344 | fn on_rx(&mut self) { | ||
| 345 | let r = T::regs(); | ||
| 346 | unsafe { | 365 | unsafe { |
| 347 | let sr = sr(r).read(); | 366 | state.tx_buf.deinit(); |
| 348 | clear_interrupt_flags(r, sr); | ||
| 349 | 367 | ||
| 350 | // This read also clears the error and idle interrupt flags on v1. | 368 | // RX is inactive if the the buffer is not available. |
| 351 | let b = rdr(r).read_volatile(); | 369 | // We can now unregister the interrupt handler |
| 370 | if state.rx_buf.len() == 0 { | ||
| 371 | T::Interrupt::steal().disable(); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | } | ||
| 375 | } | ||
| 352 | 376 | ||
| 353 | if sr.rxne() { | 377 | unsafe fn on_interrupt<T: BasicInstance>(_: *mut ()) { |
| 354 | if sr.pe() { | 378 | let r = T::regs(); |
| 355 | warn!("Parity error"); | 379 | let state = T::buffered_state(); |
| 356 | } | ||
| 357 | if sr.fe() { | ||
| 358 | warn!("Framing error"); | ||
| 359 | } | ||
| 360 | if sr.ne() { | ||
| 361 | warn!("Noise error"); | ||
| 362 | } | ||
| 363 | if sr.ore() { | ||
| 364 | warn!("Overrun error"); | ||
| 365 | } | ||
| 366 | 380 | ||
| 367 | let buf = self.rx.push_buf(); | 381 | // RX |
| 368 | if !buf.is_empty() { | 382 | unsafe { |
| 369 | buf[0] = b; | 383 | let sr = sr(r).read(); |
| 370 | self.rx.push(1); | 384 | clear_interrupt_flags(r, sr); |
| 371 | } else { | ||
| 372 | warn!("RX buffer full, discard received byte"); | ||
| 373 | } | ||
| 374 | 385 | ||
| 375 | if self.rx.is_full() { | 386 | if sr.rxne() { |
| 376 | self.rx_waker.wake(); | 387 | if sr.pe() { |
| 377 | } | 388 | warn!("Parity error"); |
| 389 | } | ||
| 390 | if sr.fe() { | ||
| 391 | warn!("Framing error"); | ||
| 392 | } | ||
| 393 | if sr.ne() { | ||
| 394 | warn!("Noise error"); | ||
| 395 | } | ||
| 396 | if sr.ore() { | ||
| 397 | warn!("Overrun error"); | ||
| 378 | } | 398 | } |
| 379 | 399 | ||
| 380 | if sr.idle() { | 400 | let mut rx_writer = state.rx_buf.writer(); |
| 381 | self.rx_waker.wake(); | 401 | let buf = rx_writer.push_slice(); |
| 382 | }; | 402 | if !buf.is_empty() { |
| 383 | } | 403 | // This read also clears the error and idle interrupt flags on v1. |
| 384 | } | 404 | buf[0] = rdr(r).read_volatile(); |
| 405 | rx_writer.push_done(1); | ||
| 406 | } else { | ||
| 407 | // FIXME: Should we disable any further RX interrupts when the buffer becomes full. | ||
| 408 | } | ||
| 385 | 409 | ||
| 386 | fn on_tx(&mut self) { | 410 | if state.rx_buf.is_full() { |
| 387 | let r = T::regs(); | 411 | state.rx_waker.wake(); |
| 388 | unsafe { | ||
| 389 | if sr(r).read().txe() { | ||
| 390 | let buf = self.tx.pop_buf(); | ||
| 391 | if !buf.is_empty() { | ||
| 392 | r.cr1().modify(|w| { | ||
| 393 | w.set_txeie(true); | ||
| 394 | }); | ||
| 395 | tdr(r).write_volatile(buf[0].into()); | ||
| 396 | self.tx.pop(1); | ||
| 397 | self.tx_waker.wake(); | ||
| 398 | } else { | ||
| 399 | // Disable interrupt until we have something to transmit again | ||
| 400 | r.cr1().modify(|w| { | ||
| 401 | w.set_txeie(false); | ||
| 402 | }); | ||
| 403 | } | ||
| 404 | } | 412 | } |
| 405 | } | 413 | } |
| 406 | } | ||
| 407 | } | ||
| 408 | 414 | ||
| 409 | impl<'d, T: BasicInstance> PeripheralState for StateInner<'d, T> | 415 | if sr.idle() { |
| 410 | where | 416 | state.rx_waker.wake(); |
| 411 | Self: 'd, | 417 | }; |
| 412 | { | 418 | } |
| 413 | type Interrupt = T::Interrupt; | 419 | |
| 414 | fn on_interrupt(&mut self) { | 420 | // TX |
| 415 | self.on_rx(); | 421 | unsafe { |
| 416 | self.on_tx(); | 422 | if sr(r).read().txe() { |
| 423 | let mut tx_reader = state.tx_buf.reader(); | ||
| 424 | let buf = tx_reader.pop_slice(); | ||
| 425 | if !buf.is_empty() { | ||
| 426 | r.cr1().modify(|w| { | ||
| 427 | w.set_txeie(true); | ||
| 428 | }); | ||
| 429 | tdr(r).write_volatile(buf[0].into()); | ||
| 430 | tx_reader.pop_done(1); | ||
| 431 | state.tx_waker.wake(); | ||
| 432 | } else { | ||
| 433 | // Disable interrupt until we have something to transmit again | ||
| 434 | r.cr1().modify(|w| { | ||
| 435 | w.set_txeie(false); | ||
| 436 | }); | ||
| 437 | } | ||
| 438 | } | ||
| 417 | } | 439 | } |
| 418 | } | 440 | } |
| 419 | 441 | ||
| @@ -427,94 +449,284 @@ impl<'d, T: BasicInstance> embedded_io::Io for BufferedUart<'d, T> { | |||
| 427 | type Error = Error; | 449 | type Error = Error; |
| 428 | } | 450 | } |
| 429 | 451 | ||
| 430 | impl<'u, 'd, T: BasicInstance> embedded_io::Io for BufferedUartRx<'u, 'd, T> { | 452 | impl<'d, T: BasicInstance> embedded_io::Io for BufferedUartRx<'d, T> { |
| 431 | type Error = Error; | 453 | type Error = Error; |
| 432 | } | 454 | } |
| 433 | 455 | ||
| 434 | impl<'u, 'd, T: BasicInstance> embedded_io::Io for BufferedUartTx<'u, 'd, T> { | 456 | impl<'d, T: BasicInstance> embedded_io::Io for BufferedUartTx<'d, T> { |
| 435 | type Error = Error; | 457 | type Error = Error; |
| 436 | } | 458 | } |
| 437 | 459 | ||
| 438 | impl<'d, T: BasicInstance> embedded_io::asynch::Read for BufferedUart<'d, T> { | 460 | impl<'d, T: BasicInstance> embedded_io::asynch::Read for BufferedUart<'d, T> { |
| 439 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | 461 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 440 | self.inner_read(buf).await | 462 | self.rx.read(buf).await |
| 441 | } | 463 | } |
| 442 | } | 464 | } |
| 443 | 465 | ||
| 444 | impl<'u, 'd, T: BasicInstance> embedded_io::asynch::Read for BufferedUartRx<'u, 'd, T> { | 466 | impl<'d, T: BasicInstance> embedded_io::asynch::Read for BufferedUartRx<'d, T> { |
| 445 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | 467 | async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 446 | self.inner.inner_read(buf).await | 468 | Self::read(self, buf).await |
| 447 | } | 469 | } |
| 448 | } | 470 | } |
| 449 | 471 | ||
| 450 | impl<'d, T: BasicInstance> embedded_io::asynch::BufRead for BufferedUart<'d, T> { | 472 | impl<'d, T: BasicInstance> embedded_io::asynch::BufRead for BufferedUart<'d, T> { |
| 451 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { | 473 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { |
| 452 | self.inner_fill_buf().await | 474 | self.rx.fill_buf().await |
| 453 | } | 475 | } |
| 454 | 476 | ||
| 455 | fn consume(&mut self, amt: usize) { | 477 | fn consume(&mut self, amt: usize) { |
| 456 | self.inner_consume(amt) | 478 | self.rx.consume(amt) |
| 457 | } | 479 | } |
| 458 | } | 480 | } |
| 459 | 481 | ||
| 460 | impl<'u, 'd, T: BasicInstance> embedded_io::asynch::BufRead for BufferedUartRx<'u, 'd, T> { | 482 | impl<'d, T: BasicInstance> embedded_io::asynch::BufRead for BufferedUartRx<'d, T> { |
| 461 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { | 483 | async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> { |
| 462 | self.inner.inner_fill_buf().await | 484 | Self::fill_buf(self).await |
| 463 | } | 485 | } |
| 464 | 486 | ||
| 465 | fn consume(&mut self, amt: usize) { | 487 | fn consume(&mut self, amt: usize) { |
| 466 | self.inner.inner_consume(amt) | 488 | Self::consume(self, amt) |
| 467 | } | 489 | } |
| 468 | } | 490 | } |
| 469 | 491 | ||
| 470 | impl<'d, T: BasicInstance> embedded_io::asynch::Write for BufferedUart<'d, T> { | 492 | impl<'d, T: BasicInstance> embedded_io::asynch::Write for BufferedUart<'d, T> { |
| 471 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 493 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 472 | self.inner_write(buf).await | 494 | self.tx.write(buf).await |
| 473 | } | 495 | } |
| 474 | 496 | ||
| 475 | async fn flush(&mut self) -> Result<(), Self::Error> { | 497 | async fn flush(&mut self) -> Result<(), Self::Error> { |
| 476 | self.inner_flush().await | 498 | self.tx.flush().await |
| 477 | } | 499 | } |
| 478 | } | 500 | } |
| 479 | 501 | ||
| 480 | impl<'u, 'd, T: BasicInstance> embedded_io::asynch::Write for BufferedUartTx<'u, 'd, T> { | 502 | impl<'d, T: BasicInstance> embedded_io::asynch::Write for BufferedUartTx<'d, T> { |
| 481 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 503 | async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 482 | self.inner.inner_write(buf).await | 504 | Self::write(self, buf).await |
| 483 | } | 505 | } |
| 484 | 506 | ||
| 485 | async fn flush(&mut self) -> Result<(), Self::Error> { | 507 | async fn flush(&mut self) -> Result<(), Self::Error> { |
| 486 | self.inner.inner_flush().await | 508 | Self::flush(self).await |
| 487 | } | 509 | } |
| 488 | } | 510 | } |
| 489 | 511 | ||
| 490 | impl<'d, T: BasicInstance> embedded_io::blocking::Read for BufferedUart<'d, T> { | 512 | impl<'d, T: BasicInstance> embedded_io::blocking::Read for BufferedUart<'d, T> { |
| 491 | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | 513 | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 492 | self.inner_blocking_read(buf) | 514 | self.rx.blocking_read(buf) |
| 493 | } | 515 | } |
| 494 | } | 516 | } |
| 495 | 517 | ||
| 496 | impl<'u, 'd, T: BasicInstance> embedded_io::blocking::Read for BufferedUartRx<'u, 'd, T> { | 518 | impl<'d, T: BasicInstance> embedded_io::blocking::Read for BufferedUartRx<'d, T> { |
| 497 | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | 519 | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 498 | self.inner.inner_blocking_read(buf) | 520 | self.blocking_read(buf) |
| 499 | } | 521 | } |
| 500 | } | 522 | } |
| 501 | 523 | ||
| 502 | impl<'d, T: BasicInstance> embedded_io::blocking::Write for BufferedUart<'d, T> { | 524 | impl<'d, T: BasicInstance> embedded_io::blocking::Write for BufferedUart<'d, T> { |
| 503 | fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 525 | fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 504 | self.inner_blocking_write(buf) | 526 | self.tx.blocking_write(buf) |
| 505 | } | 527 | } |
| 506 | 528 | ||
| 507 | fn flush(&mut self) -> Result<(), Self::Error> { | 529 | fn flush(&mut self) -> Result<(), Self::Error> { |
| 508 | self.inner_blocking_flush() | 530 | self.tx.blocking_flush() |
| 509 | } | 531 | } |
| 510 | } | 532 | } |
| 511 | 533 | ||
| 512 | impl<'u, 'd, T: BasicInstance> embedded_io::blocking::Write for BufferedUartTx<'u, 'd, T> { | 534 | impl<'d, T: BasicInstance> embedded_io::blocking::Write for BufferedUartTx<'d, T> { |
| 513 | fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | 535 | fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 514 | self.inner.inner_blocking_write(buf) | 536 | Self::blocking_write(self, buf) |
| 515 | } | 537 | } |
| 516 | 538 | ||
| 517 | fn flush(&mut self) -> Result<(), Self::Error> { | 539 | fn flush(&mut self) -> Result<(), Self::Error> { |
| 518 | self.inner.inner_blocking_flush() | 540 | Self::blocking_flush(self) |
| 541 | } | ||
| 542 | } | ||
| 543 | |||
| 544 | mod eh02 { | ||
| 545 | use super::*; | ||
| 546 | |||
| 547 | impl<'d, T: BasicInstance> embedded_hal_02::serial::Read<u8> for BufferedUartRx<'d, T> { | ||
| 548 | type Error = Error; | ||
| 549 | |||
| 550 | fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { | ||
| 551 | let r = T::regs(); | ||
| 552 | unsafe { | ||
| 553 | let sr = sr(r).read(); | ||
| 554 | if sr.pe() { | ||
| 555 | rdr(r).read_volatile(); | ||
| 556 | Err(nb::Error::Other(Error::Parity)) | ||
| 557 | } else if sr.fe() { | ||
| 558 | rdr(r).read_volatile(); | ||
| 559 | Err(nb::Error::Other(Error::Framing)) | ||
| 560 | } else if sr.ne() { | ||
| 561 | rdr(r).read_volatile(); | ||
| 562 | Err(nb::Error::Other(Error::Noise)) | ||
| 563 | } else if sr.ore() { | ||
| 564 | rdr(r).read_volatile(); | ||
| 565 | Err(nb::Error::Other(Error::Overrun)) | ||
| 566 | } else if sr.rxne() { | ||
| 567 | Ok(rdr(r).read_volatile()) | ||
| 568 | } else { | ||
| 569 | Err(nb::Error::WouldBlock) | ||
| 570 | } | ||
| 571 | } | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | impl<'d, T: BasicInstance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUartTx<'d, T> { | ||
| 576 | type Error = Error; | ||
| 577 | |||
| 578 | fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> { | ||
| 579 | while !buffer.is_empty() { | ||
| 580 | match self.blocking_write(buffer) { | ||
| 581 | Ok(0) => panic!("zero-length write."), | ||
| 582 | Ok(n) => buffer = &buffer[n..], | ||
| 583 | Err(e) => return Err(e), | ||
| 584 | } | ||
| 585 | } | ||
| 586 | Ok(()) | ||
| 587 | } | ||
| 588 | |||
| 589 | fn bflush(&mut self) -> Result<(), Self::Error> { | ||
| 590 | self.blocking_flush() | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | impl<'d, T: BasicInstance> embedded_hal_02::serial::Read<u8> for BufferedUart<'d, T> { | ||
| 595 | type Error = Error; | ||
| 596 | |||
| 597 | fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { | ||
| 598 | embedded_hal_02::serial::Read::read(&mut self.rx) | ||
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 | impl<'d, T: BasicInstance> embedded_hal_02::blocking::serial::Write<u8> for BufferedUart<'d, T> { | ||
| 603 | type Error = Error; | ||
| 604 | |||
| 605 | fn bwrite_all(&mut self, mut buffer: &[u8]) -> Result<(), Self::Error> { | ||
| 606 | while !buffer.is_empty() { | ||
| 607 | match self.tx.blocking_write(buffer) { | ||
| 608 | Ok(0) => panic!("zero-length write."), | ||
| 609 | Ok(n) => buffer = &buffer[n..], | ||
| 610 | Err(e) => return Err(e), | ||
| 611 | } | ||
| 612 | } | ||
| 613 | Ok(()) | ||
| 614 | } | ||
| 615 | |||
| 616 | fn bflush(&mut self) -> Result<(), Self::Error> { | ||
| 617 | self.tx.blocking_flush() | ||
| 618 | } | ||
| 619 | } | ||
| 620 | } | ||
| 621 | |||
| 622 | #[cfg(feature = "unstable-traits")] | ||
| 623 | mod eh1 { | ||
| 624 | use super::*; | ||
| 625 | |||
| 626 | impl<'d, T: BasicInstance> embedded_hal_1::serial::ErrorType for BufferedUart<'d, T> { | ||
| 627 | type Error = Error; | ||
| 628 | } | ||
| 629 | |||
| 630 | impl<'d, T: BasicInstance> embedded_hal_1::serial::ErrorType for BufferedUartTx<'d, T> { | ||
| 631 | type Error = Error; | ||
| 632 | } | ||
| 633 | |||
| 634 | impl<'d, T: BasicInstance> embedded_hal_1::serial::ErrorType for BufferedUartRx<'d, T> { | ||
| 635 | type Error = Error; | ||
| 636 | } | ||
| 637 | |||
| 638 | impl<'d, T: BasicInstance> embedded_hal_nb::serial::Read for BufferedUartRx<'d, T> { | ||
| 639 | fn read(&mut self) -> nb::Result<u8, Self::Error> { | ||
| 640 | embedded_hal_02::serial::Read::read(self) | ||
| 641 | } | ||
| 642 | } | ||
| 643 | |||
| 644 | impl<'d, T: BasicInstance> embedded_hal_1::serial::Write for BufferedUartTx<'d, T> { | ||
| 645 | fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { | ||
| 646 | self.blocking_write(buffer).map(drop) | ||
| 647 | } | ||
| 648 | |||
| 649 | fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 650 | self.blocking_flush() | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | impl<'d, T: BasicInstance> embedded_hal_nb::serial::Write for BufferedUartTx<'d, T> { | ||
| 655 | fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> { | ||
| 656 | self.blocking_write(&[char]).map(drop).map_err(nb::Error::Other) | ||
| 657 | } | ||
| 658 | |||
| 659 | fn flush(&mut self) -> nb::Result<(), Self::Error> { | ||
| 660 | self.blocking_flush().map_err(nb::Error::Other) | ||
| 661 | } | ||
| 662 | } | ||
| 663 | |||
| 664 | impl<'d, T: BasicInstance> embedded_hal_nb::serial::Read for BufferedUart<'d, T> { | ||
| 665 | fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { | ||
| 666 | embedded_hal_02::serial::Read::read(&mut self.rx) | ||
| 667 | } | ||
| 668 | } | ||
| 669 | |||
| 670 | impl<'d, T: BasicInstance> embedded_hal_1::serial::Write for BufferedUart<'d, T> { | ||
| 671 | fn write(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { | ||
| 672 | self.tx.blocking_write(buffer).map(drop) | ||
| 673 | } | ||
| 674 | |||
| 675 | fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 676 | self.tx.blocking_flush() | ||
| 677 | } | ||
| 678 | } | ||
| 679 | |||
| 680 | impl<'d, T: BasicInstance> embedded_hal_nb::serial::Write for BufferedUart<'d, T> { | ||
| 681 | fn write(&mut self, char: u8) -> nb::Result<(), Self::Error> { | ||
| 682 | self.tx.blocking_write(&[char]).map(drop).map_err(nb::Error::Other) | ||
| 683 | } | ||
| 684 | |||
| 685 | fn flush(&mut self) -> nb::Result<(), Self::Error> { | ||
| 686 | self.tx.blocking_flush().map_err(nb::Error::Other) | ||
| 687 | } | ||
| 688 | } | ||
| 689 | } | ||
| 690 | |||
| 691 | #[cfg(all( | ||
| 692 | feature = "unstable-traits", | ||
| 693 | feature = "nightly", | ||
| 694 | feature = "_todo_embedded_hal_serial" | ||
| 695 | ))] | ||
| 696 | mod eha { | ||
| 697 | use core::future::Future; | ||
| 698 | |||
| 699 | use super::*; | ||
| 700 | |||
| 701 | impl<'d, T: BasicInstance> embedded_hal_async::serial::Write for BufferedUartTx<'d, T> { | ||
| 702 | async fn write(&mut self, buf: &[u8]) -> Result<(), Self::Error> { | ||
| 703 | Self::write(buf) | ||
| 704 | } | ||
| 705 | |||
| 706 | async fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 707 | Self::flush() | ||
| 708 | } | ||
| 709 | } | ||
| 710 | |||
| 711 | impl<'d, T: BasicInstance> embedded_hal_async::serial::Read for BufferedUartRx<'d, T> { | ||
| 712 | async fn read(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { | ||
| 713 | Self::read(buf) | ||
| 714 | } | ||
| 715 | } | ||
| 716 | |||
| 717 | impl<'d, T: BasicInstance> embedded_hal_async::serial::Write for BufferedUart<'d, T> { | ||
| 718 | async fn write(&mut self, buf: &[u8]) -> Result<(), Self::Error> { | ||
| 719 | self.tx.write(buf) | ||
| 720 | } | ||
| 721 | |||
| 722 | async fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 723 | self.tx.flush() | ||
| 724 | } | ||
| 725 | } | ||
| 726 | |||
| 727 | impl<'d, T: BasicInstance> embedded_hal_async::serial::Read for BufferedUart<'d, T> { | ||
| 728 | async fn read(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { | ||
| 729 | self.rx.read(buf) | ||
| 730 | } | ||
| 519 | } | 731 | } |
| 520 | } | 732 | } |
diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index f80323e37..a42eede18 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs | |||
| @@ -1112,6 +1112,9 @@ pub(crate) mod sealed { | |||
| 1112 | 1112 | ||
| 1113 | fn regs() -> Regs; | 1113 | fn regs() -> Regs; |
| 1114 | fn state() -> &'static State; | 1114 | fn state() -> &'static State; |
| 1115 | |||
| 1116 | #[cfg(feature = "nightly")] | ||
| 1117 | fn buffered_state() -> &'static buffered::State; | ||
| 1115 | } | 1118 | } |
| 1116 | 1119 | ||
| 1117 | pub trait FullInstance: BasicInstance { | 1120 | pub trait FullInstance: BasicInstance { |
| @@ -1147,6 +1150,12 @@ macro_rules! impl_lpuart { | |||
| 1147 | static STATE: crate::usart::sealed::State = crate::usart::sealed::State::new(); | 1150 | static STATE: crate::usart::sealed::State = crate::usart::sealed::State::new(); |
| 1148 | &STATE | 1151 | &STATE |
| 1149 | } | 1152 | } |
| 1153 | |||
| 1154 | #[cfg(feature = "nightly")] | ||
| 1155 | fn buffered_state() -> &'static buffered::State { | ||
| 1156 | static STATE: buffered::State = buffered::State::new(); | ||
| 1157 | &STATE | ||
| 1158 | } | ||
| 1150 | } | 1159 | } |
| 1151 | 1160 | ||
| 1152 | impl BasicInstance for peripherals::$inst {} | 1161 | impl BasicInstance for peripherals::$inst {} |
