aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2020-12-30 00:56:32 +0100
committerDario Nieuwenhuis <[email protected]>2020-12-30 00:57:35 +0100
commit015b6bbce4fa93a58458bd72a5f7168d746f1e37 (patch)
treea12a7ef8d4ee0e837bf4cf9f3d0bc8fbb6e5b62d /examples
parent2bf9b14ef07c4d2a33ee8a45b2f07b4cdd050e9e (diff)
Ensure timers always yield at least once.
This prevents a task that's constantly running late from monopolizing the CPU. Add executor_fairness_test example showcasing it.
Diffstat (limited to 'examples')
-rw-r--r--examples/src/bin/executor_fairness_test.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/examples/src/bin/executor_fairness_test.rs b/examples/src/bin/executor_fairness_test.rs
new file mode 100644
index 000000000..9b2c1bd26
--- /dev/null
+++ b/examples/src/bin/executor_fairness_test.rs
@@ -0,0 +1,74 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5#[path = "../example_common.rs"]
6mod example_common;
7use example_common::*;
8
9use core::task::Poll;
10use cortex_m_rt::entry;
11use defmt::panic;
12use embassy::executor::{task, Executor};
13use embassy::time::{Duration, Instant, Timer};
14use embassy::util::Forever;
15use embassy_nrf::pac;
16use embassy_nrf::{interrupt, rtc};
17use nrf52840_hal::clocks;
18
19#[task]
20async fn run1() {
21 loop {
22 info!("DING DONG");
23 Timer::after(Duration::from_ticks(16000)).await;
24 }
25}
26
27#[task]
28async fn run2() {
29 loop {
30 Timer::at(Instant::from_ticks(0)).await;
31 }
32}
33
34#[task]
35async fn run3() {
36 futures::future::poll_fn(|cx| {
37 cx.waker().wake_by_ref();
38 Poll::<()>::Pending
39 })
40 .await;
41}
42
43static RTC: Forever<rtc::RTC<pac::RTC1>> = Forever::new();
44static ALARM: Forever<rtc::Alarm<pac::RTC1>> = Forever::new();
45static EXECUTOR: Forever<Executor> = Forever::new();
46
47#[entry]
48fn main() -> ! {
49 info!("Hello World!");
50
51 let p = unwrap!(embassy_nrf::pac::Peripherals::take());
52
53 clocks::Clocks::new(p.CLOCK)
54 .enable_ext_hfosc()
55 .set_lfclk_src_external(clocks::LfOscConfiguration::NoExternalNoBypass)
56 .start_lfclk();
57
58 let rtc = RTC.put(rtc::RTC::new(p.RTC1, interrupt::take!(RTC1)));
59 rtc.start();
60
61 unsafe { embassy::time::set_clock(rtc) };
62
63 let alarm = ALARM.put(rtc.alarm0());
64 let executor = EXECUTOR.put(Executor::new_with_alarm(alarm, cortex_m::asm::sev));
65
66 unwrap!(executor.spawn(run1()));
67 unwrap!(executor.spawn(run2()));
68 unwrap!(executor.spawn(run3()));
69
70 loop {
71 executor.run();
72 cortex_m::asm::wfe();
73 }
74}