Last active
January 6, 2019 14:52
-
-
Save jamesmunns/c63845be8bc6cdf913de86e7fb6e29c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn new() -> PeripheralX { | |
// Syntax could look a little like this: | |
let int_handle = interrupt!(UARTE0_UART0, |mut cons: Consumer| { | |
// Do some interrupty things here | |
cons.read(); | |
// ... | |
}); | |
// Enable the interrupt, but do not pend | |
int_handle.spawn(); | |
// You could also do this, which would disable the interrupt, | |
// and maybe give back the resources? | |
let cons = int_handle.join().unwrap(); | |
// Which would expand to something like this: | |
{ | |
static mut CONS: Option<Consumer> = None; | |
unsafe { | |
assert!(CONS.is_none()); | |
CONS = Some(cons); | |
} | |
#[interrupt] | |
unsafe fn UARTE0_UART0() { | |
// This is generated by the macro | |
// shadow the static muts | |
let mut cons = CONS.as_mut().unwrap(); | |
// This is the body of the "closure" | |
{ | |
// Do some interrupty things here | |
cons.read(); | |
// ... | |
} | |
} | |
// This would probably be defined somewhere else | |
struct Handle<T, D> { | |
interrupt: T, | |
data: Option<&'static mut D> | |
} | |
impl<T, D> Handle<T, D> { | |
fn spawn(&self) { NVIC.enable(self.interrupt) } | |
fn join(self) { self.data.take() } | |
} | |
// return the handle | |
Handle { | |
interrupt: UARTE0_UART0, | |
data: CONS.as_mut(), | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment