Skip to main content

ch32_hal/
interrupt_ext.rs

1use core::sync::atomic::{compiler_fence, Ordering};
2
3use critical_section::CriticalSection;
4pub use qingke::interrupt::Priority;
5use qingke::pfic;
6
7use crate::pac::InterruptNumber;
8
9/// Generate a standard `mod interrupt` for a HAL.
10#[macro_export]
11macro_rules! interrupt_mod {
12    ($($irqs:ident),* $(,)?) => {
13
14        /// Interrupt definitions.
15        pub mod interrupt {
16            pub use crate::interrupt_ext::{InterruptExt, Priority};
17            pub use crate::pac::Interrupt::*;
18            pub use crate::pac::Interrupt;
19
20            /// Type-level interrupt infrastructure.
21            ///
22            /// This module contains one *type* per interrupt. This is used for checking at compile time that
23            /// the interrupts are correctly bound to HAL drivers.
24            ///
25            /// As an end user, you shouldn't need to use this module directly. Use the [`crate::bind_interrupts!`] macro
26            /// to bind interrupts, and the [`crate::interrupt`] module to manually register interrupt handlers and manipulate
27            /// interrupts directly (pending/unpending, enabling/disabling, setting the priority, etc...)
28            pub mod typelevel {
29                use super::InterruptExt;
30
31                trait SealedInterrupt {}
32
33                /// Type-level interrupt.
34                ///
35                /// This trait is implemented for all typelevel interrupt types in this module.
36                #[allow(private_bounds)]
37                pub trait Interrupt: SealedInterrupt {
38
39                    /// Interrupt enum variant.
40                    ///
41                    /// This allows going from typelevel interrupts (one type per interrupt) to
42                    /// non-typelevel interrupts (a single `Interrupt` enum type, with one variant per interrupt).
43                    const IRQ: super::Interrupt;
44
45                    /// Enable the interrupt.
46                    #[inline]
47                    unsafe fn enable() {
48                        Self::IRQ.enable()
49                    }
50
51                    /// Disable the interrupt.
52                    #[inline]
53                    fn disable() {
54                        Self::IRQ.disable()
55                    }
56
57                    /// Check if interrupt is enabled.
58                    #[inline]
59                    fn is_enabled() -> bool {
60                        Self::IRQ.is_enabled()
61                    }
62
63                    /// Check if interrupt is pending.
64                    #[inline]
65                    fn is_pending() -> bool {
66                        Self::IRQ.is_pending()
67                    }
68
69                    /// Set interrupt pending.
70                    #[inline]
71                    fn pend() {
72                        Self::IRQ.pend()
73                    }
74
75                    /// Unset interrupt pending.
76                    #[inline]
77                    fn unpend() {
78                        Self::IRQ.unpend()
79                    }
80
81                    /// Get the priority of the interrupt.
82                    #[inline]
83                    fn get_priority() -> crate::interrupt::Priority {
84                        Self::IRQ.get_priority()
85                    }
86
87                    /// Set the interrupt priority.
88                    #[inline]
89                    fn set_priority(prio: crate::interrupt::Priority) {
90                        Self::IRQ.set_priority(prio)
91                    }
92
93                    /// Set the interrupt priority with an already-acquired critical section
94                    #[inline]
95                    fn set_priority_with_cs(cs: critical_section::CriticalSection, prio: crate::interrupt::Priority) {
96                        Self::IRQ.set_priority_with_cs(cs, prio)
97                    }
98                }
99
100                $(
101                    #[allow(non_camel_case_types)]
102                    #[doc=stringify!($irqs)]
103                    #[doc=" typelevel interrupt."]
104                    pub enum $irqs {}
105                    impl SealedInterrupt for $irqs{}
106                    impl Interrupt for $irqs {
107                        const IRQ: super::Interrupt = super::Interrupt::$irqs;
108                    }
109                )*
110
111                /// Interrupt handler trait.
112                ///
113                /// Drivers that need to handle interrupts implement this trait.
114                /// The user must ensure `on_interrupt()` is called every time the interrupt fires.
115                /// Drivers must use use [`Binding`] to assert at compile time that the user has done so.
116                pub trait Handler<I: Interrupt> {
117                    /// Interrupt handler function.
118                    ///
119                    /// Must be called every time the `I` interrupt fires, synchronously from
120                    /// the interrupt handler context.
121                    ///
122                    /// # Safety
123                    ///
124                    /// This function must ONLY be called from the interrupt handler for `I`.
125                    unsafe fn on_interrupt();
126                }
127
128                /// Compile-time assertion that an interrupt has been bound to a handler.
129                ///
130                /// For the vast majority of cases, you should use the `bind_interrupts!`
131                /// macro instead of writing `unsafe impl`s of this trait.
132                ///
133                /// # Safety
134                ///
135                /// By implementing this trait, you are asserting that you have arranged for `H::on_interrupt()`
136                /// to be called every time the `I` interrupt fires.
137                ///
138                /// This allows drivers to check bindings at compile-time.
139                pub unsafe trait Binding<I: Interrupt, H: Handler<I>> {}
140            }
141        }
142    };
143}
144
145/// Represents an interrupt type that can be configured by embassy to handle
146/// interrupts.
147pub unsafe trait InterruptExt: InterruptNumber + Copy {
148    /// Enable the interrupt.
149    #[inline]
150    unsafe fn enable(self) {
151        compiler_fence(Ordering::SeqCst);
152        pfic::enable_interrupt(self.number() as u8)
153    }
154
155    /// Disable the interrupt.
156    #[inline]
157    fn disable(self) {
158        unsafe { pfic::disable_interrupt(self.number() as u8) }; // TODO: fix unsafe
159        compiler_fence(Ordering::SeqCst);
160    }
161
162    /// Check if interrupt is being handled.
163    #[inline]
164    #[cfg(not(armv6m))]
165    fn is_active(self) -> bool {
166        pfic::is_active(self.number() as u8)
167    }
168
169    /// Check if interrupt is enabled.
170    #[inline]
171    fn is_enabled(self) -> bool {
172        pfic::is_enabled(self.number() as u8)
173    }
174
175    /// Check if interrupt is pending.
176    #[inline]
177    fn is_pending(self) -> bool {
178        pfic::is_pending(self.number() as u8)
179    }
180
181    /// Set interrupt pending.
182    #[inline]
183    fn pend(self) {
184        unsafe { pfic::pend_interrupt(self.number() as u8) }
185    }
186
187    /// Unset interrupt pending.
188    #[inline]
189    fn unpend(self) {
190        unsafe { pfic::unpend_interrupt(self.number() as u8) }
191    }
192
193    /// Get the priority of the interrupt.
194    #[inline]
195    fn get_priority(self) -> Priority {
196        Priority::from(pfic::get_priority(self.number() as u8))
197    }
198
199    /// Set the interrupt priority.
200    #[inline]
201    fn set_priority(self, prio: Priority) {
202        unsafe { pfic::set_priority(self.number() as u8, prio.into()) }
203    }
204
205    /// Set the interrupt priority with an already-acquired critical section
206    ///
207    /// Equivalent to `set_priority`, except you pass a `CriticalSection` to prove
208    /// you've already acquired a critical section. This prevents acquiring another
209    /// one, which saves code size.
210    #[inline]
211    fn set_priority_with_cs(self, _cs: CriticalSection, prio: Priority) {
212        unsafe { pfic::set_priority(self.number() as u8, prio.into()) }
213    }
214}
215
216unsafe impl<T: InterruptNumber + Copy> InterruptExt for T {}