From 305e8295d5727502bcbd13a5a0b7a60759e5152b Mon Sep 17 00:00:00 2001 From: Per Lindgren Date: Tue, 4 Jan 2022 20:59:22 +0100 Subject: [PATCH] Drift free timing examples --- CHANGELOG.md | 2 ++ ci/expected/periodic-at.run | 4 +++ ci/expected/periodic-at2.run | 7 +++++ examples/periodic-at.rs | 49 +++++++++++++++++++++++++++++ examples/periodic-at2.rs | 61 ++++++++++++++++++++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 ci/expected/periodic-at.run create mode 100644 ci/expected/periodic-at2.run create mode 100644 examples/periodic-at.rs create mode 100644 examples/periodic-at2.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 378bd11dc7..930a338356 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - CI changelog entry enforcer +- `examples/periodic-at.rs`, an example of a periodic timer without accumulated drift. +- `examples/periodic-at2.rs`, an example of a periodic process with two tasks, with offset timing. Here we depict two alternative usages of the timer type, explicit and trait based. ## [v1.0.0] - 2021-12-25 diff --git a/ci/expected/periodic-at.run b/ci/expected/periodic-at.run new file mode 100644 index 0000000000..54020f9e95 --- /dev/null +++ b/ci/expected/periodic-at.run @@ -0,0 +1,4 @@ +foo Instant { ticks: 0 } +foo Instant { ticks: 100 } +foo Instant { ticks: 200 } +foo Instant { ticks: 300 } diff --git a/ci/expected/periodic-at2.run b/ci/expected/periodic-at2.run new file mode 100644 index 0000000000..47adbef486 --- /dev/null +++ b/ci/expected/periodic-at2.run @@ -0,0 +1,7 @@ +foo Instant { ticks: 0 } +bar Instant { ticks: 10 } +foo Instant { ticks: 110 } +bar Instant { ticks: 120 } +foo Instant { ticks: 220 } +bar Instant { ticks: 230 } +foo Instant { ticks: 330 } diff --git a/examples/periodic-at.rs b/examples/periodic-at.rs new file mode 100644 index 0000000000..f9fd995fb0 --- /dev/null +++ b/examples/periodic-at.rs @@ -0,0 +1,49 @@ +//! examples/periodic-at.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] +mod app { + use cortex_m_semihosting::{debug, hprintln}; + use systick_monotonic::*; + + #[monotonic(binds = SysTick, default = true)] + type MyMono = Systick<100>; // 100 Hz / 10 ms granularity + + #[shared] + struct Shared {} + + #[local] + struct Local {} + + #[init] + fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { + let systick = cx.core.SYST; + + // Initialize the monotonic (SysTick rate in QEMU is 12 MHz) + let mut mono = Systick::new(systick, 12_000_000); + + foo::spawn_after(1.secs(), mono.now()).unwrap(); + + (Shared {}, Local {}, init::Monotonics(mono)) + } + + #[task(local = [cnt: u32 = 0])] + fn foo(cx: foo::Context, instant: fugit::TimerInstantU64<100>) { + hprintln!("foo {:?}", instant).ok(); + *cx.local.cnt += 1; + + if *cx.local.cnt == 4 { + debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator + } + + // Periodic ever 1 seconds + let next_instant = instant + 1.secs(); + foo::spawn_at(next_instant, next_instant).unwrap(); + } +} diff --git a/examples/periodic-at2.rs b/examples/periodic-at2.rs new file mode 100644 index 0000000000..879f709c65 --- /dev/null +++ b/examples/periodic-at2.rs @@ -0,0 +1,61 @@ +//! examples/periodic-at2.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] +mod app { + use cortex_m_semihosting::{debug, hprintln}; + use systick_monotonic::*; + + #[monotonic(binds = SysTick, default = true)] + type MyMono = Systick<100>; // 100 Hz / 10 ms granularity + + #[shared] + struct Shared {} + + #[local] + struct Local {} + + #[init] + fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { + let systick = cx.core.SYST; + + // Initialize the monotonic (SysTick rate in QEMU is 12 MHz) + let mut mono = Systick::new(systick, 12_000_000); + + foo::spawn_after(1.secs(), mono.now()).unwrap(); + + (Shared {}, Local {}, init::Monotonics(mono)) + } + + // Using the explicit type of the timer implementation + #[task(local = [cnt: u32 = 0])] + fn foo(cx: foo::Context, instant: fugit::TimerInstantU64<100>) { + hprintln!("foo {:?}", instant).ok(); + *cx.local.cnt += 1; + + if *cx.local.cnt == 4 { + debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator + } + + // Spawn a new message with 100 ms offset to spawned time + let next_instant = instant + 100.millis(); + bar::spawn_at(next_instant, next_instant).unwrap(); + } + + // Using the Instant from the Monotonic trait + // This remains agnostic to the timer implementation + #[task(local = [cnt: u32 = 0])] + fn bar(_cx: bar::Context, instant: ::Instant) { + hprintln!("bar {:?}", instant).ok(); + + // Spawn a new message with 1s offset to spawned time + let next_instant = instant + 1.secs(); + foo::spawn_at(next_instant, next_instant).unwrap(); + } +}