diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-12-09 23:33:53 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-12-09 23:33:53 +0100 |
| commit | 5d4f09156af094732edc5c01332af8b13db10e0f (patch) | |
| tree | ff98446b20b6d48f929b4298a0de4e339a3262c8 /examples | |
| parent | e94ca0efd68924075d090368393f4dacbda64487 (diff) | |
nrf: add manually_create_executor example.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/manually_create_executor.rs | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/manually_create_executor.rs b/examples/nrf/src/bin/manually_create_executor.rs new file mode 100644 index 000000000..12ce660f9 --- /dev/null +++ b/examples/nrf/src/bin/manually_create_executor.rs | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | // This example showcases how to manually create an executor. | ||
| 2 | // This is what the #[embassy::main] macro does behind the scenes. | ||
| 3 | |||
| 4 | #![no_std] | ||
| 5 | #![no_main] | ||
| 6 | #![feature(type_alias_impl_trait)] | ||
| 7 | |||
| 8 | use cortex_m_rt::entry; | ||
| 9 | use defmt::{info, unwrap}; | ||
| 10 | use embassy_executor::Executor; | ||
| 11 | use embassy_time::{Duration, Timer}; | ||
| 12 | use static_cell::StaticCell; | ||
| 13 | use {defmt_rtt as _, panic_probe as _}; | ||
| 14 | |||
| 15 | #[embassy_executor::task] | ||
| 16 | async fn run1() { | ||
| 17 | loop { | ||
| 18 | info!("BIG INFREQUENT TICK"); | ||
| 19 | Timer::after(Duration::from_ticks(64000)).await; | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | #[embassy_executor::task] | ||
| 24 | async fn run2() { | ||
| 25 | loop { | ||
| 26 | info!("tick"); | ||
| 27 | Timer::after(Duration::from_ticks(13000)).await; | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | static EXECUTOR: StaticCell<Executor> = StaticCell::new(); | ||
| 32 | |||
| 33 | #[entry] | ||
| 34 | fn main() -> ! { | ||
| 35 | info!("Hello World!"); | ||
| 36 | |||
| 37 | let _p = embassy_nrf::init(Default::default()); | ||
| 38 | |||
| 39 | // Create the executor and put it in a StaticCell, because `run` needs `&'static mut Executor`. | ||
| 40 | let executor = EXECUTOR.init(Executor::new()); | ||
| 41 | |||
| 42 | // Run it. | ||
| 43 | // `run` calls the closure then runs the executor forever. It never returns. | ||
| 44 | executor.run(|spawner| { | ||
| 45 | // Here we get access to a spawner to spawn the initial tasks. | ||
| 46 | unwrap!(spawner.spawn(run1())); | ||
| 47 | unwrap!(spawner.spawn(run2())); | ||
| 48 | }); | ||
| 49 | } | ||
