Skip to content

Commit

Permalink
Add function to clear PIO fifos
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ThadHouse committed Aug 16, 2023
1 parent 76efb36 commit 672547b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions rp2040-hal/src/pio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,24 @@ impl<SM: ValidStateMachine, State> StateMachine<SM, State> {
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:
Expand Down

0 comments on commit 672547b

Please sign in to comment.