From b7e56d0869de0136625eff9885d6ebe809fbe9ec Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Sat, 26 Feb 2022 10:30:28 +0100 Subject: [PATCH] Added so stopped PIO state machines can change their clock divider --- rp2040-hal/CHANGELOG.md | 2 +- rp2040-hal/src/pio.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/rp2040-hal/CHANGELOG.md b/rp2040-hal/CHANGELOG.md index 429a110..7126b06 100644 --- a/rp2040-hal/CHANGELOG.md +++ b/rp2040-hal/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- None +- Stopped PIO state machines can change their clock divider ### Changed diff --git a/rp2040-hal/src/pio.rs b/rp2040-hal/src/pio.rs index 360926d..7847764 100644 --- a/rp2040-hal/src/pio.rs +++ b/rp2040-hal/src/pio.rs @@ -559,6 +559,22 @@ impl StateMachine { } } + /// Change the clock divider of a stopped state machine. + pub fn set_clock_divisor(&mut self, divisor: f32) { + // sm frequency = clock freq / (CLKDIV_INT + CLKDIV_FRAC / 256) + let int = divisor as u16; + let frac = ((divisor - int as f32) * 256.0) as u8; + + self.sm.sm().sm_clkdiv.write(|w| { + unsafe { + w.int().bits(int); + w.frac().bits(frac); + } + + w + }); + } + /// Sets the pin state for the specified pins. /// /// The user has to make sure that they do not select any pins that are in use by any