Merge pull request #3317 from GrantM11235/simplepwmchannel

embassy-stm32: Add SimplePwmChannel
This commit is contained in:
Ulf Lilleengen
2024-10-23 10:30:13 +00:00
committed by GitHub
9 changed files with 269 additions and 90 deletions

View File

@@ -6,7 +6,6 @@ use embassy_executor::Spawner;
use embassy_stm32::gpio::OutputType;
use embassy_stm32::time::khz;
use embassy_stm32::timer::simple_pwm::{PwmPin, SimplePwm};
use embassy_stm32::timer::Channel;
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};
@@ -15,22 +14,22 @@ async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
info!("Hello World!");
let ch1 = PwmPin::new_ch1(p.PE9, OutputType::PushPull);
let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10), Default::default());
let max = pwm.get_max_duty();
pwm.enable(Channel::Ch1);
let ch1_pin = PwmPin::new_ch1(p.PE9, OutputType::PushPull);
let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), None, None, None, khz(10), Default::default());
let mut ch1 = pwm.ch1();
ch1.enable();
info!("PWM initialized");
info!("PWM max duty {}", max);
info!("PWM max duty {}", ch1.max_duty_cycle());
loop {
pwm.set_duty(Channel::Ch1, 0);
ch1.set_duty_cycle_fully_off();
Timer::after_millis(300).await;
pwm.set_duty(Channel::Ch1, max / 4);
ch1.set_duty_cycle_fraction(1, 4);
Timer::after_millis(300).await;
pwm.set_duty(Channel::Ch1, max / 2);
ch1.set_duty_cycle_fraction(1, 2);
Timer::after_millis(300).await;
pwm.set_duty(Channel::Ch1, max - 1);
ch1.set_duty_cycle(ch1.max_duty_cycle() - 1);
Timer::after_millis(300).await;
}
}

View File

@@ -61,7 +61,7 @@ async fn main(_spawner: Spawner) {
// construct ws2812 non-return-to-zero (NRZ) code bit by bit
// ws2812 only need 24 bits for each LED, but we add one bit more to keep PWM output low
let max_duty = ws2812_pwm.get_max_duty() as u16;
let max_duty = ws2812_pwm.max_duty_cycle();
let n0 = 8 * max_duty / 25; // ws2812 Bit 0 high level timing
let n1 = 2 * n0; // ws2812 Bit 1 high level timing
@@ -84,7 +84,7 @@ async fn main(_spawner: Spawner) {
let pwm_channel = Channel::Ch1;
// make sure PWM output keep low on first start
ws2812_pwm.set_duty(pwm_channel, 0);
ws2812_pwm.channel(pwm_channel).set_duty_cycle(0);
// flip color at 2 Hz
let mut ticker = Ticker::every(Duration::from_millis(500));