stm32f1_hal/timer/
timer1.rs

1type TimerX = pac::TIM1;
2type Width = u16;
3
4// sync begin
5
6use super::*;
7use crate::{Mcu, pac};
8
9impl Instance for TimerX {}
10
11impl TimerInit<TimerX> for TimerX {
12    fn constrain(self, mcu: &mut Mcu) -> Timer<TimerX> {
13        Timer::new(self, mcu)
14    }
15}
16
17impl GeneralTimer for TimerX {
18    #[inline(always)]
19    fn reset_config(&mut self) {
20        self.cr1().reset();
21    }
22
23    #[inline(always)]
24    fn enable_counter(&mut self) {
25        self.cr1().modify(|_, w| w.cen().set_bit());
26    }
27
28    #[inline(always)]
29    fn disable_counter(&mut self) {
30        self.cr1().modify(|_, w| w.cen().clear_bit());
31    }
32
33    #[inline(always)]
34    fn is_counter_enabled(&self) -> bool {
35        self.cr1().read().cen().is_enabled()
36    }
37
38    #[inline(always)]
39    fn reset_counter(&mut self) {
40        self.cnt().reset();
41    }
42
43    #[inline(always)]
44    fn max_auto_reload() -> u32 {
45        Width::MAX as u32
46    }
47
48    #[inline(always)]
49    unsafe fn set_auto_reload_unchecked(&mut self, arr: u32) {
50        unsafe {
51            self.arr().write(|w| w.bits(arr));
52        }
53    }
54
55    #[inline(always)]
56    fn set_auto_reload(&mut self, arr: u32) -> Result<(), Error> {
57        // Note: Make it impossible to set the ARR value to 0, since this
58        // would cause an infinite loop.
59        if arr > 0 && arr <= Self::max_auto_reload() {
60            Ok(unsafe { self.set_auto_reload_unchecked(arr) })
61        } else {
62            Err(Error::WrongAutoReload)
63        }
64    }
65
66    #[inline(always)]
67    fn read_auto_reload(&self) -> u32 {
68        self.arr().read().bits()
69    }
70
71    #[inline(always)]
72    fn set_prescaler(&mut self, psc: u16) {
73        self.psc().write(|w| w.psc().set(psc));
74    }
75
76    #[inline(always)]
77    fn read_prescaler(&self) -> u16 {
78        self.psc().read().psc().bits()
79    }
80
81    #[inline(always)]
82    fn read_count(&self) -> u32 {
83        self.cnt().read().bits() as u32
84    }
85
86    #[inline(always)]
87    fn trigger_update(&mut self) {
88        // Sets the URS bit to prevent an interrupt from being triggered by
89        // the UG bit
90        self.cr1().modify(|_, w| w.urs().set_bit());
91        self.egr().write(|w| w.ug().set_bit());
92        self.cr1().modify(|_, w| w.urs().clear_bit());
93    }
94
95    #[inline]
96    fn config_freq(&mut self, clock: Hertz, count_freq: Hertz, update_freq: Hertz) {
97        let (prescaler, arr) = freq_to_presc_arr(clock.raw(), count_freq.raw(), update_freq.raw());
98        self.set_prescaler(prescaler as u16);
99        self.set_auto_reload(arr).unwrap();
100        // Trigger update event to load the registers
101        self.trigger_update();
102    }
103
104    #[inline(always)]
105    fn clear_interrupt_flag(&mut self, event: Event) {
106        self.sr()
107            .write(|w| unsafe { w.bits(0xffff & !event.bits()) });
108    }
109
110    #[inline(always)]
111    fn listen_interrupt(&mut self, event: Event, b: bool) {
112        self.dier().modify(|r, w| unsafe {
113            w.bits(if b {
114                r.bits() | event.bits()
115            } else {
116                r.bits() & !event.bits()
117            })
118        });
119    }
120
121    #[inline(always)]
122    fn get_interrupt_flag(&self) -> Event {
123        Event::from_bits_truncate(self.sr().read().bits())
124    }
125
126    #[inline(always)]
127    fn start_one_pulse(&mut self) {
128        self.cr1().modify(|_, w| w.opm().set_bit().cen().set_bit());
129    }
130
131    #[inline(always)]
132    fn stop_in_debug(&mut self, state: bool) {
133        let dbg = unsafe { DBG::steal() };
134        // sync dbg_t1
135        dbg.cr().modify(|_, w| w.dbg_tim1_stop().bit(state));
136        // sync dbg_end
137    }
138}
139
140impl GeneralTimerExt for TimerX {
141    #[inline(always)]
142    fn enable_preload(&mut self, b: bool) {
143        self.cr1().modify(|_, w| w.arpe().bit(b));
144    }
145}
146
147// sync pwm
148// PWM ------------------------------------------------------------------------
149
150impl TimerWithPwm for TimerX {
151    fn stop_pwm(&mut self) {
152        self.disable_counter();
153    }
154
155    // sync start_pwm_aoe
156
157    #[inline(always)]
158    fn start_pwm(&mut self) {
159        // self.bdtr().modify(|_, w| w.moe().set_bit());
160        self.bdtr().modify(|_, w| w.aoe().set_bit());
161        self.reset_counter();
162        self.enable_counter();
163    }
164
165    // sync pwm_cfg_4
166
167    #[inline(always)]
168    fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: PwmMode) {
169        let mode = Ocm::from(mode);
170        match channel {
171            Channel::C1 => {
172                self.ccmr1_output()
173                    .modify(|_, w| w.oc1pe().set_bit().oc1m().set(mode as _));
174            }
175            Channel::C2 => {
176                self.ccmr1_output()
177                    .modify(|_, w| w.oc2pe().set_bit().oc2m().set(mode as _));
178            }
179            Channel::C3 => {
180                self.ccmr2_output()
181                    .modify(|_, w| w.oc3pe().set_bit().oc3m().set(mode as _));
182            }
183            Channel::C4 => {
184                self.ccmr2_output()
185                    .modify(|_, w| w.oc4pe().set_bit().oc4m().set(mode as _));
186            }
187        }
188    }
189
190    fn set_polarity(&mut self, channel: Channel, polarity: PwmPolarity) {
191        match channel {
192            Channel::C1 => {
193                self.ccer()
194                    .modify(|_, w| w.cc1p().bit(polarity == PwmPolarity::ActiveLow));
195            }
196            Channel::C2 => {
197                self.ccer()
198                    .modify(|_, w| w.cc2p().bit(polarity == PwmPolarity::ActiveLow));
199            }
200            Channel::C3 => {
201                self.ccer()
202                    .modify(|_, w| w.cc3p().bit(polarity == PwmPolarity::ActiveLow));
203            }
204            Channel::C4 => {
205                self.ccer()
206                    .modify(|_, w| w.cc4p().bit(polarity == PwmPolarity::ActiveLow));
207            }
208        }
209    }
210}
211
212// sync pwm_ch1
213// PWM Channels ---------------------------------------------------------------
214
215impl TimerWithPwm1Ch for TimerX {
216    #[inline(always)]
217    fn enable_ch1(&mut self, en: bool) {
218        self.ccer().modify(|_, w| w.cc1e().bit(en));
219    }
220
221    #[inline(always)]
222    fn set_ch1_cc_value(&mut self, value: u32) {
223        unsafe { self.ccr1().write(|w| w.bits(value)) };
224    }
225
226    #[inline(always)]
227    fn get_ch1_cc_value(&self) -> u32 {
228        self.ccr1().read().bits()
229    }
230}
231
232// sync pwm_ch2
233
234impl TimerWithPwm2Ch for TimerX {
235    #[inline(always)]
236    fn enable_ch2(&mut self, en: bool) {
237        self.ccer().modify(|_, w| w.cc2e().bit(en));
238    }
239
240    #[inline(always)]
241    fn set_ch2_cc_value(&mut self, value: u32) {
242        unsafe { self.ccr2().write(|w| w.bits(value)) };
243    }
244
245    #[inline(always)]
246    fn get_ch2_cc_value(&self) -> u32 {
247        self.ccr2().read().bits()
248    }
249}
250
251// sync pwm_ch4
252
253impl TimerWithPwm3Ch for TimerX {
254    #[inline(always)]
255    fn enable_ch3(&mut self, en: bool) {
256        self.ccer().modify(|_, w| w.cc3e().bit(en));
257    }
258
259    #[inline(always)]
260    fn set_ch3_cc_value(&mut self, value: u32) {
261        unsafe { self.ccr3().write(|w| w.bits(value)) };
262    }
263
264    #[inline(always)]
265    fn get_ch3_cc_value(&self) -> u32 {
266        self.ccr3().read().bits()
267    }
268}
269
270impl TimerWithPwm4Ch for TimerX {
271    #[inline(always)]
272    fn enable_ch4(&mut self, en: bool) {
273        self.ccer().modify(|_, w| w.cc4e().bit(en));
274    }
275
276    #[inline(always)]
277    fn set_ch4_cc_value(&mut self, value: u32) {
278        unsafe { self.ccr4().write(|w| w.bits(value)) };
279    }
280
281    #[inline(always)]
282    fn get_ch4_cc_value(&self) -> u32 {
283        self.ccr4().read().bits()
284    }
285}
286
287// Other ----------------------------------------------------------------------
288
289// sync master_type
290type Mms = pac::tim1::cr2::MMS;
291// sync master
292impl MasterTimer for TimerX {
293    type Mms = Mms;
294    #[inline(always)]
295    fn master_mode(&mut self, mode: Self::Mms) {
296        self.cr2().modify(|_, w| w.mms().variant(mode));
297    }
298}
299
300// sync dir
301
302impl TimerDirection for TimerX {
303    #[inline(always)]
304    fn set_count_direction(&mut self, dir: CountDirection) {
305        self.cr1()
306            .modify(|_, w| w.dir().bit(dir == CountDirection::Down));
307    }
308}
309
310// sync end