stm32/timer: docs.
This commit is contained in:
@@ -17,17 +17,27 @@ pub mod low_level {
|
||||
}
|
||||
|
||||
pub(crate) mod sealed {
|
||||
|
||||
use super::*;
|
||||
|
||||
/// Basic 16-bit timer instance.
|
||||
pub trait Basic16bitInstance: RccPeripheral {
|
||||
/// Interrupt for this timer.
|
||||
type Interrupt: interrupt::typelevel::Interrupt;
|
||||
|
||||
/// Get access to the basic 16bit timer registers.
|
||||
///
|
||||
/// Note: This works even if the timer is more capable, because registers
|
||||
/// for the less capable timers are a subset. This allows writing a driver
|
||||
/// for a given set of capabilities, and having it transparently work with
|
||||
/// more capable timers.
|
||||
fn regs() -> crate::pac::timer::TimBasic;
|
||||
|
||||
/// Start the timer.
|
||||
fn start(&mut self) {
|
||||
Self::regs().cr1().modify(|r| r.set_cen(true));
|
||||
}
|
||||
|
||||
/// Stop the timer.
|
||||
fn stop(&mut self) {
|
||||
Self::regs().cr1().modify(|r| r.set_cen(false));
|
||||
}
|
||||
@@ -63,6 +73,9 @@ pub(crate) mod sealed {
|
||||
regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
|
||||
}
|
||||
|
||||
/// Clear update interrupt.
|
||||
///
|
||||
/// Returns whether the update interrupt flag was set.
|
||||
fn clear_update_interrupt(&mut self) -> bool {
|
||||
let regs = Self::regs();
|
||||
let sr = regs.sr().read();
|
||||
@@ -76,14 +89,17 @@ pub(crate) mod sealed {
|
||||
}
|
||||
}
|
||||
|
||||
/// Enable/disable the update interrupt.
|
||||
fn enable_update_interrupt(&mut self, enable: bool) {
|
||||
Self::regs().dier().write(|r| r.set_uie(enable));
|
||||
}
|
||||
|
||||
/// Enable/disable autoreload preload.
|
||||
fn set_autoreload_preload(&mut self, enable: bool) {
|
||||
Self::regs().cr1().modify(|r| r.set_arpe(enable));
|
||||
}
|
||||
|
||||
/// Get the timer frequency.
|
||||
fn get_frequency(&self) -> Hertz {
|
||||
let timer_f = Self::frequency();
|
||||
|
||||
@@ -95,9 +111,17 @@ pub(crate) mod sealed {
|
||||
}
|
||||
}
|
||||
|
||||
/// Gneral-purpose 16-bit timer instance.
|
||||
pub trait GeneralPurpose16bitInstance: Basic16bitInstance {
|
||||
/// Get access to the general purpose 16bit timer registers.
|
||||
///
|
||||
/// Note: This works even if the timer is more capable, because registers
|
||||
/// for the less capable timers are a subset. This allows writing a driver
|
||||
/// for a given set of capabilities, and having it transparently work with
|
||||
/// more capable timers.
|
||||
fn regs_gp16() -> crate::pac::timer::TimGp16;
|
||||
|
||||
/// Set counting mode.
|
||||
fn set_counting_mode(&mut self, mode: CountingMode) {
|
||||
let (cms, dir) = mode.into();
|
||||
|
||||
@@ -110,19 +134,29 @@ pub(crate) mod sealed {
|
||||
Self::regs_gp16().cr1().modify(|r| r.set_cms(cms))
|
||||
}
|
||||
|
||||
/// Get counting mode.
|
||||
fn get_counting_mode(&self) -> CountingMode {
|
||||
let cr1 = Self::regs_gp16().cr1().read();
|
||||
(cr1.cms(), cr1.dir()).into()
|
||||
}
|
||||
|
||||
/// Set clock divider.
|
||||
fn set_clock_division(&mut self, ckd: vals::Ckd) {
|
||||
Self::regs_gp16().cr1().modify(|r| r.set_ckd(ckd));
|
||||
}
|
||||
}
|
||||
|
||||
/// Gneral-purpose 32-bit timer instance.
|
||||
pub trait GeneralPurpose32bitInstance: GeneralPurpose16bitInstance {
|
||||
/// Get access to the general purpose 32bit timer registers.
|
||||
///
|
||||
/// Note: This works even if the timer is more capable, because registers
|
||||
/// for the less capable timers are a subset. This allows writing a driver
|
||||
/// for a given set of capabilities, and having it transparently work with
|
||||
/// more capable timers.
|
||||
fn regs_gp32() -> crate::pac::timer::TimGp32;
|
||||
|
||||
/// Set timer frequency.
|
||||
fn set_frequency(&mut self, frequency: Hertz) {
|
||||
let f = frequency.0;
|
||||
assert!(f > 0);
|
||||
@@ -140,6 +174,7 @@ pub(crate) mod sealed {
|
||||
regs.cr1().modify(|r| r.set_urs(vals::Urs::ANYEVENT));
|
||||
}
|
||||
|
||||
/// Get timer frequency.
|
||||
fn get_frequency(&self) -> Hertz {
|
||||
let timer_f = Self::frequency();
|
||||
|
||||
@@ -151,141 +186,177 @@ pub(crate) mod sealed {
|
||||
}
|
||||
}
|
||||
|
||||
/// Advanced control timer instance.
|
||||
pub trait AdvancedControlInstance: GeneralPurpose16bitInstance {
|
||||
/// Get access to the advanced timer registers.
|
||||
fn regs_advanced() -> crate::pac::timer::TimAdv;
|
||||
}
|
||||
|
||||
/// Capture/Compare 16-bit timer instance.
|
||||
pub trait CaptureCompare16bitInstance: GeneralPurpose16bitInstance {
|
||||
/// Set input capture filter.
|
||||
fn set_input_capture_filter(&mut self, channel: Channel, icf: vals::Icf) {
|
||||
let raw_channel = channel.raw();
|
||||
let raw_channel = channel.index();
|
||||
Self::regs_gp16()
|
||||
.ccmr_input(raw_channel / 2)
|
||||
.modify(|r| r.set_icf(raw_channel % 2, icf));
|
||||
}
|
||||
|
||||
/// Clear input interrupt.
|
||||
fn clear_input_interrupt(&mut self, channel: Channel) {
|
||||
Self::regs_gp16().sr().modify(|r| r.set_ccif(channel.raw(), false));
|
||||
Self::regs_gp16().sr().modify(|r| r.set_ccif(channel.index(), false));
|
||||
}
|
||||
|
||||
/// Enable input interrupt.
|
||||
fn enable_input_interrupt(&mut self, channel: Channel, enable: bool) {
|
||||
Self::regs_gp16().dier().modify(|r| r.set_ccie(channel.raw(), enable));
|
||||
Self::regs_gp16().dier().modify(|r| r.set_ccie(channel.index(), enable));
|
||||
}
|
||||
|
||||
/// Set input capture prescaler.
|
||||
fn set_input_capture_prescaler(&mut self, channel: Channel, factor: u8) {
|
||||
let raw_channel = channel.raw();
|
||||
let raw_channel = channel.index();
|
||||
Self::regs_gp16()
|
||||
.ccmr_input(raw_channel / 2)
|
||||
.modify(|r| r.set_icpsc(raw_channel % 2, factor));
|
||||
}
|
||||
|
||||
/// Set input TI selection.
|
||||
fn set_input_ti_selection(&mut self, channel: Channel, tisel: InputTISelection) {
|
||||
let raw_channel = channel.raw();
|
||||
let raw_channel = channel.index();
|
||||
Self::regs_gp16()
|
||||
.ccmr_input(raw_channel / 2)
|
||||
.modify(|r| r.set_ccs(raw_channel % 2, tisel.into()));
|
||||
}
|
||||
|
||||
/// Set input capture mode.
|
||||
fn set_input_capture_mode(&mut self, channel: Channel, mode: InputCaptureMode) {
|
||||
Self::regs_gp16().ccer().modify(|r| match mode {
|
||||
InputCaptureMode::Rising => {
|
||||
r.set_ccnp(channel.raw(), false);
|
||||
r.set_ccp(channel.raw(), false);
|
||||
r.set_ccnp(channel.index(), false);
|
||||
r.set_ccp(channel.index(), false);
|
||||
}
|
||||
InputCaptureMode::Falling => {
|
||||
r.set_ccnp(channel.raw(), false);
|
||||
r.set_ccp(channel.raw(), true);
|
||||
r.set_ccnp(channel.index(), false);
|
||||
r.set_ccp(channel.index(), true);
|
||||
}
|
||||
InputCaptureMode::BothEdges => {
|
||||
r.set_ccnp(channel.raw(), true);
|
||||
r.set_ccp(channel.raw(), true);
|
||||
r.set_ccnp(channel.index(), true);
|
||||
r.set_ccp(channel.index(), true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Enable timer outputs.
|
||||
fn enable_outputs(&mut self);
|
||||
|
||||
/// Set output compare mode.
|
||||
fn set_output_compare_mode(&mut self, channel: Channel, mode: OutputCompareMode) {
|
||||
let r = Self::regs_gp16();
|
||||
let raw_channel: usize = channel.raw();
|
||||
let raw_channel: usize = channel.index();
|
||||
r.ccmr_output(raw_channel / 2)
|
||||
.modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
|
||||
}
|
||||
|
||||
/// Set output polarity.
|
||||
fn set_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
|
||||
Self::regs_gp16()
|
||||
.ccer()
|
||||
.modify(|w| w.set_ccp(channel.raw(), polarity.into()));
|
||||
.modify(|w| w.set_ccp(channel.index(), polarity.into()));
|
||||
}
|
||||
|
||||
/// Enable/disable a channel.
|
||||
fn enable_channel(&mut self, channel: Channel, enable: bool) {
|
||||
Self::regs_gp16().ccer().modify(|w| w.set_cce(channel.raw(), enable));
|
||||
Self::regs_gp16().ccer().modify(|w| w.set_cce(channel.index(), enable));
|
||||
}
|
||||
|
||||
/// Set compare value for a channel.
|
||||
fn set_compare_value(&mut self, channel: Channel, value: u16) {
|
||||
Self::regs_gp16().ccr(channel.raw()).modify(|w| w.set_ccr(value));
|
||||
Self::regs_gp16().ccr(channel.index()).modify(|w| w.set_ccr(value));
|
||||
}
|
||||
|
||||
/// Get capture value for a channel.
|
||||
fn get_capture_value(&mut self, channel: Channel) -> u16 {
|
||||
Self::regs_gp16().ccr(channel.raw()).read().ccr()
|
||||
Self::regs_gp16().ccr(channel.index()).read().ccr()
|
||||
}
|
||||
|
||||
/// Get max compare value. This depends on the timer frequency and the clock frequency from RCC.
|
||||
fn get_max_compare_value(&self) -> u16 {
|
||||
Self::regs_gp16().arr().read().arr()
|
||||
}
|
||||
|
||||
/// Get compare value for a channel.
|
||||
fn get_compare_value(&self, channel: Channel) -> u16 {
|
||||
Self::regs_gp16().ccr(channel.raw()).read().ccr()
|
||||
Self::regs_gp16().ccr(channel.index()).read().ccr()
|
||||
}
|
||||
}
|
||||
|
||||
/// Capture/Compare 16-bit timer instance with complementary pin support.
|
||||
pub trait ComplementaryCaptureCompare16bitInstance: CaptureCompare16bitInstance + AdvancedControlInstance {
|
||||
/// Set complementary output polarity.
|
||||
fn set_complementary_output_polarity(&mut self, channel: Channel, polarity: OutputPolarity) {
|
||||
Self::regs_advanced()
|
||||
.ccer()
|
||||
.modify(|w| w.set_ccnp(channel.raw(), polarity.into()));
|
||||
.modify(|w| w.set_ccnp(channel.index(), polarity.into()));
|
||||
}
|
||||
|
||||
/// Set clock divider for the dead time.
|
||||
fn set_dead_time_clock_division(&mut self, value: vals::Ckd) {
|
||||
Self::regs_advanced().cr1().modify(|w| w.set_ckd(value));
|
||||
}
|
||||
|
||||
/// Set dead time, as a fraction of the max duty value.
|
||||
fn set_dead_time_value(&mut self, value: u8) {
|
||||
Self::regs_advanced().bdtr().modify(|w| w.set_dtg(value));
|
||||
}
|
||||
|
||||
/// Enable/disable a complementary channel.
|
||||
fn enable_complementary_channel(&mut self, channel: Channel, enable: bool) {
|
||||
Self::regs_advanced()
|
||||
.ccer()
|
||||
.modify(|w| w.set_ccne(channel.raw(), enable));
|
||||
.modify(|w| w.set_ccne(channel.index(), enable));
|
||||
}
|
||||
}
|
||||
|
||||
/// Capture/Compare 32-bit timer instance.
|
||||
pub trait CaptureCompare32bitInstance: GeneralPurpose32bitInstance + CaptureCompare16bitInstance {
|
||||
/// Set comapre value for a channel.
|
||||
fn set_compare_value(&mut self, channel: Channel, value: u32) {
|
||||
Self::regs_gp32().ccr(channel.raw()).modify(|w| w.set_ccr(value));
|
||||
Self::regs_gp32().ccr(channel.index()).modify(|w| w.set_ccr(value));
|
||||
}
|
||||
|
||||
/// Get capture value for a channel.
|
||||
fn get_capture_value(&mut self, channel: Channel) -> u32 {
|
||||
Self::regs_gp32().ccr(channel.raw()).read().ccr()
|
||||
Self::regs_gp32().ccr(channel.index()).read().ccr()
|
||||
}
|
||||
|
||||
/// Get max compare value. This depends on the timer frequency and the clock frequency from RCC.
|
||||
fn get_max_compare_value(&self) -> u32 {
|
||||
Self::regs_gp32().arr().read().arr()
|
||||
}
|
||||
|
||||
/// Get compare value for a channel.
|
||||
fn get_compare_value(&self, channel: Channel) -> u32 {
|
||||
Self::regs_gp32().ccr(channel.raw()).read().ccr()
|
||||
Self::regs_gp32().ccr(channel.index()).read().ccr()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Timer channel.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Channel {
|
||||
/// Channel 1.
|
||||
Ch1,
|
||||
/// Channel 2.
|
||||
Ch2,
|
||||
/// Channel 3.
|
||||
Ch3,
|
||||
/// Channel 4.
|
||||
Ch4,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
pub fn raw(&self) -> usize {
|
||||
/// Get the channel index (0..3)
|
||||
pub fn index(&self) -> usize {
|
||||
match self {
|
||||
Channel::Ch1 => 0,
|
||||
Channel::Ch2 => 1,
|
||||
@@ -295,17 +366,25 @@ impl Channel {
|
||||
}
|
||||
}
|
||||
|
||||
/// Input capture mode.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum InputCaptureMode {
|
||||
/// Rising edge only.
|
||||
Rising,
|
||||
/// Falling edge only.
|
||||
Falling,
|
||||
/// Both rising or falling edges.
|
||||
BothEdges,
|
||||
}
|
||||
|
||||
/// Input TI selection.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum InputTISelection {
|
||||
/// Normal
|
||||
Normal,
|
||||
/// Alternate
|
||||
Alternate,
|
||||
/// TRC
|
||||
TRC,
|
||||
}
|
||||
|
||||
@@ -319,6 +398,7 @@ impl From<InputTISelection> for stm32_metapac::timer::vals::CcmrInputCcs {
|
||||
}
|
||||
}
|
||||
|
||||
/// Timer counting mode.
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum CountingMode {
|
||||
@@ -345,6 +425,7 @@ pub enum CountingMode {
|
||||
}
|
||||
|
||||
impl CountingMode {
|
||||
/// Return whether this mode is edge-aligned (up or down).
|
||||
pub fn is_edge_aligned(&self) -> bool {
|
||||
match self {
|
||||
CountingMode::EdgeAlignedUp | CountingMode::EdgeAlignedDown => true,
|
||||
@@ -352,6 +433,7 @@ impl CountingMode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Return whether this mode is center-aligned.
|
||||
pub fn is_center_aligned(&self) -> bool {
|
||||
match self {
|
||||
CountingMode::CenterAlignedDownInterrupts
|
||||
@@ -386,16 +468,34 @@ impl From<(vals::Cms, vals::Dir)> for CountingMode {
|
||||
}
|
||||
}
|
||||
|
||||
/// Output compare mode.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum OutputCompareMode {
|
||||
/// The comparison between the output compare register TIMx_CCRx and
|
||||
/// the counter TIMx_CNT has no effect on the outputs.
|
||||
/// (this mode is used to generate a timing base).
|
||||
Frozen,
|
||||
/// Set channel to active level on match. OCxREF signal is forced high when the
|
||||
/// counter TIMx_CNT matches the capture/compare register x (TIMx_CCRx).
|
||||
ActiveOnMatch,
|
||||
/// Set channel to inactive level on match. OCxREF signal is forced low when the
|
||||
/// counter TIMx_CNT matches the capture/compare register x (TIMx_CCRx).
|
||||
InactiveOnMatch,
|
||||
/// Toggle - OCxREF toggles when TIMx_CNT=TIMx_CCRx.
|
||||
Toggle,
|
||||
/// Force inactive level - OCxREF is forced low.
|
||||
ForceInactive,
|
||||
/// Force active level - OCxREF is forced high.
|
||||
ForceActive,
|
||||
/// PWM mode 1 - In upcounting, channel is active as long as TIMx_CNT<TIMx_CCRx
|
||||
/// else inactive. In downcounting, channel is inactive (OCxREF=0) as long as
|
||||
/// TIMx_CNT>TIMx_CCRx else active (OCxREF=1).
|
||||
PwmMode1,
|
||||
/// PWM mode 2 - In upcounting, channel is inactive as long as
|
||||
/// TIMx_CNT<TIMx_CCRx else active. In downcounting, channel is active as long as
|
||||
/// TIMx_CNT>TIMx_CCRx else inactive.
|
||||
PwmMode2,
|
||||
// TODO: there's more modes here depending on the chip family.
|
||||
}
|
||||
|
||||
impl From<OutputCompareMode> for stm32_metapac::timer::vals::Ocm {
|
||||
@@ -413,9 +513,12 @@ impl From<OutputCompareMode> for stm32_metapac::timer::vals::Ocm {
|
||||
}
|
||||
}
|
||||
|
||||
/// Timer output pin polarity.
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum OutputPolarity {
|
||||
/// Active high (higher duty value makes the pin spend more time high).
|
||||
ActiveHigh,
|
||||
/// Active low (higher duty value makes the pin spend more time low).
|
||||
ActiveLow,
|
||||
}
|
||||
|
||||
@@ -428,24 +531,31 @@ impl From<OutputPolarity> for bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Basic 16-bit timer instance.
|
||||
pub trait Basic16bitInstance: sealed::Basic16bitInstance + 'static {}
|
||||
|
||||
/// Gneral-purpose 16-bit timer instance.
|
||||
pub trait GeneralPurpose16bitInstance: sealed::GeneralPurpose16bitInstance + 'static {}
|
||||
|
||||
/// Gneral-purpose 32-bit timer instance.
|
||||
pub trait GeneralPurpose32bitInstance: sealed::GeneralPurpose32bitInstance + 'static {}
|
||||
|
||||
/// Advanced control timer instance.
|
||||
pub trait AdvancedControlInstance: sealed::AdvancedControlInstance + 'static {}
|
||||
|
||||
/// Capture/Compare 16-bit timer instance.
|
||||
pub trait CaptureCompare16bitInstance:
|
||||
sealed::CaptureCompare16bitInstance + GeneralPurpose16bitInstance + 'static
|
||||
{
|
||||
}
|
||||
|
||||
/// Capture/Compare 16-bit timer instance with complementary pin support.
|
||||
pub trait ComplementaryCaptureCompare16bitInstance:
|
||||
sealed::ComplementaryCaptureCompare16bitInstance + AdvancedControlInstance + 'static
|
||||
{
|
||||
}
|
||||
|
||||
/// Capture/Compare 32-bit timer instance.
|
||||
pub trait CaptureCompare32bitInstance:
|
||||
sealed::CaptureCompare32bitInstance + CaptureCompare16bitInstance + GeneralPurpose32bitInstance + 'static
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user