diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-05-12 04:56:11 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-05-17 00:57:37 +0200 |
| commit | f9bcf6df6b6797c35941e64d1f67ca6ede74d30a (patch) | |
| tree | 0982a72ff6f418f62094ba997edaf1276076c65d /embassy-nrf/src | |
| parent | 0310e4d458b86df31f1765104eb3aa9a6ee09bfc (diff) | |
nrf: add PWM
Diffstat (limited to 'embassy-nrf/src')
| -rw-r--r-- | embassy-nrf/src/chips/nrf52810.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf52811.rs | 5 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf52832.rs | 9 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf52833.rs | 11 | ||||
| -rw-r--r-- | embassy-nrf/src/chips/nrf52840.rs | 11 | ||||
| -rw-r--r-- | embassy-nrf/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-nrf/src/pwm.rs | 219 |
7 files changed, 262 insertions, 0 deletions
diff --git a/embassy-nrf/src/chips/nrf52810.rs b/embassy-nrf/src/chips/nrf52810.rs index 2e77a2fad..9e9f80090 100644 --- a/embassy-nrf/src/chips/nrf52810.rs +++ b/embassy-nrf/src/chips/nrf52810.rs | |||
| @@ -18,6 +18,9 @@ embassy_extras::peripherals! { | |||
| 18 | // SAADC | 18 | // SAADC |
| 19 | SAADC, | 19 | SAADC, |
| 20 | 20 | ||
| 21 | // PWM | ||
| 22 | PWM0, | ||
| 23 | |||
| 21 | // TIMER | 24 | // TIMER |
| 22 | TIMER0, | 25 | TIMER0, |
| 23 | TIMER1, | 26 | TIMER1, |
| @@ -115,6 +118,8 @@ impl_spim!(SPI0, SPIM0, SPIM0_SPIS0_SPI0); | |||
| 115 | 118 | ||
| 116 | impl_twim!(TWI0, TWIM0, TWIM0_TWIS0_TWI0); | 119 | impl_twim!(TWI0, TWIM0, TWIM0_TWIS0_TWI0); |
| 117 | 120 | ||
| 121 | impl_pwm!(PWM0, PWM0, PWM0); | ||
| 122 | |||
| 118 | impl_timer!(TIMER0, TIMER0, TIMER0); | 123 | impl_timer!(TIMER0, TIMER0, TIMER0); |
| 119 | impl_timer!(TIMER1, TIMER1, TIMER1); | 124 | impl_timer!(TIMER1, TIMER1, TIMER1); |
| 120 | impl_timer!(TIMER2, TIMER2, TIMER2); | 125 | impl_timer!(TIMER2, TIMER2, TIMER2); |
diff --git a/embassy-nrf/src/chips/nrf52811.rs b/embassy-nrf/src/chips/nrf52811.rs index b3ad5817a..a9ef0ed19 100644 --- a/embassy-nrf/src/chips/nrf52811.rs +++ b/embassy-nrf/src/chips/nrf52811.rs | |||
| @@ -18,6 +18,9 @@ embassy_extras::peripherals! { | |||
| 18 | // SAADC | 18 | // SAADC |
| 19 | SAADC, | 19 | SAADC, |
| 20 | 20 | ||
| 21 | // PWM | ||
| 22 | PWM0, | ||
| 23 | |||
| 21 | // TIMER | 24 | // TIMER |
| 22 | TIMER0, | 25 | TIMER0, |
| 23 | TIMER1, | 26 | TIMER1, |
| @@ -116,6 +119,8 @@ impl_spim!(SPI1, SPIM1, SPIM1_SPIS1_SPI1); | |||
| 116 | 119 | ||
| 117 | impl_twim!(TWISPI0, TWIM0, TWIM0_TWIS0_TWI0_SPIM0_SPIS0_SPI0); | 120 | impl_twim!(TWISPI0, TWIM0, TWIM0_TWIS0_TWI0_SPIM0_SPIS0_SPI0); |
| 118 | 121 | ||
| 122 | impl_pwm!(PWM0, PWM0, PWM0); | ||
| 123 | |||
| 119 | impl_timer!(TIMER0, TIMER0, TIMER0); | 124 | impl_timer!(TIMER0, TIMER0, TIMER0); |
| 120 | impl_timer!(TIMER1, TIMER1, TIMER1); | 125 | impl_timer!(TIMER1, TIMER1, TIMER1); |
| 121 | impl_timer!(TIMER2, TIMER2, TIMER2); | 126 | impl_timer!(TIMER2, TIMER2, TIMER2); |
diff --git a/embassy-nrf/src/chips/nrf52832.rs b/embassy-nrf/src/chips/nrf52832.rs index ed94430e4..4e57f2fa4 100644 --- a/embassy-nrf/src/chips/nrf52832.rs +++ b/embassy-nrf/src/chips/nrf52832.rs | |||
| @@ -21,6 +21,11 @@ embassy_extras::peripherals! { | |||
| 21 | // SAADC | 21 | // SAADC |
| 22 | SAADC, | 22 | SAADC, |
| 23 | 23 | ||
| 24 | // PWM | ||
| 25 | PWM0, | ||
| 26 | PWM1, | ||
| 27 | PWM2, | ||
| 28 | |||
| 24 | // TIMER | 29 | // TIMER |
| 25 | TIMER0, | 30 | TIMER0, |
| 26 | TIMER1, | 31 | TIMER1, |
| @@ -123,6 +128,10 @@ impl_spim!(SPI2, SPIM2, SPIM2_SPIS2_SPI2); | |||
| 123 | impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | 128 | impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); |
| 124 | impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); | 129 | impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); |
| 125 | 130 | ||
| 131 | impl_pwm!(PWM0, PWM0, PWM0); | ||
| 132 | impl_pwm!(PWM1, PWM1, PWM1); | ||
| 133 | impl_pwm!(PWM2, PWM2, PWM2); | ||
| 134 | |||
| 126 | impl_timer!(TIMER0, TIMER0, TIMER0); | 135 | impl_timer!(TIMER0, TIMER0, TIMER0); |
| 127 | impl_timer!(TIMER1, TIMER1, TIMER1); | 136 | impl_timer!(TIMER1, TIMER1, TIMER1); |
| 128 | impl_timer!(TIMER2, TIMER2, TIMER2); | 137 | impl_timer!(TIMER2, TIMER2, TIMER2); |
diff --git a/embassy-nrf/src/chips/nrf52833.rs b/embassy-nrf/src/chips/nrf52833.rs index 080157d69..30b0ab007 100644 --- a/embassy-nrf/src/chips/nrf52833.rs +++ b/embassy-nrf/src/chips/nrf52833.rs | |||
| @@ -22,6 +22,12 @@ embassy_extras::peripherals! { | |||
| 22 | // SAADC | 22 | // SAADC |
| 23 | SAADC, | 23 | SAADC, |
| 24 | 24 | ||
| 25 | // PWM | ||
| 26 | PWM0, | ||
| 27 | PWM1, | ||
| 28 | PWM2, | ||
| 29 | PWM3, | ||
| 30 | |||
| 25 | // TIMER | 31 | // TIMER |
| 26 | TIMER0, | 32 | TIMER0, |
| 27 | TIMER1, | 33 | TIMER1, |
| @@ -144,6 +150,11 @@ impl_spim!(SPI3, SPIM3, SPIM3); | |||
| 144 | impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | 150 | impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); |
| 145 | impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); | 151 | impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); |
| 146 | 152 | ||
| 153 | impl_pwm!(PWM0, PWM0, PWM0); | ||
| 154 | impl_pwm!(PWM1, PWM1, PWM1); | ||
| 155 | impl_pwm!(PWM2, PWM2, PWM2); | ||
| 156 | impl_pwm!(PWM3, PWM3, PWM3); | ||
| 157 | |||
| 147 | impl_timer!(TIMER0, TIMER0, TIMER0); | 158 | impl_timer!(TIMER0, TIMER0, TIMER0); |
| 148 | impl_timer!(TIMER1, TIMER1, TIMER1); | 159 | impl_timer!(TIMER1, TIMER1, TIMER1); |
| 149 | impl_timer!(TIMER2, TIMER2, TIMER2); | 160 | impl_timer!(TIMER2, TIMER2, TIMER2); |
diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs index 06b508d89..0b6c3f68e 100644 --- a/embassy-nrf/src/chips/nrf52840.rs +++ b/embassy-nrf/src/chips/nrf52840.rs | |||
| @@ -25,6 +25,12 @@ embassy_extras::peripherals! { | |||
| 25 | // SAADC | 25 | // SAADC |
| 26 | SAADC, | 26 | SAADC, |
| 27 | 27 | ||
| 28 | // PWM | ||
| 29 | PWM0, | ||
| 30 | PWM1, | ||
| 31 | PWM2, | ||
| 32 | PWM3, | ||
| 33 | |||
| 28 | // TIMER | 34 | // TIMER |
| 29 | TIMER0, | 35 | TIMER0, |
| 30 | TIMER1, | 36 | TIMER1, |
| @@ -147,6 +153,11 @@ impl_spim!(SPI3, SPIM3, SPIM3); | |||
| 147 | impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); | 153 | impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); |
| 148 | impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); | 154 | impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); |
| 149 | 155 | ||
| 156 | impl_pwm!(PWM0, PWM0, PWM0); | ||
| 157 | impl_pwm!(PWM1, PWM1, PWM1); | ||
| 158 | impl_pwm!(PWM2, PWM2, PWM2); | ||
| 159 | impl_pwm!(PWM3, PWM3, PWM3); | ||
| 160 | |||
| 150 | impl_timer!(TIMER0, TIMER0, TIMER0); | 161 | impl_timer!(TIMER0, TIMER0, TIMER0); |
| 151 | impl_timer!(TIMER1, TIMER1, TIMER1); | 162 | impl_timer!(TIMER1, TIMER1, TIMER1); |
| 152 | impl_timer!(TIMER2, TIMER2, TIMER2); | 163 | impl_timer!(TIMER2, TIMER2, TIMER2); |
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs index 6ab9cbb27..1c496be19 100644 --- a/embassy-nrf/src/lib.rs +++ b/embassy-nrf/src/lib.rs | |||
| @@ -29,6 +29,8 @@ pub mod buffered_uarte; | |||
| 29 | pub mod gpio; | 29 | pub mod gpio; |
| 30 | pub mod gpiote; | 30 | pub mod gpiote; |
| 31 | pub mod ppi; | 31 | pub mod ppi; |
| 32 | #[cfg(not(any(feature = "nrf52805", feature = "nrf52820")))] | ||
| 33 | pub mod pwm; | ||
| 32 | #[cfg(feature = "nrf52840")] | 34 | #[cfg(feature = "nrf52840")] |
| 33 | pub mod qspi; | 35 | pub mod qspi; |
| 34 | pub mod rtc; | 36 | pub mod rtc; |
diff --git a/embassy-nrf/src/pwm.rs b/embassy-nrf/src/pwm.rs new file mode 100644 index 000000000..b79c4bc37 --- /dev/null +++ b/embassy-nrf/src/pwm.rs | |||
| @@ -0,0 +1,219 @@ | |||
| 1 | #![macro_use] | ||
| 2 | |||
| 3 | use core::cell::UnsafeCell; | ||
| 4 | use core::marker::PhantomData; | ||
| 5 | use core::sync::atomic::{compiler_fence, Ordering}; | ||
| 6 | use embassy::util::Unborrow; | ||
| 7 | use embassy_extras::unborrow; | ||
| 8 | |||
| 9 | use crate::fmt::{assert, panic, unreachable, *}; | ||
| 10 | use crate::gpio::sealed::Pin as _; | ||
| 11 | use crate::gpio::OptionalPin as GpioOptionalPin; | ||
| 12 | use crate::interrupt::Interrupt; | ||
| 13 | use crate::pac; | ||
| 14 | |||
| 15 | #[derive(Debug, Eq, PartialEq, Clone, Copy)] | ||
| 16 | pub enum Prescaler { | ||
| 17 | Div1, | ||
| 18 | Div2, | ||
| 19 | Div4, | ||
| 20 | Div8, | ||
| 21 | Div16, | ||
| 22 | Div32, | ||
| 23 | Div64, | ||
| 24 | Div128, | ||
| 25 | } | ||
| 26 | |||
| 27 | /// Interface to the UARTE peripheral | ||
| 28 | pub struct Pwm<'d, T: Instance> { | ||
| 29 | peri: T, | ||
| 30 | phantom: PhantomData<&'d mut T>, | ||
| 31 | } | ||
| 32 | |||
| 33 | impl<'d, T: Instance> Pwm<'d, T> { | ||
| 34 | /// Creates the interface to a UARTE instance. | ||
| 35 | /// Sets the baud rate, parity and assigns the pins to the UARTE peripheral. | ||
| 36 | /// | ||
| 37 | /// # Safety | ||
| 38 | /// | ||
| 39 | /// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms) | ||
| 40 | /// on stack allocated buffers which which have been passed to [`send()`](Pwm::send) | ||
| 41 | /// or [`receive`](Pwm::receive). | ||
| 42 | #[allow(unused_unsafe)] | ||
| 43 | pub fn new( | ||
| 44 | pwm: impl Unborrow<Target = T> + 'd, | ||
| 45 | ch0: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 46 | ch1: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 47 | ch2: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 48 | ch3: impl Unborrow<Target = impl GpioOptionalPin> + 'd, | ||
| 49 | ) -> Self { | ||
| 50 | unborrow!(pwm, ch0, ch1, ch2, ch3); | ||
| 51 | |||
| 52 | let r = T::regs(); | ||
| 53 | let s = T::state(); | ||
| 54 | |||
| 55 | if let Some(pin) = ch0.pin_mut() { | ||
| 56 | pin.set_high(); | ||
| 57 | pin.conf().write(|w| w.dir().output()); | ||
| 58 | } | ||
| 59 | if let Some(pin) = ch1.pin_mut() { | ||
| 60 | pin.set_high(); | ||
| 61 | pin.conf().write(|w| w.dir().output()); | ||
| 62 | } | ||
| 63 | if let Some(pin) = ch2.pin_mut() { | ||
| 64 | pin.set_high(); | ||
| 65 | pin.conf().write(|w| w.dir().output()); | ||
| 66 | } | ||
| 67 | if let Some(pin) = ch3.pin_mut() { | ||
| 68 | pin.set_high(); | ||
| 69 | pin.conf().write(|w| w.dir().output()); | ||
| 70 | } | ||
| 71 | r.psel.out[0].write(|w| unsafe { w.bits(ch0.psel_bits()) }); | ||
| 72 | r.psel.out[1].write(|w| unsafe { w.bits(ch1.psel_bits()) }); | ||
| 73 | r.psel.out[2].write(|w| unsafe { w.bits(ch2.psel_bits()) }); | ||
| 74 | r.psel.out[3].write(|w| unsafe { w.bits(ch3.psel_bits()) }); | ||
| 75 | |||
| 76 | // Disable all interrupts | ||
| 77 | r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) }); | ||
| 78 | |||
| 79 | // Enable | ||
| 80 | r.enable.write(|w| w.enable().enabled()); | ||
| 81 | |||
| 82 | r.seq0 | ||
| 83 | .ptr | ||
| 84 | .write(|w| unsafe { w.bits(&s.duty as *const _ as u32) }); | ||
| 85 | r.seq0.cnt.write(|w| unsafe { w.bits(4) }); | ||
| 86 | r.seq0.refresh.write(|w| unsafe { w.bits(32) }); | ||
| 87 | r.seq0.enddelay.write(|w| unsafe { w.bits(0) }); | ||
| 88 | |||
| 89 | r.decoder.write(|w| { | ||
| 90 | w.load().individual(); | ||
| 91 | w.mode().refresh_count() | ||
| 92 | }); | ||
| 93 | r.mode.write(|w| w.updown().up()); | ||
| 94 | r.prescaler.write(|w| w.prescaler().div_1()); | ||
| 95 | r.countertop | ||
| 96 | .write(|w| unsafe { w.countertop().bits(32767) }); | ||
| 97 | r.loop_.write(|w| w.cnt().disabled()); | ||
| 98 | |||
| 99 | Self { | ||
| 100 | peri: pwm, | ||
| 101 | phantom: PhantomData, | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | /// Sets duty cycle (15 bit) for a PWM channel. | ||
| 106 | pub fn set_duty(&self, channel: usize, duty: u16) { | ||
| 107 | let s = T::state(); | ||
| 108 | unsafe { (*s.duty.get())[channel] = duty & 0x7FFF }; | ||
| 109 | |||
| 110 | compiler_fence(Ordering::SeqCst); | ||
| 111 | T::regs().tasks_seqstart[0].write(|w| unsafe { w.bits(1) }); | ||
| 112 | } | ||
| 113 | |||
| 114 | /// Sets the PWM clock prescaler. | ||
| 115 | #[inline(always)] | ||
| 116 | pub fn set_prescaler(&self, div: Prescaler) { | ||
| 117 | T::regs().prescaler.write(|w| w.prescaler().bits(div as u8)); | ||
| 118 | } | ||
| 119 | |||
| 120 | /// Sets the PWM clock prescaler. | ||
| 121 | #[inline(always)] | ||
| 122 | pub fn prescaler(&self) -> Prescaler { | ||
| 123 | match T::regs().prescaler.read().prescaler().bits() { | ||
| 124 | 0 => Prescaler::Div1, | ||
| 125 | 1 => Prescaler::Div2, | ||
| 126 | 2 => Prescaler::Div4, | ||
| 127 | 3 => Prescaler::Div8, | ||
| 128 | 4 => Prescaler::Div16, | ||
| 129 | 5 => Prescaler::Div32, | ||
| 130 | 6 => Prescaler::Div64, | ||
| 131 | 7 => Prescaler::Div128, | ||
| 132 | _ => unreachable!(), | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | /// Sets the maximum duty cycle value. | ||
| 137 | #[inline(always)] | ||
| 138 | pub fn set_max_duty(&self, duty: u16) { | ||
| 139 | T::regs() | ||
| 140 | .countertop | ||
| 141 | .write(|w| unsafe { w.countertop().bits(duty.min(32767u16)) }); | ||
| 142 | } | ||
| 143 | |||
| 144 | /// Returns the maximum duty cycle value. | ||
| 145 | #[inline(always)] | ||
| 146 | pub fn max_duty(&self) -> u16 { | ||
| 147 | T::regs().countertop.read().countertop().bits() | ||
| 148 | } | ||
| 149 | |||
| 150 | /// Sets the PWM output frequency. | ||
| 151 | #[inline(always)] | ||
| 152 | pub fn set_period(&self, freq: u32) { | ||
| 153 | let clk = 16_000_000u32 >> (self.prescaler() as u8); | ||
| 154 | let duty = clk / freq; | ||
| 155 | self.set_max_duty(duty.min(32767) as u16); | ||
| 156 | } | ||
| 157 | |||
| 158 | /// Returns the PWM output frequency. | ||
| 159 | #[inline(always)] | ||
| 160 | pub fn period(&self) -> u32 { | ||
| 161 | let clk = 16_000_000u32 >> (self.prescaler() as u8); | ||
| 162 | let max_duty = self.max_duty() as u32; | ||
| 163 | clk / max_duty | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | impl<'a, T: Instance> Drop for Pwm<'a, T> { | ||
| 168 | fn drop(&mut self) { | ||
| 169 | let r = T::regs(); | ||
| 170 | r.enable.write(|w| w.enable().disabled()); | ||
| 171 | |||
| 172 | info!("pwm drop: done"); | ||
| 173 | |||
| 174 | // TODO: disable pins | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | pub(crate) mod sealed { | ||
| 179 | use super::*; | ||
| 180 | |||
| 181 | pub struct State { | ||
| 182 | pub duty: UnsafeCell<[u16; 4]>, | ||
| 183 | } | ||
| 184 | unsafe impl Sync for State {} | ||
| 185 | |||
| 186 | impl State { | ||
| 187 | pub const fn new() -> Self { | ||
| 188 | Self { | ||
| 189 | duty: UnsafeCell::new([0; 4]), | ||
| 190 | } | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | pub trait Instance { | ||
| 195 | fn regs() -> &'static pac::pwm0::RegisterBlock; | ||
| 196 | fn state() -> &'static State; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | pub trait Instance: sealed::Instance + 'static { | ||
| 201 | type Interrupt: Interrupt; | ||
| 202 | } | ||
| 203 | |||
| 204 | macro_rules! impl_pwm { | ||
| 205 | ($type:ident, $pac_type:ident, $irq:ident) => { | ||
| 206 | impl crate::pwm::sealed::Instance for peripherals::$type { | ||
| 207 | fn regs() -> &'static pac::pwm0::RegisterBlock { | ||
| 208 | unsafe { &*pac::$pac_type::ptr() } | ||
| 209 | } | ||
| 210 | fn state() -> &'static crate::pwm::sealed::State { | ||
| 211 | static STATE: crate::pwm::sealed::State = crate::pwm::sealed::State::new(); | ||
| 212 | &STATE | ||
| 213 | } | ||
| 214 | } | ||
| 215 | impl crate::pwm::Instance for peripherals::$type { | ||
| 216 | type Interrupt = crate::interrupt::$irq; | ||
| 217 | } | ||
| 218 | }; | ||
| 219 | } | ||
