diff --git a/examples/static.rs b/examples/static.rs new file mode 100644 index 0000000000..9c3110c035 --- /dev/null +++ b/examples/static.rs @@ -0,0 +1,59 @@ +//! examples/static.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use heapless::{ + consts::*, + i, + spsc::{Consumer, Producer, Queue}, +}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + + use crate::U4; + use crate::{Consumer, Producer}; + + // Late resources + #[resources] + struct Resources { + p: Producer<'static, u32, U4>, + c: Consumer<'static, u32, U4>, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + static mut Q: Queue = Queue(i::Queue::new()); + + let (p, c) = Q.split(); + + // Initialization of late resources + init::LateResources { p, c } + } + + #[idle(resources = [c])] + fn idle(c: idle::Context) -> ! { + loop { + if let Some(byte) = c.resources.c.dequeue() { + hprintln!("received message: {}", byte).unwrap(); + + debug::exit(debug::EXIT_SUCCESS); + } else { + rtic::pend(Interrupt::UART0); + } + } + } + + #[task(binds = UART0, resources = [p])] + fn uart0(c: uart0::Context) { + static mut KALLE: u32 = 0; + *KALLE += 1; + c.resources.p.enqueue(42).unwrap(); + } +} diff --git a/examples/task-local-minimal.rs b/examples/task-local-minimal.rs new file mode 100644 index 0000000000..fd5ac68a29 --- /dev/null +++ b/examples/task-local-minimal.rs @@ -0,0 +1,33 @@ +//! examples/task-local_minimal.rs +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // A local (move), late resource + #[task_local] + l: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + init::LateResources { l: 42 } + } + + // l is task_local + #[idle(resources =[l])] + fn idle(cx: idle::Context) -> ! { + hprintln!("IDLE:l = {}", cx.resources.l).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop { + cortex_m::asm::nop(); + } + } +} diff --git a/examples/task-local.rs b/examples/task-local.rs new file mode 100644 index 0000000000..8f0dfc792a --- /dev/null +++ b/examples/task-local.rs @@ -0,0 +1,82 @@ +//! examples/task-local.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // An early resource + #[init(0)] + shared: u32, + + // A local (move), early resource + #[task_local] + #[init(1)] + l1: u32, + + // An exclusive, early resource + #[lock_free] + #[init(1)] + e1: u32, + + // A local (move), late resource + #[task_local] + l2: u32, + + // An exclusive, late resource + #[lock_free] + e2: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + rtic::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART1); + init::LateResources { e2: 2, l2: 2 } + } + + // `shared` cannot be accessed from this context + // l1 ok (task_local) + // e2 ok (lock_free) + #[idle(resources =[l1, e2])] + fn idle(cx: idle::Context) -> ! { + hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap(); + hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop { + cortex_m::asm::nop(); + } + } + + // `shared` can be accessed from this context + // l2 ok (task_local) + // e1 ok (lock_free) + #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + fn uart0(cx: uart0::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + *cx.resources.e1 += 10; + hprintln!("UART0: shared = {}", shared).unwrap(); + hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap(); + hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap(); + } + + // `shared` can be accessed from this context + // e1 ok (lock_free) + #[task(priority = 1, binds = UART1, resources = [shared, e1])] + fn uart1(cx: uart1::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + + hprintln!("UART1: shared = {}", shared).unwrap(); + hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap(); + } +} diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 610890bbfb..a9f268fbf8 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -19,7 +19,8 @@ proc-macro = true [dependencies] proc-macro2 = "1" +proc-macro-error = "1" quote = "1" syn = "1" -rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.4.0" } +rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.5.0-alpha.0" } diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index e89776c5a5..a256ac751c 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -126,6 +126,20 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { let user_code = app.user_code.clone(); let name = &app.name; let device = extra.device; + + // Get the list of all tasks + // Currently unused, might be useful + let task_list = analysis.tasks.clone(); + + let mut tasks = vec![]; + if !task_list.is_empty() { + tasks.push(quote!( + enum Tasks { + #(#task_list),* + } + )); + } + quote!( #(#user)* @@ -141,6 +155,9 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#root_software_tasks)* + /// Unused + #(#tasks)* + /// Implementation details mod #name { /// Always include the device crate which contains the vector table diff --git a/ui/single/local-cfg-task-local-err.rs b/ui/single/local-cfg-task-local-err.rs new file mode 100644 index 0000000000..412f614226 --- /dev/null +++ b/ui/single/local-cfg-task-local-err.rs @@ -0,0 +1,66 @@ +//! examples/local-cfg-task-local.rs + +#![deny(unsafe_code)] +//#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::debug; +use cortex_m_semihosting::hprintln; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // A local (move), early resource + #[cfg(feature = "feature_l1")] + #[task_local] + #[init(1)] + l1: u32, + + // A local (move), late resource + #[task_local] + l2: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + rtic::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART1); + init::LateResources { + #[cfg(feature = "feature_l2")] + l2: 2, + #[cfg(not(feature = "feature_l2"))] + l2: 5, + } + } + + // l1 ok (task_local) + #[idle(resources =[#[cfg(feature = "feature_l1")]l1])] + fn idle(_cx: idle::Context) -> ! { + #[cfg(feature = "feature_l1")] + hprintln!("IDLE:l1 = {}", _cx.resources.l1).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop {} + } + + // l2 ok (task_local) + #[task(priority = 1, binds = UART0, resources = [ + #[cfg(feature = "feature_l2")]l2, + ])] + fn uart0(_cx: uart0::Context) { + #[cfg(feature = "feature_l2")] + hprintln!("UART0:l2 = {}", _cx.resources.l2).unwrap(); + } + + // l2 error, conflicting with uart0 for l2 (task_local) + #[task(priority = 1, binds = UART1, resources = [ + #[cfg(not(feature = "feature_l2"))]l2 + ])] + fn uart1(_cx: uart1::Context) { + #[cfg(not(feature = "feature_l2"))] + hprintln!("UART0:l2 = {}", _cx.resources.l2).unwrap(); + } +} diff --git a/ui/single/local-cfg-task-local-err.stderr b/ui/single/local-cfg-task-local-err.stderr new file mode 100644 index 0000000000..9a84ead413 --- /dev/null +++ b/ui/single/local-cfg-task-local-err.stderr @@ -0,0 +1,37 @@ +error: task local resource "l2" is used by multiple tasks + --> $DIR/local-cfg-task-local-err.rs:25:9 + | +25 | l2: u32, + | ^^ + +error: task local resource "l2" is used by task "uart0" with priority 1 + --> $DIR/local-cfg-task-local-err.rs:51:39 + | +51 | #[cfg(feature = "feature_l2")]l2, + | ^^ + +error: task local resource "l2" is used by task "uart1" with priority 1 + --> $DIR/local-cfg-task-local-err.rs:60:44 + | +60 | #[cfg(not(feature = "feature_l2"))]l2 + | ^^ + +warning: unused import: `cortex_m_semihosting::debug` + --> $DIR/local-cfg-task-local-err.rs:8:5 + | +8 | use cortex_m_semihosting::debug; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[warn(unused_imports)]` on by default + +warning: unused import: `cortex_m_semihosting::hprintln` + --> $DIR/local-cfg-task-local-err.rs:9:5 + | +9 | use cortex_m_semihosting::hprintln; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +warning: unused import: `lm3s6965::Interrupt` + --> $DIR/local-cfg-task-local-err.rs:10:5 + | +10 | use lm3s6965::Interrupt; + | ^^^^^^^^^^^^^^^^^^^ diff --git a/ui/single/local-err.rs b/ui/single/local-err.rs new file mode 100644 index 0000000000..0fe98a4b0c --- /dev/null +++ b/ui/single/local-err.rs @@ -0,0 +1,83 @@ +//! examples/local_err.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +// errors here, since we cannot bail compilation or generate stubs +// run cargo expand, then you see the root of the problem... +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // An early resource + #[init(0)] + shared: u32, + + // A local (move), early resource + #[task_local] + #[init(1)] + l1: u32, + + // An exclusive, early resource + #[lock_free] + #[init(1)] + e1: u32, + + // A local (move), late resource + #[task_local] + l2: u32, + + // An exclusive, late resource + #[lock_free] + e2: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + rtic::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART1); + init::LateResources { e2: 2, l2: 2 } + } + + // `shared` cannot be accessed from this context + // l1 ok + // l2 rejeceted (not task_local) + // e2 ok + #[idle(resources =[l1, l2, e2])] + fn idle(cx: idle::Context) -> ! { + hprintln!("IDLE:l1 = {}", cx.resources.l1).unwrap(); + hprintln!("IDLE:e2 = {}", cx.resources.e2).unwrap(); + debug::exit(debug::EXIT_SUCCESS); + loop {} + } + + // `shared` can be accessed from this context + // l2 rejected (not task_local) + // e1 rejected (not lock_free) + #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + fn uart0(cx: uart0::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + *cx.resources.e1 += 10; + hprintln!("UART0: shared = {}", shared).unwrap(); + hprintln!("UART0:l2 = {}", cx.resources.l2).unwrap(); + hprintln!("UART0:e1 = {}", cx.resources.e1).unwrap(); + } + + // l2 rejected (not task_local) + #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])] + fn uart1(cx: uart1::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + + hprintln!("UART1: shared = {}", shared).unwrap(); + hprintln!("UART1:l2 = {}", cx.resources.l2).unwrap(); + hprintln!("UART1:e1 = {}", cx.resources.e1).unwrap(); + } +} diff --git a/ui/single/local-err.stderr b/ui/single/local-err.stderr new file mode 100644 index 0000000000..88369d8e3a --- /dev/null +++ b/ui/single/local-err.stderr @@ -0,0 +1,60 @@ +error: task local resource "l2" is used by multiple tasks + --> $DIR/local-err.rs:34:9 + | +34 | l2: u32, + | ^^ + +error: task local resource "l2" is used by task "idle" with priority 0 + --> $DIR/local-err.rs:52:28 + | +52 | #[idle(resources =[l1, l2, e2])] + | ^^ + +error: task local resource "l2" is used by task "uart0" with priority 1 + --> $DIR/local-err.rs:63:62 + | +63 | #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + | ^^ + +error: task local resource "l2" is used by task "uart1" with priority 2 + --> $DIR/local-err.rs:74:62 + | +74 | #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])] + | ^^ + +error: Lock free resource "e1" is used by tasks at different priorities + --> $DIR/local-err.rs:30:9 + | +30 | e1: u32, + | ^^ + +error: Resource "e1" is declared lock free but used by tasks at different priorities + --> $DIR/local-err.rs:63:66 + | +63 | #[task(priority = 1, binds = UART0, resources = [shared, l2, e1])] + | ^^ + +error: Resource "e1" is declared lock free but used by tasks at different priorities + --> $DIR/local-err.rs:74:66 + | +74 | #[task(priority = 2, binds = UART1, resources = [shared, l2, e1])] + | ^^ + +error: unused imports: `debug`, `hprintln` + --> $DIR/local-err.rs:10:28 + | +10 | use cortex_m_semihosting::{debug, hprintln}; + | ^^^^^ ^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/local-err.rs:4:9 + | +4 | #![deny(warnings)] + | ^^^^^^^^ + = note: `#[deny(unused_imports)]` implied by `#[deny(warnings)]` + +error: unused import: `lm3s6965::Interrupt` + --> $DIR/local-err.rs:11:5 + | +11 | use lm3s6965::Interrupt; + | ^^^^^^^^^^^^^^^^^^^