Skip to content

Commit 39c0b8f

Browse files
bors[bot]DirbaioJonathan Pallant (42 Technology)
authored
Merge #318 #319
318: Enable PWM for nrf52832 r=jonathanpallant a=Dirbaio It compiles cleanly, except for the PAC missing fields for the tasks registers. Replaced those with `.bits(1)`, which works on all chips. Untested on hardware as I don't have a nrf52832 at hand. 319: Apply the UARTE workaround for nrf91 and nrf53 r=Dirbaio a=jonathanpallant See ttps://github.com/NordicSemiconductor/nrfx/blob/master/drivers/src/nrfx_uarte.c#L197 Co-authored-by: Dario Nieuwenhuis <[email protected]> Co-authored-by: Jonathan Pallant (42 Technology) <[email protected]>
3 parents ed5d87d + d044dad + 60a3c09 commit 39c0b8f

File tree

5 files changed

+172
-69
lines changed

5 files changed

+172
-69
lines changed

examples/pwm-blinky-demo/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ features = ["rt"]
2121
path = "../../nrf9160-hal"
2222
optional = true
2323

24+
[dependencies.nrf52832-hal]
25+
features = ["rt"]
26+
path = "../../nrf52832-hal"
27+
optional = true
28+
2429
[dependencies.nrf52840-hal]
2530
features = ["rt"]
2631
path = "../../nrf52840-hal"
@@ -29,3 +34,4 @@ optional = true
2934
[features]
3035
9160 = ["nrf9160-hal"]
3136
52840 = ["nrf52840-hal"]
37+
52832 = ["nrf52832-hal"]

examples/pwm-blinky-demo/src/main.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use hal::{gpio, prelude::*, pwm, pwm::Pwm, timer, timer::Timer};
55
use nb::block;
66
#[cfg(feature = "52840")]
77
use nrf52840_hal as hal;
8+
#[cfg(feature = "52832")]
9+
use nrf52832_hal as hal;
810
#[cfg(feature = "9160")]
911
use nrf9160_hal as hal;
1012
use rtt_target::{rprintln, rtt_init_print};
@@ -27,11 +29,13 @@ fn main() -> ! {
2729
pwm.set_period(500u32.hz());
2830

2931
rprintln!("PWM Blinky demo starting");
32+
33+
let wait_time = 1_000_000u32 / pwm.get_max_duty() as u32;
3034
loop {
31-
pwm.set_duty_on_common(pwm.get_max_duty());
32-
delay(&mut timer, 250_000); // 250ms
33-
pwm.set_duty_on_common(0);
34-
delay(&mut timer, 1_000_000); // 1s
35+
for duty in 0..pwm.get_max_duty() {
36+
pwm.set_duty_on_common(duty);
37+
delay(&mut timer, wait_time);
38+
}
3539
}
3640
}
3741

@@ -65,6 +69,22 @@ fn init_device(p: hal::pac::Peripherals) -> (Pwm<hal::pac::PWM0>, Timer<hal::pac
6569
(pwm, timer)
6670
}
6771

72+
#[cfg(feature = "52832")]
73+
fn init_device(p: hal::pac::Peripherals) -> (Pwm<hal::pac::PWM0>, Timer<hal::pac::TIMER0>) {
74+
let p0 = gpio::p0::Parts::new(p.P0);
75+
76+
let pwm = Pwm::new(p.PWM0);
77+
pwm.set_output_pin(
78+
pwm::Channel::C0,
79+
&p0.p0_30.into_push_pull_output(gpio::Level::High).degrade(),
80+
);
81+
82+
let timer = Timer::new(p.TIMER0);
83+
84+
(pwm, timer)
85+
}
86+
87+
6888
fn delay<T>(timer: &mut Timer<T>, cycles: u32)
6989
where
7090
T: timer::Instance,

nrf-hal-common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub mod ieee802154;
4949
pub mod lpcomp;
5050
#[cfg(not(feature = "9160"))]
5151
pub mod ppi;
52-
#[cfg(not(any(feature = "51", feature = "52832")))]
52+
#[cfg(not(feature = "51"))]
5353
pub mod pwm;
5454
#[cfg(not(any(feature = "51", feature = "9160")))]
5555
pub mod qdec;

nrf-hal-common/src/pwm.rs

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,9 @@
33
//! The pulse with modulation (PWM) module enables the generation of pulse width modulated signals on GPIO.
44
55
#[cfg(not(any(feature = "9160")))]
6-
use crate::pac::{
7-
pwm0::*, PWM0,
8-
};
6+
use crate::pac::pwm0::*;
97
#[cfg(any(feature = "9160"))]
10-
use crate::pac::{
11-
pwm0_ns::*, PWM0_NS, PWM1_NS, PWM2_NS, PWM3_NS,
12-
};
13-
#[cfg(not(any(feature = "52810", feature = "52811", feature = "9160")))]
14-
use crate::pac::{
15-
PWM1, PWM2, PWM3,
16-
};
8+
use crate::pac::pwm0_ns::*;
179
use crate::{
1810
gpio::{Output, Pin, PushPull},
1911
pac::{generic::Reg, Interrupt},
@@ -379,9 +371,12 @@ where
379371
T::buffer().set(buffer);
380372
self.one_shot();
381373
self.set_load_mode(LoadMode::Individual);
382-
self.pwm.seq0.ptr.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
374+
self.pwm
375+
.seq0
376+
.ptr
377+
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
383378
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(4) });
384-
self.start_seq(Seq::Seq0);
379+
self.start_seq(Seq::Seq0);
385380
}
386381

387382
/// Sets inverted duty cycle (15 bit) for a PWM channel.
@@ -392,9 +387,12 @@ where
392387
T::buffer().set(buffer);
393388
self.one_shot();
394389
self.set_load_mode(LoadMode::Individual);
395-
self.pwm.seq0.ptr.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
390+
self.pwm
391+
.seq0
392+
.ptr
393+
.write(|w| unsafe { w.bits(T::buffer().as_ptr() as u32) });
396394
self.pwm.seq0.cnt.write(|w| unsafe { w.bits(4) });
397-
self.start_seq(Seq::Seq0);
395+
self.start_seq(Seq::Seq0);
398396
}
399397

400398
/// Returns the duty cycle value for a PWM channel.
@@ -470,7 +468,7 @@ where
470468
pub fn start_seq(&self, seq: Seq) {
471469
compiler_fence(Ordering::SeqCst);
472470
self.pwm.enable.write(|w| w.enable().enabled());
473-
self.pwm.tasks_seqstart[usize::from(seq)].write(|w| w.tasks_seqstart().set_bit());
471+
self.pwm.tasks_seqstart[usize::from(seq)].write(|w| unsafe { w.bits(1) });
474472
while self.pwm.events_seqstarted[usize::from(seq)].read().bits() == 0 {}
475473
self.pwm.events_seqend[0].write(|w| w);
476474
self.pwm.events_seqend[1].write(|w| w);
@@ -480,16 +478,14 @@ where
480478
/// Does not cause PWM generation to start if not running.
481479
#[inline(always)]
482480
pub fn next_step(&self) {
483-
self.pwm
484-
.tasks_nextstep
485-
.write(|w| w.tasks_nextstep().set_bit());
481+
self.pwm.tasks_nextstep.write(|w| unsafe { w.bits(1) });
486482
}
487483

488484
/// Stops PWM pulse generation on all channels at the end of current PWM period, and stops sequence playback.
489485
#[inline(always)]
490486
pub fn stop(&self) {
491487
compiler_fence(Ordering::SeqCst);
492-
self.pwm.tasks_stop.write(|w| w.tasks_stop().set_bit());
488+
self.pwm.tasks_stop.write(|w| unsafe { w.bits(1) });
493489
while self.pwm.events_stopped.read().bits() == 0 {}
494490
}
495491

@@ -1082,94 +1078,104 @@ static mut BUF2: Cell<[u16; 4]> = Cell::new([0; 4]);
10821078
static mut BUF3: Cell<[u16; 4]> = Cell::new([0; 4]);
10831079

10841080
#[cfg(not(any(feature = "9160")))]
1085-
impl Instance for PWM0 {
1081+
impl Instance for crate::pac::PWM0 {
10861082
const INTERRUPT: Interrupt = Interrupt::PWM0;
10871083
#[inline(always)]
10881084
fn buffer() -> &'static Cell<[u16; 4]> {
1089-
unsafe { &BUF0 }
1085+
unsafe { &BUF0 }
10901086
}
10911087
}
10921088

10931089
#[cfg(not(any(feature = "52810", feature = "52811", feature = "9160")))]
1094-
impl Instance for PWM1 {
1090+
impl Instance for crate::pac::PWM1 {
10951091
const INTERRUPT: Interrupt = Interrupt::PWM1;
10961092
fn buffer() -> &'static Cell<[u16; 4]> {
1097-
unsafe { &BUF1 }
1093+
unsafe { &BUF1 }
10981094
}
10991095
}
11001096

11011097
#[cfg(not(any(feature = "52810", feature = "52811", feature = "9160")))]
1102-
impl Instance for PWM2 {
1098+
impl Instance for crate::pac::PWM2 {
11031099
const INTERRUPT: Interrupt = Interrupt::PWM2;
11041100
fn buffer() -> &'static Cell<[u16; 4]> {
1105-
unsafe { &BUF2 }
1101+
unsafe { &BUF2 }
11061102
}
11071103
}
11081104

1109-
#[cfg(not(any(feature = "52810", feature = "52811", feature = "52832", feature = "9160")))]
1110-
impl Instance for PWM3 {
1105+
#[cfg(not(any(
1106+
feature = "52810",
1107+
feature = "52811",
1108+
feature = "52832",
1109+
feature = "9160"
1110+
)))]
1111+
impl Instance for crate::pac::PWM3 {
11111112
const INTERRUPT: Interrupt = Interrupt::PWM3;
11121113
fn buffer() -> &'static Cell<[u16; 4]> {
1113-
unsafe { &BUF3 }
1114+
unsafe { &BUF3 }
11141115
}
11151116
}
11161117

11171118
#[cfg(any(feature = "9160"))]
1118-
impl Instance for PWM0_NS {
1119+
impl Instance for crate::pac::PWM0_NS {
11191120
const INTERRUPT: Interrupt = Interrupt::PWM0;
11201121
#[inline(always)]
11211122
fn buffer() -> &'static Cell<[u16; 4]> {
1122-
unsafe { &BUF0 }
1123+
unsafe { &BUF0 }
11231124
}
11241125
}
11251126

11261127
#[cfg(any(feature = "9160"))]
1127-
impl Instance for PWM1_NS {
1128+
impl Instance for crate::pac::PWM1_NS {
11281129
const INTERRUPT: Interrupt = Interrupt::PWM1;
11291130
fn buffer() -> &'static Cell<[u16; 4]> {
1130-
unsafe { &BUF1 }
1131+
unsafe { &BUF1 }
11311132
}
11321133
}
11331134

11341135
#[cfg(any(feature = "9160"))]
1135-
impl Instance for PWM2_NS {
1136+
impl Instance for crate::pac::PWM2_NS {
11361137
const INTERRUPT: Interrupt = Interrupt::PWM2;
11371138
fn buffer() -> &'static Cell<[u16; 4]> {
1138-
unsafe { &BUF2 }
1139+
unsafe { &BUF2 }
11391140
}
11401141
}
11411142

11421143
#[cfg(any(feature = "9160"))]
1143-
impl Instance for PWM3_NS {
1144+
impl Instance for crate::pac::PWM3_NS {
11441145
const INTERRUPT: Interrupt = Interrupt::PWM3;
11451146
fn buffer() -> &'static Cell<[u16; 4]> {
1146-
unsafe { &BUF3 }
1147+
unsafe { &BUF3 }
11471148
}
11481149
}
11491150
mod sealed {
11501151
pub trait Sealed {}
11511152

11521153
#[cfg(not(any(feature = "9160")))]
1153-
impl Sealed for crate::pwm::PWM0 {}
1154+
impl Sealed for crate::pac::PWM0 {}
11541155

11551156
#[cfg(not(any(feature = "52810", feature = "52811", feature = "9160")))]
1156-
impl Sealed for crate::pwm::PWM1 {}
1157+
impl Sealed for crate::pac::PWM1 {}
11571158

11581159
#[cfg(not(any(feature = "52810", feature = "52811", feature = "9160")))]
1159-
impl Sealed for crate::pwm::PWM2 {}
1160+
impl Sealed for crate::pac::PWM2 {}
11601161

1161-
#[cfg(not(any(feature = "52810", feature = "52811", feature = "52832", feature = "9160")))]
1162-
impl Sealed for crate::pwm::PWM3 {}
1162+
#[cfg(not(any(
1163+
feature = "52810",
1164+
feature = "52811",
1165+
feature = "52832",
1166+
feature = "9160"
1167+
)))]
1168+
impl Sealed for crate::pac::PWM3 {}
11631169

11641170
#[cfg(any(feature = "9160"))]
1165-
impl Sealed for crate::pwm::PWM0_NS {}
1171+
impl Sealed for crate::pac::PWM0_NS {}
11661172

11671173
#[cfg(any(feature = "9160"))]
1168-
impl Sealed for crate::pwm::PWM1_NS {}
1174+
impl Sealed for crate::pac::PWM1_NS {}
11691175

11701176
#[cfg(any(feature = "9160"))]
1171-
impl Sealed for crate::pwm::PWM2_NS {}
1177+
impl Sealed for crate::pac::PWM2_NS {}
11721178

11731179
#[cfg(any(feature = "9160"))]
1174-
impl Sealed for crate::pwm::PWM3_NS {}
1180+
impl Sealed for crate::pac::PWM3_NS {}
11751181
}

0 commit comments

Comments
 (0)