aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf52840/src/bin/manually_create_executor.rs
blob: f0639eb23edce066f368ae77cb5ad13e51053268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// This example showcases how to manually create an executor.
// This is what the #[embassy::main] macro does behind the scenes.

#![no_std]
#![no_main]

use cortex_m_rt::entry;
use defmt::{info, unwrap};
use embassy_executor::Executor;
use embassy_time::Timer;
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};

#[embassy_executor::task]
async fn run1() {
    loop {
        info!("BIG INFREQUENT TICK");
        Timer::after_ticks(64000).await;
    }
}

#[embassy_executor::task]
async fn run2() {
    loop {
        info!("tick");
        Timer::after_ticks(13000).await;
    }
}

static EXECUTOR: StaticCell<Executor> = StaticCell::new();

#[entry]
fn main() -> ! {
    info!("Hello World!");

    let _p = embassy_nrf::init(Default::default());

    // Create the executor and put it in a StaticCell, because `run` needs `&'static mut Executor`.
    let executor = EXECUTOR.init(Executor::new());

    // Run it.
    // `run` calls the closure then runs the executor forever. It never returns.
    executor.run(|spawner| {
        // Here we get access to a spawner to spawn the initial tasks.
        spawner.spawn(unwrap!(run1()));
        spawner.spawn(unwrap!(run2()));
    });
}