Fix PWM for advanced timers
This commit is contained in:
parent
4a6f69e2d9
commit
3cdd8c1aeb
@ -55,6 +55,9 @@ pub(crate) mod sealed {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub trait CaptureCompare16bitInstance: crate::timer::sealed::Basic16bitInstance {
|
pub trait CaptureCompare16bitInstance: crate::timer::sealed::Basic16bitInstance {
|
||||||
|
/// Global output enable. Does not do anything on non-advanced timers.
|
||||||
|
unsafe fn enable_outputs(&mut self, enable: bool);
|
||||||
|
|
||||||
unsafe fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode);
|
unsafe fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode);
|
||||||
|
|
||||||
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool);
|
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool);
|
||||||
@ -88,6 +91,8 @@ pub trait CaptureCompare32bitInstance:
|
|||||||
macro_rules! impl_compare_capable_16bit {
|
macro_rules! impl_compare_capable_16bit {
|
||||||
($inst:ident) => {
|
($inst:ident) => {
|
||||||
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
||||||
|
unsafe fn enable_outputs(&mut self, _enable: bool) {}
|
||||||
|
|
||||||
unsafe fn set_output_compare_mode(&mut self, channel: crate::pwm::Channel, mode: OutputCompareMode) {
|
unsafe fn set_output_compare_mode(&mut self, channel: crate::pwm::Channel, mode: OutputCompareMode) {
|
||||||
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
||||||
let r = Self::regs_gp16();
|
let r = Self::regs_gp16();
|
||||||
@ -118,38 +123,7 @@ macro_rules! impl_compare_capable_16bit {
|
|||||||
|
|
||||||
foreach_interrupt! {
|
foreach_interrupt! {
|
||||||
($inst:ident, timer, TIM_GP16, UP, $irq:ident) => {
|
($inst:ident, timer, TIM_GP16, UP, $irq:ident) => {
|
||||||
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
impl_compare_capable_16bit!($inst);
|
||||||
unsafe fn set_output_compare_mode(
|
|
||||||
&mut self,
|
|
||||||
channel: crate::pwm::Channel,
|
|
||||||
mode: OutputCompareMode,
|
|
||||||
) {
|
|
||||||
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
|
||||||
let r = Self::regs_gp16();
|
|
||||||
let raw_channel: usize = channel.raw();
|
|
||||||
r.ccmr_output(raw_channel / 2)
|
|
||||||
.modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) {
|
|
||||||
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
|
||||||
Self::regs_gp16()
|
|
||||||
.ccer()
|
|
||||||
.modify(|w| w.set_cce(channel.raw(), enable));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) {
|
|
||||||
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
|
||||||
Self::regs_gp16()
|
|
||||||
.ccr(channel.raw())
|
|
||||||
.modify(|w| w.set_ccr(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn get_max_compare_value(&self) -> u16 {
|
|
||||||
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
|
||||||
Self::regs_gp16().arr().read().arr()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CaptureCompare16bitInstance for crate::peripherals::$inst {
|
impl CaptureCompare16bitInstance for crate::peripherals::$inst {
|
||||||
|
|
||||||
@ -194,6 +168,12 @@ foreach_interrupt! {
|
|||||||
|
|
||||||
($inst:ident, timer, TIM_ADV, UP, $irq:ident) => {
|
($inst:ident, timer, TIM_ADV, UP, $irq:ident) => {
|
||||||
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
||||||
|
unsafe fn enable_outputs(&mut self, enable: bool) {
|
||||||
|
use crate::timer::sealed::AdvancedControlInstance;
|
||||||
|
let r = Self::regs_advanced();
|
||||||
|
r.bdtr().modify(|w| w.set_moe(enable));
|
||||||
|
}
|
||||||
|
|
||||||
unsafe fn set_output_compare_mode(
|
unsafe fn set_output_compare_mode(
|
||||||
&mut self,
|
&mut self,
|
||||||
channel: crate::pwm::Channel,
|
channel: crate::pwm::Channel,
|
||||||
|
@ -92,6 +92,8 @@ impl<'d, T: CaptureCompare16bitInstance> SimplePwm<'d, T> {
|
|||||||
this.inner.start();
|
this.inner.start();
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
|
this.inner.enable_outputs(true);
|
||||||
|
|
||||||
this.inner
|
this.inner
|
||||||
.set_output_compare_mode(Channel::Ch1, OutputCompareMode::PwmMode1);
|
.set_output_compare_mode(Channel::Ch1, OutputCompareMode::PwmMode1);
|
||||||
this.inner
|
this.inner
|
||||||
|
42
examples/stm32f4/src/bin/pwm.rs
Normal file
42
examples/stm32f4/src/bin/pwm.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
#![feature(type_alias_impl_trait)]
|
||||||
|
|
||||||
|
use defmt::*;
|
||||||
|
use embassy::executor::Spawner;
|
||||||
|
use embassy::time::{Duration, Timer};
|
||||||
|
use embassy_stm32::pwm::simple_pwm::SimplePwm;
|
||||||
|
use embassy_stm32::pwm::Channel;
|
||||||
|
use embassy_stm32::time::{Hertz, U32Ext};
|
||||||
|
use embassy_stm32::{Config, Peripherals};
|
||||||
|
use {defmt_rtt as _, panic_probe as _};
|
||||||
|
|
||||||
|
fn config() -> Config {
|
||||||
|
let mut config = Config::default();
|
||||||
|
config.rcc.hse = Some(Hertz(8_000_000));
|
||||||
|
config.rcc.sys_ck = Some(Hertz(84_000_000));
|
||||||
|
config
|
||||||
|
}
|
||||||
|
|
||||||
|
#[embassy::main(config = "config()")]
|
||||||
|
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||||
|
info!("Hello World!");
|
||||||
|
|
||||||
|
let mut pwm = SimplePwm::new_1ch(p.TIM1, p.PE9, 10000.hz());
|
||||||
|
let max = pwm.get_max_duty();
|
||||||
|
pwm.enable(Channel::Ch1);
|
||||||
|
|
||||||
|
info!("PWM initialized");
|
||||||
|
info!("PWM max duty {}", max);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
pwm.set_duty(Channel::Ch1, 0);
|
||||||
|
Timer::after(Duration::from_millis(300)).await;
|
||||||
|
pwm.set_duty(Channel::Ch1, max / 4);
|
||||||
|
Timer::after(Duration::from_millis(300)).await;
|
||||||
|
pwm.set_duty(Channel::Ch1, max / 2);
|
||||||
|
Timer::after(Duration::from_millis(300)).await;
|
||||||
|
pwm.set_duty(Channel::Ch1, max - 1);
|
||||||
|
Timer::after(Duration::from_millis(300)).await;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user