2021-12-08 17:37:46 +01:00
|
|
|
pub mod simple_pwm;
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
#[cfg(feature = "unstable-pac")]
|
|
|
|
pub mod low_level {
|
|
|
|
pub use super::sealed::*;
|
|
|
|
}
|
2021-09-29 16:10:20 +02:00
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum Channel {
|
|
|
|
Ch1,
|
|
|
|
Ch2,
|
|
|
|
Ch3,
|
|
|
|
Ch4,
|
|
|
|
}
|
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
impl Channel {
|
|
|
|
pub fn raw(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
Channel::Ch1 => 0,
|
|
|
|
Channel::Ch2 => 1,
|
|
|
|
Channel::Ch3 => 2,
|
|
|
|
Channel::Ch4 => 3,
|
2021-09-29 16:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 17:37:46 +01:00
|
|
|
}
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum OutputCompareMode {
|
|
|
|
Frozen,
|
|
|
|
ActiveOnMatch,
|
|
|
|
InactiveOnMatch,
|
|
|
|
Toggle,
|
|
|
|
ForceInactive,
|
|
|
|
ForceActive,
|
|
|
|
PwmMode1,
|
|
|
|
PwmMode2,
|
|
|
|
}
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
impl From<OutputCompareMode> for stm32_metapac::timer::vals::Ocm {
|
|
|
|
fn from(mode: OutputCompareMode) -> Self {
|
|
|
|
match mode {
|
|
|
|
OutputCompareMode::Frozen => stm32_metapac::timer::vals::Ocm::FROZEN,
|
|
|
|
OutputCompareMode::ActiveOnMatch => stm32_metapac::timer::vals::Ocm::ACTIVEONMATCH,
|
|
|
|
OutputCompareMode::InactiveOnMatch => stm32_metapac::timer::vals::Ocm::INACTIVEONMATCH,
|
|
|
|
OutputCompareMode::Toggle => stm32_metapac::timer::vals::Ocm::TOGGLE,
|
|
|
|
OutputCompareMode::ForceInactive => stm32_metapac::timer::vals::Ocm::FORCEINACTIVE,
|
|
|
|
OutputCompareMode::ForceActive => stm32_metapac::timer::vals::Ocm::FORCEACTIVE,
|
|
|
|
OutputCompareMode::PwmMode1 => stm32_metapac::timer::vals::Ocm::PWMMODE1,
|
|
|
|
OutputCompareMode::PwmMode2 => stm32_metapac::timer::vals::Ocm::PWMMODE2,
|
2021-09-29 16:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
2021-12-08 17:37:46 +01:00
|
|
|
}
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
pub trait CaptureCompare16bitInstance: crate::timer::sealed::Basic16bitInstance {
|
2021-12-08 17:37:46 +01:00
|
|
|
unsafe fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode);
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool);
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
unsafe fn set_compare_value(&mut self, channel: Channel, value: u16);
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
unsafe fn get_max_compare_value(&self) -> u16;
|
2021-09-29 16:10:20 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
pub trait CaptureCompare32bitInstance:
|
2021-12-08 17:37:46 +01:00
|
|
|
crate::timer::sealed::GeneralPurpose32bitInstance
|
|
|
|
{
|
|
|
|
unsafe fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode);
|
|
|
|
|
|
|
|
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool);
|
|
|
|
|
|
|
|
unsafe fn set_compare_value(&mut self, channel: Channel, value: u32);
|
|
|
|
|
|
|
|
unsafe fn get_max_compare_value(&self) -> u32;
|
2021-09-29 16:10:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
pub trait CaptureCompare16bitInstance:
|
|
|
|
sealed::CaptureCompare16bitInstance + crate::timer::Basic16bitInstance + 'static
|
2021-12-08 17:37:46 +01:00
|
|
|
{
|
|
|
|
}
|
2022-02-10 21:38:03 +01:00
|
|
|
pub trait CaptureCompare32bitInstance:
|
|
|
|
sealed::CaptureCompare32bitInstance
|
|
|
|
+ CaptureCompare16bitInstance
|
|
|
|
+ crate::timer::GeneralPurpose32bitInstance
|
|
|
|
+ 'static
|
2021-12-08 17:37:46 +01:00
|
|
|
{
|
|
|
|
}
|
2021-09-29 16:10:20 +02:00
|
|
|
|
|
|
|
#[allow(unused)]
|
2021-12-08 17:37:46 +01:00
|
|
|
macro_rules! impl_compare_capable_16bit {
|
2021-09-29 16:10:20 +02:00
|
|
|
($inst:ident) => {
|
2022-02-10 21:38:03 +01:00
|
|
|
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
2021-12-08 17:37:46 +01:00
|
|
|
unsafe fn set_output_compare_mode(
|
|
|
|
&mut self,
|
|
|
|
channel: crate::pwm::Channel,
|
|
|
|
mode: OutputCompareMode,
|
|
|
|
) {
|
|
|
|
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = Self::regs_gp16();
|
2021-12-08 17:37:46 +01:00
|
|
|
let raw_channel: usize = channel.raw();
|
|
|
|
r.ccmr_output(raw_channel / 2)
|
|
|
|
.modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
|
2021-09-29 16:10:20 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) {
|
|
|
|
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp16()
|
2021-12-08 17:37:46 +01:00
|
|
|
.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;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp16()
|
2021-12-08 17:37:46 +01:00
|
|
|
.ccr(channel.raw())
|
|
|
|
.modify(|w| w.set_ccr(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_max_compare_value(&self) -> u16 {
|
|
|
|
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp16().arr().read().arr()
|
2021-12-08 17:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 16:10:20 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_interrupt! {
|
2021-12-08 17:37:46 +01:00
|
|
|
($inst:ident, timer, TIM_GP16, UP, $irq:ident) => {
|
2022-02-10 21:38:03 +01:00
|
|
|
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
2022-01-18 11:18:54 +01:00
|
|
|
unsafe fn set_output_compare_mode(
|
|
|
|
&mut self,
|
|
|
|
channel: crate::pwm::Channel,
|
|
|
|
mode: OutputCompareMode,
|
|
|
|
) {
|
|
|
|
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = Self::regs_gp16();
|
2022-01-18 11:18:54 +01:00
|
|
|
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;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp16()
|
2022-01-18 11:18:54 +01:00
|
|
|
.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;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp16()
|
2022-01-18 11:18:54 +01:00
|
|
|
.ccr(channel.raw())
|
|
|
|
.modify(|w| w.set_ccr(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_max_compare_value(&self) -> u16 {
|
|
|
|
use crate::timer::sealed::GeneralPurpose16bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp16().arr().read().arr()
|
2022-01-18 11:18:54 +01:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
impl CaptureCompare16bitInstance for crate::peripherals::$inst {
|
2021-09-29 16:10:20 +02:00
|
|
|
|
2021-12-08 17:37:46 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($inst:ident, timer, TIM_GP32, UP, $irq:ident) => {
|
|
|
|
impl_compare_capable_16bit!($inst);
|
2022-02-10 21:38:03 +01:00
|
|
|
impl crate::pwm::sealed::CaptureCompare32bitInstance for crate::peripherals::$inst {
|
2021-12-08 17:37:46 +01:00
|
|
|
unsafe fn set_output_compare_mode(
|
|
|
|
&mut self,
|
|
|
|
channel: crate::pwm::Channel,
|
|
|
|
mode: OutputCompareMode,
|
|
|
|
) {
|
|
|
|
use crate::timer::sealed::GeneralPurpose32bitInstance;
|
|
|
|
let raw_channel = channel.raw();
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp32().ccmr_output(raw_channel / 2).modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
|
2021-09-29 16:10:20 +02:00
|
|
|
}
|
2021-12-08 17:37:46 +01:00
|
|
|
|
|
|
|
unsafe fn enable_channel(&mut self, channel: Channel, enable: bool) {
|
|
|
|
use crate::timer::sealed::GeneralPurpose32bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp32().ccer().modify(|w| w.set_cce(channel.raw(), enable));
|
2021-12-08 17:37:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn set_compare_value(&mut self, channel: Channel, value: u32) {
|
|
|
|
use crate::timer::sealed::GeneralPurpose32bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value));
|
2021-12-08 17:37:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_max_compare_value(&self) -> u32 {
|
|
|
|
use crate::timer::sealed::GeneralPurpose32bitInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_gp32().arr().read().arr() as u32
|
2021-12-08 17:37:46 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 21:38:03 +01:00
|
|
|
impl CaptureCompare16bitInstance for crate::peripherals::$inst {
|
2021-12-08 17:37:46 +01:00
|
|
|
|
|
|
|
}
|
2022-02-10 21:38:03 +01:00
|
|
|
impl CaptureCompare32bitInstance for crate::peripherals::$inst {
|
2021-12-08 17:37:46 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
($inst:ident, timer, TIM_ADV, UP, $irq:ident) => {
|
2022-02-10 21:38:03 +01:00
|
|
|
impl crate::pwm::sealed::CaptureCompare16bitInstance for crate::peripherals::$inst {
|
2022-01-18 11:18:54 +01:00
|
|
|
unsafe fn set_output_compare_mode(
|
|
|
|
&mut self,
|
|
|
|
channel: crate::pwm::Channel,
|
|
|
|
mode: OutputCompareMode,
|
|
|
|
) {
|
|
|
|
use crate::timer::sealed::AdvancedControlInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = Self::regs_advanced();
|
2022-01-18 11:18:54 +01:00
|
|
|
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::AdvancedControlInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_advanced()
|
2022-01-18 11:18:54 +01:00
|
|
|
.ccer()
|
|
|
|
.modify(|w| w.set_cce(channel.raw(), enable));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn set_compare_value(&mut self, channel: Channel, value: u16) {
|
|
|
|
use crate::timer::sealed::AdvancedControlInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_advanced()
|
2022-01-18 11:18:54 +01:00
|
|
|
.ccr(channel.raw())
|
|
|
|
.modify(|w| w.set_ccr(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn get_max_compare_value(&self) -> u16 {
|
|
|
|
use crate::timer::sealed::AdvancedControlInstance;
|
2022-02-28 16:20:42 +01:00
|
|
|
Self::regs_advanced().arr().read().arr()
|
2022-01-18 11:18:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
impl CaptureCompare16bitInstance for crate::peripherals::$inst {
|
2021-12-08 17:37:46 +01:00
|
|
|
|
2021-09-29 16:10:20 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
pin_trait!(Channel1Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(Channel1ComplementaryPin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(Channel2Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(Channel2ComplementaryPin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(Channel3Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(Channel3ComplementaryPin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(Channel4Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(Channel4ComplementaryPin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(ExternalTriggerPin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(BreakInputPin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(BreakInputComparator1Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(BreakInputComparator2Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(BreakInput2Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(BreakInput2Comparator1Pin, CaptureCompare16bitInstance);
|
|
|
|
pin_trait!(BreakInput2Comparator2Pin, CaptureCompare16bitInstance);
|