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![] +}