diff options
| author | Ulf Lilleengen <[email protected]> | 2023-10-18 06:30:08 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-10-18 06:30:08 +0000 |
| commit | 88b2cdd6a0fb009a81f5a7fabf358cfda5840cff (patch) | |
| tree | 882dd4bb8e71018c7db64c047910a25877a41b8d | |
| parent | 35ffdf2143991422c67f4b3517303a0b8f7a3bbe (diff) | |
| parent | 6906cc9c2532fd690f4f43a0d97ea58f887e673b (diff) | |
Merge pull request #2087 from riley-williams/rp2040-pwm-docs
Add docs to RP2040 PWM config
| -rw-r--r-- | embassy-rp/src/pwm.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/embassy-rp/src/pwm.rs b/embassy-rp/src/pwm.rs index c297d69a2..516b8254b 100644 --- a/embassy-rp/src/pwm.rs +++ b/embassy-rp/src/pwm.rs | |||
| @@ -10,16 +10,39 @@ use crate::gpio::sealed::Pin as _; | |||
| 10 | use crate::gpio::{AnyPin, Pin as GpioPin}; | 10 | use crate::gpio::{AnyPin, Pin as GpioPin}; |
| 11 | use crate::{pac, peripherals, RegExt}; | 11 | use crate::{pac, peripherals, RegExt}; |
| 12 | 12 | ||
| 13 | /// The configuration of a PWM slice. | ||
| 14 | /// Note the period in clock cycles of a slice can be computed as: | ||
| 15 | /// `(top + 1) * (phase_correct ? 1 : 2) * divider` | ||
| 13 | #[non_exhaustive] | 16 | #[non_exhaustive] |
| 14 | #[derive(Clone)] | 17 | #[derive(Clone)] |
| 15 | pub struct Config { | 18 | pub struct Config { |
| 19 | /// Inverts the PWM output signal on channel A. | ||
| 16 | pub invert_a: bool, | 20 | pub invert_a: bool, |
| 21 | /// Inverts the PWM output signal on channel B. | ||
| 17 | pub invert_b: bool, | 22 | pub invert_b: bool, |
| 23 | /// Enables phase-correct mode for PWM operation. | ||
| 24 | /// In phase-correct mode, the PWM signal is generated in such a way that | ||
| 25 | /// the pulse is always centered regardless of the duty cycle. | ||
| 26 | /// The output frequency is halved when phase-correct mode is enabled. | ||
| 18 | pub phase_correct: bool, | 27 | pub phase_correct: bool, |
| 28 | /// Enables the PWM slice, allowing it to generate an output. | ||
| 19 | pub enable: bool, | 29 | pub enable: bool, |
| 30 | /// A fractional clock divider, represented as a fixed-point number with | ||
| 31 | /// 8 integer bits and 4 fractional bits. It allows precise control over | ||
| 32 | /// the PWM output frequency by gating the PWM counter increment. | ||
| 33 | /// A higher value will result in a slower output frequency. | ||
| 20 | pub divider: fixed::FixedU16<fixed::types::extra::U4>, | 34 | pub divider: fixed::FixedU16<fixed::types::extra::U4>, |
| 35 | /// The output on channel A goes high when `compare_a` is higher than the | ||
| 36 | /// counter. A compare of 0 will produce an always low output, while a | ||
| 37 | /// compare of `top + 1` will produce an always high output. | ||
| 21 | pub compare_a: u16, | 38 | pub compare_a: u16, |
| 39 | /// The output on channel B goes high when `compare_b` is higher than the | ||
| 40 | /// counter. A compare of 0 will produce an always low output, while a | ||
| 41 | /// compare of `top + 1` will produce an always high output. | ||
| 22 | pub compare_b: u16, | 42 | pub compare_b: u16, |
| 43 | /// The point at which the counter wraps, representing the maximum possible | ||
| 44 | /// period. The counter will either wrap to 0 or reverse depending on the | ||
| 45 | /// setting of `phase_correct`. | ||
| 23 | pub top: u16, | 46 | pub top: u16, |
| 24 | } | 47 | } |
| 25 | 48 | ||
| @@ -173,6 +196,9 @@ impl<'d, T: Channel> Pwm<'d, T> { | |||
| 173 | }); | 196 | }); |
| 174 | } | 197 | } |
| 175 | 198 | ||
| 199 | /// Advances a slice’s output phase by one count while it is running | ||
| 200 | /// by inserting a pulse into the clock enable. The counter | ||
| 201 | /// will not count faster than once per cycle. | ||
| 176 | #[inline] | 202 | #[inline] |
| 177 | pub fn phase_advance(&mut self) { | 203 | pub fn phase_advance(&mut self) { |
| 178 | let p = self.inner.regs(); | 204 | let p = self.inner.regs(); |
| @@ -180,6 +206,9 @@ impl<'d, T: Channel> Pwm<'d, T> { | |||
| 180 | while p.csr().read().ph_adv() {} | 206 | while p.csr().read().ph_adv() {} |
| 181 | } | 207 | } |
| 182 | 208 | ||
| 209 | /// Retards a slice’s output phase by one count while it is running | ||
| 210 | /// by deleting a pulse from the clock enable. The counter will not | ||
| 211 | /// count backward when clock enable is permenantly low. | ||
| 183 | #[inline] | 212 | #[inline] |
| 184 | pub fn phase_retard(&mut self) { | 213 | pub fn phase_retard(&mut self) { |
| 185 | let p = self.inner.regs(); | 214 | let p = self.inner.regs(); |
