diff options
| author | Mathias <[email protected]> | 2023-03-28 14:28:44 +0200 |
|---|---|---|
| committer | Mathias <[email protected]> | 2023-03-28 14:28:44 +0200 |
| commit | 2d7f35cf571cf46716f01c63cf21a2e2c95afd5d (patch) | |
| tree | 601ff59b757c4a35898e4589288a6ca1ee014821 | |
| parent | 299689dfa2a5e160dbd6aa474772a9317a219084 (diff) | |
Add embedded-io blocking Read + Write for BufferedUart
| -rw-r--r-- | embassy-stm32/src/usart/buffered.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/embassy-stm32/src/usart/buffered.rs b/embassy-stm32/src/usart/buffered.rs index a27fcc1ca..3377b3f9f 100644 --- a/embassy-stm32/src/usart/buffered.rs +++ b/embassy-stm32/src/usart/buffered.rs | |||
| @@ -197,6 +197,40 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 197 | .await | 197 | .await |
| 198 | } | 198 | } |
| 199 | 199 | ||
| 200 | fn inner_blocking_read<'a>(&'a self, buf: &'a mut [u8]) -> Result<usize, Error> { | ||
| 201 | loop { | ||
| 202 | let mut do_pend = false; | ||
| 203 | let mut inner = self.inner.borrow_mut(); | ||
| 204 | let n = inner.with(|state| { | ||
| 205 | compiler_fence(Ordering::SeqCst); | ||
| 206 | |||
| 207 | // We have data ready in buffer? Return it. | ||
| 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 | }); | ||
| 223 | |||
| 224 | if do_pend { | ||
| 225 | inner.pend(); | ||
| 226 | } | ||
| 227 | |||
| 228 | if n > 0 { | ||
| 229 | return Ok(n); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 200 | async fn inner_write<'a>(&'a self, buf: &'a [u8]) -> Result<usize, Error> { | 234 | async fn inner_write<'a>(&'a self, buf: &'a [u8]) -> Result<usize, Error> { |
| 201 | poll_fn(move |cx| { | 235 | poll_fn(move |cx| { |
| 202 | let mut inner = self.inner.borrow_mut(); | 236 | let mut inner = self.inner.borrow_mut(); |
| @@ -236,6 +270,39 @@ impl<'d, T: BasicInstance> BufferedUart<'d, T> { | |||
| 236 | .await | 270 | .await |
| 237 | } | 271 | } |
| 238 | 272 | ||
| 273 | fn inner_blocking_write<'a>(&'a self, buf: &'a [u8]) -> Result<usize, Error> { | ||
| 274 | loop { | ||
| 275 | let mut inner = self.inner.borrow_mut(); | ||
| 276 | let (n, empty) = inner.with(|state| { | ||
| 277 | let empty = state.tx.is_empty(); | ||
| 278 | let tx_buf = state.tx.push_buf(); | ||
| 279 | if tx_buf.is_empty() { | ||
| 280 | return (0, empty); | ||
| 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 | }); | ||
| 289 | if empty { | ||
| 290 | inner.pend(); | ||
| 291 | } | ||
| 292 | if n != 0 { | ||
| 293 | return Ok(n); | ||
| 294 | } | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | fn inner_blocking_flush(&self) -> Result<(), Error> { | ||
| 299 | loop { | ||
| 300 | if !self.inner.borrow_mut().with(|state| state.tx.is_empty()) { | ||
| 301 | return Ok(()); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 239 | async fn inner_fill_buf<'a>(&'a self) -> Result<&'a [u8], Error> { | 306 | async fn inner_fill_buf<'a>(&'a self) -> Result<&'a [u8], Error> { |
| 240 | poll_fn(move |cx| { | 307 | poll_fn(move |cx| { |
| 241 | self.inner.borrow_mut().with(|state| { | 308 | self.inner.borrow_mut().with(|state| { |
| @@ -419,3 +486,35 @@ impl<'u, 'd, T: BasicInstance> embedded_io::asynch::Write for BufferedUartTx<'u, | |||
| 419 | self.inner.inner_flush().await | 486 | self.inner.inner_flush().await |
| 420 | } | 487 | } |
| 421 | } | 488 | } |
| 489 | |||
| 490 | impl<'d, T: BasicInstance> embedded_io::blocking::Read for BufferedUart<'d, T> { | ||
| 491 | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | ||
| 492 | self.inner_blocking_read(buf) | ||
| 493 | } | ||
| 494 | } | ||
| 495 | |||
| 496 | impl<'u, 'd, T: BasicInstance> embedded_io::blocking::Read for BufferedUartRx<'u, 'd, T> { | ||
| 497 | fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { | ||
| 498 | self.inner.inner_blocking_read(buf) | ||
| 499 | } | ||
| 500 | } | ||
| 501 | |||
| 502 | impl<'d, T: BasicInstance> embedded_io::blocking::Write for BufferedUart<'d, T> { | ||
| 503 | fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
| 504 | self.inner_blocking_write(buf) | ||
| 505 | } | ||
| 506 | |||
| 507 | fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 508 | self.inner_blocking_flush() | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | impl<'u, 'd, T: BasicInstance> embedded_io::blocking::Write for BufferedUartTx<'u, 'd, T> { | ||
| 513 | fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
| 514 | self.inner.inner_blocking_write(buf) | ||
| 515 | } | ||
| 516 | |||
| 517 | fn flush(&mut self) -> Result<(), Self::Error> { | ||
| 518 | self.inner.inner_blocking_flush() | ||
| 519 | } | ||
| 520 | } | ||
