sifli_hal/
timer.rs

1use embassy_hal_internal::Peripheral;
2// use embassy_sync::waitqueue::AtomicWaker;
3
4use crate::interrupt;
5use crate::rcc::SealedRccEnableReset;
6
7/// Timer channel.
8#[derive(Clone, Copy)]
9pub enum Channel {
10    /// Channel 1.
11    Ch1,
12    /// Channel 2.
13    Ch2,
14    /// Channel 3.
15    Ch3,
16    /// Channel 4.
17    Ch4,
18    /// Channel 5, only available on ATIM.
19    Ch5,
20    /// Channel 6, only available on ATIM.
21    Ch6,
22}
23
24impl Channel {
25    /// Get the channel index (0..3)
26    pub fn index(&self) -> usize {
27        match self {
28            Channel::Ch1 => 0,
29            Channel::Ch2 => 1,
30            Channel::Ch3 => 2,
31            Channel::Ch4 => 3,
32            Channel::Ch5 => 4,
33            Channel::Ch6 => 5,
34        }
35    }
36}
37
38/// Amount of bits of a timer.
39#[derive(Clone, Copy, PartialEq, Eq, Debug)]
40#[cfg_attr(feature = "defmt", derive(defmt::Format))]
41pub enum TimerBits {
42    /// 16 bits.
43    Bits16,
44    /// 32 bits.
45    Bits32,
46}
47
48// struct State {
49//     up_waker: AtomicWaker,
50//     cc_waker: [AtomicWaker; 4],
51// }
52
53// impl State {
54//     const fn new() -> Self {
55//         Self {
56//             up_waker: AtomicWaker::new(),
57//             cc_waker: [const { AtomicWaker::new() }; 4],
58//         }
59//     }
60// }
61
62trait SealedInstance: SealedRccEnableReset + Peripheral<P = Self> {
63    // /// Async state for this timer
64    // fn state() -> &'static State;
65}
66
67/// timer instance.
68#[allow(private_bounds)]
69pub trait Instance: SealedInstance + 'static {
70    /// Interrupt for this timer.
71    type Interrupt: interrupt::typelevel::Interrupt;
72
73    /// Amount of bits this timer has.
74    const BITS: TimerBits;
75
76    /// Registers for this timer.
77    ///
78    /// This is a raw pointer to the register block. The actual register block layout varies depending on the timer type.
79    fn regs() -> *mut ();
80}
81
82pub trait AtimInstance: Instance + 'static {}
83pub trait GptimInstance: Instance + 'static {}
84pub trait BimInstance: Instance + 'static {}
85
86
87
88// TODO: move to _generated.rs
89use crate::peripherals;
90
91impl SealedInstance for peripherals::ATIM1 {}
92impl Instance for peripherals::ATIM1 {
93    type Interrupt = interrupt::typelevel::ATIM1;
94    const BITS: TimerBits = TimerBits::Bits32;
95    fn regs() -> *mut () {
96        crate::pac::ATIM1.as_ptr()
97    }
98}
99impl AtimInstance for peripherals::ATIM1 {}
100
101impl SealedInstance for peripherals::GPTIM1 {}
102impl Instance for peripherals::GPTIM1 {
103    type Interrupt = interrupt::typelevel::GPTIM1;
104    const BITS: TimerBits = TimerBits::Bits32;
105    fn regs() -> *mut () { 
106        crate::pac::GPTIM1.as_ptr()
107    }
108}
109impl GptimInstance for peripherals::GPTIM1 {}
110
111impl SealedInstance for peripherals::GPTIM2 {}
112impl Instance for peripherals::GPTIM2 {
113    type Interrupt = interrupt::typelevel::GPTIM2;
114    const BITS: TimerBits = TimerBits::Bits32;
115    fn regs() -> *mut () { 
116        crate::pac::GPTIM2.as_ptr()
117    }
118}
119impl GptimInstance for peripherals::GPTIM2 {}
120
121impl SealedInstance for peripherals::BTIM1 {}
122impl Instance for peripherals::BTIM1 {
123    type Interrupt = interrupt::typelevel::BTIM1;
124    const BITS: TimerBits = TimerBits::Bits32;
125    fn regs() -> *mut () { 
126        crate::pac::BTIM1.as_ptr()
127    }
128}
129impl BimInstance for peripherals::BTIM1 {}
130
131impl SealedInstance for peripherals::BTIM2 {}
132impl Instance for peripherals::BTIM2 {
133    type Interrupt = interrupt::typelevel::BTIM2;
134    const BITS: TimerBits = TimerBits::Bits32;
135    fn regs() -> *mut () { 
136        crate::pac::BTIM2.as_ptr()
137    }
138}
139impl BimInstance for peripherals::BTIM2 {}