diff options
| -rw-r--r-- | embassy-rp/src/uart/buffered.rs | 2 | ||||
| -rw-r--r-- | embassy-rp/src/uart/mod.rs | 151 | ||||
| -rw-r--r-- | examples/rp/src/bin/uart_unidir.rs | 9 | ||||
| -rw-r--r-- | tests/rp/src/bin/uart_dma.rs | 238 |
4 files changed, 375 insertions, 25 deletions
diff --git a/embassy-rp/src/uart/buffered.rs b/embassy-rp/src/uart/buffered.rs index 58fede765..59fec8f1b 100644 --- a/embassy-rp/src/uart/buffered.rs +++ b/embassy-rp/src/uart/buffered.rs | |||
| @@ -74,7 +74,7 @@ pub(crate) fn init_buffers<'d, T: Instance + 'd>( | |||
| 74 | // to pend the ISR when we want data transmission to start. | 74 | // to pend the ISR when we want data transmission to start. |
| 75 | let regs = T::regs(); | 75 | let regs = T::regs(); |
| 76 | unsafe { | 76 | unsafe { |
| 77 | regs.uartimsc().write_set(|w| { | 77 | regs.uartimsc().write(|w| { |
| 78 | w.set_rxim(true); | 78 | w.set_rxim(true); |
| 79 | w.set_rtim(true); | 79 | w.set_rtim(true); |
| 80 | w.set_txim(true); | 80 | w.set_txim(true); |
diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index 8612e9cda..ebe32cc66 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs | |||
| @@ -1,7 +1,14 @@ | |||
| 1 | use core::future::poll_fn; | ||
| 1 | use core::marker::PhantomData; | 2 | use core::marker::PhantomData; |
| 3 | use core::task::Poll; | ||
| 2 | 4 | ||
| 5 | use atomic_polyfill::{AtomicU16, Ordering}; | ||
| 6 | use embassy_cortex_m::interrupt::{Interrupt, InterruptExt}; | ||
| 7 | use embassy_futures::select::{select, Either}; | ||
| 3 | use embassy_hal_common::{into_ref, PeripheralRef}; | 8 | use embassy_hal_common::{into_ref, PeripheralRef}; |
| 9 | use embassy_sync::waitqueue::AtomicWaker; | ||
| 4 | use embassy_time::{Duration, Timer}; | 10 | use embassy_time::{Duration, Timer}; |
| 11 | use pac::uart::regs::Uartris; | ||
| 5 | 12 | ||
| 6 | use crate::clocks::clk_peri_freq; | 13 | use crate::clocks::clk_peri_freq; |
| 7 | use crate::dma::{AnyChannel, Channel}; | 14 | use crate::dma::{AnyChannel, Channel}; |
| @@ -97,6 +104,11 @@ pub enum Error { | |||
| 97 | Framing, | 104 | Framing, |
| 98 | } | 105 | } |
| 99 | 106 | ||
| 107 | pub struct DmaState { | ||
| 108 | rx_err_waker: AtomicWaker, | ||
| 109 | rx_errs: AtomicU16, | ||
| 110 | } | ||
| 111 | |||
| 100 | pub struct Uart<'d, T: Instance, M: Mode> { | 112 | pub struct Uart<'d, T: Instance, M: Mode> { |
| 101 | tx: UartTx<'d, T, M>, | 113 | tx: UartTx<'d, T, M>, |
| 102 | rx: UartRx<'d, T, M>, | 114 | rx: UartRx<'d, T, M>, |
| @@ -223,15 +235,26 @@ impl<'d, T: Instance, M: Mode> UartRx<'d, T, M> { | |||
| 223 | pub fn new( | 235 | pub fn new( |
| 224 | _uart: impl Peripheral<P = T> + 'd, | 236 | _uart: impl Peripheral<P = T> + 'd, |
| 225 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 237 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 238 | irq: impl Peripheral<P = T::Interrupt> + 'd, | ||
| 226 | rx_dma: impl Peripheral<P = impl Channel> + 'd, | 239 | rx_dma: impl Peripheral<P = impl Channel> + 'd, |
| 227 | config: Config, | 240 | config: Config, |
| 228 | ) -> Self { | 241 | ) -> Self { |
| 229 | into_ref!(rx, rx_dma); | 242 | into_ref!(rx, irq, rx_dma); |
| 230 | Uart::<T, M>::init(None, Some(rx.map_into()), None, None, config); | 243 | Uart::<T, M>::init(None, Some(rx.map_into()), None, None, config); |
| 231 | Self::new_inner(Some(rx_dma.map_into())) | 244 | Self::new_inner(Some(irq), Some(rx_dma.map_into())) |
| 232 | } | 245 | } |
| 233 | 246 | ||
| 234 | fn new_inner(rx_dma: Option<PeripheralRef<'d, AnyChannel>>) -> Self { | 247 | fn new_inner(irq: Option<PeripheralRef<'d, T::Interrupt>>, rx_dma: Option<PeripheralRef<'d, AnyChannel>>) -> Self { |
| 248 | debug_assert_eq!(irq.is_some(), rx_dma.is_some()); | ||
| 249 | if let Some(irq) = irq { | ||
| 250 | unsafe { | ||
| 251 | // disable all error interrupts initially | ||
| 252 | T::regs().uartimsc().write(|w| w.0 = 0); | ||
| 253 | } | ||
| 254 | irq.set_handler(on_interrupt::<T>); | ||
| 255 | irq.unpend(); | ||
| 256 | irq.enable(); | ||
| 257 | } | ||
| 235 | Self { | 258 | Self { |
| 236 | rx_dma, | 259 | rx_dma, |
| 237 | phantom: PhantomData, | 260 | phantom: PhantomData, |
| @@ -271,6 +294,16 @@ impl<'d, T: Instance, M: Mode> UartRx<'d, T, M> { | |||
| 271 | } | 294 | } |
| 272 | } | 295 | } |
| 273 | 296 | ||
| 297 | impl<'d, T: Instance, M: Mode> Drop for UartRx<'d, T, M> { | ||
| 298 | fn drop(&mut self) { | ||
| 299 | if let Some(_) = self.rx_dma { | ||
| 300 | unsafe { | ||
| 301 | T::Interrupt::steal().disable(); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 274 | impl<'d, T: Instance> UartRx<'d, T, Blocking> { | 307 | impl<'d, T: Instance> UartRx<'d, T, Blocking> { |
| 275 | pub fn new_blocking( | 308 | pub fn new_blocking( |
| 276 | _uart: impl Peripheral<P = T> + 'd, | 309 | _uart: impl Peripheral<P = T> + 'd, |
| @@ -279,7 +312,7 @@ impl<'d, T: Instance> UartRx<'d, T, Blocking> { | |||
| 279 | ) -> Self { | 312 | ) -> Self { |
| 280 | into_ref!(rx); | 313 | into_ref!(rx); |
| 281 | Uart::<T, Blocking>::init(None, Some(rx.map_into()), None, None, config); | 314 | Uart::<T, Blocking>::init(None, Some(rx.map_into()), None, None, config); |
| 282 | Self::new_inner(None) | 315 | Self::new_inner(None, None) |
| 283 | } | 316 | } |
| 284 | 317 | ||
| 285 | #[cfg(feature = "nightly")] | 318 | #[cfg(feature = "nightly")] |
| @@ -296,19 +329,93 @@ impl<'d, T: Instance> UartRx<'d, T, Blocking> { | |||
| 296 | } | 329 | } |
| 297 | } | 330 | } |
| 298 | 331 | ||
| 332 | unsafe fn on_interrupt<T: Instance>(_: *mut ()) { | ||
| 333 | let uart = T::regs(); | ||
| 334 | let state = T::dma_state(); | ||
| 335 | let errs = uart.uartris().read(); | ||
| 336 | state.rx_errs.store(errs.0 as u16, Ordering::Relaxed); | ||
| 337 | state.rx_err_waker.wake(); | ||
| 338 | // disable the error interrupts instead of clearing the flags. clearing the | ||
| 339 | // flags would allow the dma transfer to continue, potentially signaling | ||
| 340 | // completion before we can check for errors that happened *during* the transfer. | ||
| 341 | uart.uartimsc().write_clear(|w| w.0 = errs.0); | ||
| 342 | } | ||
| 343 | |||
| 299 | impl<'d, T: Instance> UartRx<'d, T, Async> { | 344 | impl<'d, T: Instance> UartRx<'d, T, Async> { |
| 300 | pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> { | 345 | pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> { |
| 346 | // clear error flags before we drain the fifo. errors that have accumulated | ||
| 347 | // in the flags will also be present in the fifo. | ||
| 348 | T::dma_state().rx_errs.store(0, Ordering::Relaxed); | ||
| 349 | unsafe { | ||
| 350 | T::regs().uarticr().write(|w| { | ||
| 351 | w.set_oeic(true); | ||
| 352 | w.set_beic(true); | ||
| 353 | w.set_peic(true); | ||
| 354 | w.set_feic(true); | ||
| 355 | }); | ||
| 356 | } | ||
| 357 | |||
| 358 | // then drain the fifo. we need to read at most 32 bytes. errors that apply | ||
| 359 | // to fifo bytes will be reported directly. | ||
| 360 | let buffer = match { | ||
| 361 | let limit = buffer.len().min(32); | ||
| 362 | self.drain_fifo(&mut buffer[0..limit]) | ||
| 363 | } { | ||
| 364 | Ok(len) if len < buffer.len() => &mut buffer[len..], | ||
| 365 | Ok(_) => return Ok(()), | ||
| 366 | Err(e) => return Err(e), | ||
| 367 | }; | ||
| 368 | |||
| 369 | // start a dma transfer. if errors have happened in the interim some error | ||
| 370 | // interrupt flags will have been raised, and those will be picked up immediately | ||
| 371 | // by the interrupt handler. | ||
| 301 | let ch = self.rx_dma.as_mut().unwrap(); | 372 | let ch = self.rx_dma.as_mut().unwrap(); |
| 302 | let transfer = unsafe { | 373 | let transfer = unsafe { |
| 374 | T::regs().uartimsc().write_set(|w| { | ||
| 375 | w.set_oeim(true); | ||
| 376 | w.set_beim(true); | ||
| 377 | w.set_peim(true); | ||
| 378 | w.set_feim(true); | ||
| 379 | }); | ||
| 303 | T::regs().uartdmacr().write_set(|reg| { | 380 | T::regs().uartdmacr().write_set(|reg| { |
| 304 | reg.set_rxdmae(true); | 381 | reg.set_rxdmae(true); |
| 382 | reg.set_dmaonerr(true); | ||
| 305 | }); | 383 | }); |
| 306 | // If we don't assign future to a variable, the data register pointer | 384 | // If we don't assign future to a variable, the data register pointer |
| 307 | // is held across an await and makes the future non-Send. | 385 | // is held across an await and makes the future non-Send. |
| 308 | crate::dma::read(ch, T::regs().uartdr().ptr() as *const _, buffer, T::RX_DREQ) | 386 | crate::dma::read(ch, T::regs().uartdr().ptr() as *const _, buffer, T::RX_DREQ) |
| 309 | }; | 387 | }; |
| 310 | transfer.await; | 388 | |
| 311 | Ok(()) | 389 | // wait for either the transfer to complete or an error to happen. |
| 390 | let transfer_result = select( | ||
| 391 | transfer, | ||
| 392 | poll_fn(|cx| { | ||
| 393 | T::dma_state().rx_err_waker.register(cx.waker()); | ||
| 394 | match T::dma_state().rx_errs.swap(0, Ordering::Relaxed) { | ||
| 395 | 0 => Poll::Pending, | ||
| 396 | e => Poll::Ready(Uartris(e as u32)), | ||
| 397 | } | ||
| 398 | }), | ||
| 399 | ) | ||
| 400 | .await; | ||
| 401 | |||
| 402 | let errors = match transfer_result { | ||
| 403 | Either::First(()) => return Ok(()), | ||
| 404 | Either::Second(e) => e, | ||
| 405 | }; | ||
| 406 | |||
| 407 | if errors.0 == 0 { | ||
| 408 | return Ok(()); | ||
| 409 | } else if errors.oeris() { | ||
| 410 | return Err(Error::Overrun); | ||
| 411 | } else if errors.beris() { | ||
| 412 | return Err(Error::Break); | ||
| 413 | } else if errors.peris() { | ||
| 414 | return Err(Error::Parity); | ||
| 415 | } else if errors.feris() { | ||
| 416 | return Err(Error::Framing); | ||
| 417 | } | ||
| 418 | unreachable!("unrecognized rx error"); | ||
| 312 | } | 419 | } |
| 313 | } | 420 | } |
| 314 | 421 | ||
| @@ -321,7 +428,7 @@ impl<'d, T: Instance> Uart<'d, T, Blocking> { | |||
| 321 | config: Config, | 428 | config: Config, |
| 322 | ) -> Self { | 429 | ) -> Self { |
| 323 | into_ref!(tx, rx); | 430 | into_ref!(tx, rx); |
| 324 | Self::new_inner(uart, tx.map_into(), rx.map_into(), None, None, None, None, config) | 431 | Self::new_inner(uart, tx.map_into(), rx.map_into(), None, None, None, None, None, config) |
| 325 | } | 432 | } |
| 326 | 433 | ||
| 327 | /// Create a new UART with hardware flow control (RTS/CTS) | 434 | /// Create a new UART with hardware flow control (RTS/CTS) |
| @@ -342,6 +449,7 @@ impl<'d, T: Instance> Uart<'d, T, Blocking> { | |||
| 342 | Some(cts.map_into()), | 449 | Some(cts.map_into()), |
| 343 | None, | 450 | None, |
| 344 | None, | 451 | None, |
| 452 | None, | ||
| 345 | config, | 453 | config, |
| 346 | ) | 454 | ) |
| 347 | } | 455 | } |
| @@ -370,17 +478,19 @@ impl<'d, T: Instance> Uart<'d, T, Async> { | |||
| 370 | uart: impl Peripheral<P = T> + 'd, | 478 | uart: impl Peripheral<P = T> + 'd, |
| 371 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, | 479 | tx: impl Peripheral<P = impl TxPin<T>> + 'd, |
| 372 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 480 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 481 | irq: impl Peripheral<P = T::Interrupt> + 'd, | ||
| 373 | tx_dma: impl Peripheral<P = impl Channel> + 'd, | 482 | tx_dma: impl Peripheral<P = impl Channel> + 'd, |
| 374 | rx_dma: impl Peripheral<P = impl Channel> + 'd, | 483 | rx_dma: impl Peripheral<P = impl Channel> + 'd, |
| 375 | config: Config, | 484 | config: Config, |
| 376 | ) -> Self { | 485 | ) -> Self { |
| 377 | into_ref!(tx, rx, tx_dma, rx_dma); | 486 | into_ref!(tx, rx, irq, tx_dma, rx_dma); |
| 378 | Self::new_inner( | 487 | Self::new_inner( |
| 379 | uart, | 488 | uart, |
| 380 | tx.map_into(), | 489 | tx.map_into(), |
| 381 | rx.map_into(), | 490 | rx.map_into(), |
| 382 | None, | 491 | None, |
| 383 | None, | 492 | None, |
| 493 | Some(irq), | ||
| 384 | Some(tx_dma.map_into()), | 494 | Some(tx_dma.map_into()), |
| 385 | Some(rx_dma.map_into()), | 495 | Some(rx_dma.map_into()), |
| 386 | config, | 496 | config, |
| @@ -394,17 +504,19 @@ impl<'d, T: Instance> Uart<'d, T, Async> { | |||
| 394 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, | 504 | rx: impl Peripheral<P = impl RxPin<T>> + 'd, |
| 395 | rts: impl Peripheral<P = impl RtsPin<T>> + 'd, | 505 | rts: impl Peripheral<P = impl RtsPin<T>> + 'd, |
| 396 | cts: impl Peripheral<P = impl CtsPin<T>> + 'd, | 506 | cts: impl Peripheral<P = impl CtsPin<T>> + 'd, |
| 507 | irq: impl Peripheral<P = T::Interrupt> + 'd, | ||
| 397 | tx_dma: impl Peripheral<P = impl Channel> + 'd, | 508 | tx_dma: impl Peripheral<P = impl Channel> + 'd, |
| 398 | rx_dma: impl Peripheral<P = impl Channel> + 'd, | 509 | rx_dma: impl Peripheral<P = impl Channel> + 'd, |
| 399 | config: Config, | 510 | config: Config, |
| 400 | ) -> Self { | 511 | ) -> Self { |
| 401 | into_ref!(tx, rx, cts, rts, tx_dma, rx_dma); | 512 | into_ref!(tx, rx, cts, rts, irq, tx_dma, rx_dma); |
| 402 | Self::new_inner( | 513 | Self::new_inner( |
| 403 | uart, | 514 | uart, |
| 404 | tx.map_into(), | 515 | tx.map_into(), |
| 405 | rx.map_into(), | 516 | rx.map_into(), |
| 406 | Some(rts.map_into()), | 517 | Some(rts.map_into()), |
| 407 | Some(cts.map_into()), | 518 | Some(cts.map_into()), |
| 519 | Some(irq), | ||
| 408 | Some(tx_dma.map_into()), | 520 | Some(tx_dma.map_into()), |
| 409 | Some(rx_dma.map_into()), | 521 | Some(rx_dma.map_into()), |
| 410 | config, | 522 | config, |
| @@ -419,6 +531,7 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { | |||
| 419 | mut rx: PeripheralRef<'d, AnyPin>, | 531 | mut rx: PeripheralRef<'d, AnyPin>, |
| 420 | mut rts: Option<PeripheralRef<'d, AnyPin>>, | 532 | mut rts: Option<PeripheralRef<'d, AnyPin>>, |
| 421 | mut cts: Option<PeripheralRef<'d, AnyPin>>, | 533 | mut cts: Option<PeripheralRef<'d, AnyPin>>, |
| 534 | irq: Option<PeripheralRef<'d, T::Interrupt>>, | ||
| 422 | tx_dma: Option<PeripheralRef<'d, AnyChannel>>, | 535 | tx_dma: Option<PeripheralRef<'d, AnyChannel>>, |
| 423 | rx_dma: Option<PeripheralRef<'d, AnyChannel>>, | 536 | rx_dma: Option<PeripheralRef<'d, AnyChannel>>, |
| 424 | config: Config, | 537 | config: Config, |
| @@ -433,7 +546,7 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { | |||
| 433 | 546 | ||
| 434 | Self { | 547 | Self { |
| 435 | tx: UartTx::new_inner(tx_dma), | 548 | tx: UartTx::new_inner(tx_dma), |
| 436 | rx: UartRx::new_inner(rx_dma), | 549 | rx: UartRx::new_inner(irq, rx_dma), |
| 437 | } | 550 | } |
| 438 | } | 551 | } |
| 439 | 552 | ||
| @@ -761,6 +874,7 @@ mod sealed { | |||
| 761 | pub trait Instance { | 874 | pub trait Instance { |
| 762 | const TX_DREQ: u8; | 875 | const TX_DREQ: u8; |
| 763 | const RX_DREQ: u8; | 876 | const RX_DREQ: u8; |
| 877 | const ID: usize; | ||
| 764 | 878 | ||
| 765 | type Interrupt: crate::interrupt::Interrupt; | 879 | type Interrupt: crate::interrupt::Interrupt; |
| 766 | 880 | ||
| @@ -768,6 +882,8 @@ mod sealed { | |||
| 768 | 882 | ||
| 769 | #[cfg(feature = "nightly")] | 883 | #[cfg(feature = "nightly")] |
| 770 | fn buffered_state() -> &'static buffered::State; | 884 | fn buffered_state() -> &'static buffered::State; |
| 885 | |||
| 886 | fn dma_state() -> &'static DmaState; | ||
| 771 | } | 887 | } |
| 772 | pub trait TxPin<T: Instance> {} | 888 | pub trait TxPin<T: Instance> {} |
| 773 | pub trait RxPin<T: Instance> {} | 889 | pub trait RxPin<T: Instance> {} |
| @@ -793,10 +909,11 @@ impl_mode!(Async); | |||
| 793 | pub trait Instance: sealed::Instance {} | 909 | pub trait Instance: sealed::Instance {} |
| 794 | 910 | ||
| 795 | macro_rules! impl_instance { | 911 | macro_rules! impl_instance { |
| 796 | ($inst:ident, $irq:ident, $tx_dreq:expr, $rx_dreq:expr) => { | 912 | ($inst:ident, $irq:ident, $id:expr, $tx_dreq:expr, $rx_dreq:expr) => { |
| 797 | impl sealed::Instance for peripherals::$inst { | 913 | impl sealed::Instance for peripherals::$inst { |
| 798 | const TX_DREQ: u8 = $tx_dreq; | 914 | const TX_DREQ: u8 = $tx_dreq; |
| 799 | const RX_DREQ: u8 = $rx_dreq; | 915 | const RX_DREQ: u8 = $rx_dreq; |
| 916 | const ID: usize = $id; | ||
| 800 | 917 | ||
| 801 | type Interrupt = crate::interrupt::$irq; | 918 | type Interrupt = crate::interrupt::$irq; |
| 802 | 919 | ||
| @@ -809,13 +926,21 @@ macro_rules! impl_instance { | |||
| 809 | static STATE: buffered::State = buffered::State::new(); | 926 | static STATE: buffered::State = buffered::State::new(); |
| 810 | &STATE | 927 | &STATE |
| 811 | } | 928 | } |
| 929 | |||
| 930 | fn dma_state() -> &'static DmaState { | ||
| 931 | static STATE: DmaState = DmaState { | ||
| 932 | rx_err_waker: AtomicWaker::new(), | ||
| 933 | rx_errs: AtomicU16::new(0), | ||
| 934 | }; | ||
| 935 | &STATE | ||
| 936 | } | ||
| 812 | } | 937 | } |
| 813 | impl Instance for peripherals::$inst {} | 938 | impl Instance for peripherals::$inst {} |
| 814 | }; | 939 | }; |
| 815 | } | 940 | } |
| 816 | 941 | ||
| 817 | impl_instance!(UART0, UART0_IRQ, 20, 21); | 942 | impl_instance!(UART0, UART0_IRQ, 0, 20, 21); |
| 818 | impl_instance!(UART1, UART1_IRQ, 22, 23); | 943 | impl_instance!(UART1, UART1_IRQ, 1, 22, 23); |
| 819 | 944 | ||
| 820 | pub trait TxPin<T: Instance>: sealed::TxPin<T> + crate::gpio::Pin {} | 945 | pub trait TxPin<T: Instance>: sealed::TxPin<T> + crate::gpio::Pin {} |
| 821 | pub trait RxPin<T: Instance>: sealed::RxPin<T> + crate::gpio::Pin {} | 946 | pub trait RxPin<T: Instance>: sealed::RxPin<T> + crate::gpio::Pin {} |
diff --git a/examples/rp/src/bin/uart_unidir.rs b/examples/rp/src/bin/uart_unidir.rs index f56e7009f..4119a309f 100644 --- a/examples/rp/src/bin/uart_unidir.rs +++ b/examples/rp/src/bin/uart_unidir.rs | |||
| @@ -7,6 +7,7 @@ | |||
| 7 | 7 | ||
| 8 | use defmt::*; | 8 | use defmt::*; |
| 9 | use embassy_executor::Spawner; | 9 | use embassy_executor::Spawner; |
| 10 | use embassy_rp::interrupt; | ||
| 10 | use embassy_rp::peripherals::UART1; | 11 | use embassy_rp::peripherals::UART1; |
| 11 | use embassy_rp::uart::{Async, Config, UartRx, UartTx}; | 12 | use embassy_rp::uart::{Async, Config, UartRx, UartTx}; |
| 12 | use embassy_time::{Duration, Timer}; | 13 | use embassy_time::{Duration, Timer}; |
| @@ -17,7 +18,13 @@ async fn main(spawner: Spawner) { | |||
| 17 | let p = embassy_rp::init(Default::default()); | 18 | let p = embassy_rp::init(Default::default()); |
| 18 | 19 | ||
| 19 | let mut uart_tx = UartTx::new(p.UART0, p.PIN_0, p.DMA_CH0, Config::default()); | 20 | let mut uart_tx = UartTx::new(p.UART0, p.PIN_0, p.DMA_CH0, Config::default()); |
| 20 | let uart_rx = UartRx::new(p.UART1, p.PIN_5, p.DMA_CH1, Config::default()); | 21 | let uart_rx = UartRx::new( |
| 22 | p.UART1, | ||
| 23 | p.PIN_5, | ||
| 24 | interrupt::take!(UART1_IRQ), | ||
| 25 | p.DMA_CH1, | ||
| 26 | Config::default(), | ||
| 27 | ); | ||
| 21 | 28 | ||
| 22 | unwrap!(spawner.spawn(reader(uart_rx))); | 29 | unwrap!(spawner.spawn(reader(uart_rx))); |
| 23 | 30 | ||
diff --git a/tests/rp/src/bin/uart_dma.rs b/tests/rp/src/bin/uart_dma.rs index 963c22707..92aa205c9 100644 --- a/tests/rp/src/bin/uart_dma.rs +++ b/tests/rp/src/bin/uart_dma.rs | |||
| @@ -4,28 +4,246 @@ | |||
| 4 | 4 | ||
| 5 | use defmt::{assert_eq, *}; | 5 | use defmt::{assert_eq, *}; |
| 6 | use embassy_executor::Spawner; | 6 | use embassy_executor::Spawner; |
| 7 | use embassy_rp::uart::{Config, Uart}; | 7 | use embassy_rp::gpio::{Level, Output}; |
| 8 | use embassy_rp::interrupt; | ||
| 9 | use embassy_rp::uart::{Async, Config, Error, Instance, Parity, Uart, UartRx}; | ||
| 10 | use embassy_time::{Duration, Timer}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | 11 | use {defmt_rtt as _, panic_probe as _}; |
| 9 | 12 | ||
| 13 | async fn read<const N: usize>(uart: &mut Uart<'_, impl Instance, Async>) -> Result<[u8; N], Error> { | ||
| 14 | let mut buf = [255; N]; | ||
| 15 | uart.read(&mut buf).await?; | ||
| 16 | Ok(buf) | ||
| 17 | } | ||
| 18 | |||
| 19 | async fn read1<const N: usize>(uart: &mut UartRx<'_, impl Instance, Async>) -> Result<[u8; N], Error> { | ||
| 20 | let mut buf = [255; N]; | ||
| 21 | uart.read(&mut buf).await?; | ||
| 22 | Ok(buf) | ||
| 23 | } | ||
| 24 | |||
| 25 | async fn send(pin: &mut Output<'_, impl embassy_rp::gpio::Pin>, v: u8, parity: Option<bool>) { | ||
| 26 | pin.set_low(); | ||
| 27 | Timer::after(Duration::from_millis(1)).await; | ||
| 28 | for i in 0..8 { | ||
| 29 | if v & (1 << i) == 0 { | ||
| 30 | pin.set_low(); | ||
| 31 | } else { | ||
| 32 | pin.set_high(); | ||
| 33 | } | ||
| 34 | Timer::after(Duration::from_millis(1)).await; | ||
| 35 | } | ||
| 36 | if let Some(b) = parity { | ||
| 37 | if b { | ||
| 38 | pin.set_high(); | ||
| 39 | } else { | ||
| 40 | pin.set_low(); | ||
| 41 | } | ||
| 42 | Timer::after(Duration::from_millis(1)).await; | ||
| 43 | } | ||
| 44 | pin.set_high(); | ||
| 45 | Timer::after(Duration::from_millis(1)).await; | ||
| 46 | } | ||
| 47 | |||
| 10 | #[embassy_executor::main] | 48 | #[embassy_executor::main] |
| 11 | async fn main(_spawner: Spawner) { | 49 | async fn main(_spawner: Spawner) { |
| 12 | let p = embassy_rp::init(Default::default()); | 50 | let mut p = embassy_rp::init(Default::default()); |
| 13 | info!("Hello World!"); | 51 | info!("Hello World!"); |
| 14 | 52 | ||
| 15 | let (tx, rx, uart) = (p.PIN_0, p.PIN_1, p.UART0); | 53 | let (mut tx, mut rx, mut uart) = (p.PIN_0, p.PIN_1, p.UART0); |
| 54 | let mut irq = interrupt::take!(UART0_IRQ); | ||
| 16 | 55 | ||
| 17 | let config = Config::default(); | 56 | // TODO |
| 18 | let mut uart = Uart::new(uart, tx, rx, p.DMA_CH0, p.DMA_CH1, config); | 57 | // nuclear error reporting. just abort the entire transfer and invalidate the |
| 58 | // dma buffer, buffered buffer, fifo etc. | ||
| 19 | 59 | ||
| 20 | // We can't send too many bytes, they have to fit in the FIFO. | 60 | // We can't send too many bytes, they have to fit in the FIFO. |
| 21 | // This is because we aren't sending+receiving at the same time. | 61 | // This is because we aren't sending+receiving at the same time. |
| 62 | { | ||
| 63 | let config = Config::default(); | ||
| 64 | let mut uart = Uart::new( | ||
| 65 | &mut uart, | ||
| 66 | &mut tx, | ||
| 67 | &mut rx, | ||
| 68 | &mut irq, | ||
| 69 | &mut p.DMA_CH0, | ||
| 70 | &mut p.DMA_CH1, | ||
| 71 | config, | ||
| 72 | ); | ||
| 73 | |||
| 74 | let data = [0xC0, 0xDE]; | ||
| 75 | uart.write(&data).await.unwrap(); | ||
| 76 | |||
| 77 | let mut buf = [0; 2]; | ||
| 78 | uart.read(&mut buf).await.unwrap(); | ||
| 79 | assert_eq!(buf, data); | ||
| 80 | } | ||
| 81 | |||
| 82 | info!("test overflow detection"); | ||
| 83 | { | ||
| 84 | let config = Config::default(); | ||
| 85 | let mut uart = Uart::new( | ||
| 86 | &mut uart, | ||
| 87 | &mut tx, | ||
| 88 | &mut rx, | ||
| 89 | &mut irq, | ||
| 90 | &mut p.DMA_CH0, | ||
| 91 | &mut p.DMA_CH1, | ||
| 92 | config, | ||
| 93 | ); | ||
| 94 | |||
| 95 | uart.blocking_write(&[42; 32]).unwrap(); | ||
| 96 | uart.blocking_write(&[1, 2, 3]).unwrap(); | ||
| 97 | uart.blocking_flush().unwrap(); | ||
| 98 | |||
| 99 | // can receive regular fifo contents | ||
| 100 | assert_eq!(read(&mut uart).await, Ok([42; 16])); | ||
| 101 | assert_eq!(read(&mut uart).await, Ok([42; 16])); | ||
| 102 | // receiving the rest fails with overrun | ||
| 103 | assert_eq!(read::<16>(&mut uart).await, Err(Error::Overrun)); | ||
| 104 | // new data is accepted, latest overrunning byte first | ||
| 105 | assert_eq!(read(&mut uart).await, Ok([3])); | ||
| 106 | uart.blocking_write(&[8, 9]).unwrap(); | ||
| 107 | Timer::after(Duration::from_millis(1)).await; | ||
| 108 | assert_eq!(read(&mut uart).await, Ok([8, 9])); | ||
| 109 | } | ||
| 110 | |||
| 111 | info!("test break detection"); | ||
| 112 | { | ||
| 113 | let config = Config::default(); | ||
| 114 | let (mut tx, mut rx) = Uart::new( | ||
| 115 | &mut uart, | ||
| 116 | &mut tx, | ||
| 117 | &mut rx, | ||
| 118 | &mut irq, | ||
| 119 | &mut p.DMA_CH0, | ||
| 120 | &mut p.DMA_CH1, | ||
| 121 | config, | ||
| 122 | ) | ||
| 123 | .split(); | ||
| 124 | |||
| 125 | // break before read | ||
| 126 | tx.send_break(20).await; | ||
| 127 | tx.write(&[64]).await.unwrap(); | ||
| 128 | assert_eq!(read1::<1>(&mut rx).await.unwrap_err(), Error::Break); | ||
| 129 | assert_eq!(read1(&mut rx).await.unwrap(), [64]); | ||
| 130 | |||
| 131 | // break during read | ||
| 132 | { | ||
| 133 | let r = read1::<2>(&mut rx); | ||
| 134 | tx.write(&[2]).await.unwrap(); | ||
| 135 | tx.send_break(20).await; | ||
| 136 | tx.write(&[3]).await.unwrap(); | ||
| 137 | assert_eq!(r.await.unwrap_err(), Error::Break); | ||
| 138 | assert_eq!(read1(&mut rx).await.unwrap(), [3]); | ||
| 139 | } | ||
| 140 | |||
| 141 | // break after read | ||
| 142 | { | ||
| 143 | let r = read1(&mut rx); | ||
| 144 | tx.write(&[2]).await.unwrap(); | ||
| 145 | tx.send_break(20).await; | ||
| 146 | tx.write(&[3]).await.unwrap(); | ||
| 147 | assert_eq!(r.await.unwrap(), [2]); | ||
| 148 | assert_eq!(read1::<1>(&mut rx).await.unwrap_err(), Error::Break); | ||
| 149 | assert_eq!(read1(&mut rx).await.unwrap(), [3]); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | // parity detection. here we bitbang to not require two uarts. | ||
| 154 | info!("test parity error detection"); | ||
| 155 | { | ||
| 156 | let mut pin = Output::new(&mut tx, Level::High); | ||
| 157 | // choose a very slow baud rate to make tests reliable even with O0 | ||
| 158 | let mut config = Config::default(); | ||
| 159 | config.baudrate = 1000; | ||
| 160 | config.parity = Parity::ParityEven; | ||
| 161 | let mut uart = UartRx::new(&mut uart, &mut rx, &mut irq, &mut p.DMA_CH0, config); | ||
| 162 | |||
| 163 | async fn chr(pin: &mut Output<'_, impl embassy_rp::gpio::Pin>, v: u8, parity: u32) { | ||
| 164 | send(pin, v, Some(parity != 0)).await; | ||
| 165 | } | ||
| 166 | |||
| 167 | // first check that we can send correctly | ||
| 168 | chr(&mut pin, 32, 1).await; | ||
| 169 | assert_eq!(read1(&mut uart).await.unwrap(), [32]); | ||
| 170 | |||
| 171 | // parity error before read | ||
| 172 | chr(&mut pin, 32, 0).await; | ||
| 173 | chr(&mut pin, 31, 1).await; | ||
| 174 | assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Parity); | ||
| 175 | assert_eq!(read1(&mut uart).await.unwrap(), [31]); | ||
| 176 | |||
| 177 | // parity error during read | ||
| 178 | { | ||
| 179 | let r = read1::<2>(&mut uart); | ||
| 180 | chr(&mut pin, 2, 1).await; | ||
| 181 | chr(&mut pin, 32, 0).await; | ||
| 182 | chr(&mut pin, 3, 0).await; | ||
| 183 | assert_eq!(r.await.unwrap_err(), Error::Parity); | ||
| 184 | assert_eq!(read1(&mut uart).await.unwrap(), [3]); | ||
| 185 | } | ||
| 186 | |||
| 187 | // parity error after read | ||
| 188 | { | ||
| 189 | let r = read1(&mut uart); | ||
| 190 | chr(&mut pin, 2, 1).await; | ||
| 191 | chr(&mut pin, 32, 0).await; | ||
| 192 | chr(&mut pin, 3, 0).await; | ||
| 193 | assert_eq!(r.await.unwrap(), [2]); | ||
| 194 | assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Parity); | ||
| 195 | assert_eq!(read1(&mut uart).await.unwrap(), [3]); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | // framing error detection. here we bitbang because there's no other way. | ||
| 200 | info!("test framing error detection"); | ||
| 201 | { | ||
| 202 | let mut pin = Output::new(&mut tx, Level::High); | ||
| 203 | // choose a very slow baud rate to make tests reliable even with O0 | ||
| 204 | let mut config = Config::default(); | ||
| 205 | config.baudrate = 1000; | ||
| 206 | let mut uart = UartRx::new(&mut uart, &mut rx, &mut irq, &mut p.DMA_CH0, config); | ||
| 207 | |||
| 208 | async fn chr(pin: &mut Output<'_, impl embassy_rp::gpio::Pin>, v: u8, good: bool) { | ||
| 209 | if good { | ||
| 210 | send(pin, v, None).await; | ||
| 211 | } else { | ||
| 212 | send(pin, v, Some(false)).await; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | // first check that we can send correctly | ||
| 217 | chr(&mut pin, 32, true).await; | ||
| 218 | assert_eq!(read1(&mut uart).await.unwrap(), [32]); | ||
| 219 | |||
| 220 | // parity error before read | ||
| 221 | chr(&mut pin, 32, false).await; | ||
| 222 | chr(&mut pin, 31, true).await; | ||
| 223 | assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Framing); | ||
| 224 | assert_eq!(read1(&mut uart).await.unwrap(), [31]); | ||
| 22 | 225 | ||
| 23 | let data = [0xC0, 0xDE]; | 226 | // parity error during read |
| 24 | uart.write(&data).await.unwrap(); | 227 | { |
| 228 | let r = read1::<2>(&mut uart); | ||
| 229 | chr(&mut pin, 2, true).await; | ||
| 230 | chr(&mut pin, 32, false).await; | ||
| 231 | chr(&mut pin, 3, true).await; | ||
| 232 | assert_eq!(r.await.unwrap_err(), Error::Framing); | ||
| 233 | assert_eq!(read1(&mut uart).await.unwrap(), [3]); | ||
| 234 | } | ||
| 25 | 235 | ||
| 26 | let mut buf = [0; 2]; | 236 | // parity error after read |
| 27 | uart.read(&mut buf).await.unwrap(); | 237 | { |
| 28 | assert_eq!(buf, data); | 238 | let r = read1(&mut uart); |
| 239 | chr(&mut pin, 2, true).await; | ||
| 240 | chr(&mut pin, 32, false).await; | ||
| 241 | chr(&mut pin, 3, true).await; | ||
| 242 | assert_eq!(r.await.unwrap(), [2]); | ||
| 243 | assert_eq!(read1::<1>(&mut uart).await.unwrap_err(), Error::Framing); | ||
| 244 | assert_eq!(read1(&mut uart).await.unwrap(), [3]); | ||
| 245 | } | ||
| 246 | } | ||
| 29 | 247 | ||
| 30 | info!("Test OK"); | 248 | info!("Test OK"); |
| 31 | cortex_m::asm::bkpt(); | 249 | cortex_m::asm::bkpt(); |
