stm32f7xx_hal/
timer.rs

1//! Interface to the timer peripherals
2
3#![allow(non_upper_case_globals)]
4
5use core::convert::TryFrom;
6use cortex_m::peripheral::syst::SystClkSource;
7use cortex_m::peripheral::SYST;
8
9use crate::pac;
10
11use crate::rcc::{self, Clocks};
12use fugit::HertzU32 as Hertz;
13
14pub mod counter;
15pub use counter::*;
16pub mod delay;
17pub use delay::*;
18mod pins;
19pub use pins::*;
20pub mod pwm;
21pub use pwm::*;
22pub mod pwm_input;
23pub use pwm_input::PwmInput;
24#[cfg(feature = "rtic")]
25pub mod monotonic;
26#[cfg(feature = "rtic")]
27pub use monotonic::*;
28
29mod hal_02;
30
31/// Timer wrapper
32pub struct Timer<TIM> {
33    pub(crate) tim: TIM,
34    pub(crate) clk: Hertz,
35}
36
37#[derive(Clone, Copy, PartialEq)]
38#[repr(u8)]
39pub enum Channel {
40    C1 = 0,
41    C2 = 1,
42    C3 = 2,
43    C4 = 3,
44}
45
46/// Interrupt events
47#[derive(Clone, Copy, PartialEq)]
48pub enum SysEvent {
49    /// [Timer] timed out / count down ended
50    Update,
51}
52
53bitflags::bitflags! {
54    pub struct Event: u32 {
55        const Update  = 1 << 0;
56        const C1 = 1 << 1;
57        const C2 = 1 << 2;
58        const C3 = 1 << 3;
59        const C4 = 1 << 4;
60    }
61}
62
63#[derive(Debug, Eq, PartialEq, Copy, Clone)]
64pub enum Error {
65    /// Timer is disabled
66    Disabled,
67    WrongAutoReload,
68}
69
70pub trait TimerExt: Sized {
71    /// Non-blocking [Counter] with custom fixed precision
72    fn counter<const FREQ: u32>(self, clocks: &Clocks) -> Counter<Self, FREQ>;
73    /// Non-blocking [Counter] with fixed precision of 1 ms (1 kHz sampling)
74    ///
75    /// Can wait from 2 ms to 65 sec for 16-bit timer and from 2 ms to 49 days for 32-bit timer.
76    ///
77    /// NOTE: don't use this if your system frequency more than 65 MHz
78    fn counter_ms(self, clocks: &Clocks) -> CounterMs<Self> {
79        self.counter::<1_000>(clocks)
80    }
81    /// Non-blocking [Counter] with fixed precision of 1 μs (1 MHz sampling)
82    ///
83    /// Can wait from 2 μs to 65 ms for 16-bit timer and from 2 μs to 71 min for 32-bit timer.
84    fn counter_us(self, clocks: &Clocks) -> CounterUs<Self> {
85        self.counter::<1_000_000>(clocks)
86    }
87    /// Non-blocking [Counter] with dynamic precision which uses `Hertz` as Duration units
88    fn counter_hz(self, clocks: &Clocks) -> CounterHz<Self>;
89
90    /// Blocking [Delay] with custom fixed precision
91    fn delay<const FREQ: u32>(self, clocks: &Clocks) -> Delay<Self, FREQ>;
92    /// Blocking [Delay] with fixed precision of 1 ms (1 kHz sampling)
93    ///
94    /// Can wait from 2 ms to 49 days.
95    ///
96    /// NOTE: don't use this if your system frequency more than 65 MHz
97    fn delay_ms(self, clocks: &Clocks) -> DelayMs<Self> {
98        self.delay::<1_000>(clocks)
99    }
100    /// Blocking [Delay] with fixed precision of 1 μs (1 MHz sampling)
101    ///
102    /// Can wait from 2 μs to 71 min.
103    fn delay_us(self, clocks: &Clocks) -> DelayUs<Self> {
104        self.delay::<1_000_000>(clocks)
105    }
106}
107
108impl<TIM: Instance> TimerExt for TIM {
109    fn counter<const FREQ: u32>(self, clocks: &Clocks) -> Counter<Self, FREQ> {
110        FTimer::new(self, clocks).counter()
111    }
112    fn counter_hz(self, clocks: &Clocks) -> CounterHz<Self> {
113        Timer::new(self, clocks).counter_hz()
114    }
115    fn delay<const FREQ: u32>(self, clocks: &Clocks) -> Delay<Self, FREQ> {
116        FTimer::new(self, clocks).delay()
117    }
118}
119
120pub trait SysTimerExt: Sized {
121    /// Creates timer which takes [Hertz] as Duration
122    fn counter_hz(self, clocks: &Clocks) -> SysCounterHz;
123
124    /// Creates timer with custom precision (core frequency recommended is known)
125    fn counter<const FREQ: u32>(self, clocks: &Clocks) -> SysCounter<FREQ>;
126    /// Creates timer with precision of 1 μs (1 MHz sampling)
127    fn counter_us(self, clocks: &Clocks) -> SysCounterUs {
128        self.counter::<1_000_000>(clocks)
129    }
130    /// Blocking [Delay] with custom precision
131    fn delay(self, clocks: &Clocks) -> SysDelay;
132}
133
134impl SysTimerExt for SYST {
135    fn counter_hz(self, clocks: &Clocks) -> SysCounterHz {
136        Timer::syst(self, clocks).counter_hz()
137    }
138    fn counter<const FREQ: u32>(self, clocks: &Clocks) -> SysCounter<FREQ> {
139        Timer::syst(self, clocks).counter()
140    }
141    fn delay(self, clocks: &Clocks) -> SysDelay {
142        Timer::syst_external(self, clocks).delay()
143    }
144}
145
146impl Timer<SYST> {
147    /// Initialize SysTick timer
148    pub fn syst(mut tim: SYST, clocks: &Clocks) -> Self {
149        tim.set_clock_source(SystClkSource::Core);
150        Self {
151            tim,
152            clk: clocks.hclk(),
153        }
154    }
155
156    /// Initialize SysTick timer and set it frequency to `HCLK / 8`
157    pub fn syst_external(mut tim: SYST, clocks: &Clocks) -> Self {
158        tim.set_clock_source(SystClkSource::External);
159        Self {
160            tim,
161            clk: clocks.hclk() / 8,
162        }
163    }
164
165    pub fn configure(&mut self, clocks: &Clocks) {
166        self.tim.set_clock_source(SystClkSource::Core);
167        self.clk = clocks.hclk();
168    }
169
170    pub fn configure_external(&mut self, clocks: &Clocks) {
171        self.tim.set_clock_source(SystClkSource::External);
172        self.clk = clocks.hclk() / 8;
173    }
174
175    pub fn release(self) -> SYST {
176        self.tim
177    }
178
179    /// Starts listening for an `event`
180    pub fn listen(&mut self, event: SysEvent) {
181        match event {
182            SysEvent::Update => self.tim.enable_interrupt(),
183        }
184    }
185
186    /// Stops listening for an `event`
187    pub fn unlisten(&mut self, event: SysEvent) {
188        match event {
189            SysEvent::Update => self.tim.disable_interrupt(),
190        }
191    }
192}
193
194#[derive(Clone, Copy, Debug, PartialEq)]
195#[repr(u8)]
196pub enum Ocm {
197    Frozen = 0,
198    ActiveOnMatch = 1,
199    InactiveOnMatch = 2,
200    Toggle = 3,
201    ForceInactive = 4,
202    ForceActive = 5,
203    PwmMode1 = 6,
204    PwmMode2 = 7,
205}
206
207mod sealed {
208    use super::{Channel, Event, Ocm};
209    pub trait General {
210        type Width: Into<u32> + From<u16>;
211        fn max_auto_reload() -> u32;
212        unsafe fn set_auto_reload_unchecked(&mut self, arr: u32);
213        fn set_auto_reload(&mut self, arr: u32) -> Result<(), super::Error>;
214        fn read_auto_reload() -> u32;
215        fn enable_preload(&mut self, b: bool);
216        fn enable_counter(&mut self);
217        fn disable_counter(&mut self);
218        fn is_counter_enabled(&self) -> bool;
219        fn reset_counter(&mut self);
220        fn set_prescaler(&mut self, psc: u16);
221        fn read_prescaler(&self) -> u16;
222        fn trigger_update(&mut self);
223        fn clear_interrupt_flag(&mut self, event: Event);
224        fn listen_interrupt(&mut self, event: Event, b: bool);
225        fn get_interrupt_flag(&self) -> Event;
226        fn read_count(&self) -> Self::Width;
227        fn start_one_pulse(&mut self);
228        fn start_no_update(&mut self);
229        fn cr1_reset(&mut self);
230    }
231
232    pub trait WithPwm: General {
233        const CH_NUMBER: u8;
234        fn read_cc_value(channel: u8) -> u32;
235        fn set_cc_value(channel: u8, value: u32);
236        fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: Ocm);
237        fn start_pwm(&mut self);
238        fn enable_channel(channel: u8, b: bool);
239    }
240
241    pub trait MasterTimer: General {
242        type Mms;
243        fn master_mode(&mut self, mode: Self::Mms);
244    }
245}
246pub(crate) use sealed::{General, MasterTimer, WithPwm};
247
248pub trait Instance:
249    crate::Sealed + rcc::Enable + rcc::Reset + rcc::BusTimerClock + General
250{
251}
252
253macro_rules! hal {
254    ($($TIM:ty: [
255        $Timer:ident,
256        $bits:ty,
257        $(c: ($cnum:ident $(, $aoe:ident)?),)?
258        $(m: $timbase:ident,)?
259    ],)+) => {
260        $(
261            impl Instance for $TIM { }
262            pub type $Timer = Timer<$TIM>;
263
264            impl General for $TIM {
265                type Width = $bits;
266
267                #[inline(always)]
268                fn max_auto_reload() -> u32 {
269                    <$bits>::MAX as u32
270                }
271                #[inline(always)]
272                unsafe fn set_auto_reload_unchecked(&mut self, arr: u32) {
273                    self.arr.write(|w| w.bits(arr))
274                }
275                #[inline(always)]
276                fn set_auto_reload(&mut self, arr: u32) -> Result<(), Error> {
277                    // Note: Make it impossible to set the ARR value to 0, since this
278                    // would cause an infinite loop.
279                    if arr > 0 && arr <= Self::max_auto_reload() {
280                        Ok(unsafe { self.set_auto_reload_unchecked(arr) })
281                    } else {
282                        Err(Error::WrongAutoReload)
283                    }
284                }
285                #[inline(always)]
286                fn read_auto_reload() -> u32 {
287                    let tim = unsafe { &*<$TIM>::ptr() };
288                    tim.arr.read().bits()
289                }
290                #[inline(always)]
291                fn enable_preload(&mut self, b: bool) {
292                    self.cr1.modify(|_, w| w.arpe().bit(b));
293                }
294                #[inline(always)]
295                fn enable_counter(&mut self) {
296                    self.cr1.modify(|_, w| w.cen().set_bit());
297                }
298                #[inline(always)]
299                fn disable_counter(&mut self) {
300                    self.cr1.modify(|_, w| w.cen().clear_bit());
301                }
302                #[inline(always)]
303                fn is_counter_enabled(&self) -> bool {
304                    self.cr1.read().cen().is_enabled()
305                }
306                #[inline(always)]
307                fn reset_counter(&mut self) {
308                    self.cnt.reset();
309                }
310                #[inline(always)]
311                fn set_prescaler(&mut self, psc: u16) {
312                    self.psc.write(|w| w.psc().bits(psc) );
313                }
314                #[inline(always)]
315                fn read_prescaler(&self) -> u16 {
316                    self.psc.read().psc().bits()
317                }
318                #[inline(always)]
319                fn trigger_update(&mut self) {
320                    self.cr1.modify(|_, w| w.urs().set_bit());
321                    self.egr.write(|w| w.ug().set_bit());
322                    self.cr1.modify(|_, w| w.urs().clear_bit());
323                }
324                #[inline(always)]
325                fn clear_interrupt_flag(&mut self, event: Event) {
326                    self.sr.write(|w| unsafe { w.bits(0xffff & !event.bits()) });
327                }
328                #[inline(always)]
329                fn listen_interrupt(&mut self, event: Event, b: bool) {
330                    if b {
331                        self.dier.modify(|r, w| unsafe { w.bits(r.bits() | event.bits()) });
332                    } else {
333                        self.dier.modify(|r, w| unsafe { w.bits(r.bits() & !event.bits()) });
334                    }
335                }
336                #[inline(always)]
337                fn get_interrupt_flag(&self) -> Event {
338                    Event::from_bits_truncate(self.sr.read().bits())
339                }
340                #[inline(always)]
341                fn read_count(&self) -> Self::Width {
342                    self.cnt.read().bits() as Self::Width
343                }
344                #[inline(always)]
345                fn start_one_pulse(&mut self) {
346                    self.cr1.write(|w| unsafe { w.bits(1 << 3) }.cen().set_bit());
347                }
348                #[inline(always)]
349                fn start_no_update(&mut self) {
350                    self.cr1.write(|w| w.cen().set_bit().udis().set_bit());
351                }
352                #[inline(always)]
353                fn cr1_reset(&mut self) {
354                    self.cr1.reset();
355                }
356            }
357            $(with_pwm!($TIM: $cnum $(, $aoe)?);)?
358
359            $(impl MasterTimer for $TIM {
360                type Mms = pac::$timbase::cr2::MMS_A;
361                fn master_mode(&mut self, mode: Self::Mms) {
362                    self.cr2.modify(|_,w| w.mms().variant(mode));
363                }
364            })?
365        )+
366    }
367}
368
369macro_rules! with_pwm {
370    ($TIM:ty: CH1) => {
371        impl WithPwm for $TIM {
372            const CH_NUMBER: u8 = 1;
373
374            #[inline(always)]
375            fn read_cc_value(channel: u8) -> u32 {
376                let tim = unsafe { &*<$TIM>::ptr() };
377                match channel {
378                    0 => {
379                        tim.ccr1().read().bits()
380                    }
381                    _ => 0,
382                }
383            }
384
385            #[inline(always)]
386            fn set_cc_value(channel: u8, value: u32) {
387                let tim = unsafe { &*<$TIM>::ptr() };
388                #[allow(unused_unsafe)]
389                match channel {
390                    0 => {
391                        tim.ccr1().write(|w| unsafe { w.bits(value) })
392                    }
393                    _ => {},
394                }
395            }
396
397            #[inline(always)]
398            fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: Ocm) {
399                match channel {
400                    Channel::C1 => {
401                        self.ccmr1_output()
402                        .modify(|_, w| w.oc1pe().set_bit().oc1m().bits(mode as _) );
403                    }
404                    _ => {},
405                }
406            }
407
408            #[inline(always)]
409            fn start_pwm(&mut self) {
410                self.cr1.write(|w| w.cen().set_bit());
411            }
412
413            #[inline(always)]
414            fn enable_channel(c: u8, b: bool) {
415                let tim = unsafe { &*<$TIM>::ptr() };
416                if c < Self::CH_NUMBER {
417                    let mask = (1 << c*4);
418                    if b {
419                        tim.ccer.modify(|r, w| unsafe { w.bits(r.bits() | mask) });
420                    } else {
421                        tim.ccer.modify(|r, w| unsafe { w.bits(r.bits() & !mask) });
422                    }
423                }
424            }
425        }
426    };
427    ($TIM:ty: CH2) => {
428        impl WithPwm for $TIM {
429            const CH_NUMBER: u8 = 2;
430
431            #[inline(always)]
432            fn read_cc_value(channel: u8) -> u32 {
433                let tim = unsafe { &*<$TIM>::ptr() };
434                match channel {
435                    0 => {
436                        tim.ccr1().read().bits()
437                    }
438                    1 => {
439                        tim.ccr2().read().bits()
440                    }
441                    _ => 0,
442                }
443            }
444
445            #[inline(always)]
446            fn set_cc_value(channel: u8, value: u32) {
447                let tim = unsafe { &*<$TIM>::ptr() };
448                #[allow(unused_unsafe)]
449                match channel {
450                    0 => {
451                        tim.ccr1().write(|w| unsafe { w.bits(value) })
452                    }
453                    1 => {
454                        tim.ccr2().write(|w| unsafe { w.bits(value) })
455                    }
456                    _ => {},
457                }
458            }
459
460            #[inline(always)]
461            fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: Ocm) {
462                match channel {
463                    Channel::C1 => {
464                        self.ccmr1_output()
465                        .modify(|_, w| w.oc1pe().set_bit().oc1m().bits(mode as _) );
466                    }
467                    Channel::C2 => {
468                        self.ccmr1_output()
469                        .modify(|_, w| w.oc2pe().set_bit().oc2m().bits(mode as _) );
470                    }
471                    _ => {},
472                }
473            }
474
475            #[inline(always)]
476            fn start_pwm(&mut self) {
477                self.cr1.write(|w| w.cen().set_bit());
478            }
479
480            #[inline(always)]
481            fn enable_channel(c: u8, b: bool) {
482                let tim = unsafe { &*<$TIM>::ptr() };
483                if c < Self::CH_NUMBER {
484                    let mask = (1 << c*4);
485                    if b {
486                        tim.ccer.modify(|r, w| unsafe { w.bits(r.bits() | mask) });
487                    } else {
488                        tim.ccer.modify(|r, w| unsafe { w.bits(r.bits() & !mask) });
489                    }
490                }
491            }
492        }
493    };
494    ($TIM:ty: CH4 $(, $aoe:ident)?) => {
495        impl WithPwm for $TIM {
496            const CH_NUMBER: u8 = 4;
497
498            #[inline(always)]
499            fn read_cc_value(channel: u8) -> u32 {
500                let tim = unsafe { &*<$TIM>::ptr() };
501                let ccr = match channel {
502                    0 => {
503                        tim.ccr1()
504                    }
505                    1 => {
506                        tim.ccr2()
507                    }
508                    2 => {
509                        tim.ccr3()
510                    }
511                    _ => {
512                        tim.ccr4()
513                    }
514                };
515                ccr.read().bits()
516            }
517
518            #[inline(always)]
519            fn set_cc_value(channel: u8, value: u32) {
520                let tim = unsafe { &*<$TIM>::ptr() };
521                let ccr = match channel {
522                    0 => {
523                        tim.ccr1()
524                    }
525                    1 => {
526                        tim.ccr2()
527                    }
528                    2 => {
529                        tim.ccr3()
530                    }
531                    _ => {
532                        tim.ccr4()
533                    }
534                };
535                ccr.write(|w| unsafe { w.bits(value) })
536            }
537
538            #[inline(always)]
539            fn preload_output_channel_in_mode(&mut self, channel: Channel, mode: Ocm) {
540                match channel {
541                    Channel::C1 => {
542                        self.ccmr1_output()
543                        .modify(|_, w| w.oc1pe().set_bit().oc1m().bits(mode as _) );
544                    }
545                    Channel::C2 => {
546                        self.ccmr1_output()
547                        .modify(|_, w| w.oc2pe().set_bit().oc2m().bits(mode as _) );
548                    }
549                    Channel::C3 => {
550                        self.ccmr2_output()
551                        .modify(|_, w| w.oc3pe().set_bit().oc3m().bits(mode as _) );
552                    }
553                    Channel::C4 => {
554                        self.ccmr2_output()
555                        .modify(|_, w| w.oc4pe().set_bit().oc4m().bits(mode as _) );
556                    }
557                }
558            }
559
560            #[inline(always)]
561            fn start_pwm(&mut self) {
562                $(let $aoe = self.bdtr.modify(|_, w| w.aoe().set_bit());)?
563                self.cr1.write(|w| w.cen().set_bit());
564            }
565
566            #[inline(always)]
567            fn enable_channel(c: u8, b: bool) {
568                let tim = unsafe { &*<$TIM>::ptr() };
569                if c < Self::CH_NUMBER {
570                    let mask = (1 << c*4);
571                    if b {
572                        tim.ccer.modify(|r, w| unsafe { w.bits(r.bits() | mask) });
573                    } else {
574                        tim.ccer.modify(|r, w| unsafe { w.bits(r.bits() & !mask) });
575                    }
576                }
577            }
578        }
579    }
580}
581
582impl<TIM: Instance> Timer<TIM> {
583    /// Initialize timer
584    pub fn new(tim: TIM, clocks: &Clocks) -> Self {
585        unsafe {
586            // Enable and reset the timer peripheral
587            TIM::enable_unchecked();
588            TIM::reset_unchecked();
589        }
590
591        Self {
592            clk: TIM::timer_clock(clocks),
593            tim,
594        }
595    }
596
597    pub fn configure(&mut self, clocks: &Clocks) {
598        self.clk = TIM::timer_clock(clocks);
599    }
600
601    pub fn counter_hz(self) -> CounterHz<TIM> {
602        CounterHz(self)
603    }
604
605    pub fn release(self) -> TIM {
606        self.tim
607    }
608
609    /// Starts listening for an `event`
610    ///
611    /// Note, you will also have to enable the TIM2 interrupt in the NVIC to start
612    /// receiving events.
613    pub fn listen(&mut self, event: Event) {
614        self.tim.listen_interrupt(event, true);
615    }
616
617    /// Clears interrupt associated with `event`.
618    ///
619    /// If the interrupt is not cleared, it will immediately retrigger after
620    /// the ISR has finished.
621    pub fn clear_interrupt(&mut self, event: Event) {
622        self.tim.clear_interrupt_flag(event);
623    }
624
625    /// Stops listening for an `event`
626    pub fn unlisten(&mut self, event: Event) {
627        self.tim.listen_interrupt(event, false);
628    }
629}
630
631impl<TIM: Instance + MasterTimer> Timer<TIM> {
632    pub fn set_master_mode(&mut self, mode: TIM::Mms) {
633        self.tim.master_mode(mode)
634    }
635}
636
637/// Timer wrapper for fixed precision timers.
638///
639/// Uses `fugit::TimerDurationU32` for most of operations
640pub struct FTimer<TIM, const FREQ: u32> {
641    tim: TIM,
642}
643
644/// `FTimer` with precision of 1 μs (1 MHz sampling)
645pub type FTimerUs<TIM> = FTimer<TIM, 1_000_000>;
646
647/// `FTimer` with precision of 1 ms (1 kHz sampling)
648///
649/// NOTE: don't use this if your system frequency more than 65 MHz
650pub type FTimerMs<TIM> = FTimer<TIM, 1_000>;
651
652impl<TIM: Instance, const FREQ: u32> FTimer<TIM, FREQ> {
653    /// Initialize timer
654    pub fn new(tim: TIM, clocks: &Clocks) -> Self {
655        unsafe {
656            // Enable and reset the timer peripheral
657            TIM::enable_unchecked();
658            TIM::reset_unchecked();
659        }
660
661        let mut t = Self { tim };
662        t.configure(clocks);
663        t
664    }
665
666    /// Calculate prescaler depending on `Clocks` state
667    pub fn configure(&mut self, clocks: &Clocks) {
668        let clk = TIM::timer_clock(clocks);
669        assert!(clk.raw() % FREQ == 0);
670        let psc = clk.raw() / FREQ;
671        self.tim.set_prescaler(u16::try_from(psc - 1).unwrap());
672    }
673
674    /// Creates `Counter` that imlements [embedded_hal::timer::CountDown]
675    pub fn counter(self) -> Counter<TIM, FREQ> {
676        Counter(self)
677    }
678
679    /// Creates `Delay` that imlements [embedded_hal::blocking::delay] traits
680    pub fn delay(self) -> Delay<TIM, FREQ> {
681        Delay(self)
682    }
683
684    /// Releases the TIM peripheral
685    pub fn release(self) -> TIM {
686        self.tim
687    }
688
689    /// Starts listening for an `event`
690    ///
691    /// Note, you will also have to enable the TIM2 interrupt in the NVIC to start
692    /// receiving events.
693    pub fn listen(&mut self, event: Event) {
694        self.tim.listen_interrupt(event, true);
695    }
696
697    /// Clears interrupt associated with `event`.
698    ///
699    /// If the interrupt is not cleared, it will immediately retrigger after
700    /// the ISR has finished.
701    pub fn clear_interrupt(&mut self, event: Event) {
702        self.tim.clear_interrupt_flag(event);
703    }
704
705    pub fn get_interrupt(&mut self) -> Event {
706        self.tim.get_interrupt_flag()
707    }
708
709    /// Stops listening for an `event`
710    pub fn unlisten(&mut self, event: Event) {
711        self.tim.listen_interrupt(event, false);
712    }
713}
714
715impl<TIM: Instance + MasterTimer, const FREQ: u32> FTimer<TIM, FREQ> {
716    pub fn set_master_mode(&mut self, mode: TIM::Mms) {
717        self.tim.master_mode(mode)
718    }
719}
720
721#[inline(always)]
722pub(crate) const fn compute_arr_presc(freq: u32, clock: u32) -> (u16, u32) {
723    let ticks = clock / freq;
724    let psc = (ticks - 1) / (1 << 16);
725    let arr = ticks / (psc + 1) - 1;
726    (psc as u16, arr)
727}
728
729hal! {
730    pac::TIM2: [Timer2, u32, c: (CH4), m: tim2,],
731    pac::TIM3: [Timer3, u16, c: (CH4), m: tim3,],
732    pac::TIM4: [Timer4, u16, c: (CH4), m: tim3,],
733    pac::TIM5: [Timer5, u32, c: (CH4), m: tim5,],
734    pac::TIM6: [Timer6, u16, m: tim6,],
735    pac::TIM7: [Timer7, u16, m: tim6,],
736    pac::TIM12: [Timer12, u16, c: (CH2),],
737    pac::TIM13: [Timer13, u16, c: (CH1),],
738    pac::TIM14: [Timer14, u16, c: (CH1),],
739
740    pac::TIM1: [Timer1, u16, c: (CH4, _aoe), m: tim1,],
741    pac::TIM8: [Timer8, u16, c: (CH4, _aoe), m: tim1,],
742    pac::TIM9: [Timer9, u16, c: (CH2),],
743    pac::TIM10: [Timer10, u16, c: (CH1),],
744    pac::TIM11: [Timer11, u16, c: (CH1),],
745}