diff options
| author | Oleksandr Babak <alexanderbabak@proton.me> | 2025-03-27 11:23:42 +0100 |
|---|---|---|
| committer | Emil Fresk <emil.fresk@gmail.com> | 2025-03-27 14:47:11 +0000 |
| commit | 8127208eb5a8e4a5a62dc4a9cd147945cc6f595d (patch) | |
| tree | 6b1aeb2913eaa4d5dde6348a36d05f2726289fe4 /examples | |
| parent | b75b8f98b0b3c00e608778aa64d2c90bc3cddb97 (diff) | |
feat: add example for divergent tasks
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/lm3s6965/examples/static-recources-in-divergent.rs | 67 | ||||
| -rw-r--r-- | examples/lm3s6965/examples/static-recources-in-init.rs (renamed from examples/lm3s6965/examples/static.rs) | 2 |
2 files changed, 68 insertions, 1 deletions
diff --git a/examples/lm3s6965/examples/static-recources-in-divergent.rs b/examples/lm3s6965/examples/static-recources-in-divergent.rs new file mode 100644 index 0000000..af8e982 --- /dev/null +++ b/examples/lm3s6965/examples/static-recources-in-divergent.rs @@ -0,0 +1,67 @@ +//! examples/static-resources-in-divergent.rs + +#![no_main] +#![no_std] +#![deny(warnings)] +#![deny(unsafe_code)] +#![deny(missing_docs)] + +use panic_semihosting as _; +use rtic_monotonics::systick::prelude::*; +systick_monotonic!(Mono, 100); + +#[rtic::app(device = lm3s6965, dispatchers = [UART0])] +mod app { + use super::*; + + use cortex_m_semihosting::{debug, hprintln}; + use rtic_sync::channel::{Channel, Receiver}; + + #[shared] + struct Shared {} + + #[local] + struct Local {} + + #[init] + fn init(cx: init::Context) -> (Shared, Local) { + Mono::start(cx.core.SYST, 12_000_000); + + divergent::spawn().ok(); + + (Shared {}, Local {}) + } + + #[task(local = [q: Channel<u32, 5> = Channel::new()], priority = 1)] + async fn divergent(cx: divergent::Context) -> ! { + // `q` has `'static` lifetime. You can put references to it in `static` variables, + // structs with references to `q` do not need a generic lifetime parameter, etc. + + let (mut tx, rx) = cx.local.q.split(); + let mut state = 0; + + bar::spawn(rx).unwrap(); + + loop { + tx.send(state).await.unwrap(); + state += 1; + Mono::delay(100.millis()).await; + } + } + + #[task(priority = 1)] + async fn bar(_cx: bar::Context, mut rx: Receiver<'static, u32, 5>) -> ! { + loop { + // Lock-free access to the same underlying queue! + if let Some(data) = rx.recv().await.ok() { + hprintln!("received message: {}", data); + + if data == 3 { + debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator + } else { + Mono::delay(100.millis()).await; + } + } + } + } +} diff --git a/examples/lm3s6965/examples/static.rs b/examples/lm3s6965/examples/static-recources-in-init.rs index fec73fc..419ea97 100644 --- a/examples/lm3s6965/examples/static.rs +++ b/examples/lm3s6965/examples/static-recources-in-init.rs @@ -1,4 +1,4 @@ -//! examples/static.rs +//! examples/static-resources-in-init.rs #![no_main] #![no_std] |
