embassy/examples/stm32l0/src/bin/raw_spawn.rs

53 lines
1.2 KiB
Rust
Raw Normal View History

2022-02-12 02:26:15 +01:00
#![no_std]
#![no_main]
use core::mem;
2022-06-12 22:15:44 +02:00
use cortex_m_rt::entry;
use defmt::*;
use embassy_executor::executor::raw::TaskStorage;
use embassy_executor::executor::Executor;
use embassy_executor::time::{Duration, Timer};
use embassy_util::Forever;
2022-06-12 22:15:44 +02:00
use {defmt_rtt as _, panic_probe as _};
2022-02-12 02:26:15 +01:00
async fn run1() {
loop {
info!("BIG INFREQUENT TICK");
Timer::after(Duration::from_ticks(64000)).await;
}
}
async fn run2() {
loop {
info!("tick");
Timer::after(Duration::from_ticks(13000)).await;
}
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
info!("Hello World!");
let _p = embassy_stm32::init(Default::default());
let executor = EXECUTOR.put(Executor::new());
let run1_task = TaskStorage::new();
let run2_task = TaskStorage::new();
// Safety: these variables do live forever if main never returns.
let run1_task = unsafe { make_static(&run1_task) };
let run2_task = unsafe { make_static(&run2_task) };
executor.run(|spawner| {
unwrap!(spawner.spawn(run1_task.spawn(|| run1())));
unwrap!(spawner.spawn(run2_task.spawn(|| run2())));
});
}
unsafe fn make_static<T>(t: &T) -> &'static T {
mem::transmute(t)
}