diff options
| -rw-r--r-- | embassy-rp/src/pwm.rs | 35 | ||||
| -rw-r--r-- | examples/rp/src/bin/pwm_input.rs | 3 | ||||
| -rw-r--r-- | tests/rp/src/bin/pwm.rs | 46 |
3 files changed, 73 insertions, 11 deletions
diff --git a/embassy-rp/src/pwm.rs b/embassy-rp/src/pwm.rs index 7613e4e58..a1f400cfb 100644 --- a/embassy-rp/src/pwm.rs +++ b/embassy-rp/src/pwm.rs | |||
| @@ -6,7 +6,7 @@ use fixed::FixedU16; | |||
| 6 | use pac::pwm::regs::{ChDiv, Intr}; | 6 | use pac::pwm::regs::{ChDiv, Intr}; |
| 7 | use pac::pwm::vals::Divmode; | 7 | use pac::pwm::vals::Divmode; |
| 8 | 8 | ||
| 9 | use crate::gpio::{AnyPin, Pin as GpioPin, SealedPin as _}; | 9 | use crate::gpio::{AnyPin, Pin as GpioPin, Pull, SealedPin as _}; |
| 10 | use crate::{pac, peripherals, RegExt}; | 10 | use crate::{pac, peripherals, RegExt}; |
| 11 | 11 | ||
| 12 | /// The configuration of a PWM slice. | 12 | /// The configuration of a PWM slice. |
| @@ -92,6 +92,7 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 92 | inner: impl Peripheral<P = T> + 'd, | 92 | inner: impl Peripheral<P = T> + 'd, |
| 93 | a: Option<PeripheralRef<'d, AnyPin>>, | 93 | a: Option<PeripheralRef<'d, AnyPin>>, |
| 94 | b: Option<PeripheralRef<'d, AnyPin>>, | 94 | b: Option<PeripheralRef<'d, AnyPin>>, |
| 95 | b_pull: Pull, | ||
| 95 | config: Config, | 96 | config: Config, |
| 96 | divmode: Divmode, | 97 | divmode: Divmode, |
| 97 | ) -> Self { | 98 | ) -> Self { |
| @@ -110,6 +111,10 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 110 | } | 111 | } |
| 111 | if let Some(pin) = &b { | 112 | if let Some(pin) = &b { |
| 112 | pin.gpio().ctrl().write(|w| w.set_funcsel(4)); | 113 | pin.gpio().ctrl().write(|w| w.set_funcsel(4)); |
| 114 | pin.pad_ctrl().modify(|w| { | ||
| 115 | w.set_pue(b_pull == Pull::Up); | ||
| 116 | w.set_pde(b_pull == Pull::Down); | ||
| 117 | }); | ||
| 113 | } | 118 | } |
| 114 | Self { | 119 | Self { |
| 115 | inner, | 120 | inner, |
| @@ -121,7 +126,7 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 121 | /// Create PWM driver without any configured pins. | 126 | /// Create PWM driver without any configured pins. |
| 122 | #[inline] | 127 | #[inline] |
| 123 | pub fn new_free(inner: impl Peripheral<P = T> + 'd, config: Config) -> Self { | 128 | pub fn new_free(inner: impl Peripheral<P = T> + 'd, config: Config) -> Self { |
| 124 | Self::new_inner(inner, None, None, config, Divmode::DIV) | 129 | Self::new_inner(inner, None, None, Pull::None, config, Divmode::DIV) |
| 125 | } | 130 | } |
| 126 | 131 | ||
| 127 | /// Create PWM driver with a single 'a' as output. | 132 | /// Create PWM driver with a single 'a' as output. |
| @@ -132,7 +137,7 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 132 | config: Config, | 137 | config: Config, |
| 133 | ) -> Self { | 138 | ) -> Self { |
| 134 | into_ref!(a); | 139 | into_ref!(a); |
| 135 | Self::new_inner(inner, Some(a.map_into()), None, config, Divmode::DIV) | 140 | Self::new_inner(inner, Some(a.map_into()), None, Pull::None, config, Divmode::DIV) |
| 136 | } | 141 | } |
| 137 | 142 | ||
| 138 | /// Create PWM driver with a single 'b' pin as output. | 143 | /// Create PWM driver with a single 'b' pin as output. |
| @@ -143,7 +148,7 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 143 | config: Config, | 148 | config: Config, |
| 144 | ) -> Self { | 149 | ) -> Self { |
| 145 | into_ref!(b); | 150 | into_ref!(b); |
| 146 | Self::new_inner(inner, None, Some(b.map_into()), config, Divmode::DIV) | 151 | Self::new_inner(inner, None, Some(b.map_into()), Pull::None, config, Divmode::DIV) |
| 147 | } | 152 | } |
| 148 | 153 | ||
| 149 | /// Create PWM driver with a 'a' and 'b' pins as output. | 154 | /// Create PWM driver with a 'a' and 'b' pins as output. |
| @@ -155,7 +160,14 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 155 | config: Config, | 160 | config: Config, |
| 156 | ) -> Self { | 161 | ) -> Self { |
| 157 | into_ref!(a, b); | 162 | into_ref!(a, b); |
| 158 | Self::new_inner(inner, Some(a.map_into()), Some(b.map_into()), config, Divmode::DIV) | 163 | Self::new_inner( |
| 164 | inner, | ||
| 165 | Some(a.map_into()), | ||
| 166 | Some(b.map_into()), | ||
| 167 | Pull::None, | ||
| 168 | config, | ||
| 169 | Divmode::DIV, | ||
| 170 | ) | ||
| 159 | } | 171 | } |
| 160 | 172 | ||
| 161 | /// Create PWM driver with a single 'b' as input pin. | 173 | /// Create PWM driver with a single 'b' as input pin. |
| @@ -163,11 +175,12 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 163 | pub fn new_input( | 175 | pub fn new_input( |
| 164 | inner: impl Peripheral<P = T> + 'd, | 176 | inner: impl Peripheral<P = T> + 'd, |
| 165 | b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, | 177 | b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, |
| 178 | b_pull: Pull, | ||
| 166 | mode: InputMode, | 179 | mode: InputMode, |
| 167 | config: Config, | 180 | config: Config, |
| 168 | ) -> Self { | 181 | ) -> Self { |
| 169 | into_ref!(b); | 182 | into_ref!(b); |
| 170 | Self::new_inner(inner, None, Some(b.map_into()), config, mode.into()) | 183 | Self::new_inner(inner, None, Some(b.map_into()), b_pull, config, mode.into()) |
| 171 | } | 184 | } |
| 172 | 185 | ||
| 173 | /// Create PWM driver with a 'a' and 'b' pins in the desired input mode. | 186 | /// Create PWM driver with a 'a' and 'b' pins in the desired input mode. |
| @@ -176,11 +189,19 @@ impl<'d, T: Slice> Pwm<'d, T> { | |||
| 176 | inner: impl Peripheral<P = T> + 'd, | 189 | inner: impl Peripheral<P = T> + 'd, |
| 177 | a: impl Peripheral<P = impl ChannelAPin<T>> + 'd, | 190 | a: impl Peripheral<P = impl ChannelAPin<T>> + 'd, |
| 178 | b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, | 191 | b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, |
| 192 | b_pull: Pull, | ||
| 179 | mode: InputMode, | 193 | mode: InputMode, |
| 180 | config: Config, | 194 | config: Config, |
| 181 | ) -> Self { | 195 | ) -> Self { |
| 182 | into_ref!(a, b); | 196 | into_ref!(a, b); |
| 183 | Self::new_inner(inner, Some(a.map_into()), Some(b.map_into()), config, mode.into()) | 197 | Self::new_inner( |
| 198 | inner, | ||
| 199 | Some(a.map_into()), | ||
| 200 | Some(b.map_into()), | ||
| 201 | b_pull, | ||
| 202 | config, | ||
| 203 | mode.into(), | ||
| 204 | ) | ||
| 184 | } | 205 | } |
| 185 | 206 | ||
| 186 | /// Set the PWM config. | 207 | /// Set the PWM config. |
diff --git a/examples/rp/src/bin/pwm_input.rs b/examples/rp/src/bin/pwm_input.rs index 0652dc42b..bf454a936 100644 --- a/examples/rp/src/bin/pwm_input.rs +++ b/examples/rp/src/bin/pwm_input.rs | |||
| @@ -5,6 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | use defmt::*; | 6 | use defmt::*; |
| 7 | use embassy_executor::Spawner; | 7 | use embassy_executor::Spawner; |
| 8 | use embassy_rp::gpio::Pull; | ||
| 8 | use embassy_rp::pwm::{Config, InputMode, Pwm}; | 9 | use embassy_rp::pwm::{Config, InputMode, Pwm}; |
| 9 | use embassy_time::{Duration, Ticker}; | 10 | use embassy_time::{Duration, Ticker}; |
| 10 | use {defmt_rtt as _, panic_probe as _}; | 11 | use {defmt_rtt as _, panic_probe as _}; |
| @@ -14,7 +15,7 @@ async fn main(_spawner: Spawner) { | |||
| 14 | let p = embassy_rp::init(Default::default()); | 15 | let p = embassy_rp::init(Default::default()); |
| 15 | 16 | ||
| 16 | let cfg: Config = Default::default(); | 17 | let cfg: Config = Default::default(); |
| 17 | let pwm = Pwm::new_input(p.PWM_SLICE2, p.PIN_5, InputMode::RisingEdge, cfg); | 18 | let pwm = Pwm::new_input(p.PWM_SLICE2, p.PIN_5, Pull::None, InputMode::RisingEdge, cfg); |
| 18 | 19 | ||
| 19 | let mut ticker = Ticker::every(Duration::from_secs(1)); | 20 | let mut ticker = Ticker::every(Duration::from_secs(1)); |
| 20 | loop { | 21 | loop { |
diff --git a/tests/rp/src/bin/pwm.rs b/tests/rp/src/bin/pwm.rs index 4b02e5bab..c05197000 100644 --- a/tests/rp/src/bin/pwm.rs +++ b/tests/rp/src/bin/pwm.rs | |||
| @@ -94,7 +94,7 @@ async fn main(_spawner: Spawner) { | |||
| 94 | // Test level-gated | 94 | // Test level-gated |
| 95 | { | 95 | { |
| 96 | let mut pin2 = Output::new(&mut p11, Level::Low); | 96 | let mut pin2 = Output::new(&mut p11, Level::Low); |
| 97 | let pwm = Pwm::new_input(&mut p.PWM_SLICE3, &mut p7, InputMode::Level, cfg.clone()); | 97 | let pwm = Pwm::new_input(&mut p.PWM_SLICE3, &mut p7, Pull::None, InputMode::Level, cfg.clone()); |
| 98 | assert_eq!(pwm.counter(), 0); | 98 | assert_eq!(pwm.counter(), 0); |
| 99 | Timer::after_millis(5).await; | 99 | Timer::after_millis(5).await; |
| 100 | assert_eq!(pwm.counter(), 0); | 100 | assert_eq!(pwm.counter(), 0); |
| @@ -110,7 +110,13 @@ async fn main(_spawner: Spawner) { | |||
| 110 | // Test rising-gated | 110 | // Test rising-gated |
| 111 | { | 111 | { |
| 112 | let mut pin2 = Output::new(&mut p11, Level::Low); | 112 | let mut pin2 = Output::new(&mut p11, Level::Low); |
| 113 | let pwm = Pwm::new_input(&mut p.PWM_SLICE3, &mut p7, InputMode::RisingEdge, cfg.clone()); | 113 | let pwm = Pwm::new_input( |
| 114 | &mut p.PWM_SLICE3, | ||
| 115 | &mut p7, | ||
| 116 | Pull::None, | ||
| 117 | InputMode::RisingEdge, | ||
| 118 | cfg.clone(), | ||
| 119 | ); | ||
| 114 | assert_eq!(pwm.counter(), 0); | 120 | assert_eq!(pwm.counter(), 0); |
| 115 | Timer::after_millis(5).await; | 121 | Timer::after_millis(5).await; |
| 116 | assert_eq!(pwm.counter(), 0); | 122 | assert_eq!(pwm.counter(), 0); |
| @@ -125,7 +131,13 @@ async fn main(_spawner: Spawner) { | |||
| 125 | // Test falling-gated | 131 | // Test falling-gated |
| 126 | { | 132 | { |
| 127 | let mut pin2 = Output::new(&mut p11, Level::High); | 133 | let mut pin2 = Output::new(&mut p11, Level::High); |
| 128 | let pwm = Pwm::new_input(&mut p.PWM_SLICE3, &mut p7, InputMode::FallingEdge, cfg.clone()); | 134 | let pwm = Pwm::new_input( |
| 135 | &mut p.PWM_SLICE3, | ||
| 136 | &mut p7, | ||
| 137 | Pull::None, | ||
| 138 | InputMode::FallingEdge, | ||
| 139 | cfg.clone(), | ||
| 140 | ); | ||
| 129 | assert_eq!(pwm.counter(), 0); | 141 | assert_eq!(pwm.counter(), 0); |
| 130 | Timer::after_millis(5).await; | 142 | Timer::after_millis(5).await; |
| 131 | assert_eq!(pwm.counter(), 0); | 143 | assert_eq!(pwm.counter(), 0); |
| @@ -137,6 +149,34 @@ async fn main(_spawner: Spawner) { | |||
| 137 | assert_eq!(pwm.counter(), 1); | 149 | assert_eq!(pwm.counter(), 1); |
| 138 | } | 150 | } |
| 139 | 151 | ||
| 152 | // pull-down | ||
| 153 | { | ||
| 154 | let pin2 = Input::new(&mut p11, Pull::None); | ||
| 155 | Pwm::new_input( | ||
| 156 | &mut p.PWM_SLICE3, | ||
| 157 | &mut p7, | ||
| 158 | Pull::Down, | ||
| 159 | InputMode::FallingEdge, | ||
| 160 | cfg.clone(), | ||
| 161 | ); | ||
| 162 | Timer::after_millis(1).await; | ||
| 163 | assert!(pin2.is_low()); | ||
| 164 | } | ||
| 165 | |||
| 166 | // pull-up | ||
| 167 | { | ||
| 168 | let pin2 = Input::new(&mut p11, Pull::None); | ||
| 169 | Pwm::new_input( | ||
| 170 | &mut p.PWM_SLICE3, | ||
| 171 | &mut p7, | ||
| 172 | Pull::Up, | ||
| 173 | InputMode::FallingEdge, | ||
| 174 | cfg.clone(), | ||
| 175 | ); | ||
| 176 | Timer::after_millis(1).await; | ||
| 177 | assert!(pin2.is_high()); | ||
| 178 | } | ||
| 179 | |||
| 140 | info!("Test OK"); | 180 | info!("Test OK"); |
| 141 | cortex_m::asm::bkpt(); | 181 | cortex_m::asm::bkpt(); |
| 142 | } | 182 | } |
