stm32f1_hal/common/timer/
mod.rs

1pub mod pwm;
2pub use pwm::*;
3pub mod counter;
4pub use counter::*;
5pub mod fix_timer;
6pub use fix_timer::*;
7pub mod delay;
8pub use delay::*;
9
10use crate::time::Hertz;
11
12pub trait PwmChannel: embedded_hal::pwm::SetDutyCycle {
13    fn config(&mut self, mode: PwmMode, polarity: PwmPolarity);
14    fn set_enable(&mut self, en: bool);
15}
16
17// ----------------------------------------------------------------------------
18
19pub trait GeneralTimer {
20    fn reset_config(&mut self);
21    fn enable_counter(&mut self);
22    fn disable_counter(&mut self);
23    fn is_counter_enabled(&self) -> bool;
24    fn reset_counter(&mut self);
25    fn max_auto_reload() -> u32;
26    unsafe fn set_auto_reload_unchecked(&mut self, arr: u32);
27    fn set_auto_reload(&mut self, arr: u32) -> Result<(), Error>;
28    fn read_auto_reload(&self) -> u32;
29    fn set_prescaler(&mut self, psc: u16);
30    fn read_prescaler(&self) -> u16;
31    fn read_count(&self) -> u32;
32    fn trigger_update(&mut self);
33    fn stop_in_debug(&mut self, state: bool);
34    fn config_freq(&mut self, clock: Hertz, count_freq: Hertz, update_freq: Hertz);
35
36    fn clear_interrupt_flag(&mut self, event: Event);
37    fn listen_interrupt(&mut self, event: Event, b: bool);
38    fn get_interrupt_flag(&self) -> Event;
39    fn start_one_pulse(&mut self);
40}
41
42pub trait TimerDirection: GeneralTimer {
43    fn set_count_direction(&mut self, dir: CountDirection);
44}
45
46pub trait TimerWithPwm: GeneralTimer {
47    fn start_pwm(&mut self);
48    fn stop_pwm(&mut self);
49
50    fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: PwmMode);
51    fn set_polarity(&mut self, channel: Channel, polarity: PwmPolarity);
52}
53
54pub trait TimerWithPwm1Ch: TimerWithPwm {
55    fn enable_ch1(&mut self, en: bool);
56    fn set_ch1_cc_value(&mut self, value: u32);
57    fn get_ch1_cc_value(&self) -> u32;
58}
59
60pub trait TimerWithPwm2Ch: TimerWithPwm1Ch {
61    fn enable_ch2(&mut self, en: bool);
62    fn set_ch2_cc_value(&mut self, value: u32);
63    fn get_ch2_cc_value(&self) -> u32;
64}
65
66pub trait TimerWithPwm3Ch: TimerWithPwm2Ch {
67    fn enable_ch3(&mut self, en: bool);
68    fn set_ch3_cc_value(&mut self, value: u32);
69    fn get_ch3_cc_value(&self) -> u32;
70}
71
72pub trait TimerWithPwm4Ch: TimerWithPwm3Ch {
73    fn enable_ch4(&mut self, en: bool);
74    fn set_ch4_cc_value(&mut self, value: u32);
75    fn get_ch4_cc_value(&self) -> u32;
76}
77
78// Enumerate ------------------------------------------------------------------
79
80#[derive(Clone, Copy, PartialEq, Eq)]
81pub enum Channel {
82    C1,
83    C2,
84    C3,
85    C4,
86}
87
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub enum CountDirection {
90    Up,
91    Down,
92}
93
94#[derive(Clone, Copy, Debug, PartialEq, Eq)]
95pub enum PwmMode {
96    Mode1,
97    Mode2,
98}
99
100#[derive(Clone, Copy, Debug, PartialEq, Eq)]
101pub enum PwmPolarity {
102    ActiveHigh,
103    ActiveLow,
104}
105
106#[derive(Debug, Eq, PartialEq, Copy, Clone)]
107pub enum Error {
108    /// Timer is disabled
109    Disabled,
110    WrongAutoReload,
111}
112
113/// Interrupt events
114#[derive(Clone, Copy, PartialEq, Eq)]
115pub enum SysEvent {
116    /// [Timer] timed out / count down ended
117    Update,
118}
119
120bitflags::bitflags! {
121    pub struct Event: u32 {
122        const Update  = 1 << 0;
123        const C1 = 1 << 1;
124        const C2 = 1 << 2;
125        const C3 = 1 << 3;
126        const C4 = 1 << 4;
127    }
128}