aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-rp/src/pwm.rs61
-rw-r--r--examples/rp/src/bin/interrupt.rs3
2 files changed, 32 insertions, 32 deletions
diff --git a/embassy-rp/src/pwm.rs b/embassy-rp/src/pwm.rs
index 2e5aebc57..20b5c4d58 100644
--- a/embassy-rp/src/pwm.rs
+++ b/embassy-rp/src/pwm.rs
@@ -84,19 +84,19 @@ impl From<InputMode> for Divmode {
84pub struct Pwm<'d> { 84pub struct Pwm<'d> {
85 pin_a: Option<PeripheralRef<'d, AnyPin>>, 85 pin_a: Option<PeripheralRef<'d, AnyPin>>,
86 pin_b: Option<PeripheralRef<'d, AnyPin>>, 86 pin_b: Option<PeripheralRef<'d, AnyPin>>,
87 channel: usize, 87 slice: usize,
88} 88}
89 89
90impl<'d> Pwm<'d> { 90impl<'d> Pwm<'d> {
91 fn new_inner( 91 fn new_inner(
92 channel: usize, 92 slice: usize,
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 b_pull: Pull,
96 config: Config, 96 config: Config,
97 divmode: Divmode, 97 divmode: Divmode,
98 ) -> Self { 98 ) -> Self {
99 let p = pac::PWM.ch(channel); 99 let p = pac::PWM.ch(slice);
100 p.csr().modify(|w| { 100 p.csr().modify(|w| {
101 w.set_divmode(divmode); 101 w.set_divmode(divmode);
102 w.set_en(false); 102 w.set_en(false);
@@ -118,26 +118,27 @@ impl<'d> Pwm<'d> {
118 // inner: p.into(), 118 // inner: p.into(),
119 pin_a: a, 119 pin_a: a,
120 pin_b: b, 120 pin_b: b,
121 channel, 121 slice,
122 } 122 }
123 } 123 }
124 124
125 /// Create PWM driver without any configured pins. 125 /// Create PWM driver without any configured pins.
126 #[inline] 126 #[inline]
127 pub fn new_free<T: Slice>(channel: impl Peripheral<P = T> + 'd, config: Config) -> Self { 127 pub fn new_free<T: Slice>(slice: impl Peripheral<P = T> + 'd, config: Config) -> Self {
128 Self::new_inner(channel.number(), None, None, Pull::None, config, Divmode::DIV) 128 into_ref!(slice);
129 Self::new_inner(slice.number(), None, None, Pull::None, config, Divmode::DIV)
129 } 130 }
130 131
131 /// Create PWM driver with a single 'a' as output. 132 /// Create PWM driver with a single 'a' as output.
132 #[inline] 133 #[inline]
133 pub fn new_output_a<T: Slice>( 134 pub fn new_output_a<T: Slice>(
134 channel: impl Peripheral<P = T> + 'd, 135 slice: impl Peripheral<P = T> + 'd,
135 a: impl Peripheral<P = impl ChannelAPin<T>> + 'd, 136 a: impl Peripheral<P = impl ChannelAPin<T>> + 'd,
136 config: Config, 137 config: Config,
137 ) -> Self { 138 ) -> Self {
138 into_ref!(a); 139 into_ref!(slice, a);
139 Self::new_inner( 140 Self::new_inner(
140 channel.number(), 141 slice.number(),
141 Some(a.map_into()), 142 Some(a.map_into()),
142 None, 143 None,
143 Pull::None, 144 Pull::None,
@@ -149,13 +150,13 @@ impl<'d> Pwm<'d> {
149 /// Create PWM driver with a single 'b' pin as output. 150 /// Create PWM driver with a single 'b' pin as output.
150 #[inline] 151 #[inline]
151 pub fn new_output_b<T: Slice>( 152 pub fn new_output_b<T: Slice>(
152 channel: impl Peripheral<P = T> + 'd, 153 slice: impl Peripheral<P = T> + 'd,
153 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, 154 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd,
154 config: Config, 155 config: Config,
155 ) -> Self { 156 ) -> Self {
156 into_ref!(b); 157 into_ref!(slice, b);
157 Self::new_inner( 158 Self::new_inner(
158 channel.number(), 159 slice.number(),
159 None, 160 None,
160 Some(b.map_into()), 161 Some(b.map_into()),
161 Pull::None, 162 Pull::None,
@@ -167,14 +168,14 @@ impl<'d> Pwm<'d> {
167 /// Create PWM driver with a 'a' and 'b' pins as output. 168 /// Create PWM driver with a 'a' and 'b' pins as output.
168 #[inline] 169 #[inline]
169 pub fn new_output_ab<T: Slice>( 170 pub fn new_output_ab<T: Slice>(
170 channel: impl Peripheral<P = T> + 'd, 171 slice: impl Peripheral<P = T> + 'd,
171 a: impl Peripheral<P = impl ChannelAPin<T>> + 'd, 172 a: impl Peripheral<P = impl ChannelAPin<T>> + 'd,
172 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, 173 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd,
173 config: Config, 174 config: Config,
174 ) -> Self { 175 ) -> Self {
175 into_ref!(a, b); 176 into_ref!(slice, a, b);
176 Self::new_inner( 177 Self::new_inner(
177 channel.number(), 178 slice.number(),
178 Some(a.map_into()), 179 Some(a.map_into()),
179 Some(b.map_into()), 180 Some(b.map_into()),
180 Pull::None, 181 Pull::None,
@@ -186,29 +187,29 @@ impl<'d> Pwm<'d> {
186 /// Create PWM driver with a single 'b' as input pin. 187 /// Create PWM driver with a single 'b' as input pin.
187 #[inline] 188 #[inline]
188 pub fn new_input<T: Slice>( 189 pub fn new_input<T: Slice>(
189 channel: impl Peripheral<P = T> + 'd, 190 slice: impl Peripheral<P = T> + 'd,
190 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, 191 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd,
191 b_pull: Pull, 192 b_pull: Pull,
192 mode: InputMode, 193 mode: InputMode,
193 config: Config, 194 config: Config,
194 ) -> Self { 195 ) -> Self {
195 into_ref!(b); 196 into_ref!(slice, b);
196 Self::new_inner(channel.number(), None, Some(b.map_into()), b_pull, config, mode.into()) 197 Self::new_inner(slice.number(), None, Some(b.map_into()), b_pull, config, mode.into())
197 } 198 }
198 199
199 /// Create PWM driver with a 'a' and 'b' pins in the desired input mode. 200 /// Create PWM driver with a 'a' and 'b' pins in the desired input mode.
200 #[inline] 201 #[inline]
201 pub fn new_output_input<T: Slice>( 202 pub fn new_output_input<T: Slice>(
202 channel: impl Peripheral<P = T> + 'd, 203 slice: impl Peripheral<P = T> + 'd,
203 a: impl Peripheral<P = impl ChannelAPin<T>> + 'd, 204 a: impl Peripheral<P = impl ChannelAPin<T>> + 'd,
204 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd, 205 b: impl Peripheral<P = impl ChannelBPin<T>> + 'd,
205 b_pull: Pull, 206 b_pull: Pull,
206 mode: InputMode, 207 mode: InputMode,
207 config: Config, 208 config: Config,
208 ) -> Self { 209 ) -> Self {
209 into_ref!(a, b); 210 into_ref!(slice, a, b);
210 Self::new_inner( 211 Self::new_inner(
211 channel.number(), 212 slice.number(),
212 Some(a.map_into()), 213 Some(a.map_into()),
213 Some(b.map_into()), 214 Some(b.map_into()),
214 b_pull, 215 b_pull,
@@ -219,7 +220,7 @@ impl<'d> Pwm<'d> {
219 220
220 /// Set the PWM config. 221 /// Set the PWM config.
221 pub fn set_config(&mut self, config: &Config) { 222 pub fn set_config(&mut self, config: &Config) {
222 Self::configure(pac::PWM.ch(self.channel), config); 223 Self::configure(pac::PWM.ch(self.slice), config);
223 } 224 }
224 225
225 fn configure(p: pac::pwm::Channel, config: &Config) { 226 fn configure(p: pac::pwm::Channel, config: &Config) {
@@ -246,7 +247,7 @@ impl<'d> Pwm<'d> {
246 /// will not count faster than once per cycle. 247 /// will not count faster than once per cycle.
247 #[inline] 248 #[inline]
248 pub fn phase_advance(&mut self) { 249 pub fn phase_advance(&mut self) {
249 let p = pac::PWM.ch(self.channel); 250 let p = pac::PWM.ch(self.slice);
250 p.csr().write_set(|w| w.set_ph_adv(true)); 251 p.csr().write_set(|w| w.set_ph_adv(true));
251 while p.csr().read().ph_adv() {} 252 while p.csr().read().ph_adv() {}
252 } 253 }
@@ -256,7 +257,7 @@ impl<'d> Pwm<'d> {
256 /// count backward when clock enable is permanently low. 257 /// count backward when clock enable is permanently low.
257 #[inline] 258 #[inline]
258 pub fn phase_retard(&mut self) { 259 pub fn phase_retard(&mut self) {
259 let p = pac::PWM.ch(self.channel); 260 let p = pac::PWM.ch(self.slice);
260 p.csr().write_set(|w| w.set_ph_ret(true)); 261 p.csr().write_set(|w| w.set_ph_ret(true));
261 while p.csr().read().ph_ret() {} 262 while p.csr().read().ph_ret() {}
262 } 263 }
@@ -264,13 +265,13 @@ impl<'d> Pwm<'d> {
264 /// Read PWM counter. 265 /// Read PWM counter.
265 #[inline] 266 #[inline]
266 pub fn counter(&self) -> u16 { 267 pub fn counter(&self) -> u16 {
267 pac::PWM.ch(self.channel).ctr().read().ctr() 268 pac::PWM.ch(self.slice).ctr().read().ctr()
268 } 269 }
269 270
270 /// Write PWM counter. 271 /// Write PWM counter.
271 #[inline] 272 #[inline]
272 pub fn set_counter(&self, ctr: u16) { 273 pub fn set_counter(&self, ctr: u16) {
273 pac::PWM.ch(self.channel).ctr().write(|w| w.set_ctr(ctr)) 274 pac::PWM.ch(self.slice).ctr().write(|w| w.set_ctr(ctr))
274 } 275 }
275 276
276 /// Wait for channel interrupt. 277 /// Wait for channel interrupt.
@@ -294,7 +295,7 @@ impl<'d> Pwm<'d> {
294 295
295 #[inline] 296 #[inline]
296 fn bit(&self) -> u32 { 297 fn bit(&self) -> u32 {
297 1 << self.channel as usize 298 1 << self.slice as usize
298 } 299 }
299} 300}
300 301
@@ -323,7 +324,7 @@ impl PwmBatch {
323 324
324impl<'d> Drop for Pwm<'d> { 325impl<'d> Drop for Pwm<'d> {
325 fn drop(&mut self) { 326 fn drop(&mut self) {
326 pac::PWM.ch(self.channel).csr().write_clear(|w| w.set_en(false)); 327 pac::PWM.ch(self.slice).csr().write_clear(|w| w.set_en(false));
327 if let Some(pin) = &self.pin_a { 328 if let Some(pin) = &self.pin_a {
328 pin.gpio().ctrl().write(|w| w.set_funcsel(31)); 329 pin.gpio().ctrl().write(|w| w.set_funcsel(31));
329 } 330 }
@@ -339,14 +340,14 @@ trait SealedSlice {}
339#[allow(private_bounds)] 340#[allow(private_bounds)]
340pub trait Slice: Peripheral<P = Self> + SealedSlice + Sized + 'static { 341pub trait Slice: Peripheral<P = Self> + SealedSlice + Sized + 'static {
341 /// Slice number. 342 /// Slice number.
342 fn number(&self) -> u8; 343 fn number(&self) -> usize;
343} 344}
344 345
345macro_rules! slice { 346macro_rules! slice {
346 ($name:ident, $num:expr) => { 347 ($name:ident, $num:expr) => {
347 impl SealedSlice for peripherals::$name {} 348 impl SealedSlice for peripherals::$name {}
348 impl Slice for peripherals::$name { 349 impl Slice for peripherals::$name {
349 fn number(&self) -> u8 { 350 fn number(&self) -> usize {
350 $num 351 $num
351 } 352 }
352 } 353 }
diff --git a/examples/rp/src/bin/interrupt.rs b/examples/rp/src/bin/interrupt.rs
index d334d35d7..5b9d7027e 100644
--- a/examples/rp/src/bin/interrupt.rs
+++ b/examples/rp/src/bin/interrupt.rs
@@ -15,7 +15,6 @@ use embassy_executor::Spawner;
15use embassy_rp::adc::{self, Adc, Blocking}; 15use embassy_rp::adc::{self, Adc, Blocking};
16use embassy_rp::gpio::Pull; 16use embassy_rp::gpio::Pull;
17use embassy_rp::interrupt; 17use embassy_rp::interrupt;
18use embassy_rp::peripherals::PWM_SLICE4;
19use embassy_rp::pwm::{Config, Pwm}; 18use embassy_rp::pwm::{Config, Pwm};
20use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; 19use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
21use embassy_sync::blocking_mutex::Mutex; 20use embassy_sync::blocking_mutex::Mutex;
@@ -26,7 +25,7 @@ use static_cell::StaticCell;
26use {defmt_rtt as _, panic_probe as _}; 25use {defmt_rtt as _, panic_probe as _};
27 26
28static COUNTER: AtomicU32 = AtomicU32::new(0); 27static COUNTER: AtomicU32 = AtomicU32::new(0);
29static PWM: Mutex<CriticalSectionRawMutex, RefCell<Option<Pwm<PWM_SLICE4>>>> = Mutex::new(RefCell::new(None)); 28static PWM: Mutex<CriticalSectionRawMutex, RefCell<Option<Pwm>>> = Mutex::new(RefCell::new(None));
30static ADC: Mutex<CriticalSectionRawMutex, RefCell<Option<(Adc<Blocking>, adc::Channel)>>> = 29static ADC: Mutex<CriticalSectionRawMutex, RefCell<Option<(Adc<Blocking>, adc::Channel)>>> =
31 Mutex::new(RefCell::new(None)); 30 Mutex::new(RefCell::new(None));
32static ADC_VALUES: Channel<CriticalSectionRawMutex, u16, 2048> = Channel::new(); 31static ADC_VALUES: Channel<CriticalSectionRawMutex, u16, 2048> = Channel::new();