diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-03-18 00:43:19 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-03-18 00:43:19 +0100 |
| commit | c403a47b7f77252c26356d69173169b5fe393d52 (patch) | |
| tree | 44c8adeddaff33967f3811a20666e0dabc034114 | |
| parent | 8c2da193b88488f0995b6d83d57b7d5d61e14ffe (diff) | |
Add raw_spawn example, showcasing how to use embassy without TAIT
| -rw-r--r-- | embassy-nrf-examples/src/bin/raw_spawn.rs | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/embassy-nrf-examples/src/bin/raw_spawn.rs b/embassy-nrf-examples/src/bin/raw_spawn.rs new file mode 100644 index 000000000..3747b49f6 --- /dev/null +++ b/embassy-nrf-examples/src/bin/raw_spawn.rs | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | #[path = "../example_common.rs"] | ||
| 5 | mod example_common; | ||
| 6 | use core::mem; | ||
| 7 | |||
| 8 | use embassy::executor::raw::Task; | ||
| 9 | use example_common::*; | ||
| 10 | |||
| 11 | use cortex_m_rt::entry; | ||
| 12 | use defmt::panic; | ||
| 13 | use embassy::executor::Executor; | ||
| 14 | use embassy::time::{Duration, Timer}; | ||
| 15 | use embassy::util::Forever; | ||
| 16 | use embassy_nrf::pac; | ||
| 17 | use embassy_nrf::{interrupt, rtc}; | ||
| 18 | use nrf52840_hal::clocks; | ||
| 19 | |||
| 20 | async fn run1() { | ||
| 21 | loop { | ||
| 22 | info!("BIG INFREQUENT TICK"); | ||
| 23 | Timer::after(Duration::from_ticks(64000)).await; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | async fn run2() { | ||
| 28 | loop { | ||
| 29 | info!("tick"); | ||
| 30 | Timer::after(Duration::from_ticks(13000)).await; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new(); | ||
| 35 | static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new(); | ||
| 36 | static EXECUTOR: Forever<Executor> = Forever::new(); | ||
| 37 | |||
| 38 | #[entry] | ||
| 39 | fn main() -> ! { | ||
| 40 | info!("Hello World!"); | ||
| 41 | |||
| 42 | let p = unwrap!(embassy_nrf::pac::Peripherals::take()); | ||
| 43 | |||
| 44 | clocks::Clocks::new(p.CLOCK) | ||
| 45 | .enable_ext_hfosc() | ||
| 46 | .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass) | ||
| 47 | .start_lfclk(); | ||
| 48 | |||
| 49 | let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1))); | ||
| 50 | rtc.start(); | ||
| 51 | |||
| 52 | unsafe { embassy::time::set_clock(rtc) }; | ||
| 53 | |||
| 54 | let alarm = ALARM.put(rtc.alarm0()); | ||
| 55 | let executor = EXECUTOR.put(Executor::new()); | ||
| 56 | executor.set_alarm(alarm); | ||
| 57 | |||
| 58 | let run1_task = Task::new(); | ||
| 59 | let run2_task = Task::new(); | ||
| 60 | |||
| 61 | // Safety: these variables do live forever if main never returns. | ||
| 62 | let run1_task = unsafe { make_static(&run1_task) }; | ||
| 63 | let run2_task = unsafe { make_static(&run2_task) }; | ||
| 64 | |||
| 65 | executor.run(|spawner| { | ||
| 66 | unwrap!(spawner.spawn(run1_task.spawn(|| run1()))); | ||
| 67 | unwrap!(spawner.spawn(run2_task.spawn(|| run2()))); | ||
| 68 | }); | ||
| 69 | } | ||
| 70 | |||
| 71 | unsafe fn make_static<T>(t: &T) -> &'static T { | ||
| 72 | mem::transmute(t) | ||
| 73 | } | ||
