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