aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2023-06-01 02:22:31 +0200
committerDario Nieuwenhuis <[email protected]>2023-06-01 02:22:31 +0200
commit2a435e53b761182dabf9496963052f8323125f3a (patch)
treeed4105de620c7effe1844fc32e6af05125bfa852
parentc036eab62c2221bd8f458bd6edd1562dfcdb8421 (diff)
cortex-m: remove PeripheralMutex.
-rw-r--r--embassy-cortex-m/src/lib.rs1
-rw-r--r--embassy-cortex-m/src/peripheral.rs144
2 files changed, 0 insertions, 145 deletions
diff --git a/embassy-cortex-m/src/lib.rs b/embassy-cortex-m/src/lib.rs
index e4b713a06..7bc16d3ba 100644
--- a/embassy-cortex-m/src/lib.rs
+++ b/embassy-cortex-m/src/lib.rs
@@ -7,4 +7,3 @@ pub(crate) mod fmt;
7 7
8pub use embassy_executor as executor; 8pub use embassy_executor as executor;
9pub mod interrupt; 9pub mod interrupt;
10pub mod peripheral;
diff --git a/embassy-cortex-m/src/peripheral.rs b/embassy-cortex-m/src/peripheral.rs
deleted file mode 100644
index e2f295579..000000000
--- a/embassy-cortex-m/src/peripheral.rs
+++ /dev/null
@@ -1,144 +0,0 @@
1//! Peripheral interrupt handling specific to cortex-m devices.
2use core::mem::MaybeUninit;
3
4use cortex_m::peripheral::scb::VectActive;
5use cortex_m::peripheral::{NVIC, SCB};
6use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
7
8use crate::interrupt::{Interrupt, InterruptExt, Priority};
9
10/// A type which can be used as state with `PeripheralMutex`.
11///
12/// It needs to be `Send` because `&mut` references are sent back and forth between the 'thread' which owns the `PeripheralMutex` and the interrupt,
13/// and `&mut T` is only `Send` where `T: Send`.
14pub trait PeripheralState: Send {
15 /// The interrupt that is used for this peripheral.
16 type Interrupt: Interrupt;
17
18 /// The interrupt handler that should be invoked for the peripheral. Implementations need to clear the appropriate interrupt flags to ensure the handle will not be called again.
19 fn on_interrupt(&mut self);
20}
21
22/// A type for storing the state of a peripheral that can be stored in a static.
23pub struct StateStorage<S>(MaybeUninit<S>);
24
25impl<S> StateStorage<S> {
26 /// Create a new instance for storing peripheral state.
27 pub const fn new() -> Self {
28 Self(MaybeUninit::uninit())
29 }
30}
31
32/// A type for a peripheral that keeps the state of a peripheral that can be accessed from thread mode and an interrupt handler in
33/// a safe way.
34pub struct PeripheralMutex<'a, S: PeripheralState> {
35 state: *mut S,
36 irq: PeripheralRef<'a, S::Interrupt>,
37}
38
39/// Whether `irq` can be preempted by the current interrupt.
40pub(crate) fn can_be_preempted(irq: &impl Interrupt) -> bool {
41 match SCB::vect_active() {
42 // Thread mode can't preempt anything.
43 VectActive::ThreadMode => false,
44 // Exceptions don't always preempt interrupts,
45 // but there isn't much of a good reason to be keeping a `PeripheralMutex` in an exception anyway.
46 VectActive::Exception(_) => true,
47 VectActive::Interrupt { irqn } => {
48 #[derive(Clone, Copy)]
49 struct NrWrap(u16);
50 unsafe impl cortex_m::interrupt::InterruptNumber for NrWrap {
51 fn number(self) -> u16 {
52 self.0
53 }
54 }
55 NVIC::get_priority(NrWrap(irqn.into())) < irq.get_priority().into()
56 }
57 }
58}
59
60impl<'a, S: PeripheralState> PeripheralMutex<'a, S> {
61 /// Create a new `PeripheralMutex` wrapping `irq`, with `init` initializing the initial state.
62 ///
63 /// Registers `on_interrupt` as the `irq`'s handler, and enables it.
64 pub fn new(
65 irq: impl Peripheral<P = S::Interrupt> + 'a,
66 storage: &'a mut StateStorage<S>,
67 init: impl FnOnce() -> S,
68 ) -> Self {
69 into_ref!(irq);
70
71 if can_be_preempted(&*irq) {
72 panic!(
73 "`PeripheralMutex` cannot be created in an interrupt with higher priority than the interrupt it wraps"
74 );
75 }
76
77 let state_ptr = storage.0.as_mut_ptr();
78
79 // Safety: The pointer is valid and not used by anyone else
80 // because we have the `&mut StateStorage`.
81 unsafe { state_ptr.write(init()) };
82
83 irq.disable();
84 irq.set_handler(|p| unsafe {
85 // Safety: it's OK to get a &mut to the state, since
86 // - We checked that the thread owning the `PeripheralMutex` can't preempt us in `new`.
87 // Interrupts' priorities can only be changed with raw embassy `Interrupts`,
88 // which can't safely store a `PeripheralMutex` across invocations.
89 // - We can't have preempted a with() call because the irq is disabled during it.
90 let state = &mut *(p as *mut S);
91 state.on_interrupt();
92 });
93 irq.set_handler_context(state_ptr as *mut ());
94 irq.enable();
95
96 Self { irq, state: state_ptr }
97 }
98
99 /// Access the peripheral state ensuring interrupts are disabled so that the state can be
100 /// safely accessed.
101 pub fn with<R>(&mut self, f: impl FnOnce(&mut S) -> R) -> R {
102 self.irq.disable();
103
104 // Safety: it's OK to get a &mut to the state, since the irq is disabled.
105 let state = unsafe { &mut *self.state };
106 let r = f(state);
107
108 self.irq.enable();
109
110 r
111 }
112
113 /// Returns whether the wrapped interrupt is currently in a pending state.
114 pub fn is_pending(&self) -> bool {
115 self.irq.is_pending()
116 }
117
118 /// Forces the wrapped interrupt into a pending state.
119 pub fn pend(&self) {
120 self.irq.pend()
121 }
122
123 /// Forces the wrapped interrupt out of a pending state.
124 pub fn unpend(&self) {
125 self.irq.unpend()
126 }
127
128 /// Gets the priority of the wrapped interrupt.
129 pub fn priority(&self) -> Priority {
130 self.irq.get_priority()
131 }
132}
133
134impl<'a, S: PeripheralState> Drop for PeripheralMutex<'a, S> {
135 fn drop(&mut self) {
136 self.irq.disable();
137 self.irq.remove_handler();
138
139 // safety:
140 // - we initialized the state in `new`, so we know it's initialized.
141 // - the irq is disabled, so it won't preempt us while dropping.
142 unsafe { self.state.drop_in_place() }
143 }
144}