diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-11-13 23:26:19 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-13 23:26:19 +0000 |
| commit | 872f1ec4c2bae370e10366b72e601f64b0008da3 (patch) | |
| tree | aee7bb0e82cba313719255f57f185a15183c9368 | |
| parent | ea99671729be91b63156097b01128c3ea6f74a75 (diff) | |
| parent | 6ccd8c051e657a233fcdc9040ad0b6aaa2b357e5 (diff) | |
Merge pull request #2183 from embassy-rs/buffereduarte-fix
nrf/buffered_uarte: fix missing hwfc enable.
| -rw-r--r-- | embassy-nrf/src/buffered_uarte.rs | 83 | ||||
| -rw-r--r-- | tests/nrf/Cargo.toml | 2 | ||||
| -rw-r--r-- | tests/nrf/src/bin/buffered_uart_full.rs | 72 |
3 files changed, 134 insertions, 23 deletions
diff --git a/embassy-nrf/src/buffered_uarte.rs b/embassy-nrf/src/buffered_uarte.rs index 10b8b0fbe..e4b556f06 100644 --- a/embassy-nrf/src/buffered_uarte.rs +++ b/embassy-nrf/src/buffered_uarte.rs | |||
| @@ -12,7 +12,7 @@ use core::cmp::min; | |||
| 12 | use core::future::poll_fn; | 12 | use core::future::poll_fn; |
| 13 | use core::marker::PhantomData; | 13 | use core::marker::PhantomData; |
| 14 | use core::slice; | 14 | use core::slice; |
| 15 | use core::sync::atomic::{compiler_fence, AtomicU8, AtomicUsize, Ordering}; | 15 | use core::sync::atomic::{compiler_fence, AtomicBool, AtomicU8, AtomicUsize, Ordering}; |
| 16 | use core::task::Poll; | 16 | use core::task::Poll; |
| 17 | 17 | ||
| 18 | use embassy_hal_internal::atomic_ring_buffer::RingBuffer; | 18 | use embassy_hal_internal::atomic_ring_buffer::RingBuffer; |
| @@ -41,7 +41,9 @@ mod sealed { | |||
| 41 | 41 | ||
| 42 | pub rx_waker: AtomicWaker, | 42 | pub rx_waker: AtomicWaker, |
| 43 | pub rx_buf: RingBuffer, | 43 | pub rx_buf: RingBuffer, |
| 44 | pub rx_bufs: AtomicU8, | 44 | pub rx_started: AtomicBool, |
| 45 | pub rx_started_count: AtomicU8, | ||
| 46 | pub rx_ended_count: AtomicU8, | ||
| 45 | pub rx_ppi_ch: AtomicU8, | 47 | pub rx_ppi_ch: AtomicU8, |
| 46 | } | 48 | } |
| 47 | } | 49 | } |
| @@ -65,7 +67,9 @@ impl State { | |||
| 65 | 67 | ||
| 66 | rx_waker: AtomicWaker::new(), | 68 | rx_waker: AtomicWaker::new(), |
| 67 | rx_buf: RingBuffer::new(), | 69 | rx_buf: RingBuffer::new(), |
| 68 | rx_bufs: AtomicU8::new(0), | 70 | rx_started: AtomicBool::new(false), |
| 71 | rx_started_count: AtomicU8::new(0), | ||
| 72 | rx_ended_count: AtomicU8::new(0), | ||
| 69 | rx_ppi_ch: AtomicU8::new(0), | 73 | rx_ppi_ch: AtomicU8::new(0), |
| 70 | } | 74 | } |
| 71 | } | 75 | } |
| @@ -104,28 +108,20 @@ impl<U: UarteInstance> interrupt::typelevel::Handler<U::Interrupt> for Interrupt | |||
| 104 | s.rx_waker.wake(); | 108 | s.rx_waker.wake(); |
| 105 | } | 109 | } |
| 106 | 110 | ||
| 107 | // If not RXing, start. | 111 | if r.events_endrx.read().bits() != 0 { |
| 108 | if s.rx_bufs.load(Ordering::Relaxed) == 0 { | 112 | //trace!(" irq_rx: endrx"); |
| 109 | let (ptr, len) = rx.push_buf(); | 113 | r.events_endrx.reset(); |
| 110 | if len >= half_len { | ||
| 111 | //trace!(" irq_rx: starting {:?}", half_len); | ||
| 112 | s.rx_bufs.store(1, Ordering::Relaxed); | ||
| 113 | |||
| 114 | // Set up the DMA read | ||
| 115 | r.rxd.ptr.write(|w| unsafe { w.ptr().bits(ptr as u32) }); | ||
| 116 | r.rxd.maxcnt.write(|w| unsafe { w.maxcnt().bits(half_len as _) }); | ||
| 117 | 114 | ||
| 118 | // Start UARTE Receive transaction | 115 | let val = s.rx_ended_count.load(Ordering::Relaxed); |
| 119 | r.tasks_startrx.write(|w| unsafe { w.bits(1) }); | 116 | s.rx_ended_count.store(val.wrapping_add(1), Ordering::Relaxed); |
| 120 | rx.push_done(half_len); | ||
| 121 | r.intenset.write(|w| w.rxstarted().set()); | ||
| 122 | } | ||
| 123 | } | 117 | } |
| 124 | 118 | ||
| 125 | if r.events_rxstarted.read().bits() != 0 { | 119 | if r.events_rxstarted.read().bits() != 0 || !s.rx_started.load(Ordering::Relaxed) { |
| 126 | //trace!(" irq_rx: rxstarted"); | 120 | //trace!(" irq_rx: rxstarted"); |
| 127 | let (ptr, len) = rx.push_buf(); | 121 | let (ptr, len) = rx.push_buf(); |
| 128 | if len >= half_len { | 122 | if len >= half_len { |
| 123 | r.events_rxstarted.reset(); | ||
| 124 | |||
| 129 | //trace!(" irq_rx: starting second {:?}", half_len); | 125 | //trace!(" irq_rx: starting second {:?}", half_len); |
| 130 | 126 | ||
| 131 | // Set up the DMA read | 127 | // Set up the DMA read |
| @@ -134,11 +130,50 @@ impl<U: UarteInstance> interrupt::typelevel::Handler<U::Interrupt> for Interrupt | |||
| 134 | 130 | ||
| 135 | let chn = s.rx_ppi_ch.load(Ordering::Relaxed); | 131 | let chn = s.rx_ppi_ch.load(Ordering::Relaxed); |
| 136 | 132 | ||
| 133 | // Enable endrx -> startrx PPI channel. | ||
| 134 | // From this point on, if endrx happens, startrx is automatically fired. | ||
| 137 | ppi::regs().chenset.write(|w| unsafe { w.bits(1 << chn) }); | 135 | ppi::regs().chenset.write(|w| unsafe { w.bits(1 << chn) }); |
| 138 | 136 | ||
| 137 | // It is possible that endrx happened BEFORE enabling the PPI. In this case | ||
| 138 | // the PPI channel doesn't trigger, and we'd hang. We have to detect this | ||
| 139 | // and manually start. | ||
| 140 | |||
| 141 | // check again in case endrx has happened between the last check and now. | ||
| 142 | if r.events_endrx.read().bits() != 0 { | ||
| 143 | //trace!(" irq_rx: endrx"); | ||
| 144 | r.events_endrx.reset(); | ||
| 145 | |||
| 146 | let val = s.rx_ended_count.load(Ordering::Relaxed); | ||
| 147 | s.rx_ended_count.store(val.wrapping_add(1), Ordering::Relaxed); | ||
| 148 | } | ||
| 149 | |||
| 150 | let rx_ended = s.rx_ended_count.load(Ordering::Relaxed); | ||
| 151 | let rx_started = s.rx_started_count.load(Ordering::Relaxed); | ||
| 152 | |||
| 153 | // If we started the same amount of transfers as ended, the last rxend has | ||
| 154 | // already occured. | ||
| 155 | let rxend_happened = rx_started == rx_ended; | ||
| 156 | |||
| 157 | // Check if the PPI channel is still enabled. The PPI channel disables itself | ||
| 158 | // when it fires, so if it's still enabled it hasn't fired. | ||
| 159 | let ppi_ch_enabled = ppi::regs().chen.read().bits() & (1 << chn) != 0; | ||
| 160 | |||
| 161 | // if rxend happened, and the ppi channel hasn't fired yet, the rxend got missed. | ||
| 162 | // this condition also naturally matches if `!started`, needed to kickstart the DMA. | ||
| 163 | if rxend_happened && ppi_ch_enabled { | ||
| 164 | //trace!("manually starting."); | ||
| 165 | |||
| 166 | // disable the ppi ch, it's of no use anymore. | ||
| 167 | ppi::regs().chenclr.write(|w| unsafe { w.bits(1 << chn) }); | ||
| 168 | |||
| 169 | // manually start | ||
| 170 | r.tasks_startrx.write(|w| unsafe { w.bits(1) }); | ||
| 171 | } | ||
| 172 | |||
| 139 | rx.push_done(half_len); | 173 | rx.push_done(half_len); |
| 140 | 174 | ||
| 141 | r.events_rxstarted.reset(); | 175 | s.rx_started_count.store(rx_started.wrapping_add(1), Ordering::Relaxed); |
| 176 | s.rx_started.store(true, Ordering::Relaxed); | ||
| 142 | } else { | 177 | } else { |
| 143 | //trace!(" irq_rx: rxstarted no buf"); | 178 | //trace!(" irq_rx: rxstarted no buf"); |
| 144 | r.intenclr.write(|w| w.rxstarted().clear()); | 179 | r.intenclr.write(|w| w.rxstarted().clear()); |
| @@ -282,6 +317,8 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 282 | 317 | ||
| 283 | let r = U::regs(); | 318 | let r = U::regs(); |
| 284 | 319 | ||
| 320 | let hwfc = cts.is_some(); | ||
| 321 | |||
| 285 | rxd.conf().write(|w| w.input().connect().drive().h0h1()); | 322 | rxd.conf().write(|w| w.input().connect().drive().h0h1()); |
| 286 | r.psel.rxd.write(|w| unsafe { w.bits(rxd.psel_bits()) }); | 323 | r.psel.rxd.write(|w| unsafe { w.bits(rxd.psel_bits()) }); |
| 287 | 324 | ||
| @@ -303,7 +340,8 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 303 | // Initialize state | 340 | // Initialize state |
| 304 | let s = U::buffered_state(); | 341 | let s = U::buffered_state(); |
| 305 | s.tx_count.store(0, Ordering::Relaxed); | 342 | s.tx_count.store(0, Ordering::Relaxed); |
| 306 | s.rx_bufs.store(0, Ordering::Relaxed); | 343 | s.rx_started_count.store(0, Ordering::Relaxed); |
| 344 | s.rx_ended_count.store(0, Ordering::Relaxed); | ||
| 307 | let len = tx_buffer.len(); | 345 | let len = tx_buffer.len(); |
| 308 | unsafe { s.tx_buf.init(tx_buffer.as_mut_ptr(), len) }; | 346 | unsafe { s.tx_buf.init(tx_buffer.as_mut_ptr(), len) }; |
| 309 | let len = rx_buffer.len(); | 347 | let len = rx_buffer.len(); |
| @@ -311,7 +349,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 311 | 349 | ||
| 312 | // Configure | 350 | // Configure |
| 313 | r.config.write(|w| { | 351 | r.config.write(|w| { |
| 314 | w.hwfc().bit(false); | 352 | w.hwfc().bit(hwfc); |
| 315 | w.parity().variant(config.parity); | 353 | w.parity().variant(config.parity); |
| 316 | w | 354 | w |
| 317 | }); | 355 | }); |
| @@ -333,6 +371,7 @@ impl<'d, U: UarteInstance, T: TimerInstance> BufferedUarte<'d, U, T> { | |||
| 333 | w.endtx().set(); | 371 | w.endtx().set(); |
| 334 | w.rxstarted().set(); | 372 | w.rxstarted().set(); |
| 335 | w.error().set(); | 373 | w.error().set(); |
| 374 | w.endrx().set(); | ||
| 336 | w | 375 | w |
| 337 | }); | 376 | }); |
| 338 | 377 | ||
diff --git a/tests/nrf/Cargo.toml b/tests/nrf/Cargo.toml index f7a104090..a3393f7e9 100644 --- a/tests/nrf/Cargo.toml +++ b/tests/nrf/Cargo.toml | |||
| @@ -12,7 +12,7 @@ embassy-sync = { version = "0.4.0", path = "../../embassy-sync", features = ["de | |||
| 12 | embassy-executor = { version = "0.3.1", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "nightly", "integrated-timers"] } | 12 | embassy-executor = { version = "0.3.1", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "nightly", "integrated-timers"] } |
| 13 | embassy-time = { version = "0.1.5", path = "../../embassy-time", features = ["defmt", "nightly", "unstable-traits", "defmt-timestamp-uptime"] } | 13 | embassy-time = { version = "0.1.5", path = "../../embassy-time", features = ["defmt", "nightly", "unstable-traits", "defmt-timestamp-uptime"] } |
| 14 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nightly", "unstable-traits", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } | 14 | embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nightly", "unstable-traits", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } |
| 15 | embedded-io-async = { version = "0.6.0" } | 15 | embedded-io-async = { version = "0.6.0", features = ["defmt-03"] } |
| 16 | embassy-net = { version = "0.2.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "nightly"] } | 16 | embassy-net = { version = "0.2.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "nightly"] } |
| 17 | embassy-net-esp-hosted = { version = "0.1.0", path = "../../embassy-net-esp-hosted", features = ["defmt"] } | 17 | embassy-net-esp-hosted = { version = "0.1.0", path = "../../embassy-net-esp-hosted", features = ["defmt"] } |
| 18 | embassy-net-enc28j60 = { version = "0.1.0", path = "../../embassy-net-enc28j60", features = ["defmt"] } | 18 | embassy-net-enc28j60 = { version = "0.1.0", path = "../../embassy-net-enc28j60", features = ["defmt"] } |
diff --git a/tests/nrf/src/bin/buffered_uart_full.rs b/tests/nrf/src/bin/buffered_uart_full.rs new file mode 100644 index 000000000..2db8f88d3 --- /dev/null +++ b/tests/nrf/src/bin/buffered_uart_full.rs | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | teleprobe_meta::target!(b"nrf52840-dk"); | ||
| 5 | |||
| 6 | use defmt::{assert_eq, *}; | ||
| 7 | use embassy_executor::Spawner; | ||
| 8 | use embassy_nrf::buffered_uarte::{self, BufferedUarte}; | ||
| 9 | use embassy_nrf::{bind_interrupts, peripherals, uarte}; | ||
| 10 | use embedded_io_async::{Read, Write}; | ||
| 11 | use {defmt_rtt as _, panic_probe as _}; | ||
| 12 | |||
| 13 | bind_interrupts!(struct Irqs { | ||
| 14 | UARTE0_UART0 => buffered_uarte::InterruptHandler<peripherals::UARTE0>; | ||
| 15 | }); | ||
| 16 | |||
| 17 | #[embassy_executor::main] | ||
| 18 | async fn main(_spawner: Spawner) { | ||
| 19 | let p = embassy_nrf::init(Default::default()); | ||
| 20 | let mut config = uarte::Config::default(); | ||
| 21 | config.parity = uarte::Parity::EXCLUDED; | ||
| 22 | config.baudrate = uarte::Baudrate::BAUD1M; | ||
| 23 | |||
| 24 | let mut tx_buffer = [0u8; 1024]; | ||
| 25 | let mut rx_buffer = [0u8; 1024]; | ||
| 26 | |||
| 27 | let mut u = BufferedUarte::new( | ||
| 28 | p.UARTE0, | ||
| 29 | p.TIMER0, | ||
| 30 | p.PPI_CH0, | ||
| 31 | p.PPI_CH1, | ||
| 32 | p.PPI_GROUP0, | ||
| 33 | Irqs, | ||
| 34 | p.P1_03, | ||
| 35 | p.P1_02, | ||
| 36 | config.clone(), | ||
| 37 | &mut rx_buffer, | ||
| 38 | &mut tx_buffer, | ||
| 39 | ); | ||
| 40 | |||
| 41 | info!("uarte initialized!"); | ||
| 42 | |||
| 43 | let (mut rx, mut tx) = u.split(); | ||
| 44 | |||
| 45 | let mut buf = [0; 1024]; | ||
| 46 | for (j, b) in buf.iter_mut().enumerate() { | ||
| 47 | *b = j as u8; | ||
| 48 | } | ||
| 49 | |||
| 50 | // Write 1024b. This causes the rx buffer to get exactly full. | ||
| 51 | unwrap!(tx.write_all(&buf).await); | ||
| 52 | unwrap!(tx.flush().await); | ||
| 53 | |||
| 54 | // Read those 1024b. | ||
| 55 | unwrap!(rx.read_exact(&mut buf).await); | ||
| 56 | for (j, b) in buf.iter().enumerate() { | ||
| 57 | assert_eq!(*b, j as u8); | ||
| 58 | } | ||
| 59 | |||
| 60 | // The buffer should now be unclogged. Write 1024b again. | ||
| 61 | unwrap!(tx.write_all(&buf).await); | ||
| 62 | unwrap!(tx.flush().await); | ||
| 63 | |||
| 64 | // Read should work again. | ||
| 65 | unwrap!(rx.read_exact(&mut buf).await); | ||
| 66 | for (j, b) in buf.iter().enumerate() { | ||
| 67 | assert_eq!(*b, j as u8); | ||
| 68 | } | ||
| 69 | |||
| 70 | info!("Test OK"); | ||
| 71 | cortex_m::asm::bkpt(); | ||
| 72 | } | ||
