aboutsummaryrefslogtreecommitdiff
path: root/embassy-mspm0/src/timer.rs
blob: 4441e56408d4ba96f14b24b1ce8fac92da6d3bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#![macro_use]

/// Amount of bits of a timer.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum TimerBits {
    /// 16 bits.
    Bits16,
    /// 32 bits.
    Bits32,
}

#[allow(private_bounds)]
pub trait Timer: SealedTimer + 'static {
    /// Amount of bits this timer has.
    const BITS: TimerBits;
}

pub(crate) trait SealedTimer {
    /// Registers for this timer.
    ///
    /// This is a raw pointer to the register block. The actual register block layout varies depending on the
    /// timer type.
    fn regs() -> *mut ();

    /// Enable the interrupt corresponding to this timer.
    unsafe fn enable_interrupt();
}

macro_rules! impl_timer {
    ($name: ident, $bits: ident) => {
        impl crate::timer::SealedTimer for crate::peripherals::$name {
            fn regs() -> *mut () {
                crate::pac::$name.as_ptr()
            }

            unsafe fn enable_interrupt() {
                use embassy_hal_internal::interrupt::InterruptExt;
                crate::interrupt::$name.unpend();
                crate::interrupt::$name.enable();
            }
        }

        impl crate::timer::Timer for crate::peripherals::$name {
            const BITS: crate::timer::TimerBits = crate::timer::TimerBits::$bits;
        }
    };
}