stm32f1_hal/timer/
timer8.rs1type TimerX = pac::TIM8;
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, update_freq: Hertz) {
99 let (prescaler, arr) = compute_prescaler_arr(clock.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_tim8_stop().bit(state));
138 }
140
141 #[inline(always)]
142 fn enable_preload(&mut self, b: bool) {
143 self.cr1().modify(|_, w| w.arpe().bit(b));
144 }
145}
146
147impl TimerWithPwm for TimerX {
151 fn stop_pwm(&mut self) {
152 self.disable_counter();
153 }
154
155 #[inline(always)]
158 fn start_pwm(&mut self) {
159 self.bdtr().modify(|_, w| w.aoe().set_bit());
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
287impl MasterTimer for TimerX {
291 #[inline(always)]
292 fn master_mode(&mut self, mode: MasterMode) {
293 self.cr2().modify(|_, w| w.mms().variant(mode.into()));
294 }
295}
296
297impl TimerDirection for TimerX {
300 #[inline(always)]
301 fn set_count_direction(&mut self, dir: CountDirection) {
302 self.cr1()
303 .modify(|_, w| w.dir().bit(dir == CountDirection::Down));
304 }
305}
306
307#[cfg(feature = "rtic")]
309mod timer_rtic {
310 use super::*;
311 use crate::Mcu;
312 use rtic_monotonic::Monotonic;
313
314 impl MonoTimerExt for TimerX {
315 fn monotonic<const FREQ: u32>(self, mcu: &mut Mcu) -> MonoTimer<Self, FREQ> {
316 mcu.rcc.enable(&self);
317 mcu.rcc.reset(&self);
318 let clk = mcu.rcc.get_timer_clock(&self);
319 FTimer::new(self, clk).monotonic()
320 }
321 }
322
323 impl<const FREQ: u32> FTimer<TimerX, FREQ> {
324 pub fn monotonic(self) -> MonoTimer<TimerX, FREQ> {
325 MonoTimer::<TimerX, FREQ>::_new(self)
326 }
327 }
328
329 impl<const FREQ: u32> MonoTimer<TimerX, FREQ> {
330 fn _new(timer: FTimer<TimerX, FREQ>) -> Self {
331 timer.tim.arr().write(|w| w.arr().set(u16::MAX));
333 timer.tim.egr().write(|w| w.ug().set_bit());
335
336 timer.tim.sr().modify(|_, w| w.uif().clear_bit());
339 timer.tim.cr1().modify(|_, w| {
340 w.cen().set_bit();
342 w.udis().clear_bit();
344 w.urs().set_bit()
346 });
347
348 Self { timer, ovf: 0 }
349 }
350 }
351
352 impl<const FREQ: u32> Monotonic for MonoTimer<TimerX, FREQ> {
353 type Instant = fugit::TimerInstantU32<FREQ>;
354 type Duration = fugit::TimerDurationU32<FREQ>;
355
356 unsafe fn reset(&mut self) {
357 self.tim.dier().modify(|_, w| w.cc1ie().set_bit());
358 }
359
360 #[inline(always)]
361 fn now(&mut self) -> Self::Instant {
362 let cnt = self.tim.cnt().read().cnt().bits() as u32;
363
364 let ovf = if self.tim.sr().read().uif().bit_is_set() {
367 0x10000
368 } else {
369 0
370 };
371
372 Self::Instant::from_ticks(cnt.wrapping_add(ovf).wrapping_add(self.ovf))
373 }
374
375 fn set_compare(&mut self, instant: Self::Instant) {
376 let now = self.now();
377 let cnt = self.tim.cnt().read().cnt().bits();
378
379 let val = match instant.checked_duration_since(now) {
382 None => cnt.wrapping_add(0xffff), Some(x) if x.ticks() <= 0xffff => instant.duration_since_epoch().ticks() as u16, Some(_) => cnt.wrapping_add(0xffff), };
386
387 self.tim.ccr1().write(|w| w.ccr().set(val));
388 }
389
390 fn clear_compare_flag(&mut self) {
391 self.tim.sr().modify(|_, w| w.cc1if().clear_bit());
392 }
393
394 fn on_interrupt(&mut self) {
395 if self.tim.sr().read().uif().bit_is_set() {
397 self.tim.sr().modify(|_, w| w.uif().clear_bit());
398
399 self.ovf += 0x10000;
400 }
401 }
402
403 #[inline(always)]
404 fn zero() -> Self::Instant {
405 Self::Instant::from_ticks(0)
406 }
407 }
408}
409
410