Expand description
§shared
A moderately low cost, easy to use, safe abstraction for sharing data between application and interrupt context.
§Example
use nrf52832_pac::Interrupt;
use bare_metal;
use cortex_m;
// Tuples are of the format:
// (VARIABLE_NAME, VARIABLE_TYPE, CORRESPONDING_INTERRUPT),
shared!(
(RADIO_PKTS, usize, Interrupt::RADIO),
(WALL_CLOCK, usize, Interrupt::RTC0),
);
#[entry]
fn main() {
// Using a `shared` data item in non-interrupt context
// requires a token. This is a singleton, sort of like
// the peripherals from a peripheral access crate
let mut token = RADIO_PKTS::set_initial(27).unwrap();
// You access the data from within a closure. The interrupt
// this data is shared with is disabled for the duration of
// the closure. Other interrupts may still occur.
token.modify_app_context(|y| {
*y -= 1;
y
}).unwrap();
}
#[interrupt]
fn RADIO() {
// Within an interrupt, access is only granted if it matches
// the declared interrupt. Inside the `RADIO` interrupt here,
// only `RADIO_PKTS` is accessible.
//
// Access from within an interrupt doesn't require a token.
RADIO_PKTS::modify_int_context(|x| {
*x += 1;
x
}).unwrap();
}
#[interrupt]
fn RTC0() {
// If `set_initial` was never called, then all attempts to
// access will return an `Err`. This code would panic at
// runtime!
BAZ::modify_int_context(|x| {
*x += 1;
x
}).unwrap();
}