1use core::mem::ManuallyDrop;
10
11use embassy_hal_internal::{into_ref, Peripheral, PeripheralRef};
12pub use stm32_metapac::timer::vals::{FilterValue, Sms as SlaveMode, Ts as TriggerSource};
14
15use super::*;
16use crate::pac::timer::vals;
17use crate::rcc;
18use crate::time::Hertz;
19
20#[derive(Clone, Copy)]
22pub enum InputCaptureMode {
23 Rising,
25 Falling,
27 BothEdges,
29}
30
31#[derive(Clone, Copy)]
33pub enum InputTISelection {
34 Normal,
36 Alternate,
38 TRC,
40}
41
42impl From<InputTISelection> for stm32_metapac::timer::vals::CcmrInputCcs {
43 fn from(tisel: InputTISelection) -> Self {
44 match tisel {
45 InputTISelection::Normal => stm32_metapac::timer::vals::CcmrInputCcs::TI4,
46 InputTISelection::Alternate => stm32_metapac::timer::vals::CcmrInputCcs::TI3,
47 InputTISelection::TRC => stm32_metapac::timer::vals::CcmrInputCcs::TRC,
48 }
49 }
50}
51
52#[repr(u8)]
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
55pub enum CountingMode {
56 #[default]
57 EdgeAlignedUp,
59 EdgeAlignedDown,
61 CenterAlignedDownInterrupts,
66 CenterAlignedUpInterrupts,
71 CenterAlignedBothInterrupts,
76}
77
78impl CountingMode {
79 pub fn is_edge_aligned(&self) -> bool {
81 matches!(self, CountingMode::EdgeAlignedUp | CountingMode::EdgeAlignedDown)
82 }
83
84 pub fn is_center_aligned(&self) -> bool {
86 matches!(
87 self,
88 CountingMode::CenterAlignedDownInterrupts
89 | CountingMode::CenterAlignedUpInterrupts
90 | CountingMode::CenterAlignedBothInterrupts
91 )
92 }
93}
94
95impl From<CountingMode> for (vals::Cms, vals::Dir) {
96 fn from(value: CountingMode) -> Self {
97 match value {
98 CountingMode::EdgeAlignedUp => (vals::Cms::EDGE_ALIGNED, vals::Dir::UP),
99 CountingMode::EdgeAlignedDown => (vals::Cms::EDGE_ALIGNED, vals::Dir::DOWN),
100 CountingMode::CenterAlignedDownInterrupts => (vals::Cms::CENTER_ALIGNED1, vals::Dir::UP),
101 CountingMode::CenterAlignedUpInterrupts => (vals::Cms::CENTER_ALIGNED2, vals::Dir::UP),
102 CountingMode::CenterAlignedBothInterrupts => (vals::Cms::CENTER_ALIGNED3, vals::Dir::UP),
103 }
104 }
105}
106
107impl From<(vals::Cms, vals::Dir)> for CountingMode {
108 fn from(value: (vals::Cms, vals::Dir)) -> Self {
109 match value {
110 (vals::Cms::EDGE_ALIGNED, vals::Dir::UP) => CountingMode::EdgeAlignedUp,
111 (vals::Cms::EDGE_ALIGNED, vals::Dir::DOWN) => CountingMode::EdgeAlignedDown,
112 (vals::Cms::CENTER_ALIGNED1, _) => CountingMode::CenterAlignedDownInterrupts,
113 (vals::Cms::CENTER_ALIGNED2, _) => CountingMode::CenterAlignedUpInterrupts,
114 (vals::Cms::CENTER_ALIGNED3, _) => CountingMode::CenterAlignedBothInterrupts,
115 }
116 }
117}
118
119#[derive(Clone, Copy)]
121pub enum OutputCompareMode {
122 Frozen,
126 ActiveOnMatch,
129 InactiveOnMatch,
132 Toggle,
134 ForceInactive,
136 ForceActive,
138 PwmMode1,
142 PwmMode2,
146 }
148
149impl From<OutputCompareMode> for stm32_metapac::timer::vals::Ocm {
150 fn from(mode: OutputCompareMode) -> Self {
151 match mode {
152 OutputCompareMode::Frozen => stm32_metapac::timer::vals::Ocm::FROZEN,
153 OutputCompareMode::ActiveOnMatch => stm32_metapac::timer::vals::Ocm::ACTIVE_ON_MATCH,
154 OutputCompareMode::InactiveOnMatch => stm32_metapac::timer::vals::Ocm::INACTIVE_ON_MATCH,
155 OutputCompareMode::Toggle => stm32_metapac::timer::vals::Ocm::TOGGLE,
156 OutputCompareMode::ForceInactive => stm32_metapac::timer::vals::Ocm::FORCE_INACTIVE,
157 OutputCompareMode::ForceActive => stm32_metapac::timer::vals::Ocm::FORCE_ACTIVE,
158 OutputCompareMode::PwmMode1 => stm32_metapac::timer::vals::Ocm::PWM_MODE1,
159 OutputCompareMode::PwmMode2 => stm32_metapac::timer::vals::Ocm::PWM_MODE2,
160 }
161 }
162}
163
164#[derive(Clone, Copy)]
166pub enum OutputPolarity {
167 ActiveHigh,
169 ActiveLow,
171}
172
173impl From<OutputPolarity> for bool {
174 fn from(mode: OutputPolarity) -> Self {
175 match mode {
176 OutputPolarity::ActiveHigh => false,
177 OutputPolarity::ActiveLow => true,
178 }
179 }
180}
181
182pub struct Timer<'d, T: CoreInstance> {
184 tim: PeripheralRef<'d, T>,
185}
186
187impl<'d, T: CoreInstance> Drop for Timer<'d, T> {
188 fn drop(&mut self) {
189 rcc::disable::<T>();
190 }
191}
192
193impl<'d, T: CoreInstance> Timer<'d, T> {
194 pub fn new(tim: impl Peripheral<P = T> + 'd) -> Self {
196 into_ref!(tim);
197
198 rcc::enable_and_reset::<T>();
199
200 Self { tim }
201 }
202
203 pub(crate) unsafe fn clone_unchecked(&self) -> ManuallyDrop<Self> {
204 let tim = unsafe { self.tim.clone_unchecked() };
205 ManuallyDrop::new(Self { tim })
206 }
207
208 pub fn regs_core(&self) -> crate::pac::timer::TimCore {
215 unsafe { crate::pac::timer::TimCore::from_ptr(T::regs()) }
216 }
217
218 #[cfg(not(stm32l0))]
219 fn regs_gp32_unchecked(&self) -> crate::pac::timer::TimGp32 {
220 unsafe { crate::pac::timer::TimGp32::from_ptr(T::regs()) }
221 }
222
223 pub fn start(&self) {
225 self.regs_core().cr1().modify(|r| r.set_cen(true));
226 }
227
228 pub fn stop(&self) {
230 self.regs_core().cr1().modify(|r| r.set_cen(false));
231 }
232
233 pub fn reset(&self) {
235 self.regs_core().cnt().write(|r| r.set_cnt(0));
236 }
237
238 pub fn set_frequency(&self, frequency: Hertz) {
245 match T::BITS {
246 TimerBits::Bits16 => {
247 self.set_frequency_internal(frequency, 16);
248 }
249 #[cfg(not(stm32l0))]
250 TimerBits::Bits32 => {
251 self.set_frequency_internal(frequency, 32);
252 }
253 }
254 }
255
256 pub(crate) fn set_frequency_internal(&self, frequency: Hertz, max_divide_by_bits: u8) {
257 let f = frequency.0;
258 assert!(f > 0);
259 let timer_f = T::frequency().0;
260
261 let pclk_ticks_per_timer_period = (timer_f / f) as u64;
262 let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << max_divide_by_bits)).try_into());
263 let divide_by = pclk_ticks_per_timer_period / (u64::from(psc) + 1);
264
265 match T::BITS {
266 TimerBits::Bits16 => {
267 let arr = unwrap!(u16::try_from(divide_by - 1));
269
270 let regs = self.regs_core();
271 regs.psc().write_value(psc);
272 regs.arr().write(|r| r.set_arr(arr));
273
274 regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTER_ONLY));
275 regs.egr().write(|r| r.set_ug(true));
276 regs.cr1().modify(|r| r.set_urs(vals::Urs::ANY_EVENT));
277 }
278 #[cfg(not(stm32l0))]
279 TimerBits::Bits32 => {
280 let arr: u32 = unwrap!(u32::try_from(divide_by - 1));
282
283 let regs = self.regs_gp32_unchecked();
284 regs.psc().write_value(psc);
285 regs.arr().write_value(arr);
286
287 regs.cr1().modify(|r| r.set_urs(vals::Urs::COUNTER_ONLY));
288 regs.egr().write(|r| r.set_ug(true));
289 regs.cr1().modify(|r| r.set_urs(vals::Urs::ANY_EVENT));
290 }
291 }
292 }
293
294 pub fn set_tick_freq(&mut self, freq: Hertz) {
296 let f = freq;
297 assert!(f.0 > 0);
298 let timer_f = self.get_clock_frequency();
299
300 let pclk_ticks_per_timer_period = timer_f / f;
301 let psc: u16 = unwrap!((pclk_ticks_per_timer_period - 1).try_into());
302
303 let regs = self.regs_core();
304 regs.psc().write_value(psc);
305
306 regs.egr().write(|r| r.set_ug(true));
308 }
309
310 pub fn clear_update_interrupt(&self) -> bool {
314 let regs = self.regs_core();
315 let sr = regs.sr().read();
316 if sr.uif() {
317 regs.sr().modify(|r| {
318 r.set_uif(false);
319 });
320 true
321 } else {
322 false
323 }
324 }
325
326 pub fn enable_update_interrupt(&self, enable: bool) {
328 self.regs_core().dier().modify(|r| r.set_uie(enable));
329 }
330
331 pub fn set_autoreload_preload(&self, enable: bool) {
333 self.regs_core().cr1().modify(|r| r.set_arpe(enable));
334 }
335
336 pub fn get_frequency(&self) -> Hertz {
338 let timer_f = T::frequency();
339
340 match T::BITS {
341 TimerBits::Bits16 => {
342 let regs = self.regs_core();
343 let arr = regs.arr().read().arr();
344 let psc = regs.psc().read();
345
346 timer_f / arr / (psc + 1)
347 }
348 #[cfg(not(stm32l0))]
349 TimerBits::Bits32 => {
350 let regs = self.regs_gp32_unchecked();
351 let arr = regs.arr().read();
352 let psc = regs.psc().read();
353
354 timer_f / arr / (psc + 1)
355 }
356 }
357 }
358
359 pub fn get_clock_frequency(&self) -> Hertz {
361 T::frequency()
362 }
363}
364
365impl<'d, T: BasicNoCr2Instance> Timer<'d, T> {
366 pub fn regs_basic_no_cr2(&self) -> crate::pac::timer::TimBasicNoCr2 {
373 unsafe { crate::pac::timer::TimBasicNoCr2::from_ptr(T::regs()) }
374 }
375
376 pub fn enable_update_dma(&self, enable: bool) {
378 self.regs_basic_no_cr2().dier().modify(|r| r.set_ude(enable));
379 }
380
381 pub fn get_update_dma_state(&self) -> bool {
383 self.regs_basic_no_cr2().dier().read().ude()
384 }
385}
386
387impl<'d, T: BasicInstance> Timer<'d, T> {
388 pub fn regs_basic(&self) -> crate::pac::timer::TimBasic {
395 unsafe { crate::pac::timer::TimBasic::from_ptr(T::regs()) }
396 }
397}
398
399impl<'d, T: GeneralInstance1Channel> Timer<'d, T> {
400 pub fn regs_1ch(&self) -> crate::pac::timer::Tim1ch {
407 unsafe { crate::pac::timer::Tim1ch::from_ptr(T::regs()) }
408 }
409
410 pub fn set_clock_division(&self, ckd: vals::Ckd) {
412 self.regs_1ch().cr1().modify(|r| r.set_ckd(ckd));
413 }
414
415 pub fn get_max_compare_value(&self) -> u32 {
417 match T::BITS {
418 TimerBits::Bits16 => self.regs_1ch().arr().read().arr() as u32,
419 #[cfg(not(stm32l0))]
420 TimerBits::Bits32 => self.regs_gp32_unchecked().arr().read(),
421 }
422 }
423}
424
425impl<'d, T: GeneralInstance2Channel> Timer<'d, T> {
426 pub fn regs_2ch(&self) -> crate::pac::timer::Tim2ch {
433 unsafe { crate::pac::timer::Tim2ch::from_ptr(T::regs()) }
434 }
435}
436
437impl<'d, T: GeneralInstance4Channel> Timer<'d, T> {
438 pub fn regs_gp16(&self) -> crate::pac::timer::TimGp16 {
445 unsafe { crate::pac::timer::TimGp16::from_ptr(T::regs()) }
446 }
447
448 pub fn enable_outputs(&self) {
450 self.tim.enable_outputs()
451 }
452
453 pub fn set_counting_mode(&self, mode: CountingMode) {
455 let (cms, dir) = mode.into();
456
457 let timer_enabled = self.regs_core().cr1().read().cen();
458 assert!(!timer_enabled);
461
462 self.regs_gp16().cr1().modify(|r| r.set_dir(dir));
463 self.regs_gp16().cr1().modify(|r| r.set_cms(cms))
464 }
465
466 pub fn get_counting_mode(&self) -> CountingMode {
468 let cr1 = self.regs_gp16().cr1().read();
469 (cr1.cms(), cr1.dir()).into()
470 }
471
472 pub fn set_input_capture_filter(&self, channel: Channel, icf: vals::FilterValue) {
474 let raw_channel = channel.index();
475 self.regs_gp16()
476 .ccmr_input(raw_channel / 2)
477 .modify(|r| r.set_icf(raw_channel % 2, icf));
478 }
479
480 pub fn clear_input_interrupt(&self, channel: Channel) {
482 self.regs_gp16().sr().modify(|r| r.set_ccif(channel.index(), false));
483 }
484
485 pub fn get_input_interrupt(&self, channel: Channel) -> bool {
487 self.regs_gp16().sr().read().ccif(channel.index())
488 }
489
490 pub fn enable_input_interrupt(&self, channel: Channel, enable: bool) {
492 self.regs_gp16().dier().modify(|r| r.set_ccie(channel.index(), enable));
493 }
494
495 pub fn set_input_capture_prescaler(&self, channel: Channel, factor: u8) {
497 let raw_channel = channel.index();
498 self.regs_gp16()
499 .ccmr_input(raw_channel / 2)
500 .modify(|r| r.set_icpsc(raw_channel % 2, factor));
501 }
502
503 pub fn set_input_ti_selection(&self, channel: Channel, tisel: InputTISelection) {
505 let raw_channel = channel.index();
506 self.regs_gp16()
507 .ccmr_input(raw_channel / 2)
508 .modify(|r| r.set_ccs(raw_channel % 2, tisel.into()));
509 }
510
511 pub fn set_input_capture_mode(&self, channel: Channel, mode: InputCaptureMode) {
513 self.regs_gp16().ccer().modify(|r| match mode {
514 InputCaptureMode::Rising => {
515 r.set_ccnp(channel.index(), false);
516 r.set_ccp(channel.index(), false);
517 }
518 InputCaptureMode::Falling => {
519 r.set_ccnp(channel.index(), false);
520 r.set_ccp(channel.index(), true);
521 }
522 InputCaptureMode::BothEdges => {
523 r.set_ccnp(channel.index(), true);
524 r.set_ccp(channel.index(), true);
525 }
526 });
527 }
528
529 pub fn set_output_compare_mode(&self, channel: Channel, mode: OutputCompareMode) {
531 let raw_channel: usize = channel.index();
532 self.regs_gp16()
533 .ccmr_output(raw_channel / 2)
534 .modify(|w| w.set_ocm(raw_channel % 2, mode.into()));
535 }
536
537 pub fn set_output_polarity(&self, channel: Channel, polarity: OutputPolarity) {
539 self.regs_gp16()
540 .ccer()
541 .modify(|w| w.set_ccp(channel.index(), polarity.into()));
542 }
543
544 pub fn enable_channel(&self, channel: Channel, enable: bool) {
546 self.regs_gp16().ccer().modify(|w| w.set_cce(channel.index(), enable));
547 }
548
549 pub fn get_channel_enable_state(&self, channel: Channel) -> bool {
551 self.regs_gp16().ccer().read().cce(channel.index())
552 }
553
554 pub fn set_compare_value(&self, channel: Channel, value: u32) {
556 match T::BITS {
557 TimerBits::Bits16 => {
558 let value = unwrap!(u16::try_from(value));
559 self.regs_gp16().ccr(channel.index()).modify(|w| w.set_ccr(value));
560 }
561 #[cfg(not(stm32l0))]
562 TimerBits::Bits32 => {
563 self.regs_gp32_unchecked().ccr(channel.index()).write_value(value);
564 }
565 }
566 }
567
568 pub fn get_compare_value(&self, channel: Channel) -> u32 {
570 match T::BITS {
571 TimerBits::Bits16 => self.regs_gp16().ccr(channel.index()).read().ccr() as u32,
572 #[cfg(not(stm32l0))]
573 TimerBits::Bits32 => self.regs_gp32_unchecked().ccr(channel.index()).read(),
574 }
575 }
576
577 pub fn get_capture_value(&self, channel: Channel) -> u32 {
579 self.get_compare_value(channel)
580 }
581
582 pub fn set_output_compare_preload(&self, channel: Channel, preload: bool) {
584 let channel_index = channel.index();
585 self.regs_gp16()
586 .ccmr_output(channel_index / 2)
587 .modify(|w| w.set_ocpe(channel_index % 2, preload));
588 }
589
590 pub fn get_cc_dma_selection(&self) -> vals::Ccds {
592 self.regs_gp16().cr2().read().ccds()
593 }
594
595 pub fn set_cc_dma_selection(&self, ccds: vals::Ccds) {
597 self.regs_gp16().cr2().modify(|w| w.set_ccds(ccds))
598 }
599
600 pub fn get_cc_dma_enable_state(&self, channel: Channel) -> bool {
602 self.regs_gp16().dier().read().ccde(channel.index())
603 }
604
605 pub fn set_cc_dma_enable_state(&self, channel: Channel, ccde: bool) {
607 self.regs_gp16().dier().modify(|w| w.set_ccde(channel.index(), ccde))
608 }
609
610 pub fn set_slave_mode(&self, sms: SlaveMode) {
612 self.regs_gp16().smcr().modify(|r| r.set_sms(sms));
613 }
614
615 pub fn set_trigger_source(&self, ts: TriggerSource) {
617 self.regs_gp16().smcr().modify(|r| r.set_ts(ts));
618 }
619}
620
621#[cfg(not(stm32l0))]
622impl<'d, T: GeneralInstance32bit4Channel> Timer<'d, T> {
623 pub fn regs_gp32(&self) -> crate::pac::timer::TimGp32 {
630 unsafe { crate::pac::timer::TimGp32::from_ptr(T::regs()) }
631 }
632}
633
634#[cfg(not(stm32l0))]
635impl<'d, T: AdvancedInstance1Channel> Timer<'d, T> {
636 pub fn regs_1ch_cmp(&self) -> crate::pac::timer::Tim1chCmp {
643 unsafe { crate::pac::timer::Tim1chCmp::from_ptr(T::regs()) }
644 }
645
646 pub fn set_dead_time_clock_division(&self, value: vals::Ckd) {
648 self.regs_1ch_cmp().cr1().modify(|w| w.set_ckd(value));
649 }
650
651 pub fn set_dead_time_value(&self, value: u8) {
653 self.regs_1ch_cmp().bdtr().modify(|w| w.set_dtg(value));
654 }
655
656 pub fn set_moe(&self, enable: bool) {
658 self.regs_1ch_cmp().bdtr().modify(|w| w.set_moe(enable));
659 }
660}
661
662#[cfg(not(stm32l0))]
663impl<'d, T: AdvancedInstance2Channel> Timer<'d, T> {
664 pub fn regs_2ch_cmp(&self) -> crate::pac::timer::Tim2chCmp {
671 unsafe { crate::pac::timer::Tim2chCmp::from_ptr(T::regs()) }
672 }
673}
674
675#[cfg(not(stm32l0))]
676impl<'d, T: AdvancedInstance4Channel> Timer<'d, T> {
677 pub fn regs_advanced(&self) -> crate::pac::timer::TimAdv {
679 unsafe { crate::pac::timer::TimAdv::from_ptr(T::regs()) }
680 }
681
682 pub fn set_complementary_output_polarity(&self, channel: Channel, polarity: OutputPolarity) {
684 self.regs_advanced()
685 .ccer()
686 .modify(|w| w.set_ccnp(channel.index(), polarity.into()));
687 }
688
689 pub fn enable_complementary_channel(&self, channel: Channel, enable: bool) {
691 self.regs_advanced()
692 .ccer()
693 .modify(|w| w.set_ccne(channel.index(), enable));
694 }
695}