Macro shared_bus::new_cortexm
source · [−]macro_rules! new_cortexm {
($bus_type:ty = $bus:expr) => { ... };
}
Expand description
Macro for creating a Cortex-M bus manager with 'static
lifetime.
This macro is a convenience helper for creating a bus manager that lives for the 'static
lifetime an thus can be safely shared across tasks/execution contexts (like interrupts).
This macro is only available with the cortex-m
feature.
Syntax
ⓘ
let bus = shared_bus::new_cortexm!(<Full Bus Type Signature> = <bus>).unwrap();
The macro returns an Option which will be Some(&'static bus_manager)
on the first run and
None
afterwards. This is necessary to uphold safety around the inner static
variable.
Example
static mut SHARED_DEVICE:
Option<MyDevice<shared_bus::I2cProxy<shared_bus::CortexMMutex<SomeI2cBus>>>>
= None;
fn main() -> ! {
// For example:
// let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 90.khz(), clocks, &mut rcc.apb1);
// The bus is a 'static reference -> it lives forever and references can be
// shared with other tasks.
let bus: &'static _ = shared_bus::new_cortexm!(SomeI2cBus = i2c).unwrap();
let mut proxy1 = bus.acquire_i2c();
let my_device = MyDevice::new(bus.acquire_i2c());
unsafe {
SHARED_DEVICE = Some(my_device);
}
cortex_m::asm::dmb();
// enable the interrupt
loop {
proxy1.write(0x39, &[0xaa]);
}
}
fn INTERRUPT() {
let dev = unsafe {SHARED_DEVICE.as_mut().unwrap()};
dev.do_something_on_the_bus();
}