580: Spawn at periodic r=korken89 a=perlindgren

Examples on drift free usage under RTIC 1.0.

Co-authored-by: Per Lindgren <per.lindgren@ltu.se>
This commit is contained in:
bors[bot] 2022-01-06 14:20:20 +00:00 committed by GitHub
commit 2c921ac67c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 0 deletions

View file

@ -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

View file

@ -0,0 +1,4 @@
foo Instant { ticks: 0 }
foo Instant { ticks: 100 }
foo Instant { ticks: 200 }
foo Instant { ticks: 300 }

View file

@ -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 }

49
examples/periodic-at.rs Normal file
View file

@ -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();
}
}

61
examples/periodic-at2.rs Normal file
View file

@ -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: <MyMono as rtic_monotonic::Monotonic>::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();
}
}