From ff3c9a630295f010d10eb62d0d989c77fe20c3e6 Mon Sep 17 00:00:00 2001 From: Thad House Date: Tue, 15 Aug 2023 22:48:24 -0700 Subject: [PATCH] Add function to clear PIO fifos The C SDK has pio_sm_clear_fifos, which clears the FIFOs without disrupting the internal state machine state. This function is needed for porting one of my projects from C to Rust. --- rp2040-hal/src/pio.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rp2040-hal/src/pio.rs b/rp2040-hal/src/pio.rs index 061faa6b8..bca32d104 100644 --- a/rp2040-hal/src/pio.rs +++ b/rp2040-hal/src/pio.rs @@ -628,6 +628,22 @@ impl StateMachine { unsafe { self.sm.sm().sm_execctrl.read().exec_stalled().bit() } } + /// Clear both TX and RX FIFOs + pub fn clear_fifos(&mut self) { + // Safety: all accesses to these registers are controlled by this instance + unsafe { + let sm = &self.sm.sm(); + let sm_shiftctrl = &sm.sm_shiftctrl; + let mut current = false; + // Toggling the FIFO join state clears the fifo + sm_shiftctrl.modify(|r, w| { + current = r.fjoin_rx().bit(); + w.fjoin_rx().bit(!current) + }); + sm_shiftctrl.modify(|_, w| w.fjoin_rx().bit(current)); + } + } + /// Drain Tx fifo. pub fn drain_tx_fifo(&mut self) { // According to the datasheet 3.5.4.2 Page 358: