stm32f1_hal/timer/
timer2.rs1type TimerX = pac::TIM2;
2type Width = u16;
3
4use 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 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 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 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 dbg.cr().modify(|_, w| w.dbg_tim2_stop().bit(state));
138 }
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
149impl TimerWithPwm for TimerX {
153 fn stop_pwm(&mut self) {
154 self.disable_counter();
155 }
156
157 #[inline(always)]
160 fn start_pwm(&mut self) {
161 self.reset_counter();
162 self.enable_counter();
163 }
164
165 #[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
212impl 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
232impl 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
251impl 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
287type Mms = pac::tim2::cr2::MMS;
291impl 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
300impl 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