aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-nrf-examples/src/bin/pwm.rs104
-rw-r--r--embassy-nrf/src/chips/nrf52810.rs5
-rw-r--r--embassy-nrf/src/chips/nrf52811.rs5
-rw-r--r--embassy-nrf/src/chips/nrf52832.rs9
-rw-r--r--embassy-nrf/src/chips/nrf52833.rs11
-rw-r--r--embassy-nrf/src/chips/nrf52840.rs11
-rw-r--r--embassy-nrf/src/lib.rs2
-rw-r--r--embassy-nrf/src/pwm.rs219
8 files changed, 366 insertions, 0 deletions
diff --git a/embassy-nrf-examples/src/bin/pwm.rs b/embassy-nrf-examples/src/bin/pwm.rs
new file mode 100644
index 000000000..6efcf22d2
--- /dev/null
+++ b/embassy-nrf-examples/src/bin/pwm.rs
@@ -0,0 +1,104 @@
1#![no_std]
2#![no_main]
3#![feature(min_type_alias_impl_trait)]
4#![feature(impl_trait_in_bindings)]
5#![feature(type_alias_impl_trait)]
6#![allow(incomplete_features)]
7
8#[path = "../example_common.rs"]
9mod example_common;
10use defmt::{panic, *};
11use embassy::executor::Spawner;
12use embassy::time::{Duration, Timer};
13use embassy_nrf::pwm::{Prescaler, Pwm};
14use embassy_nrf::{interrupt, Peripherals};
15
16// for i in range(1024): print(int((math.sin(i/512*math.pi)*0.4+0.5)**2*32767), ', ', end='')
17static DUTY: [u16; 1024] = [
18 8191, 8272, 8353, 8434, 8516, 8598, 8681, 8764, 8847, 8931, 9015, 9099, 9184, 9269, 9354, 9440,
19 9526, 9613, 9700, 9787, 9874, 9962, 10050, 10139, 10227, 10316, 10406, 10495, 10585, 10675,
20 10766, 10857, 10948, 11039, 11131, 11223, 11315, 11407, 11500, 11592, 11685, 11779, 11872,
21 11966, 12060, 12154, 12248, 12343, 12438, 12533, 12628, 12723, 12818, 12914, 13010, 13106,
22 13202, 13298, 13394, 13491, 13587, 13684, 13781, 13878, 13975, 14072, 14169, 14266, 14364,
23 14461, 14558, 14656, 14754, 14851, 14949, 15046, 15144, 15242, 15339, 15437, 15535, 15632,
24 15730, 15828, 15925, 16023, 16120, 16218, 16315, 16412, 16510, 16607, 16704, 16801, 16898,
25 16995, 17091, 17188, 17284, 17380, 17477, 17572, 17668, 17764, 17859, 17955, 18050, 18145,
26 18239, 18334, 18428, 18522, 18616, 18710, 18803, 18896, 18989, 19082, 19174, 19266, 19358,
27 19449, 19540, 19631, 19722, 19812, 19902, 19991, 20081, 20169, 20258, 20346, 20434, 20521,
28 20608, 20695, 20781, 20867, 20952, 21037, 21122, 21206, 21290, 21373, 21456, 21538, 21620,
29 21701, 21782, 21863, 21943, 22022, 22101, 22179, 22257, 22335, 22412, 22488, 22564, 22639,
30 22714, 22788, 22861, 22934, 23007, 23079, 23150, 23220, 23290, 23360, 23429, 23497, 23564,
31 23631, 23698, 23763, 23828, 23892, 23956, 24019, 24081, 24143, 24204, 24264, 24324, 24383,
32 24441, 24499, 24555, 24611, 24667, 24721, 24775, 24828, 24881, 24933, 24983, 25034, 25083,
33 25132, 25180, 25227, 25273, 25319, 25363, 25407, 25451, 25493, 25535, 25575, 25615, 25655,
34 25693, 25731, 25767, 25803, 25838, 25873, 25906, 25939, 25971, 26002, 26032, 26061, 26089,
35 26117, 26144, 26170, 26195, 26219, 26242, 26264, 26286, 26307, 26327, 26346, 26364, 26381,
36 26397, 26413, 26427, 26441, 26454, 26466, 26477, 26487, 26496, 26505, 26512, 26519, 26525,
37 26530, 26534, 26537, 26539, 26540, 26541, 26540, 26539, 26537, 26534, 26530, 26525, 26519,
38 26512, 26505, 26496, 26487, 26477, 26466, 26454, 26441, 26427, 26413, 26397, 26381, 26364,
39 26346, 26327, 26307, 26286, 26264, 26242, 26219, 26195, 26170, 26144, 26117, 26089, 26061,
40 26032, 26002, 25971, 25939, 25906, 25873, 25838, 25803, 25767, 25731, 25693, 25655, 25615,
41 25575, 25535, 25493, 25451, 25407, 25363, 25319, 25273, 25227, 25180, 25132, 25083, 25034,
42 24983, 24933, 24881, 24828, 24775, 24721, 24667, 24611, 24555, 24499, 24441, 24383, 24324,
43 24264, 24204, 24143, 24081, 24019, 23956, 23892, 23828, 23763, 23698, 23631, 23564, 23497,
44 23429, 23360, 23290, 23220, 23150, 23079, 23007, 22934, 22861, 22788, 22714, 22639, 22564,
45 22488, 22412, 22335, 22257, 22179, 22101, 22022, 21943, 21863, 21782, 21701, 21620, 21538,
46 21456, 21373, 21290, 21206, 21122, 21037, 20952, 20867, 20781, 20695, 20608, 20521, 20434,
47 20346, 20258, 20169, 20081, 19991, 19902, 19812, 19722, 19631, 19540, 19449, 19358, 19266,
48 19174, 19082, 18989, 18896, 18803, 18710, 18616, 18522, 18428, 18334, 18239, 18145, 18050,
49 17955, 17859, 17764, 17668, 17572, 17477, 17380, 17284, 17188, 17091, 16995, 16898, 16801,
50 16704, 16607, 16510, 16412, 16315, 16218, 16120, 16023, 15925, 15828, 15730, 15632, 15535,
51 15437, 15339, 15242, 15144, 15046, 14949, 14851, 14754, 14656, 14558, 14461, 14364, 14266,
52 14169, 14072, 13975, 13878, 13781, 13684, 13587, 13491, 13394, 13298, 13202, 13106, 13010,
53 12914, 12818, 12723, 12628, 12533, 12438, 12343, 12248, 12154, 12060, 11966, 11872, 11779,
54 11685, 11592, 11500, 11407, 11315, 11223, 11131, 11039, 10948, 10857, 10766, 10675, 10585,
55 10495, 10406, 10316, 10227, 10139, 10050, 9962, 9874, 9787, 9700, 9613, 9526, 9440, 9354, 9269,
56 9184, 9099, 9015, 8931, 8847, 8764, 8681, 8598, 8516, 8434, 8353, 8272, 8191, 8111, 8031, 7952,
57 7873, 7794, 7716, 7638, 7561, 7484, 7407, 7331, 7255, 7180, 7105, 7031, 6957, 6883, 6810, 6738,
58 6665, 6594, 6522, 6451, 6381, 6311, 6241, 6172, 6104, 6036, 5968, 5901, 5834, 5767, 5702, 5636,
59 5571, 5507, 5443, 5379, 5316, 5253, 5191, 5130, 5068, 5008, 4947, 4888, 4828, 4769, 4711, 4653,
60 4596, 4539, 4482, 4426, 4371, 4316, 4261, 4207, 4153, 4100, 4047, 3995, 3943, 3892, 3841, 3791,
61 3741, 3691, 3642, 3594, 3546, 3498, 3451, 3404, 3358, 3312, 3267, 3222, 3178, 3134, 3090, 3047,
62 3005, 2962, 2921, 2879, 2839, 2798, 2758, 2719, 2680, 2641, 2603, 2565, 2528, 2491, 2454, 2418,
63 2382, 2347, 2312, 2278, 2244, 2210, 2177, 2144, 2112, 2080, 2048, 2017, 1986, 1956, 1926, 1896,
64 1867, 1838, 1810, 1781, 1754, 1726, 1699, 1673, 1646, 1620, 1595, 1570, 1545, 1520, 1496, 1472,
65 1449, 1426, 1403, 1380, 1358, 1336, 1315, 1294, 1273, 1252, 1232, 1212, 1192, 1173, 1154, 1135,
66 1117, 1099, 1081, 1063, 1046, 1029, 1012, 996, 980, 964, 948, 933, 918, 903, 888, 874, 860,
67 846, 833, 819, 806, 793, 781, 768, 756, 744, 733, 721, 710, 699, 688, 677, 667, 657, 647, 637,
68 627, 618, 609, 599, 591, 582, 574, 565, 557, 549, 541, 534, 526, 519, 512, 505, 498, 492, 485,
69 479, 473, 467, 461, 455, 450, 444, 439, 434, 429, 424, 419, 415, 410, 406, 402, 398, 394, 390,
70 386, 383, 379, 376, 373, 370, 367, 364, 361, 359, 356, 354, 351, 349, 347, 345, 343, 342, 340,
71 338, 337, 336, 334, 333, 332, 331, 330, 330, 329, 328, 328, 328, 327, 327, 327, 327, 327, 328,
72 328, 328, 329, 330, 330, 331, 332, 333, 334, 336, 337, 338, 340, 342, 343, 345, 347, 349, 351,
73 354, 356, 359, 361, 364, 367, 370, 373, 376, 379, 383, 386, 390, 394, 398, 402, 406, 410, 415,
74 419, 424, 429, 434, 439, 444, 450, 455, 461, 467, 473, 479, 485, 492, 498, 505, 512, 519, 526,
75 534, 541, 549, 557, 565, 574, 582, 591, 599, 609, 618, 627, 637, 647, 657, 667, 677, 688, 699,
76 710, 721, 733, 744, 756, 768, 781, 793, 806, 819, 833, 846, 860, 874, 888, 903, 918, 933, 948,
77 964, 980, 996, 1012, 1029, 1046, 1063, 1081, 1099, 1117, 1135, 1154, 1173, 1192, 1212, 1232,
78 1252, 1273, 1294, 1315, 1336, 1358, 1380, 1403, 1426, 1449, 1472, 1496, 1520, 1545, 1570, 1595,
79 1620, 1646, 1673, 1699, 1726, 1754, 1781, 1810, 1838, 1867, 1896, 1926, 1956, 1986, 2017, 2048,
80 2080, 2112, 2144, 2177, 2210, 2244, 2278, 2312, 2347, 2382, 2418, 2454, 2491, 2528, 2565, 2603,
81 2641, 2680, 2719, 2758, 2798, 2839, 2879, 2921, 2962, 3005, 3047, 3090, 3134, 3178, 3222, 3267,
82 3312, 3358, 3404, 3451, 3498, 3546, 3594, 3642, 3691, 3741, 3791, 3841, 3892, 3943, 3995, 4047,
83 4100, 4153, 4207, 4261, 4316, 4371, 4426, 4482, 4539, 4596, 4653, 4711, 4769, 4828, 4888, 4947,
84 5008, 5068, 5130, 5191, 5253, 5316, 5379, 5443, 5507, 5571, 5636, 5702, 5767, 5834, 5901, 5968,
85 6036, 6104, 6172, 6241, 6311, 6381, 6451, 6522, 6594, 6665, 6738, 6810, 6883, 6957, 7031, 7105,
86 7180, 7255, 7331, 7407, 7484, 7561, 7638, 7716, 7794, 7873, 7952, 8031, 8111,
87];
88
89#[embassy::main]
90async fn main(spawner: Spawner, p: Peripherals) {
91 let pwm = Pwm::new(p.PWM0, p.P0_13, p.P0_14, p.P0_16, p.P0_15);
92 pwm.set_prescaler(Prescaler::Div1);
93 info!("pwm initialized!");
94
95 let mut i = 0;
96 loop {
97 i += 1;
98 pwm.set_duty(0, DUTY[i % 1024]);
99 pwm.set_duty(1, DUTY[(i + 256) % 1024]);
100 pwm.set_duty(2, DUTY[(i + 512) % 1024]);
101 pwm.set_duty(3, DUTY[(i + 768) % 1024]);
102 Timer::after(Duration::from_millis(3)).await;
103 }
104}
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
116impl_twim!(TWI0, TWIM0, TWIM0_TWIS0_TWI0); 119impl_twim!(TWI0, TWIM0, TWIM0_TWIS0_TWI0);
117 120
121impl_pwm!(PWM0, PWM0, PWM0);
122
118impl_timer!(TIMER0, TIMER0, TIMER0); 123impl_timer!(TIMER0, TIMER0, TIMER0);
119impl_timer!(TIMER1, TIMER1, TIMER1); 124impl_timer!(TIMER1, TIMER1, TIMER1);
120impl_timer!(TIMER2, TIMER2, TIMER2); 125impl_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
117impl_twim!(TWISPI0, TWIM0, TWIM0_TWIS0_TWI0_SPIM0_SPIS0_SPI0); 120impl_twim!(TWISPI0, TWIM0, TWIM0_TWIS0_TWI0_SPIM0_SPIS0_SPI0);
118 121
122impl_pwm!(PWM0, PWM0, PWM0);
123
119impl_timer!(TIMER0, TIMER0, TIMER0); 124impl_timer!(TIMER0, TIMER0, TIMER0);
120impl_timer!(TIMER1, TIMER1, TIMER1); 125impl_timer!(TIMER1, TIMER1, TIMER1);
121impl_timer!(TIMER2, TIMER2, TIMER2); 126impl_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);
123impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); 128impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);
124impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); 129impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1);
125 130
131impl_pwm!(PWM0, PWM0, PWM0);
132impl_pwm!(PWM1, PWM1, PWM1);
133impl_pwm!(PWM2, PWM2, PWM2);
134
126impl_timer!(TIMER0, TIMER0, TIMER0); 135impl_timer!(TIMER0, TIMER0, TIMER0);
127impl_timer!(TIMER1, TIMER1, TIMER1); 136impl_timer!(TIMER1, TIMER1, TIMER1);
128impl_timer!(TIMER2, TIMER2, TIMER2); 137impl_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);
144impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); 150impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);
145impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); 151impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1);
146 152
153impl_pwm!(PWM0, PWM0, PWM0);
154impl_pwm!(PWM1, PWM1, PWM1);
155impl_pwm!(PWM2, PWM2, PWM2);
156impl_pwm!(PWM3, PWM3, PWM3);
157
147impl_timer!(TIMER0, TIMER0, TIMER0); 158impl_timer!(TIMER0, TIMER0, TIMER0);
148impl_timer!(TIMER1, TIMER1, TIMER1); 159impl_timer!(TIMER1, TIMER1, TIMER1);
149impl_timer!(TIMER2, TIMER2, TIMER2); 160impl_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);
147impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0); 153impl_twim!(TWISPI0, TWIM0, SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0);
148impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1); 154impl_twim!(TWISPI1, TWIM1, SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1);
149 155
156impl_pwm!(PWM0, PWM0, PWM0);
157impl_pwm!(PWM1, PWM1, PWM1);
158impl_pwm!(PWM2, PWM2, PWM2);
159impl_pwm!(PWM3, PWM3, PWM3);
160
150impl_timer!(TIMER0, TIMER0, TIMER0); 161impl_timer!(TIMER0, TIMER0, TIMER0);
151impl_timer!(TIMER1, TIMER1, TIMER1); 162impl_timer!(TIMER1, TIMER1, TIMER1);
152impl_timer!(TIMER2, TIMER2, TIMER2); 163impl_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;
29pub mod gpio; 29pub mod gpio;
30pub mod gpiote; 30pub mod gpiote;
31pub mod ppi; 31pub mod ppi;
32#[cfg(not(any(feature = "nrf52805", feature = "nrf52820")))]
33pub mod pwm;
32#[cfg(feature = "nrf52840")] 34#[cfg(feature = "nrf52840")]
33pub mod qspi; 35pub mod qspi;
34pub mod rtc; 36pub 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
3use core::cell::UnsafeCell;
4use core::marker::PhantomData;
5use core::sync::atomic::{compiler_fence, Ordering};
6use embassy::util::Unborrow;
7use embassy_extras::unborrow;
8
9use crate::fmt::{assert, panic, unreachable, *};
10use crate::gpio::sealed::Pin as _;
11use crate::gpio::OptionalPin as GpioOptionalPin;
12use crate::interrupt::Interrupt;
13use crate::pac;
14
15#[derive(Debug, Eq, PartialEq, Clone, Copy)]
16pub 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
28pub struct Pwm<'d, T: Instance> {
29 peri: T,
30 phantom: PhantomData<&'d mut T>,
31}
32
33impl<'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
167impl<'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
178pub(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
200pub trait Instance: sealed::Instance + 'static {
201 type Interrupt: Interrupt;
202}
203
204macro_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}