From 672547b6cab049b1d2d6ceeec67f4ddbf4518e23 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 | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rp2040-hal/src/pio.rs b/rp2040-hal/src/pio.rs index 061faa6b8..c5b27eb2c 100644 --- a/rp2040-hal/src/pio.rs +++ b/rp2040-hal/src/pio.rs @@ -628,6 +628,24 @@ 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: