aboutsummaryrefslogtreecommitdiff
path: root/embassy-extras
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-18 02:29:03 +0100
committerDario Nieuwenhuis <[email protected]>2021-03-18 18:49:10 +0100
commitb57489eb5d27075e077b955b295cc97bf2fc312f (patch)
tree0fc17b38e80f0b2c9c1fd28ca916997a33dbca3a /embassy-extras
parent0cd19a58c3dcaa689ba57da80c02af75866f7e09 (diff)
peripheralmutex: separate interrupt registration to own method.
Diffstat (limited to 'embassy-extras')
-rw-r--r--embassy-extras/src/peripheral.rs25
1 files changed, 13 insertions, 12 deletions
diff --git a/embassy-extras/src/peripheral.rs b/embassy-extras/src/peripheral.rs
index aa23bc978..e2435d63f 100644
--- a/embassy-extras/src/peripheral.rs
+++ b/embassy-extras/src/peripheral.rs
@@ -32,32 +32,33 @@ impl<S: PeripheralState> PeripheralMutex<S> {
32 } 32 }
33 } 33 }
34 34
35 /// safety: self must be pinned. 35 pub fn register_interrupt(self: Pin<&mut Self>) {
36 unsafe fn setup(&mut self) { 36 let this = unsafe { self.get_unchecked_mut() };
37 self.irq.disable(); 37 if this.irq_setup_done {
38 return;
39 }
40
41 this.irq.disable();
38 compiler_fence(Ordering::SeqCst); 42 compiler_fence(Ordering::SeqCst);
39 43
40 self.irq.set_handler(|p| { 44 this.irq.set_handler(|p| {
41 // Safety: it's OK to get a &mut to the state, since 45 // Safety: it's OK to get a &mut to the state, since
42 // - We're in the IRQ, no one else can't preempt us 46 // - We're in the IRQ, no one else can't preempt us
43 // - We can't have preempted a with() call because the irq is disabled during it. 47 // - We can't have preempted a with() call because the irq is disabled during it.
44 let state = &mut *(p as *mut S); 48 let state = unsafe { &mut *(p as *mut S) };
45 state.on_interrupt(); 49 state.on_interrupt();
46 }); 50 });
47 self.irq 51 this.irq
48 .set_handler_context((&mut self.state) as *mut _ as *mut ()); 52 .set_handler_context((&mut this.state) as *mut _ as *mut ());
49 53
50 compiler_fence(Ordering::SeqCst); 54 compiler_fence(Ordering::SeqCst);
51 self.irq.enable(); 55 this.irq.enable();
52 56
53 self.irq_setup_done = true; 57 this.irq_setup_done = true;
54 } 58 }
55 59
56 pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S, &mut S::Interrupt) -> R) -> R { 60 pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S, &mut S::Interrupt) -> R) -> R {
57 let this = unsafe { self.get_unchecked_mut() }; 61 let this = unsafe { self.get_unchecked_mut() };
58 if !this.irq_setup_done {
59 unsafe { this.setup() }
60 }
61 62
62 this.irq.disable(); 63 this.irq.disable();
63 compiler_fence(Ordering::SeqCst); 64 compiler_fence(Ordering::SeqCst);