From 48e5acf294c2f035bda985c1f3441df23b5fad52 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Sun, 19 Mar 2023 22:15:36 +0100 Subject: [PATCH 1/4] Start async tasks at lowest priority --- rtic-macros/src/syntax/parse.rs | 7 ++++++- rtic/CHANGELOG.md | 2 ++ rtic/examples/async-task.rs | 4 ++-- rtic/examples/common.rs | 4 ++-- rtic/examples/destructure.rs | 4 ++-- rtic/examples/locals.rs | 4 ++-- rtic/examples/not-sync.rs | 4 ++-- rtic/examples/static.rs | 2 +- 8 files changed, 19 insertions(+), 12 deletions(-) diff --git a/rtic-macros/src/syntax/parse.rs b/rtic-macros/src/syntax/parse.rs index 72eeeaf69c..823bd82ea9 100644 --- a/rtic-macros/src/syntax/parse.rs +++ b/rtic-macros/src/syntax/parse.rs @@ -289,11 +289,13 @@ fn task_args(tokens: TokenStream2) -> parse::Result parse::Result Date: Wed, 22 Mar 2023 11:25:01 +0100 Subject: [PATCH 2/4] fix link to example sources --- book/en/src/by-example.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/en/src/by-example.md b/book/en/src/by-example.md index a2e5b27887..e5cf67a0ec 100644 --- a/book/en/src/by-example.md +++ b/book/en/src/by-example.md @@ -7,7 +7,7 @@ All examples in this part of the book are accessible at the The examples are runnable on QEMU (emulating a Cortex M3 target), thus no special hardware required to follow along. -[repoexamples]: https://github.com/rtic-rs/rtic/tree/master/examples +[repoexamples]: https://github.com/rtic-rs/rtic/tree/master/rtic/examples ## Running an example From ee6e6938f6cf216b55a7a2a6695980fc0ef9a629 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Wed, 29 Mar 2023 20:09:36 +0200 Subject: [PATCH 3/4] Update monotonic token macro names --- rtic-monotonics/src/rp2040.rs | 2 +- rtic-monotonics/src/systick.rs | 2 +- rtic/examples/async-delay.rs | 2 +- rtic/examples/async-timeout.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rtic-monotonics/src/rp2040.rs b/rtic-monotonics/src/rp2040.rs index 6aa66ce0bc..9d2f4f352c 100644 --- a/rtic-monotonics/src/rp2040.rs +++ b/rtic-monotonics/src/rp2040.rs @@ -138,7 +138,7 @@ impl embedded_hal_async::delay::DelayUs for Timer { /// Register the Timer interrupt for the monotonic. #[macro_export] -macro_rules! make_rp2040_monotonic_handler { +macro_rules! create_rp2040_monotonic_token { () => {{ #[no_mangle] #[allow(non_snake_case)] diff --git a/rtic-monotonics/src/systick.rs b/rtic-monotonics/src/systick.rs index b228e204d5..f4345d434d 100644 --- a/rtic-monotonics/src/systick.rs +++ b/rtic-monotonics/src/systick.rs @@ -156,7 +156,7 @@ impl embedded_hal_async::delay::DelayUs for Systick { /// Register the Systick interrupt for the monotonic. #[macro_export] -macro_rules! make_systick_handler { +macro_rules! create_systick_token { () => {{ #[no_mangle] #[allow(non_snake_case)] diff --git a/rtic/examples/async-delay.rs b/rtic/examples/async-delay.rs index 7b3667b094..cdffa620ba 100644 --- a/rtic/examples/async-delay.rs +++ b/rtic/examples/async-delay.rs @@ -24,7 +24,7 @@ mod app { fn init(cx: init::Context) -> (Shared, Local) { hprintln!("init"); - let systick_token = rtic_monotonics::make_systick_handler!(); + let systick_token = rtic_monotonics::create_systick_token!(); Systick::start(cx.core.SYST, 12_000_000, systick_token); foo::spawn().ok(); diff --git a/rtic/examples/async-timeout.rs b/rtic/examples/async-timeout.rs index e07e9c6f67..7690408e1e 100644 --- a/rtic/examples/async-timeout.rs +++ b/rtic/examples/async-timeout.rs @@ -27,7 +27,7 @@ mod app { fn init(cx: init::Context) -> (Shared, Local) { hprintln!("init"); - let systick_token = rtic_monotonics::make_systick_handler!(); + let systick_token = rtic_monotonics::create_systick_token!(); Systick::start(cx.core.SYST, 12_000_000, systick_token); foo::spawn().ok(); From 323b847bf692a0054647536a6495f20373f6d9c7 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Wed, 29 Mar 2023 20:43:27 +0200 Subject: [PATCH 4/4] Adding a limit that async HALs can read and have as max prio --- rtic-macros/src/analyze.rs | 9 +++++++++ rtic-macros/src/codegen.rs | 3 +++ rtic-macros/src/codegen/bindings/cortex.rs | 16 ++++++++++++++++ rtic-macros/src/codegen/bindings/template.rs | 4 ++++ 4 files changed, 32 insertions(+) diff --git a/rtic-macros/src/analyze.rs b/rtic-macros/src/analyze.rs index 65774f6c4d..2227308d0f 100644 --- a/rtic-macros/src/analyze.rs +++ b/rtic-macros/src/analyze.rs @@ -11,6 +11,7 @@ use syn::Ident; pub struct Analysis { parent: analyze::Analysis, pub interrupts: BTreeMap, + pub max_async_prio: Option, } impl ops::Deref for Analysis { @@ -42,8 +43,16 @@ pub fn app(analysis: analyze::Analysis, app: &App) -> Analysis { .map(|p| (p, available_interrupt.pop().expect("UNREACHABLE"))) .collect(); + let max_async_prio = app + .hardware_tasks + .iter() + .map(|(_, task)| task.args.priority) + .min() + .map(|v| v - 1); // One less than the smallest HW task + Analysis { parent: analysis, interrupts, + max_async_prio, } } diff --git a/rtic-macros/src/codegen.rs b/rtic-macros/src/codegen.rs index 48ee5e3389..c04f2131f1 100644 --- a/rtic-macros/src/codegen.rs +++ b/rtic-macros/src/codegen.rs @@ -45,6 +45,7 @@ pub fn app(app: &App, analysis: &Analysis) -> TokenStream2 { let device = &app.args.device; let rt_err = util::rt_err_ident(); + let async_limit = bindings::async_prio_limit(app, analysis); quote!( /// The RTIC application module @@ -52,6 +53,8 @@ pub fn app(app: &App, analysis: &Analysis) -> TokenStream2 { /// Always include the device crate which contains the vector table use #device as #rt_err; + #(#async_limit)* + #(#user_imports)* #(#user_code)* diff --git a/rtic-macros/src/codegen/bindings/cortex.rs b/rtic-macros/src/codegen/bindings/cortex.rs index 767befa96a..eba2afcaa3 100644 --- a/rtic-macros/src/codegen/bindings/cortex.rs +++ b/rtic-macros/src/codegen/bindings/cortex.rs @@ -322,3 +322,19 @@ pub fn interrupt_entry(_app: &App, _analysis: &CodegenAnalysis) -> Vec Vec { vec![] } + +pub fn async_prio_limit(app: &App, analysis: &CodegenAnalysis) -> Vec { + let max = if let Some(max) = analysis.max_async_prio { + quote!(#max) + } else { + // No limit + let device = &app.args.device; + quote!(1 << #device::NVIC_PRIO_BITS) + }; + + vec![quote!( + /// Holds the maximum priority level for use by async HAL drivers. + #[no_mangle] + static RTIC_ASYNC_MAX_LOGICAL_PRIO: u8 = #max; + )] +} diff --git a/rtic-macros/src/codegen/bindings/template.rs b/rtic-macros/src/codegen/bindings/template.rs index 18f88fe0ad..a33bae8ef9 100644 --- a/rtic-macros/src/codegen/bindings/template.rs +++ b/rtic-macros/src/codegen/bindings/template.rs @@ -42,3 +42,7 @@ pub fn interrupt_entry(_app: &App, _analysis: &CodegenAnalysis) -> Vec Vec { vec![] } + +pub fn async_prio_limit(app: &App, _analysis: &CodegenAnalysis) -> Vec { + vec![] +}