From c861b935910ea6b1b45d177fe6c3a1481c2b3fb6 Mon Sep 17 00:00:00 2001 From: Lasse Dalegaard Date: Mon, 18 Mar 2024 13:15:41 +0100 Subject: [PATCH] rtic-macros: fix #[cfg] for hardware and software tasks Disabling hardware and software tasks via `#[cfg]` flags was broken. Added test case to verify, and fixed codegen to output missing cfgs. --- rtic-macros/CHANGELOG.md | 6 +++- rtic-macros/src/codegen/async_dispatchers.rs | 12 ++++--- rtic-macros/src/codegen/hardware_tasks.rs | 2 ++ rtic-macros/src/codegen/main.rs | 11 ++++-- rtic/CHANGELOG.md | 15 ++++++--- rtic/ci/expected/t-cfg-tasks.run | 0 rtic/examples/t-cfg-tasks.rs | 35 ++++++++++++++++++++ 7 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 rtic/ci/expected/t-cfg-tasks.run create mode 100644 rtic/examples/t-cfg-tasks.rs diff --git a/rtic-macros/CHANGELOG.md b/rtic-macros/CHANGELOG.md index 66bd9bc2c213..0d0725e11f84 100644 --- a/rtic-macros/CHANGELOG.md +++ b/rtic-macros/CHANGELOG.md @@ -3,10 +3,14 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -For each category, *Added*, *Changed*, *Fixed* add new entries at the top! +For each category, _Added_, _Changed_, _Fixed_ add new entries at the top! ## [Unreleased] +### Fixed + +- Fixed `#[cfg]` tags on hardware and software tasks. + ## [v2.1.0] - 2024-02-27 ### Added diff --git a/rtic-macros/src/codegen/async_dispatchers.rs b/rtic-macros/src/codegen/async_dispatchers.rs index 9144b2a3f1f0..af1253471051 100644 --- a/rtic-macros/src/codegen/async_dispatchers.rs +++ b/rtic-macros/src/codegen/async_dispatchers.rs @@ -16,10 +16,12 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { let interrupts = &analysis.interrupts; // Generate executor definition and priority in global scope - for (name, _) in app.software_tasks.iter() { + for (name, task) in app.software_tasks.iter() { let exec_name = util::internal_task_ident(name, "EXEC"); + let cfgs = &task.cfgs; items.push(quote!( + #(#cfgs)* #[allow(non_upper_case_globals)] static #exec_name: rtic::export::executor::AsyncTaskExecutorPtr = rtic::export::executor::AsyncTaskExecutorPtr::new(); @@ -47,15 +49,15 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { for name in channel.tasks.iter() { let exec_name = util::internal_task_ident(name, "EXEC"); + let task = &app.software_tasks[name]; + let cfgs = &task.cfgs; let from_ptr_n_args = util::from_ptr_n_args_ident(app.software_tasks[name].inputs.len()); - // TODO: Fix cfg - // let task = &app.software_tasks[name]; - // let cfgs = &task.cfgs; - stmts.push(quote!( + #(#cfgs)* let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name); + #(#cfgs)* exec.poll(|| { let exec = rtic::export::executor::AsyncTaskExecutor::#from_ptr_n_args(#name, &#exec_name); exec.set_pending(); diff --git a/rtic-macros/src/codegen/hardware_tasks.rs b/rtic-macros/src/codegen/hardware_tasks.rs index ee85f59f3351..e1bc4720adf5 100644 --- a/rtic-macros/src/codegen/hardware_tasks.rs +++ b/rtic-macros/src/codegen/hardware_tasks.rs @@ -73,10 +73,12 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { if !task.is_extern { let attrs = &task.attrs; + let cfgs = &task.cfgs; let context = &task.context; let stmts = &task.stmts; user_tasks.push(quote!( #(#attrs)* + #(#cfgs)* #[allow(non_snake_case)] fn #name(#context: #name::Context) { use rtic::Mutex as _; diff --git a/rtic-macros/src/codegen/main.rs b/rtic-macros/src/codegen/main.rs index 56127962b4b8..97d0e0e4601c 100644 --- a/rtic-macros/src/codegen/main.rs +++ b/rtic-macros/src/codegen/main.rs @@ -27,14 +27,19 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 { let mut executor_allocations = Vec::new(); - for (name, _) in app.software_tasks.iter() { + for (name, task) in app.software_tasks.iter() { let exec_name = util::internal_task_ident(name, "EXEC"); let new_n_args = util::new_n_args_ident(app.software_tasks[name].inputs.len()); + let cfgs = &task.cfgs; executor_allocations.push(quote!( + #(#cfgs)* let executor = ::core::mem::ManuallyDrop::new(rtic::export::executor::AsyncTaskExecutor::#new_n_args(#name)); - executors_size += ::core::mem::size_of_val(&executor); - #exec_name.set_in_main(&executor); + #(#cfgs)* + { + executors_size += ::core::mem::size_of_val(&executor); + #exec_name.set_in_main(&executor); + } )); } diff --git a/rtic/CHANGELOG.md b/rtic/CHANGELOG.md index 0f6e85dd7262..099b508ab335 100644 --- a/rtic/CHANGELOG.md +++ b/rtic/CHANGELOG.md @@ -3,10 +3,14 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -For each category, *Added*, *Changed*, *Fixed* add new entries at the top! +For each category, _Added_, _Changed_, _Fixed_ add new entries at the top! ## [Unreleased] +### Fixed + +- Fixed `#[cfg]` tags on hardware and software tasks. + ## [v2.1.1] - 2024-03-13 ### Fixed @@ -237,6 +241,7 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top! ### Added - Allow annotating resources to activate special resource locking behaviour. + - `#[lock_free]`, there might be several tasks with the same priority accessing the resource without critical section. - `#[task_local]`, there must be only one task, similar to a task local @@ -254,7 +259,7 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top! [PR 399]: https://github.com/rtic-rs/cortex-m-rtic/pull/399 -- [breaking-change] [PR 390] Rework whole spawn/schedule, support `foo::spawn( ... )`, +- [breaking-change] [PR 390] Rework whole spawn/schedule, support `foo::spawn( ... )`, `foo::schedule( ... )`. [PR 390]: https://github.com/rtic-rs/cortex-m-rtic/pull/390 @@ -284,7 +289,7 @@ For each category, *Added*, *Changed*, *Fixed* add new entries at the top! - Added migration guide from `cortex-m-rtfm` to `cortex-m-rtic` - No code changes, only a version compatibility release with `cortex-m-rtfm` to ease the transition -for users. + for users. ## [v0.5.2] - 2020-06-11 @@ -398,7 +403,7 @@ for users. [RFC 128]: https://github.com/rtic-rs/cortex-m-rtic/issues/128 -``` rust +```rust // on v0.4.1 you had to write #[interrupt] fn USART0() { .. } @@ -443,7 +448,7 @@ fn on_new_frame() { .. } documentation page - The initialization function can now be written as `fn init() -> - init::LateResources` when late resources are used. This is preferred over the +init::LateResources` when late resources are used. This is preferred over the old `fn init()` form. See the section on late resources (resources chapter) in the book for more details. diff --git a/rtic/ci/expected/t-cfg-tasks.run b/rtic/ci/expected/t-cfg-tasks.run new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/rtic/examples/t-cfg-tasks.rs b/rtic/examples/t-cfg-tasks.rs new file mode 100644 index 000000000000..bea5f76e1f52 --- /dev/null +++ b/rtic/examples/t-cfg-tasks.rs @@ -0,0 +1,35 @@ +//! [compile-pass] check that `#[cfg]` attributes applied on tasks work + +#![no_main] +#![no_std] +#![deny(warnings)] +#![deny(unsafe_code)] +#![deny(missing_docs)] + +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] +mod app { + use cortex_m_semihosting::debug; + + #[shared] + struct Shared {} + + #[local] + struct Local {} + + #[init] + fn init(_: init::Context) -> (Shared, Local) { + debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator + + (Shared {}, Local {}) + } + + #[cfg(feature = "feature_x")] + #[task] + async fn opt_sw_task(cx: opt_sw_task::Context) {} + + #[cfg(feature = "feature_x")] + #[task(binds = UART0)] + fn opt_hw_task(cx: opt_hw_task::Context) {} +}