aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2021-01-21 10:22:30 -0600
committerxoviat <[email protected]>2021-01-21 10:22:30 -0600
commit6503f9dbf58c36d89b0f1bf9e70587905fbf3d07 (patch)
tree08b39ab8585e2366998f74c7cbd00aab43d6f10e
parent27831124a761e89563c50c4ca41005fe4cac720e (diff)
implement interruptfuture
converts an interrupt to a future
-rw-r--r--embassy/src/lib.rs2
-rw-r--r--embassy/src/util/signal.rs72
2 files changed, 74 insertions, 0 deletions
diff --git a/embassy/src/lib.rs b/embassy/src/lib.rs
index 4fa37a55c..74c69b541 100644
--- a/embassy/src/lib.rs
+++ b/embassy/src/lib.rs
@@ -2,6 +2,8 @@
2#![feature(generic_associated_types)] 2#![feature(generic_associated_types)]
3#![feature(const_fn)] 3#![feature(const_fn)]
4#![feature(const_fn_fn_ptr_basics)] 4#![feature(const_fn_fn_ptr_basics)]
5#![feature(const_in_array_repeat_expressions)]
6#![feature(const_option)]
5 7
6// This mod MUST go first, so that the others see its macros. 8// This mod MUST go first, so that the others see its macros.
7pub(crate) mod fmt; 9pub(crate) mod fmt;
diff --git a/embassy/src/util/signal.rs b/embassy/src/util/signal.rs
index 8e778d1e3..28f86505e 100644
--- a/embassy/src/util/signal.rs
+++ b/embassy/src/util/signal.rs
@@ -1,8 +1,13 @@
1use crate::executor;
1use crate::fmt::panic; 2use crate::fmt::panic;
3use crate::interrupt::OwnedInterrupt;
2use core::cell::UnsafeCell; 4use core::cell::UnsafeCell;
3use core::future::Future; 5use core::future::Future;
4use core::mem; 6use core::mem;
7use core::ptr;
5use core::task::{Context, Poll, Waker}; 8use core::task::{Context, Poll, Waker};
9use cortex_m::peripheral::NVIC;
10use cortex_m::peripheral::{scb, SCB};
6 11
7pub struct Signal<T> { 12pub struct Signal<T> {
8 state: UnsafeCell<State<T>>, 13 state: UnsafeCell<State<T>>,
@@ -67,3 +72,70 @@ impl<T: Send> Signal<T> {
67 cortex_m::interrupt::free(|_| matches!(unsafe { &*self.state.get() }, State::Signaled(_))) 72 cortex_m::interrupt::free(|_| matches!(unsafe { &*self.state.get() }, State::Signaled(_)))
68 } 73 }
69} 74}
75
76struct NrWrap(u8);
77unsafe impl cortex_m::interrupt::Nr for NrWrap {
78 fn nr(&self) -> u8 {
79 self.0
80 }
81}
82
83pub struct InterruptFuture<'a, I: OwnedInterrupt> {
84 interrupt: &'a mut I,
85}
86
87impl<'a, I: OwnedInterrupt> Drop for InterruptFuture<'a, I> {
88 fn drop(&mut self) {
89 cortex_m::interrupt::free(|_| {
90 self.interrupt.remove_handler();
91 self.interrupt.disable();
92 });
93 }
94}
95
96impl<'a, I: OwnedInterrupt> InterruptFuture<'a, I> {
97 pub fn new(interrupt: &'a mut I) -> Self {
98 cortex_m::interrupt::free(|_| {
99 interrupt.set_handler(Self::interrupt_handler, ptr::null_mut());
100 interrupt.unpend();
101 interrupt.enable();
102 });
103
104 Self {
105 interrupt: interrupt,
106 }
107 }
108
109 unsafe fn interrupt_handler(ctx: *mut ()) {
110 let irq = match SCB::vect_active() {
111 scb::VectActive::Interrupt { irqn } => irqn,
112 _ => unreachable!(),
113 };
114
115 if ctx as *const _ != ptr::null() {
116 executor::raw::wake_task(ptr::NonNull::new_unchecked(ctx));
117 }
118
119 NVIC::mask(NrWrap(irq));
120 }
121}
122
123impl<'a, I: OwnedInterrupt> Future for InterruptFuture<'a, I> {
124 type Output = ();
125
126 fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
127 cortex_m::interrupt::free(|_| unsafe {
128 let s = self.get_unchecked_mut();
129 if s.interrupt.is_enabled() {
130 s.interrupt.set_handler(
131 Self::interrupt_handler,
132 executor::raw::task_from_waker(&cx.waker()).cast().as_ptr(),
133 );
134
135 Poll::Pending
136 } else {
137 Poll::Ready(())
138 }
139 })
140 }
141}