diff options
| author | James Munns <[email protected]> | 2025-04-16 17:58:45 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2025-09-11 14:45:06 +0200 |
| commit | 0cb1ffe0258380a3dd25c1037fdfb6ceca3149d9 (patch) | |
| tree | 9757a4f14c47412d8eed7a2d67ee7732f06c6cfe /examples | |
| parent | 0e28ba1091257111f71b76a664d7038dbfcf9b5e (diff) | |
Add EDF example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf52840-edf/.cargo/config.toml | 9 | ||||
| -rw-r--r-- | examples/nrf52840-edf/Cargo.toml | 21 | ||||
| -rw-r--r-- | examples/nrf52840-edf/build.rs | 35 | ||||
| -rw-r--r-- | examples/nrf52840-edf/memory.x | 12 | ||||
| -rw-r--r-- | examples/nrf52840-edf/src/bin/basic.rs | 191 |
5 files changed, 268 insertions, 0 deletions
diff --git a/examples/nrf52840-edf/.cargo/config.toml b/examples/nrf52840-edf/.cargo/config.toml new file mode 100644 index 000000000..e0b9ce59e --- /dev/null +++ b/examples/nrf52840-edf/.cargo/config.toml | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | ||
| 2 | # replace nRF82840_xxAA with your chip as listed in `probe-rs chip list` | ||
| 3 | runner = "probe-rs run --chip nRF52840_xxAA" | ||
| 4 | |||
| 5 | [build] | ||
| 6 | target = "thumbv7em-none-eabi" | ||
| 7 | |||
| 8 | [env] | ||
| 9 | DEFMT_LOG = "debug" | ||
diff --git a/examples/nrf52840-edf/Cargo.toml b/examples/nrf52840-edf/Cargo.toml new file mode 100644 index 000000000..c7147d1af --- /dev/null +++ b/examples/nrf52840-edf/Cargo.toml | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2021" | ||
| 3 | name = "embassy-nrf52840-edf-examples" | ||
| 4 | version = "0.1.0" | ||
| 5 | license = "MIT OR Apache-2.0" | ||
| 6 | |||
| 7 | [dependencies] | ||
| 8 | # NOTE: "edf-scheduler" feature is enabled | ||
| 9 | embassy-executor = { version = "0.7.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "edf-scheduler"] } | ||
| 10 | embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | ||
| 11 | embassy-nrf = { version = "0.3.1", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac", "time"] } | ||
| 12 | |||
| 13 | defmt = "0.3" | ||
| 14 | defmt-rtt = "0.4" | ||
| 15 | |||
| 16 | cortex-m = { version = "0.7.6", features = ["inline-asm", "critical-section-single-core"] } | ||
| 17 | cortex-m-rt = "0.7.0" | ||
| 18 | panic-probe = { version = "0.3", features = ["print-defmt"] } | ||
| 19 | |||
| 20 | [profile.release] | ||
| 21 | debug = 2 | ||
diff --git a/examples/nrf52840-edf/build.rs b/examples/nrf52840-edf/build.rs new file mode 100644 index 000000000..30691aa97 --- /dev/null +++ b/examples/nrf52840-edf/build.rs | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | //! This build script copies the `memory.x` file from the crate root into | ||
| 2 | //! a directory where the linker can always find it at build time. | ||
| 3 | //! For many projects this is optional, as the linker always searches the | ||
| 4 | //! project root directory -- wherever `Cargo.toml` is. However, if you | ||
| 5 | //! are using a workspace or have a more complicated build setup, this | ||
| 6 | //! build script becomes required. Additionally, by requesting that | ||
| 7 | //! Cargo re-run the build script whenever `memory.x` is changed, | ||
| 8 | //! updating `memory.x` ensures a rebuild of the application with the | ||
| 9 | //! new memory settings. | ||
| 10 | |||
| 11 | use std::env; | ||
| 12 | use std::fs::File; | ||
| 13 | use std::io::Write; | ||
| 14 | use std::path::PathBuf; | ||
| 15 | |||
| 16 | fn main() { | ||
| 17 | // Put `memory.x` in our output directory and ensure it's | ||
| 18 | // on the linker search path. | ||
| 19 | let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
| 20 | File::create(out.join("memory.x")) | ||
| 21 | .unwrap() | ||
| 22 | .write_all(include_bytes!("memory.x")) | ||
| 23 | .unwrap(); | ||
| 24 | println!("cargo:rustc-link-search={}", out.display()); | ||
| 25 | |||
| 26 | // By default, Cargo will re-run a build script whenever | ||
| 27 | // any file in the project changes. By specifying `memory.x` | ||
| 28 | // here, we ensure the build script is only re-run when | ||
| 29 | // `memory.x` is changed. | ||
| 30 | println!("cargo:rerun-if-changed=memory.x"); | ||
| 31 | |||
| 32 | println!("cargo:rustc-link-arg-bins=--nmagic"); | ||
| 33 | println!("cargo:rustc-link-arg-bins=-Tlink.x"); | ||
| 34 | println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); | ||
| 35 | } | ||
diff --git a/examples/nrf52840-edf/memory.x b/examples/nrf52840-edf/memory.x new file mode 100644 index 000000000..15b492bce --- /dev/null +++ b/examples/nrf52840-edf/memory.x | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | MEMORY | ||
| 2 | { | ||
| 3 | /* NOTE 1 K = 1 KiBi = 1024 bytes */ | ||
| 4 | FLASH : ORIGIN = 0x00000000, LENGTH = 1024K | ||
| 5 | RAM : ORIGIN = 0x20000000, LENGTH = 256K | ||
| 6 | |||
| 7 | /* These values correspond to the NRF52840 with Softdevices S140 7.3.0 */ | ||
| 8 | /* | ||
| 9 | FLASH : ORIGIN = 0x00027000, LENGTH = 868K | ||
| 10 | RAM : ORIGIN = 0x20020000, LENGTH = 128K | ||
| 11 | */ | ||
| 12 | } | ||
diff --git a/examples/nrf52840-edf/src/bin/basic.rs b/examples/nrf52840-edf/src/bin/basic.rs new file mode 100644 index 000000000..6a7eb3c4b --- /dev/null +++ b/examples/nrf52840-edf/src/bin/basic.rs | |||
| @@ -0,0 +1,191 @@ | |||
| 1 | //! Basic side-by-side example of the Earliest Deadline First scheduler | ||
| 2 | //! | ||
| 3 | //! This test spawns a number of background "ambient system load" workers | ||
| 4 | //! that are constantly working, and runs two sets of trials. | ||
| 5 | //! | ||
| 6 | //! The first trial runs with no deadline set, so our trial task is at the | ||
| 7 | //! same prioritization level as the background worker tasks. | ||
| 8 | //! | ||
| 9 | //! The second trial sets a deadline, meaning that it will be given higher | ||
| 10 | //! scheduling priority than background tasks, that have no deadline set | ||
| 11 | |||
| 12 | #![no_std] | ||
| 13 | #![no_main] | ||
| 14 | |||
| 15 | use core::sync::atomic::{compiler_fence, Ordering}; | ||
| 16 | use embassy_executor::{raw::Deadline, Spawner}; | ||
| 17 | use embassy_time::{Duration, Instant, Timer}; | ||
| 18 | use {defmt_rtt as _, panic_probe as _}; | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(spawner: Spawner) { | ||
| 22 | embassy_nrf::init(Default::default()); | ||
| 23 | |||
| 24 | // Enable flash cache to remove some flash latency jitter | ||
| 25 | compiler_fence(Ordering::SeqCst); | ||
| 26 | embassy_nrf::pac::NVMC.icachecnf().write(|w| { | ||
| 27 | w.set_cacheen(true); | ||
| 28 | }); | ||
| 29 | compiler_fence(Ordering::SeqCst); | ||
| 30 | |||
| 31 | // | ||
| 32 | // Baseline system load tunables | ||
| 33 | // | ||
| 34 | |||
| 35 | // how many load tasks? More load tasks means more tasks contending | ||
| 36 | // for the runqueue | ||
| 37 | let tasks = 32; | ||
| 38 | // how long should each task work for? The longer the working time, | ||
| 39 | // the longer the max jitter possible, even when a task is prioritized, | ||
| 40 | // as EDF is still cooperative and not pre-emptive | ||
| 41 | // | ||
| 42 | // 33 ticks ~= 1ms | ||
| 43 | let work_time_ticks = 33; | ||
| 44 | // what fraction, 1/denominator, should the system be busy? | ||
| 45 | // bigger number means **less** busy | ||
| 46 | // | ||
| 47 | // 2 => 50% | ||
| 48 | // 4 => 25% | ||
| 49 | // 10 => 10% | ||
| 50 | let denominator = 2; | ||
| 51 | |||
| 52 | // Total time window, so each worker is working 1/denominator | ||
| 53 | // amount of the total time | ||
| 54 | let time_window = work_time_ticks * u64::from(tasks) * denominator; | ||
| 55 | |||
| 56 | // Spawn all of our load workers! | ||
| 57 | for i in 0..tasks { | ||
| 58 | spawner.must_spawn(load_task(i, work_time_ticks, time_window)); | ||
| 59 | } | ||
| 60 | |||
| 61 | // Let all the tasks spin up | ||
| 62 | defmt::println!("Spinning up load tasks..."); | ||
| 63 | Timer::after_secs(1).await; | ||
| 64 | |||
| 65 | // | ||
| 66 | // Trial task worker tunables | ||
| 67 | // | ||
| 68 | |||
| 69 | // How many steps should the workers under test run? | ||
| 70 | // More steps means more chances to have to wait for other tasks | ||
| 71 | // in line ahead of us. | ||
| 72 | let num_steps = 100; | ||
| 73 | |||
| 74 | // How many ticks should the worker take working on each step? | ||
| 75 | // | ||
| 76 | // 33 ticks ~= 1ms | ||
| 77 | let work_ticks = 33; | ||
| 78 | // How many ticks should the worker wait on each step? | ||
| 79 | // | ||
| 80 | // 66 ticks ~= 2ms | ||
| 81 | let idle_ticks = 66; | ||
| 82 | |||
| 83 | // How many times to repeat each trial? | ||
| 84 | let trials = 3; | ||
| 85 | |||
| 86 | // The total time a trial would take, in a perfect unloaded system | ||
| 87 | let theoretical = (num_steps * work_ticks) + (num_steps * idle_ticks); | ||
| 88 | |||
| 89 | defmt::println!(""); | ||
| 90 | defmt::println!("Starting UNPRIORITIZED worker trials"); | ||
| 91 | for _ in 0..trials { | ||
| 92 | // | ||
| 93 | // UNPRIORITIZED worker | ||
| 94 | // | ||
| 95 | defmt::println!(""); | ||
| 96 | defmt::println!("Starting unprioritized worker"); | ||
| 97 | let start = Instant::now(); | ||
| 98 | for _ in 0..num_steps { | ||
| 99 | let now = Instant::now(); | ||
| 100 | while now.elapsed().as_ticks() < work_ticks {} | ||
| 101 | Timer::after_ticks(idle_ticks).await; | ||
| 102 | } | ||
| 103 | let elapsed = start.elapsed().as_ticks(); | ||
| 104 | defmt::println!( | ||
| 105 | "Trial complete, theoretical ticks: {=u64}, actual ticks: {=u64}", | ||
| 106 | theoretical, | ||
| 107 | elapsed | ||
| 108 | ); | ||
| 109 | let ratio = ((elapsed as f32) / (theoretical as f32)) * 100.0; | ||
| 110 | defmt::println!("Took {=f32}% of ideal time", ratio); | ||
| 111 | Timer::after_millis(500).await; | ||
| 112 | } | ||
| 113 | |||
| 114 | Timer::after_secs(1).await; | ||
| 115 | |||
| 116 | defmt::println!(""); | ||
| 117 | defmt::println!("Starting PRIORITIZED worker trials"); | ||
| 118 | for _ in 0..trials { | ||
| 119 | // | ||
| 120 | // PRIORITIZED worker | ||
| 121 | // | ||
| 122 | defmt::println!(""); | ||
| 123 | defmt::println!("Starting prioritized worker"); | ||
| 124 | let start = Instant::now(); | ||
| 125 | // Set the deadline to ~2x the theoretical time. In practice, setting any deadline | ||
| 126 | // here elevates the current task above all other worker tasks. | ||
| 127 | Deadline::set_current_task_deadline_after(theoretical * 2).await; | ||
| 128 | |||
| 129 | // Perform the trial | ||
| 130 | for _ in 0..num_steps { | ||
| 131 | let now = Instant::now(); | ||
| 132 | while now.elapsed().as_ticks() < work_ticks {} | ||
| 133 | Timer::after_ticks(idle_ticks).await; | ||
| 134 | } | ||
| 135 | |||
| 136 | let elapsed = start.elapsed().as_ticks(); | ||
| 137 | defmt::println!( | ||
| 138 | "Trial complete, theoretical ticks: {=u64}, actual ticks: {=u64}", | ||
| 139 | theoretical, | ||
| 140 | elapsed | ||
| 141 | ); | ||
| 142 | let ratio = ((elapsed as f32) / (theoretical as f32)) * 100.0; | ||
| 143 | defmt::println!("Took {=f32}% of ideal time", ratio); | ||
| 144 | |||
| 145 | // Unset the deadline, deadlines are not automatically cleared, and if our | ||
| 146 | // deadline is in the past, then we get very high priority! | ||
| 147 | Deadline::clear_current_task_deadline().await; | ||
| 148 | |||
| 149 | Timer::after_millis(500).await; | ||
| 150 | } | ||
| 151 | |||
| 152 | defmt::println!(""); | ||
| 153 | defmt::println!("Trials Complete."); | ||
| 154 | } | ||
| 155 | |||
| 156 | #[embassy_executor::task(pool_size = 32)] | ||
| 157 | async fn load_task(id: u32, ticks_on: u64, ttl_ticks: u64) { | ||
| 158 | let mut last_print = Instant::now(); | ||
| 159 | let mut last_tick = last_print; | ||
| 160 | let mut variance = 0; | ||
| 161 | let mut max_variance = 0; | ||
| 162 | loop { | ||
| 163 | let tgt = last_tick + Duration::from_ticks(ttl_ticks); | ||
| 164 | assert!(tgt > Instant::now(), "fell too behind!"); | ||
| 165 | |||
| 166 | Timer::at(tgt).await; | ||
| 167 | let now = Instant::now(); | ||
| 168 | // How late are we from the target? | ||
| 169 | let var = now.duration_since(tgt).as_ticks(); | ||
| 170 | max_variance = max_variance.max(var); | ||
| 171 | variance += var; | ||
| 172 | |||
| 173 | // blocking work | ||
| 174 | while now.elapsed().as_ticks() < ticks_on {} | ||
| 175 | |||
| 176 | if last_print.elapsed() >= Duration::from_secs(1) { | ||
| 177 | defmt::trace!( | ||
| 178 | "Task {=u32} variance ticks (1s): {=u64}, max: {=u64}, act: {=u64}", | ||
| 179 | id, | ||
| 180 | variance, | ||
| 181 | max_variance, | ||
| 182 | ticks_on, | ||
| 183 | ); | ||
| 184 | max_variance = 0; | ||
| 185 | variance = 0; | ||
| 186 | last_print = Instant::now(); | ||
| 187 | } | ||
| 188 | |||
| 189 | last_tick = tgt; | ||
| 190 | } | ||
| 191 | } | ||
