aboutsummaryrefslogtreecommitdiff
path: root/examples/rp
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2024-10-22 07:52:03 +0000
committerGitHub <[email protected]>2024-10-22 07:52:03 +0000
commit0c22d4cccb6efa2e18db52f0df8333dda7ee3abe (patch)
tree3db87587d61b58a41b9f70d3b1cdd5b5ececec8d /examples/rp
parent6da53f082fadb888f8cd909618ba57d207e80715 (diff)
parent548f11d5aef37d5ab1edac6970ceb7bee4300f4f (diff)
Merge pull request #3433 from 1-rafael-1/rp-pwm-embedded-hal-traits
embassy_rp: implement pwm traits from embedded_hal
Diffstat (limited to 'examples/rp')
-rw-r--r--examples/rp/src/bin/pwm.rs58
1 files changed, 52 insertions, 6 deletions
diff --git a/examples/rp/src/bin/pwm.rs b/examples/rp/src/bin/pwm.rs
index 26e233260..791b88b5b 100644
--- a/examples/rp/src/bin/pwm.rs
+++ b/examples/rp/src/bin/pwm.rs
@@ -1,24 +1,36 @@
1//! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip. 1//! This example shows how to use PWM (Pulse Width Modulation) in the RP2040 chip.
2//! 2//!
3//! The LED on the RP Pico W board is connected differently. Add a LED and resistor to another pin. 3//! We demonstrate two ways of using PWM:
4//! 1. Via config
5//! 2. Via setting a duty cycle
4 6
5#![no_std] 7#![no_std]
6#![no_main] 8#![no_main]
7 9
8use defmt::*; 10use defmt::*;
9use embassy_executor::Spawner; 11use embassy_executor::Spawner;
10use embassy_rp::pwm::{Config, Pwm}; 12use embassy_rp::peripherals::{PIN_25, PIN_4, PWM_SLICE2, PWM_SLICE4};
13use embassy_rp::pwm::{Config, Pwm, SetDutyCycle};
11use embassy_time::Timer; 14use embassy_time::Timer;
12use {defmt_rtt as _, panic_probe as _}; 15use {defmt_rtt as _, panic_probe as _};
13 16
14#[embassy_executor::main] 17#[embassy_executor::main]
15async fn main(_spawner: Spawner) { 18async fn main(spawner: Spawner) {
16 let p = embassy_rp::init(Default::default()); 19 let p = embassy_rp::init(Default::default());
20 spawner.spawn(pwm_set_config(p.PWM_SLICE4, p.PIN_25)).unwrap();
21 spawner.spawn(pwm_set_dutycycle(p.PWM_SLICE2, p.PIN_4)).unwrap();
22}
17 23
18 let mut c: Config = Default::default(); 24/// Demonstrate PWM by modifying & applying the config
19 c.top = 0x8000; 25///
26/// Using the onboard led, if You are using a different Board than plain Pico2 (i.e. W variant)
27/// you must use another slice & pin and an appropriate resistor.
28#[embassy_executor::task]
29async fn pwm_set_config(slice4: PWM_SLICE4, pin25: PIN_25) {
30 let mut c = Config::default();
31 c.top = 32_768;
20 c.compare_b = 8; 32 c.compare_b = 8;
21 let mut pwm = Pwm::new_output_b(p.PWM_SLICE4, p.PIN_25, c.clone()); 33 let mut pwm = Pwm::new_output_b(slice4, pin25, c.clone());
22 34
23 loop { 35 loop {
24 info!("current LED duty cycle: {}/32768", c.compare_b); 36 info!("current LED duty cycle: {}/32768", c.compare_b);
@@ -27,3 +39,37 @@ async fn main(_spawner: Spawner) {
27 pwm.set_config(&c); 39 pwm.set_config(&c);
28 } 40 }
29} 41}
42
43/// Demonstrate PWM by setting duty cycle
44///
45/// Using GP4 in Slice2, make sure to use an appropriate resistor.
46#[embassy_executor::task]
47async fn pwm_set_dutycycle(slice2: PWM_SLICE2, pin4: PIN_4) {
48 // If we aim for a specific frequency, here is how we can calculate the top value.
49 // The top value sets the period of the PWM cycle, so a counter goes from 0 to top and then wraps around to 0.
50 // Every such wraparound is one PWM cycle. So here is how we get 25KHz:
51 let mut c = Config::default();
52 let pwm_freq = 25_000; // Hz, our desired frequency
53 let clock_freq = embassy_rp::clocks::clk_sys_freq();
54 c.top = (clock_freq / pwm_freq) as u16 - 1;
55
56 let mut pwm = Pwm::new_output_a(slice2, pin4, c.clone());
57
58 loop {
59 // 100% duty cycle, fully on
60 pwm.set_duty_cycle_fully_on().unwrap();
61 Timer::after_secs(1).await;
62
63 // 66% duty cycle. Expressed as simple percentage.
64 pwm.set_duty_cycle_percent(66).unwrap();
65 Timer::after_secs(1).await;
66
67 // 25% duty cycle. Expressed as 32768/4 = 8192.
68 pwm.set_duty_cycle(c.top / 4).unwrap();
69 Timer::after_secs(1).await;
70
71 // 0% duty cycle, fully off.
72 pwm.set_duty_cycle_fully_off().unwrap();
73 Timer::after_secs(1).await;
74 }
75}