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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
use enumset::{EnumSet, EnumSetType};

use esp_idf_sys::*;

/// For backwards compatibility
pub type IntrFlags = InterruptType;

/// Interrupt allocation flags.
/// These flags can be used to specify which interrupt qualities the code calling esp_intr_alloc* needs.
#[derive(Debug, EnumSetType)]
pub enum InterruptType {
    // Accept a Level 1 interrupt vector (lowest priority)
    Level1,
    // Accept a Level 2 interrupt vector.
    Level2,
    // Accept a Level 3 interrupt vector.
    Level3,
    // Accept a Level 4 interrupt vector.
    Level4,
    // Accept a Level 5 interrupt vector.
    Level5,
    // Accept a Level 6 interrupt vector.
    Level6,
    // Accept a Level 7 interrupt vector (highest priority)
    Nmi,
    // Interrupt can be shared between ISRs.
    Shared,
    // Edge-triggered interrupt.
    Edge,
    // ISR can be called if cache is disabled.
    // Must be used with a proper option *_ISR_IN_IRAM in SDKCONFIG
    Iram,
    // Return with this interrupt disabled.
    IntrDisabled,
    // Low and medium prio interrupts. These can be handled in C.
    LowMed,
    // High level interrupts. Need to be handled in assembly.
    High,
}

impl InterruptType {
    pub fn levels(&self) -> EnumSet<Self> {
        Self::Level1
            | Self::Level2
            | Self::Level3
            | Self::Level4
            | Self::Level5
            | Self::Level6
            | Self::Nmi
    }

    pub(crate) fn to_native(flags: EnumSet<Self>) -> u32 {
        let mut result = 0;

        for flag in flags {
            result |= u32::from(flag);
        }

        result
    }
}

impl From<InterruptType> for u32 {
    fn from(flag: InterruptType) -> Self {
        match flag {
            InterruptType::Level1 => esp_idf_sys::ESP_INTR_FLAG_LEVEL1,
            InterruptType::Level2 => esp_idf_sys::ESP_INTR_FLAG_LEVEL2,
            InterruptType::Level3 => esp_idf_sys::ESP_INTR_FLAG_LEVEL3,
            InterruptType::Level4 => esp_idf_sys::ESP_INTR_FLAG_LEVEL4,
            InterruptType::Level5 => esp_idf_sys::ESP_INTR_FLAG_LEVEL5,
            InterruptType::Level6 => esp_idf_sys::ESP_INTR_FLAG_LEVEL6,
            InterruptType::Nmi => esp_idf_sys::ESP_INTR_FLAG_NMI,
            InterruptType::Shared => esp_idf_sys::ESP_INTR_FLAG_SHARED,
            InterruptType::Edge => esp_idf_sys::ESP_INTR_FLAG_EDGE,
            InterruptType::Iram => esp_idf_sys::ESP_INTR_FLAG_IRAM,
            InterruptType::IntrDisabled => esp_idf_sys::ESP_INTR_FLAG_INTRDISABLED,
            InterruptType::LowMed => esp_idf_sys::ESP_INTR_FLAG_LOWMED,
            InterruptType::High => esp_idf_sys::ESP_INTR_FLAG_HIGH,
        }
    }
}

pub(crate) static CS: IsrCriticalSection = IsrCriticalSection::new();

/// Returns true if the currently active core is executing an ISR request
#[inline(always)]
#[link_section = ".iram1.interrupt_active"]
pub fn active() -> bool {
    unsafe { xPortInIsrContext() != 0 }
}

pub fn with_isr_yield_signal(cb: impl FnOnce()) -> bool {
    if !active() {
        panic!("with_isr_yield_signal() can only be called from an ISR context");
    }

    let mut signaled = false;

    let prev_yielder =
        unsafe { set_isr_yielder(Some((do_yield_signal, &mut signaled as *mut _ as _))) };

    cb();

    unsafe { set_isr_yielder(prev_yielder) };

    signaled
}

unsafe fn do_yield_signal(arg: *mut ()) {
    let signaled = arg.cast::<bool>().as_mut().unwrap();

    *signaled = true
}

#[allow(clippy::type_complexity)]
static mut ISR_YIELDER: Option<(unsafe fn(*mut ()), *mut ())> = None;

#[allow(clippy::type_complexity)]
#[inline(always)]
#[link_section = ".iram1.interrupt_get_isr_yielder"]
pub(crate) unsafe fn get_isr_yielder() -> Option<(unsafe fn(*mut ()), *mut ())> {
    if active() {
        free(|| {
            if let Some((func, arg)) = unsafe { ISR_YIELDER } {
                Some((func, arg))
            } else {
                None
            }
        })
    } else {
        None
    }
}

/// # Safety
///
/// This function should only be called from within an ISR handler, so as to set
/// a custom ISR yield function (e.g. when using the ESP-IDF timer service).
///
/// Thus, if some function further down the ISR call chain invokes `do_yield`,
/// the custom yield function set here will be called.
///
/// Users should not forget to call again `set_isr_yielder` at the end of the
/// ISR handler so as to reastore the yield function which was valid before the
/// ISR handler was invoked.
#[allow(clippy::type_complexity)]
#[inline(always)]
#[link_section = ".iram1.interrupt_set_isr_yielder"]
pub unsafe fn set_isr_yielder(
    yielder: Option<(unsafe fn(*mut ()), *mut ())>,
) -> Option<(unsafe fn(*mut ()), *mut ())> {
    if active() {
        free(|| {
            let previous = unsafe { ISR_YIELDER };

            unsafe { ISR_YIELDER = yielder };

            previous
        })
    } else {
        None
    }
}

/// A critical section allows the user to disable interrupts
#[cfg(any(esp32, esp32s2, esp32s3, esp32p4))]
pub struct IsrCriticalSection(core::cell::UnsafeCell<portMUX_TYPE>);

#[cfg(not(any(esp32, esp32s2, esp32s3, esp32p4)))]
pub struct IsrCriticalSection(core::marker::PhantomData<*const ()>);

#[cfg(not(any(esp32, esp32s2, esp32s3, esp32p4)))]
#[inline(always)]
#[link_section = ".iram1.interrupt_enter"]
fn enter(_cs: &IsrCriticalSection) {
    unsafe {
        vPortEnterCritical();
    }
}

#[cfg(any(esp32, esp32s2, esp32s3, esp32p4))]
#[inline(always)]
#[link_section = ".iram1.interrupt_enter"]
fn enter(cs: &IsrCriticalSection) {
    unsafe {
        xPortEnterCriticalTimeout(cs.0.get(), portMUX_NO_TIMEOUT);
    }
}

#[cfg(not(any(esp32, esp32s2, esp32s3, esp32p4)))]
#[inline(always)]
#[link_section = ".iram1.interrupt_exit"]
fn exit(_cs: &IsrCriticalSection) {
    unsafe {
        vPortExitCritical();
    }
}

#[cfg(any(esp32, esp32s2, esp32s3, esp32p4))]
#[inline(always)]
#[link_section = ".iram1.interrupt_exit"]
fn exit(cs: &IsrCriticalSection) {
    unsafe {
        vPortExitCritical(cs.0.get());
    }
}

impl IsrCriticalSection {
    /// Constructs a new `IsrCriticalSection` instance
    #[inline(always)]
    #[link_section = ".iram1.interrupt_cs_new"]
    pub const fn new() -> Self {
        #[cfg(any(esp32, esp32s2, esp32s3, esp32p4))]
        let mux = core::cell::UnsafeCell::new(portMUX_TYPE {
            owner: portMUX_FREE_VAL,
            count: 0,
            #[cfg(esp_idf_freertos_portmux_debug)]
            lastLockedFn: b"(never locked)",
            #[cfg(esp_idf_freertos_portmux_debug)]
            lastLockedLine: -1,
        });

        #[cfg(not(any(esp32, esp32s2, esp32s3, esp32p4)))]
        let mux = core::marker::PhantomData;

        Self(mux)
    }

    /// Disables all interrupts for the lifetime of the returned guard instance.
    /// This method supports nesting in that is safe to be called multiple times.
    /// This method is also safe to call from ISR routines.
    ///
    /// NOTE: On dual-core esp32* chips, interrupts will be disabled only on one of
    /// the cores (the one where `IsrCriticalSection::enter` is called), while the other
    /// core will continue its execution. Moreover, if the same `IsrCriticalSection` instance
    /// is shared across multiple threads, where some of these happen to be scheduled on
    /// the second core (which has its interrupts enabled), the second core will then spinlock
    /// (busy-wait) in `IsrCriticalSection::enter`, until the first CPU releases the critical
    /// section and re-enables its interrupts. The second core will then - in turn - disable
    /// its interrupts and own the spinlock.
    ///
    /// For more information, refer to https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/freertos-smp.html#critical-sections
    #[inline(always)]
    #[link_section = ".iram1.interrupt_cs_enter"]
    pub fn enter(&self) -> IsrCriticalSectionGuard {
        enter(self);

        IsrCriticalSectionGuard(self)
    }
}

impl Default for IsrCriticalSection {
    #[inline(always)]
    #[link_section = ".iram1.interrupt_cs_default"]
    fn default() -> Self {
        Self::new()
    }
}

unsafe impl Send for IsrCriticalSection {}
unsafe impl Sync for IsrCriticalSection {}

pub struct IsrCriticalSectionGuard<'a>(&'a IsrCriticalSection);

impl<'a> Drop for IsrCriticalSectionGuard<'a> {
    /// Drops the critical section guard thus potentially re-enabling
    /// al interrupts for the currently active core.
    ///
    /// Note that - due to the fact that calling `IsrCriticalSection::enter`
    /// multiple times on the same or multiple critical sections is supported -
    /// interrupts for the core will be re-enabled only when the last guard that
    /// disabled interrupts for the concrete core is dropped.
    #[inline(always)]
    #[link_section = ".iram1.interrupt_csg_drop"]
    fn drop(&mut self) {
        exit(self.0);
    }
}

/// Executes closure f in an interrupt-free context
#[inline(always)]
#[link_section = ".iram1.interrupt_free"]
pub fn free<R>(f: impl FnOnce() -> R) -> R {
    let _guard = CS.enter();

    f()
}

#[cfg(feature = "wake-from-isr")]
pub mod asynch {
    pub type HalIsrNotification = crate::task::asynch::Notification;
}

#[cfg(not(feature = "wake-from-isr"))]
pub mod asynch {
    use core::{
        cell::UnsafeCell,
        ffi::{c_void, CStr},
        future::Future,
        num::NonZeroU32,
        sync::atomic::{AtomicPtr, Ordering},
        task::{Context, Poll, Waker},
    };

    use esp_idf_sys::EspError;

    use log::info;

    use crate::{
        cpu::Core,
        delay,
        task::{asynch::Notification, CriticalSection},
    };

    use super::IsrCriticalSection;

    /// The HAL-global wake runner.
    /// You should use no more than 64 tasks with it.
    ///
    /// `*IsrNotification` instances use this wake runner when they are triggered from an ISR context.
    pub static HAL_ISR_REACTOR: IsrReactor<64> = IsrReactor::new(IsrReactorConfig::new());

    /// Wake runner configuration
    #[derive(Clone, Debug)]
    pub struct IsrReactorConfig {
        pub task_name: &'static CStr,
        pub task_stack_size: usize,
        pub task_priority: u8,
        pub task_pin_to_core: Option<Core>,
    }

    impl IsrReactorConfig {
        pub const fn new() -> Self {
            Self {
                task_name: unsafe { CStr::from_bytes_with_nul_unchecked(b"IsrReactor\0") },
                task_stack_size: 3084,
                task_priority: 11,
                task_pin_to_core: None,
            }
        }
    }

    impl Default for IsrReactorConfig {
        fn default() -> Self {
            Self::new()
        }
    }

    /// IsrReactor is a utility allowing `Waker` instances to be awoken fron an ISR context.
    ///
    /// General problem:
    /// In an interrupt, using Waker instances coming from generic executors is impossible,
    /// because these are not designed with an ISR-safety in mind.
    ///
    /// Waking a waker means that its task would be scheduled on the executor queue, which might involve
    /// allocation, and/or synchronization primitives which are not safe to use from an ISR context.
    ///
    /// Similarly, dropping a waker might also drop the executor task, resulting in a deallocation, which is also
    /// not safe in an ISR context.
    ///
    /// These problems are alleviated by replacing direct `waker.wake()` calls to `WakerRunner::schedule(waker)`.
    /// What `IsrReactor::schedule` does is to push the waker into a bounded queue and then notify a hidden FreeRTOS task.
    /// Once the FreeRTOS task gets awoken, it wakes all wakers scheduled on the bounded queue and empties the queue.
    pub struct IsrReactor<const N: usize> {
        wakers_cs: IsrCriticalSection,
        wakers: UnsafeCell<heapless::Deque<Waker, N>>,
        task_cs: CriticalSection,
        task: AtomicPtr<crate::sys::tskTaskControlBlock>,
        task_config: IsrReactorConfig,
    }

    impl<const N: usize> IsrReactor<N> {
        /// Create a new `IsrReactor` instance.
        pub const fn new(config: IsrReactorConfig) -> Self {
            Self {
                wakers_cs: IsrCriticalSection::new(),
                wakers: UnsafeCell::new(heapless::Deque::new()),
                task_cs: CriticalSection::new(),
                task: AtomicPtr::new(core::ptr::null_mut()),
                task_config: config,
            }
        }

        /// Returns `true` if the wake runner is started.
        pub fn is_started(&self) -> bool {
            !self.task.load(Ordering::SeqCst).is_null()
        }

        /// Starts the wake runner. Returns `false` if it had been already started.
        pub fn start(&'static self) -> Result<bool, EspError> {
            let _guard = self.task_cs.enter();

            if self.task.load(Ordering::SeqCst).is_null() {
                let task = unsafe {
                    crate::task::create(
                        Self::task_run,
                        self.task_config.task_name,
                        self.task_config.task_stack_size,
                        self as *const _ as *const c_void as *mut _,
                        self.task_config.task_priority,
                        self.task_config.task_pin_to_core,
                    )?
                };

                self.task.store(task as _, Ordering::SeqCst);

                info!("IsrReactor {:?} started.", self.task_config.task_name);

                Ok(true)
            } else {
                Ok(false)
            }
        }

        /// Stops the wake runner. Returns `false` if it had been already stopped.
        pub fn stop(&self) -> bool {
            let _guard = self.task_cs.enter();

            let task = self.task.swap(core::ptr::null_mut(), Ordering::SeqCst);

            if !task.is_null() {
                unsafe {
                    crate::task::destroy(task as _);
                }

                info!("IsrReactor {:?} stopped.", self.task_config.task_name);

                true
            } else {
                false
            }
        }

        /// Schedules a waker to be awoken by the hidden FreeRTOS task running in the background.
        /// If not called from within an ISR context, calls `waker.wake()` directly instead of scheduling the waker.
        /// NOTE: If the wake runner is not started yet, scheduling fron an ISR content would fail silently.
        ///
        /// This and only this method is safe to call from an ISR context.
        pub fn schedule(&self, waker: Waker) {
            if super::active() {
                self.wakers(|wakers| {
                    let earlier_waker = wakers.iter_mut().find(|a_waker| a_waker.will_wake(&waker));

                    if let Some(earlier_waker) = earlier_waker {
                        *earlier_waker = waker;
                    } else if wakers.push_back(waker).is_err() {
                        panic!("IsrReactor queue overflow");
                    }

                    let task = self.task.load(Ordering::SeqCst);

                    if !task.is_null() {
                        unsafe {
                            crate::task::notify_and_yield(task as _, NonZeroU32::new(1).unwrap());
                        }
                    }
                })
            } else {
                waker.wake();
            }
        }

        fn run(&self) {
            loop {
                loop {
                    let waker = self.wakers(|wakers| wakers.pop_front());

                    if let Some(waker) = waker {
                        waker.wake();
                    } else {
                        break;
                    }
                }

                crate::task::wait_notification(delay::BLOCK);
            }
        }

        fn wakers<F: FnOnce(&mut heapless::Deque<Waker, N>) -> R, R>(&self, f: F) -> R {
            // if super::active() {
            //     let wakers = unsafe { self.wakers.get().as_mut().unwrap() };

            //     f(wakers)
            // } else {
            let _guard = self.wakers_cs.enter();

            let wakers = unsafe { self.wakers.get().as_mut().unwrap() };

            f(wakers)
            // }
        }

        extern "C" fn task_run(ctx: *mut c_void) {
            let this =
                unsafe { (ctx as *mut IsrReactor<N> as *const IsrReactor<N>).as_ref() }.unwrap();

            this.run();
        }
    }

    impl<const N: usize> Drop for IsrReactor<N> {
        fn drop(&mut self) {
            self.stop();
        }
    }

    unsafe impl<const N: usize> Send for IsrReactor<N> {}
    unsafe impl<const N: usize> Sync for IsrReactor<N> {}

    /// Single-slot lock-free signaling primitive supporting signalling with a `u32` bit-set.
    /// A variation of the `Notification` HAL primitive which is however safe to be notified from an ISR context.
    ///
    /// It is useful for sending data between an ISR routine (or a regular task context) and an async task when the
    /// receiver only cares about the latest data, and therefore it's fine to "lose" messages.
    /// This is often the case for "state" updates.
    ///
    /// The sending part of the primitive is non-blocking and ISR-safe, so it can be called from anywhere.
    ///
    /// Similar in spirit to the ESP-IDF FreeRTOS task notifications in that it is light-weight and operates on bit-sets,
    /// but for synchronization between an asynchronous task, and another one, which might be blocking or asynchronous.
    pub struct IsrNotification<const N: usize> {
        inner: Notification,
        reactor: &'static IsrReactor<N>,
    }

    impl<const N: usize> IsrNotification<N> {
        /// Creates a new `IsrNotification`.
        /// This method is safe to call from an ISR context, yet such use cases should not normally occur in practice.
        pub const fn new(reactor: &'static IsrReactor<N>) -> Self {
            Self {
                inner: Notification::new(),
                reactor,
            }
        }

        /// Marks the least significant bit (bit 0) in this `IsrNotification` as nofified.
        /// This method is safe to call from an ISR context.
        /// Returns `true` if there was a registered waker which got awoken.
        pub fn notify_lsb(&self) -> bool {
            self.notify(NonZeroU32::new(1).unwrap())
        }

        /// Marks the supplied bits in this `IsrNotification` as notified.
        /// This method is safe to call from an ISR context.
        /// Returns `true` if there was a registered waker which got awoken.
        pub fn notify(&self, bits: NonZeroU32) -> bool {
            if let Some(waker) = self.inner.notify_waker(bits) {
                self.reactor.schedule(waker);

                true
            } else {
                false
            }
        }

        /// Clears the state of this notification by removing any registered waker and setting all bits to 0.
        /// This method is NOT safe to call from an ISR context.
        pub fn reset(&self) {
            self.inner.reset();
        }

        /// Future that completes when this `IsrNotification` has been notified.
        /// This method is NOT safe to call from an ISR context.
        #[allow(unused)]
        pub fn wait(&self) -> impl Future<Output = NonZeroU32> + '_ {
            self.reactor.start().unwrap();

            self.inner.wait()
        }

        /// Non-blocking method to check whether this notification has been notified.
        /// This method is NOT safe to call from an ISR context.
        pub fn poll_wait(&self, cx: &Context<'_>) -> Poll<NonZeroU32> {
            self.reactor.start().unwrap();

            self.inner.poll_wait(cx)
        }
    }

    /// Single-slot lock-free signaling primitive supporting signalling with a `u32` bit-set.
    /// A variation of the `IsrNotification` HAL primitive which is however safe to be notified from an ISR context.
    /// The difference between this primitive and `IsrNotification` is that this one is hard-wired to the
    /// global HAL wake runner (`HAL_WAKE_RUNNER`) and is thus occupying less space.
    ///
    /// It is useful for sending data between an ISR routine (or a regular task context) and an async task when the
    /// receiver only cares about the latest data, and therefore it's fine to "lose" messages.
    /// This is often the case for "state" updates.
    ///
    /// The sending part of the primitive is non-blocking and ISR-safe, so it can be called from anywhere.
    ///
    /// Similar in spirit to the ESP-IDF FreeRTOS task notifications in that it is light-weight and operates on bit-sets,
    /// but for synchronization between an asynchronous task, and another one, which might be blocking or asynchronous.
    pub struct HalIsrNotification {
        inner: Notification,
    }

    impl Default for HalIsrNotification {
        fn default() -> Self {
            Self::new()
        }
    }

    impl HalIsrNotification {
        /// Creates a new `HalIsrNotification`.
        /// This method is safe to call from an ISR context, yet such use cases should not normally occur in practice.
        pub const fn new() -> Self {
            Self {
                inner: Notification::new(),
            }
        }

        /// Marks the least significant bit (bit 0) in this `IsrNotification` as nofified.
        /// This method is safe to call from an ISR context.
        /// Returns `true` if there was a registered waker which got awoken.
        pub fn notify_lsb(&self) -> bool {
            self.notify(NonZeroU32::new(1).unwrap())
        }

        /// Marks the supplied bits in this `HalIsrNotification` as notified.
        /// This method is safe to call from an ISR context.
        /// Returns `true` if there was a registered waker which got awoken.
        pub fn notify(&self, bits: NonZeroU32) -> bool {
            if let Some(waker) = self.inner.notify_waker(bits) {
                HAL_ISR_REACTOR.schedule(waker);

                true
            } else {
                false
            }
        }

        /// Clears the state of this notification by removing any registered waker and setting all bits to 0.
        /// This method is NOT safe to call from an ISR context.
        pub fn reset(&self) {
            self.inner.reset();
        }

        /// Future that completes when this `HalIsrNotification` has been notified.
        /// This method is NOT safe to call from an ISR context.
        #[allow(unused)]
        pub fn wait(&self) -> impl Future<Output = NonZeroU32> + '_ {
            HAL_ISR_REACTOR.start().unwrap();

            self.inner.wait()
        }

        /// Non-blocking method to check whether this notification has been notified.
        /// This method is NOT safe to call from an ISR context.
        pub fn poll_wait(&self, cx: &Context<'_>) -> Poll<NonZeroU32> {
            HAL_ISR_REACTOR.start().unwrap();

            self.inner.poll_wait(cx)
        }
    }
}

#[cfg(feature = "embassy-sync")]
pub mod embassy_sync {
    use embassy_sync::blocking_mutex::raw::RawMutex;

    /// A mutex that allows borrowing data across executors and interrupts.
    ///
    /// # Safety
    ///
    /// This mutex is safe to share between different executors and interrupts.
    pub struct IsrRawMutex(());

    unsafe impl Send for IsrRawMutex {}
    unsafe impl Sync for IsrRawMutex {}

    impl IsrRawMutex {
        /// Create a new `IsrRawMutex`.
        pub const fn new() -> Self {
            Self(())
        }
    }

    unsafe impl RawMutex for IsrRawMutex {
        const INIT: Self = Self::new();

        fn lock<R>(&self, f: impl FnOnce() -> R) -> R {
            super::free(f)
        }
    }
}