You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
555: Reset timer CNT register when PWM period is changed r=burrbull a=scd31
When changing the period of a PWM timer repeatedly, it will sporadically stop working. This is because the CNT register isn't being reset.
Minimum reproducible example:
```rust
#![no_main]
#![no_std]
// use panic_halt as _;
use panic_semihosting as _;
use cortex_m_rt::entry;
use stm32f4xx_hal as hal;
use crate::hal::{pac, prelude::*, timer::Channel};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let buzzer = dp.GPIOA.split().pa0.into_alternate();
// Set up the system clock. We want to run at 48MHz for this one.
let rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(48.MHz()).freeze();
let mut pwm = dp.TIM5.pwm_hz(buzzer, 1000.Hz(), &clocks);
pwm.set_duty(Channel::C1, pwm.get_max_duty() / 2);
pwm.enable(Channel::C1);
let mut delay = dp.TIM1.delay_us(&clocks);
loop {
pwm.set_period(1000.Hz());
pwm.set_duty(Channel::C1, pwm.get_max_duty() / 2);
pwm.configure(&clocks);
delay.delay_ms(1000_u32);
pwm.set_period(2000.Hz());
pwm.set_duty(Channel::C1, pwm.get_max_duty() / 2);
pwm.configure(&clocks);
delay.delay(1.secs());
}
}
```
Before this PR, this would work for a few cycles before the signal disappears. It would occasionally reappear every few minutes. After this PR, the code is consistently outputting a signal that alternates between 1 and 2 KHz.
Co-authored-by: Stephen D <[email protected]>
0 commit comments