stm32f1_hal/timer/
timer3.rs

1type TimerX = pac::TIM3;
2type Width = u16;
3
4// Do NOT manually modify the code between begin and end!
5// It's synced by scripts/sync_code.py.
6// sync begin
7
8use super::*;
9use crate::{Mcu, pac};
10
11impl Instance for TimerX {}
12
13impl TimerInit<TimerX> for TimerX {
14    fn constrain(self, mcu: &mut Mcu) -> Timer<TimerX> {
15        Timer::new(self, mcu)
16    }
17}
18
19impl GeneralTimer for TimerX {
20    #[inline(always)]
21    fn reset_config(&mut self) {
22        self.cr1().reset();
23    }
24
25    #[inline(always)]
26    fn enable_counter(&mut self) {
27        self.cr1().modify(|_, w| w.cen().set_bit());
28    }
29
30    #[inline(always)]
31    fn disable_counter(&mut self) {
32        self.cr1().modify(|_, w| w.cen().clear_bit());
33    }
34
35    #[inline(always)]
36    fn is_counter_enabled(&self) -> bool {
37        self.cr1().read().cen().is_enabled()
38    }
39
40    #[inline(always)]
41    fn reset_counter(&mut self) {
42        self.cnt().reset();
43    }
44
45    #[inline(always)]
46    fn max_auto_reload() -> u32 {
47        Width::MAX as u32
48    }
49
50    #[inline(always)]
51    unsafe fn set_auto_reload_unchecked(&mut self, arr: u32) {
52        unsafe {
53            self.arr().write(|w| w.bits(arr));
54        }
55    }
56
57    #[inline(always)]
58    fn set_auto_reload(&mut self, arr: u32) -> Result<(), Error> {
59        // Note: Make it impossible to set the ARR value to 0, since this
60        // would cause an infinite loop.
61        if arr > 0 && arr <= Self::max_auto_reload() {
62            Ok(unsafe { self.set_auto_reload_unchecked(arr) })
63        } else {
64            Err(Error::WrongAutoReload)
65        }
66    }
67
68    #[inline(always)]
69    fn read_auto_reload(&self) -> u32 {
70        self.arr().read().bits()
71    }
72
73    #[inline(always)]
74    fn set_prescaler(&mut self, psc: u16) {
75        self.psc().write(|w| w.psc().set(psc));
76    }
77
78    #[inline(always)]
79    fn read_prescaler(&self) -> u16 {
80        self.psc().read().psc().bits()
81    }
82
83    #[inline(always)]
84    fn read_count(&self) -> u32 {
85        self.cnt().read().bits() as u32
86    }
87
88    #[inline(always)]
89    fn trigger_update(&mut self) {
90        // Sets the URS bit to prevent an interrupt from being triggered by
91        // the UG bit
92        self.cr1().modify(|_, w| w.urs().set_bit());
93        self.egr().write(|w| w.ug().set_bit());
94        self.cr1().modify(|_, w| w.urs().clear_bit());
95    }
96
97    #[inline]
98    fn config_freq(&mut self, clock: Hertz, count_freq: Hertz, update_freq: Hertz) {
99        let (prescaler, arr) = freq_to_presc_arr(clock.raw(), count_freq.raw(), update_freq.raw());
100        self.set_prescaler(prescaler as u16);
101        self.set_auto_reload(arr).unwrap();
102        // Trigger update event to load the registers
103        self.trigger_update();
104    }
105
106    #[inline(always)]
107    fn clear_interrupt_flag(&mut self, event: Event) {
108        self.sr()
109            .write(|w| unsafe { w.bits(0xffff & !event.bits()) });
110    }
111
112    #[inline(always)]
113    fn listen_interrupt(&mut self, event: Event, b: bool) {
114        self.dier().modify(|r, w| unsafe {
115            w.bits(if b {
116                r.bits() | event.bits()
117            } else {
118                r.bits() & !event.bits()
119            })
120        });
121    }
122
123    #[inline(always)]
124    fn get_interrupt_flag(&self) -> Event {
125        Event::from_bits_truncate(self.sr().read().bits())
126    }
127
128    #[inline(always)]
129    fn start_one_pulse(&mut self) {
130        self.cr1().modify(|_, w| w.opm().set_bit().cen().set_bit());
131    }
132
133    #[inline(always)]
134    fn stop_in_debug(&mut self, state: bool) {
135        let dbg = unsafe { DBG::steal() };
136        // sync dbg_t3
137        dbg.cr().modify(|_, w| w.dbg_tim3_stop().bit(state));
138        // sync dbg_end
139    }
140}
141
142impl GeneralTimerExt for TimerX {
143    #[inline(always)]
144    fn enable_preload(&mut self, b: bool) {
145        self.cr1().modify(|_, w| w.arpe().bit(b));
146    }
147}
148
149// sync pwm
150// PWM ------------------------------------------------------------------------
151
152impl TimerWithPwm for TimerX {
153    fn stop_pwm(&mut self) {
154        self.disable_counter();
155    }
156
157    // sync start_pwm
158
159    #[inline(always)]
160    fn start_pwm(&mut self) {
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_t2
290type Mms = pac::tim2::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