diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-11-11 16:26:36 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-11-11 16:26:36 +0000 |
| commit | 4112759a8fefe33c776f6eddd519a1c33c2a30d3 (patch) | |
| tree | 83547e24e46418cadc38be6534db5ca2b942a2c9 /embassy-nrf/src | |
| parent | 8193885cb5c1e4faa3ce884ccc6922b808208804 (diff) | |
| parent | 156caa9330f4ce8bf9af6b90655e8dbbac6ad74a (diff) | |
Merge #455
455: simple_playback api from nrf sdk r=Dirbaio a=jacobrosenthal
Port of the nrf_drv_pwm_simple_playback call from the nordic sdk that allows you to set up a sequence to play across leds with no interaction necessary using the 'shorts' registers to trigger looping sequences
Co-authored-by: Jacob Rosenthal <[email protected]>
Diffstat (limited to 'embassy-nrf/src')
| -rw-r--r-- | embassy-nrf/src/pwm.rs | 435 | ||||
| -rw-r--r-- | embassy-nrf/src/util.rs | 8 |
2 files changed, 392 insertions, 51 deletions
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs index 5e996e882..08e9add0e 100644 --- a/embassy-nrf/src/pwm.rs +++ b/embassy-nrf/src/pwm.rs | |||
| @@ -1,16 +1,299 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | 2 | ||
| 3 | use core::cell::UnsafeCell; | ||
| 4 | use core::marker::PhantomData; | 3 | use core::marker::PhantomData; |
| 5 | use core::sync::atomic::{compiler_fence, Ordering}; | 4 | use core::sync::atomic::{compiler_fence, Ordering}; |
| 6 | use embassy::util::Unborrow; | 5 | use embassy::util::Unborrow; |
| 7 | use embassy_hal_common::unborrow; | 6 | use embassy_hal_common::unborrow; |
| 8 | 7 | ||
| 9 | use crate::gpio::sealed::Pin as _; | 8 | use crate::gpio::sealed::Pin as _; |
| 10 | use crate::gpio::OptionalPin as GpioOptionalPin; | 9 | use crate::gpio::{AnyPin, OptionalPin as GpioOptionalPin}; |
| 11 | use crate::interrupt::Interrupt; | 10 | use crate::interrupt::Interrupt; |
| 12 | use crate::pac; | 11 | use crate::pac; |
| 12 | use crate::util::slice_in_ram_or; | ||
| 13 | 13 | ||
| 14 | /// SimplePwm is the traditional pwm interface you're probably used to, allowing | ||
| 15 | /// to simply set a duty cycle across up to four channels. | ||
| 16 | pub struct SimplePwm<'d, T: Instance> { | ||
| 17 | phantom: PhantomData<&'d mut T>, | ||
| 18 | duty: [u16; 4], | ||
| 19 | ch0: Option<AnyPin>, | ||
| 20 | ch1: Option<AnyPin>, | ||
| 21 | ch2: Option<AnyPin>, | ||
| 22 | ch3: Option<AnyPin>, | ||
| 23 | } | ||
| 24 | |||
| 25 | /// SequencePwm allows you to offload the updating of a sequence of duty cycles | ||
| 26 | /// to up to four channels, as well as repeat that sequence n times. | ||
| 27 | pub struct SequencePwm<'d, T: Instance> { | ||
| 28 | phantom: PhantomData<&'d mut T>, | ||
| 29 | ch0: Option<AnyPin>, | ||
| 30 | ch1: Option<AnyPin>, | ||
| 31 | ch2: Option<AnyPin>, | ||
| 32 | ch3: Option<AnyPin>, | ||
| 33 | } | ||
| 34 | |||
| 35 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| 36 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 37 | #[non_exhaustive] | ||
| 38 | pub enum Error { | ||
| 39 | /// Max Sequence size is 32767 | ||
| 40 | SequenceTooLong, | ||
| 41 | /// Min Sequence count is 1 | ||
| 42 | SequenceTimesAtLeastOne, | ||
| 43 | /// EasyDMA can only read from data memory, read only buffers in flash will fail. | ||
| 44 | DMABufferNotInDataMemory, | ||
| 45 | } | ||
| 46 | |||
| 47 | impl<'d, T: Instance> SequencePwm<'d, T> { | ||
| 48 | /// Creates the interface to a `SequencePwm`. | ||
| 49 | /// | ||
| 50 | /// Must be started by calling `start` | ||
| 51 | /// | ||
| 52 | /// # Safety | ||
| 53 | /// | ||
| 54 | /// The returned API is safe unless you use `mem::forget` (or similar safe | ||
| 55 | /// mechanisms) on stack allocated buffers which which have been passed to | ||
| 56 | /// [`new()`](SequencePwm::new). | ||
| 57 | #[allow(unused_unsafe)] | ||
| 58 | pub fn new<'a>( | ||
| 59 | _pwm: impl Unborrow<Target = T> + 'd, | ||
| 60 | ch0: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 61 | ch1: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 62 | ch2: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 63 | ch3: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 64 | config: SequenceConfig, | ||
| 65 | sequence: &'a [u16], | ||
| 66 | ) -> Result<Self, Error> { | ||
| 67 | slice_in_ram_or(sequence, Error::DMABufferNotInDataMemory)?; | ||
| 68 | |||
| 69 | if sequence.len() > 32767 { | ||
| 70 | return Err(Error::SequenceTooLong); | ||
| 71 | } | ||
| 72 | |||
| 73 | unborrow!(ch0, ch1, ch2, ch3); | ||
| 74 | |||
| 75 | let r = T::regs(); | ||
| 76 | |||
| 77 | if let Some(pin) = ch0.pin_mut() { | ||
| 78 | pin.set_low(); | ||
| 79 | pin.conf().write(|w| w.dir().output()); | ||
| 80 | } | ||
| 81 | if let Some(pin) = ch1.pin_mut() { | ||
| 82 | pin.set_low(); | ||
| 83 | pin.conf().write(|w| w.dir().output()); | ||
| 84 | } | ||
| 85 | if let Some(pin) = ch2.pin_mut() { | ||
| 86 | pin.set_low(); | ||
| 87 | pin.conf().write(|w| w.dir().output()); | ||
| 88 | } | ||
| 89 | if let Some(pin) = ch3.pin_mut() { | ||
| 90 | pin.set_low(); | ||
| 91 | pin.conf().write(|w| w.dir().output()); | ||
| 92 | } | ||
| 93 | |||
| 94 | // if NoPin provided writes disconnected (top bit 1) 0x80000000 else | ||
| 95 | // writes pin number ex 13 (0x0D) which is connected (top bit 0) | ||
| 96 | r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); | ||
| 97 | r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); | ||
| 98 | r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); | ||
| 99 | r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); | ||
| 100 | |||
| 101 | // Disable all interrupts | ||
| 102 | r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); | ||
| 103 | r.shorts.reset(); | ||
| 104 | |||
| 105 | // Enable | ||
| 106 | r.enable.write(|w| w.enable().enabled()); | ||
| 107 | |||
| 108 | r.seq0 | ||
| 109 | .ptr | ||
| 110 | .write(|w| unsafe { w.bits(sequence.as_ptr() as u32) }); | ||
| 111 | r.seq0 | ||
| 112 | .cnt | ||
| 113 | .write(|w| unsafe { w.bits(sequence.len() as u32) }); | ||
| 114 | r.seq0.refresh.write(|w| unsafe { w.bits(config.refresh) }); | ||
| 115 | r.seq0 | ||
| 116 | .enddelay | ||
| 117 | .write(|w| unsafe { w.bits(config.end_delay) }); | ||
| 118 | |||
| 119 | r.seq1 | ||
| 120 | .ptr | ||
| 121 | .write(|w| unsafe { w.bits(sequence.as_ptr() as u32) }); | ||
| 122 | r.seq1 | ||
| 123 | .cnt | ||
| 124 | .write(|w| unsafe { w.bits(sequence.len() as u32) }); | ||
| 125 | r.seq1.refresh.write(|w| unsafe { w.bits(config.refresh) }); | ||
| 126 | r.seq1 | ||
| 127 | .enddelay | ||
| 128 | .write(|w| unsafe { w.bits(config.end_delay) }); | ||
| 129 | |||
| 130 | r.decoder.write(|w| { | ||
| 131 | w.load().bits(config.sequence_load as u8); | ||
| 132 | w.mode().refresh_count() | ||
| 133 | }); | ||
| 134 | |||
| 135 | r.mode.write(|w| match config.counter_mode { | ||
| 136 | CounterMode::UpAndDown => w.updown().up_and_down(), | ||
| 137 | CounterMode::Up => w.updown().up(), | ||
| 138 | }); | ||
| 139 | r.prescaler | ||
| 140 | .write(|w| w.prescaler().bits(config.prescaler as u8)); | ||
| 141 | r.countertop | ||
| 142 | .write(|w| unsafe { w.countertop().bits(config.top) }); | ||
| 143 | |||
| 144 | Ok(Self { | ||
| 145 | phantom: PhantomData, | ||
| 146 | ch0: ch0.degrade_optional(), | ||
| 147 | ch1: ch1.degrade_optional(), | ||
| 148 | ch2: ch2.degrade_optional(), | ||
| 149 | ch3: ch3.degrade_optional(), | ||
| 150 | }) | ||
| 151 | } | ||
| 152 | |||
| 153 | /// Start or restart playback | ||
| 154 | #[inline(always)] | ||
| 155 | pub fn start(&self, times: SequenceMode) -> Result<(), Error> { | ||
| 156 | if let SequenceMode::Times(0) = times { | ||
| 157 | return Err(Error::SequenceTimesAtLeastOne); | ||
| 158 | } | ||
| 159 | let r = T::regs(); | ||
| 160 | |||
| 161 | self.stop(); | ||
| 162 | |||
| 163 | r.enable.write(|w| w.enable().enabled()); | ||
| 164 | |||
| 165 | // defensive before seqstart | ||
| 166 | compiler_fence(Ordering::SeqCst); | ||
| 167 | |||
| 168 | match times { | ||
| 169 | // just the one time, no loop count | ||
| 170 | SequenceMode::Times(1) => { | ||
| 171 | r.loop_.write(|w| w.cnt().disabled()); | ||
| 172 | // tasks_seqstart() doesn't exist in all svds so write its bit instead | ||
| 173 | r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); | ||
| 174 | } | ||
| 175 | // loop count is how many times to play BOTH sequences | ||
| 176 | // 2 total (1 x 2) | ||
| 177 | // 3 total, (2 x 2) - 1 | ||
| 178 | SequenceMode::Times(n) => { | ||
| 179 | let odd = n & 1 == 1; | ||
| 180 | let times = if odd { (n / 2) + 1 } else { n / 2 }; | ||
| 181 | |||
| 182 | r.loop_.write(|w| unsafe { w.cnt().bits(times) }); | ||
| 183 | |||
| 184 | // we can subtract 1 by starting at seq1 instead of seq0 | ||
| 185 | if odd { | ||
| 186 | // tasks_seqstart() doesn't exist in all svds so write its bit instead | ||
| 187 | r.tasks_seqstart[1].write(|w| unsafe { w.bits(0x01) }); | ||
| 188 | } else { | ||
| 189 | // tasks_seqstart() doesn't exist in all svds so write its bit instead | ||
| 190 | r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | // to play infinitely, repeat the sequence one time, then have loops done self trigger seq0 again | ||
| 194 | SequenceMode::Infinite => { | ||
| 195 | r.loop_.write(|w| unsafe { w.cnt().bits(0x1) }); | ||
| 196 | r.shorts.write(|w| w.loopsdone_seqstart0().enabled()); | ||
| 197 | |||
| 198 | // tasks_seqstart() doesn't exist in all svds so write its bit instead | ||
| 199 | r.tasks_seqstart[0].write(|w| unsafe { w.bits(0x01) }); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | Ok(()) | ||
| 204 | } | ||
| 205 | |||
| 206 | /// Stop playback. | ||
| 207 | #[inline(always)] | ||
| 208 | pub fn stop(&self) { | ||
| 209 | let r = T::regs(); | ||
| 210 | |||
| 211 | r.shorts.reset(); | ||
| 212 | |||
| 213 | compiler_fence(Ordering::SeqCst); | ||
| 214 | |||
| 215 | // tasks_stop() doesn't exist in all svds so write its bit instead | ||
| 216 | r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); | ||
| 217 | } | ||
| 218 | |||
| 219 | /// Disables the PWM generator. | ||
| 220 | #[inline(always)] | ||
| 221 | pub fn disable(&self) { | ||
| 222 | let r = T::regs(); | ||
| 223 | r.enable.write(|w| w.enable().disabled()); | ||
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | impl<'a, T: Instance> Drop for SequencePwm<'a, T> { | ||
| 228 | fn drop(&mut self) { | ||
| 229 | let r = T::regs(); | ||
| 230 | |||
| 231 | self.stop(); | ||
| 232 | self.disable(); | ||
| 233 | |||
| 234 | if let Some(pin) = &self.ch0 { | ||
| 235 | pin.set_low(); | ||
| 236 | pin.conf().write(|w| w); | ||
| 237 | r.psel.out[0].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 238 | } | ||
| 239 | if let Some(pin) = &self.ch1 { | ||
| 240 | pin.set_low(); | ||
| 241 | pin.conf().write(|w| w); | ||
| 242 | r.psel.out[1].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 243 | } | ||
| 244 | if let Some(pin) = &self.ch2 { | ||
| 245 | pin.set_low(); | ||
| 246 | pin.conf().write(|w| w); | ||
| 247 | r.psel.out[2].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 248 | } | ||
| 249 | if let Some(pin) = &self.ch3 { | ||
| 250 | pin.set_low(); | ||
| 251 | pin.conf().write(|w| w); | ||
| 252 | r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 253 | } | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | /// Configure an infinite looping sequence for `SequencePwm` | ||
| 258 | #[non_exhaustive] | ||
| 259 | pub struct SequenceConfig { | ||
| 260 | /// Selects up mode or up-and-down mode for the counter | ||
| 261 | pub counter_mode: CounterMode, | ||
| 262 | /// Top value to be compared against buffer values | ||
| 263 | pub top: u16, | ||
| 264 | /// Configuration for PWM_CLK | ||
| 265 | pub prescaler: Prescaler, | ||
| 266 | /// How a sequence is read from RAM and is spread to the compare register | ||
| 267 | pub sequence_load: SequenceLoad, | ||
| 268 | /// Number of PWM periods to delay between each sequence sample | ||
| 269 | pub refresh: u32, | ||
| 270 | /// Number of PWM periods after the sequence ends before starting the next sequence | ||
| 271 | pub end_delay: u32, | ||
| 272 | } | ||
| 273 | |||
| 274 | impl Default for SequenceConfig { | ||
| 275 | fn default() -> SequenceConfig { | ||
| 276 | SequenceConfig { | ||
| 277 | counter_mode: CounterMode::Up, | ||
| 278 | top: 1000, | ||
| 279 | prescaler: Prescaler::Div16, | ||
| 280 | sequence_load: SequenceLoad::Common, | ||
| 281 | refresh: 0, | ||
| 282 | end_delay: 0, | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | /// How many times to run the sequence | ||
| 288 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | ||
| 289 | pub enum SequenceMode { | ||
| 290 | /// Run sequence n Times total | ||
| 291 | Times(u16), | ||
| 292 | /// Repeat until `stop` is called. | ||
| 293 | Infinite, | ||
| 294 | } | ||
| 295 | |||
| 296 | /// PWM Base clock is system clock (16MHz) divided by prescaler | ||
| 14 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | 297 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 15 | pub enum Prescaler { | 298 | pub enum Prescaler { |
| 16 | Div1, | 299 | Div1, |
| @@ -23,20 +306,42 @@ pub enum Prescaler { | |||
| 23 | Div128, | 306 | Div128, |
| 24 | } | 307 | } |
| 25 | 308 | ||
| 26 | /// Interface to the UARTE peripheral | 309 | /// How the sequence values are distributed across the channels |
| 27 | pub struct Pwm<'d, T: Instance> { | 310 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 28 | phantom: PhantomData<&'d mut T>, | 311 | pub enum SequenceLoad { |
| 312 | /// Provided sequence will be used across all channels | ||
| 313 | Common, | ||
| 314 | /// Provided sequence contains grouped values for each channel ex: | ||
| 315 | /// [ch0_0_and_ch1_0, ch2_0_and_ch3_0, ... ch0_n_and_ch1_n, ch2_n_and_ch3_n] | ||
| 316 | Grouped, | ||
| 317 | /// Provided sequence contains individual values for each channel ex: | ||
| 318 | /// [ch0_0, ch1_0, ch2_0, ch3_0... ch0_n, ch1_n, ch2_n, ch3_n] | ||
| 319 | Individual, | ||
| 320 | /// Similar to Individual mode, but only three channels are used. The fourth | ||
| 321 | /// value is loaded into the pulse generator counter as its top value. | ||
| 322 | Waveform, | ||
| 29 | } | 323 | } |
| 30 | 324 | ||
| 31 | impl<'d, T: Instance> Pwm<'d, T> { | 325 | /// Selects up mode or up-and-down mode for the counter |
| 32 | /// Creates the interface to a UARTE instance. | 326 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] |
| 33 | /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. | 327 | pub enum CounterMode { |
| 328 | /// Up counter (edge-aligned PWM duty cycle) | ||
| 329 | Up, | ||
| 330 | /// Up and down counter (center-aligned PWM duty cycle) | ||
| 331 | UpAndDown, | ||
| 332 | } | ||
| 333 | |||
| 334 | impl<'d, T: Instance> SimplePwm<'d, T> { | ||
| 335 | /// Creates the interface to a `SimplePwm` | ||
| 336 | /// | ||
| 337 | /// Defaults the freq to 1Mhz, max_duty 1000, duty 0, up mode, and pins low. | ||
| 338 | /// Must be started by calling `set_duty` | ||
| 34 | /// | 339 | /// |
| 35 | /// # Safety | 340 | /// # Safety |
| 36 | /// | 341 | /// |
| 37 | /// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms) | 342 | /// The returned API is safe unless you use `mem::forget` (or similar safe |
| 38 | /// on stack allocated buffers which which have been passed to [`send()`](Pwm::send) | 343 | /// mechanisms) on stack allocated buffers which which have been passed to |
| 39 | /// or [`receive`](Pwm::receive). | 344 | /// [`new()`](SimplePwm::new). |
| 40 | #[allow(unused_unsafe)] | 345 | #[allow(unused_unsafe)] |
| 41 | pub fn new( | 346 | pub fn new( |
| 42 | _pwm: impl Unborrow<Target = T> + 'd, | 347 | _pwm: impl Unborrow<Target = T> + 'd, |
| @@ -48,7 +353,6 @@ impl<'d, T: Instance> Pwm<'d, T> { | |||
| 48 | unborrow!(ch0, ch1, ch2, ch3); | 353 | unborrow!(ch0, ch1, ch2, ch3); |
| 49 | 354 | ||
| 50 | let r = T::regs(); | 355 | let r = T::regs(); |
| 51 | let s = T::state(); | ||
| 52 | 356 | ||
| 53 | if let Some(pin) = ch0.pin_mut() { | 357 | if let Some(pin) = ch0.pin_mut() { |
| 54 | pin.set_low(); | 358 | pin.set_low(); |
| @@ -66,22 +370,36 @@ impl<'d, T: Instance> Pwm<'d, T> { | |||
| 66 | pin.set_low(); | 370 | pin.set_low(); |
| 67 | pin.conf().write(|w| w.dir().output()); | 371 | pin.conf().write(|w| w.dir().output()); |
| 68 | } | 372 | } |
| 373 | |||
| 374 | // if NoPin provided writes disconnected (top bit 1) 0x80000000 else | ||
| 375 | // writes pin number ex 13 (0x0D) which is connected (top bit 0) | ||
| 69 | r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); | 376 | r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); |
| 70 | r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); | 377 | r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); |
| 71 | r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); | 378 | r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); |
| 72 | r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); | 379 | r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); |
| 73 | 380 | ||
| 381 | let pwm = Self { | ||
| 382 | phantom: PhantomData, | ||
| 383 | ch0: ch0.degrade_optional(), | ||
| 384 | ch1: ch1.degrade_optional(), | ||
| 385 | ch2: ch2.degrade_optional(), | ||
| 386 | ch3: ch3.degrade_optional(), | ||
| 387 | duty: [0; 4], | ||
| 388 | }; | ||
| 389 | |||
| 74 | // Disable all interrupts | 390 | // Disable all interrupts |
| 75 | r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); | 391 | r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); |
| 392 | r.shorts.reset(); | ||
| 76 | 393 | ||
| 77 | // Enable | 394 | // Enable |
| 78 | r.enable.write(|w| w.enable().enabled()); | 395 | r.enable.write(|w| w.enable().enabled()); |
| 79 | 396 | ||
| 80 | r.seq0 | 397 | r.seq0 |
| 81 | .ptr | 398 | .ptr |
| 82 | .write(|w| unsafe { w.bits(&s.duty as *const _ as u32) }); | 399 | .write(|w| unsafe { w.bits(&pwm.duty as *const _ as u32) }); |
| 400 | |||
| 83 | r.seq0.cnt.write(|w| unsafe { w.bits(4) }); | 401 | r.seq0.cnt.write(|w| unsafe { w.bits(4) }); |
| 84 | r.seq0.refresh.write(|w| unsafe { w.bits(32) }); | 402 | r.seq0.refresh.write(|w| unsafe { w.bits(0) }); |
| 85 | r.seq0.enddelay.write(|w| unsafe { w.bits(0) }); | 403 | r.seq0.enddelay.write(|w| unsafe { w.bits(0) }); |
| 86 | 404 | ||
| 87 | r.decoder.write(|w| { | 405 | r.decoder.write(|w| { |
| @@ -89,14 +407,24 @@ impl<'d, T: Instance> Pwm<'d, T> { | |||
| 89 | w.mode().refresh_count() | 407 | w.mode().refresh_count() |
| 90 | }); | 408 | }); |
| 91 | r.mode.write(|w| w.updown().up()); | 409 | r.mode.write(|w| w.updown().up()); |
| 92 | r.prescaler.write(|w| w.prescaler().div_1()); | 410 | r.prescaler.write(|w| w.prescaler().div_16()); |
| 93 | r.countertop | 411 | r.countertop.write(|w| unsafe { w.countertop().bits(1000) }); |
| 94 | .write(|w| unsafe { w.countertop().bits(32767) }); | ||
| 95 | r.loop_.write(|w| w.cnt().disabled()); | 412 | r.loop_.write(|w| w.cnt().disabled()); |
| 96 | 413 | ||
| 97 | Self { | 414 | pwm |
| 98 | phantom: PhantomData, | 415 | } |
| 99 | } | 416 | |
| 417 | /// Stop playback | ||
| 418 | #[inline(always)] | ||
| 419 | pub fn stop(&self) { | ||
| 420 | let r = T::regs(); | ||
| 421 | |||
| 422 | r.shorts.reset(); | ||
| 423 | |||
| 424 | compiler_fence(Ordering::SeqCst); | ||
| 425 | |||
| 426 | // tasks_stop() doesn't exist in all svds so write its bit instead | ||
| 427 | r.tasks_stop.write(|w| unsafe { w.bits(0x01) }); | ||
| 100 | } | 428 | } |
| 101 | 429 | ||
| 102 | /// Enables the PWM generator. | 430 | /// Enables the PWM generator. |
| @@ -114,12 +442,24 @@ impl<'d, T: Instance> Pwm<'d, T> { | |||
| 114 | } | 442 | } |
| 115 | 443 | ||
| 116 | /// Sets duty cycle (15 bit) for a PWM channel. | 444 | /// Sets duty cycle (15 bit) for a PWM channel. |
| 117 | pub fn set_duty(&self, channel: usize, duty: u16) { | 445 | pub fn set_duty(&mut self, channel: usize, duty: u16) { |
| 118 | let s = T::state(); | 446 | let r = T::regs(); |
| 119 | unsafe { (*s.duty.get())[channel] = duty & 0x7FFF }; | ||
| 120 | 447 | ||
| 448 | self.duty[channel] = duty & 0x7FFF; | ||
| 449 | |||
| 450 | r.seq0 | ||
| 451 | .ptr | ||
| 452 | .write(|w| unsafe { w.bits(&self.duty as *const _ as u32) }); | ||
| 453 | |||
| 454 | // defensive before seqstart | ||
| 121 | compiler_fence(Ordering::SeqCst); | 455 | compiler_fence(Ordering::SeqCst); |
| 122 | T::regs().tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); | 456 | |
| 457 | // tasks_seqstart() doesn't exist in all svds so write its bit instead | ||
| 458 | r.tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); | ||
| 459 | |||
| 460 | // defensive wait until waveform is loaded after seqstart | ||
| 461 | while r.events_seqend[0].read().bits() == 0 {} | ||
| 462 | r.events_seqend[0].write(|w| w); | ||
| 123 | } | 463 | } |
| 124 | 464 | ||
| 125 | /// Sets the PWM clock prescaler. | 465 | /// Sets the PWM clock prescaler. |
| @@ -128,7 +468,7 @@ impl<'d, T: Instance> Pwm<'d, T> { | |||
| 128 | T::regs().prescaler.write(|w| w.prescaler().bits(div as u8)); | 468 | T::regs().prescaler.write(|w| w.prescaler().bits(div as u8)); |
| 129 | } | 469 | } |
| 130 | 470 | ||
| 131 | /// Sets the PWM clock prescaler. | 471 | /// Gets the PWM clock prescaler. |
| 132 | #[inline(always)] | 472 | #[inline(always)] |
| 133 | pub fn prescaler(&self) -> Prescaler { | 473 | pub fn prescaler(&self) -> Prescaler { |
| 134 | match T::regs().prescaler.read().prescaler().bits() { | 474 | match T::regs().prescaler.read().prescaler().bits() { |
| @@ -175,36 +515,41 @@ impl<'d, T: Instance> Pwm<'d, T> { | |||
| 175 | } | 515 | } |
| 176 | } | 516 | } |
| 177 | 517 | ||
| 178 | impl<'a, T: Instance> Drop for Pwm<'a, T> { | 518 | impl<'a, T: Instance> Drop for SimplePwm<'a, T> { |
| 179 | fn drop(&mut self) { | 519 | fn drop(&mut self) { |
| 180 | let r = T::regs(); | 520 | let r = T::regs(); |
| 181 | r.enable.write(|w| w.enable().disabled()); | ||
| 182 | 521 | ||
| 183 | info!("pwm drop: done"); | 522 | self.stop(); |
| 523 | self.disable(); | ||
| 184 | 524 | ||
| 185 | // TODO: disable pins | 525 | if let Some(pin) = &self.ch0 { |
| 526 | pin.set_low(); | ||
| 527 | pin.conf().write(|w| w); | ||
| 528 | r.psel.out[0].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 529 | } | ||
| 530 | if let Some(pin) = &self.ch1 { | ||
| 531 | pin.set_low(); | ||
| 532 | pin.conf().write(|w| w); | ||
| 533 | r.psel.out[1].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 534 | } | ||
| 535 | if let Some(pin) = &self.ch2 { | ||
| 536 | pin.set_low(); | ||
| 537 | pin.conf().write(|w| w); | ||
| 538 | r.psel.out[2].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 539 | } | ||
| 540 | if let Some(pin) = &self.ch3 { | ||
| 541 | pin.set_low(); | ||
| 542 | pin.conf().write(|w| w); | ||
| 543 | r.psel.out[3].write(|w| unsafe { w.bits(0x80000000) }); | ||
| 544 | } | ||
| 186 | } | 545 | } |
| 187 | } | 546 | } |
| 188 | 547 | ||
| 189 | pub(crate) mod sealed { | 548 | pub(crate) mod sealed { |
| 190 | use super::*; | 549 | use super::*; |
| 191 | 550 | ||
| 192 | pub struct State { | ||
| 193 | pub duty: UnsafeCell<[u16; 4]>, | ||
| 194 | } | ||
| 195 | unsafe impl Sync for State {} | ||
| 196 | |||
| 197 | impl State { | ||
| 198 | pub const fn new() -> Self { | ||
| 199 | Self { | ||
| 200 | duty: UnsafeCell::new([0; 4]), | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | pub trait Instance { | 551 | pub trait Instance { |
| 206 | fn regs() -> &'static pac::pwm0::RegisterBlock; | 552 | fn regs() -> &'static pac::pwm0::RegisterBlock; |
| 207 | fn state() -> &'static State; | ||
| 208 | } | 553 | } |
| 209 | } | 554 | } |
| 210 | 555 | ||
| @@ -218,10 +563,6 @@ macro_rules! impl_pwm { | |||
| 218 | fn regs() -> &'static pac::pwm0::RegisterBlock { | 563 | fn regs() -> &'static pac::pwm0::RegisterBlock { |
| 219 | unsafe { &*pac::$pac_type::ptr() } | 564 | unsafe { &*pac::$pac_type::ptr() } |
| 220 | } | 565 | } |
| 221 | fn state() -> &'static crate::pwm::sealed::State { | ||
| 222 | static STATE: crate::pwm::sealed::State = crate::pwm::sealed::State::new(); | ||
| 223 | &STATE | ||
| 224 | } | ||
| 225 | } | 566 | } |
| 226 | impl crate::pwm::Instance for peripherals::$type { | 567 | impl crate::pwm::Instance for peripherals::$type { |
| 227 | type Interrupt = crate::interrupt::$irq; | 568 | type Interrupt = crate::interrupt::$irq; |
diff --git a/embassy-nrf/src/util.rs b/embassy-nrf/src/util.rs index 344fb01f9..2fd0bc5a8 100644 --- a/embassy-nrf/src/util.rs +++ b/embassy-nrf/src/util.rs | |||
| @@ -2,15 +2,15 @@ const SRAM_LOWER: usize = 0x2000_0000; | |||
| 2 | const SRAM_UPPER: usize = 0x3000_0000; | 2 | const SRAM_UPPER: usize = 0x3000_0000; |
| 3 | 3 | ||
| 4 | /// Does this slice reside entirely within RAM? | 4 | /// Does this slice reside entirely within RAM? |
| 5 | pub(crate) fn slice_in_ram(slice: &[u8]) -> bool { | 5 | pub(crate) fn slice_in_ram<T>(slice: &[T]) -> bool { |
| 6 | let ptr = slice.as_ptr() as usize; | 6 | let ptr = slice.as_ptr() as usize; |
| 7 | ptr >= SRAM_LOWER && (ptr + slice.len()) < SRAM_UPPER | 7 | ptr >= SRAM_LOWER && (ptr + slice.len() * core::mem::size_of::<T>()) < SRAM_UPPER |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | /// Return an error if slice is not in RAM. | 10 | /// Return an error if slice is not in RAM. |
| 11 | #[cfg(not(feature = "nrf51"))] | 11 | #[cfg(not(feature = "nrf51"))] |
| 12 | pub(crate) fn slice_in_ram_or<T>(slice: &[u8], err: T) -> Result<(), T> { | 12 | pub(crate) fn slice_in_ram_or<T, E>(slice: &[T], err: E) -> Result<(), E> { |
| 13 | if slice.len() == 0 || slice_in_ram(slice) { | 13 | if slice.is_empty() || slice_in_ram(slice) { |
| 14 | Ok(()) | 14 | Ok(()) |
| 15 | } else { | 15 | } else { |
| 16 | Err(err) | 16 | Err(err) |
