From 27985009579e82673dcaf7a6a715fcf50c184863 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Mon, 29 Jan 2024 20:56:15 +0100 Subject: Make RTIC 2 work on stable by using `main`'s stack as an allocator --- rtic-macros/src/codegen/bindings/cortex.rs | 24 ++++++++++++++++++++ rtic-macros/src/codegen/bindings/esp32c3.rs | 33 +++++++++++++++++++++++----- rtic-macros/src/codegen/bindings/template.rs | 7 ++++++ 3 files changed, 58 insertions(+), 6 deletions(-) (limited to 'rtic-macros/src/codegen/bindings') diff --git a/rtic-macros/src/codegen/bindings/cortex.rs b/rtic-macros/src/codegen/bindings/cortex.rs index ffa0245..69b5ee5 100644 --- a/rtic-macros/src/codegen/bindings/cortex.rs +++ b/rtic-macros/src/codegen/bindings/cortex.rs @@ -29,11 +29,35 @@ fn is_exception(name: &Ident) -> bool { | "SysTick" ) } + pub fn interrupt_ident() -> Ident { let span = Span::call_site(); Ident::new("interrupt", span) } +pub fn check_stack_overflow_before_init( + _app: &App, + _analysis: &CodegenAnalysis, +) -> Vec { + vec![quote!( + // Check for stack overflow using symbols from `cortex-m-rt`. + extern "C" { + pub static _stack_start: u32; + pub static __ebss: u32; + } + + let stack_start = &_stack_start as *const _ as u32; + let ebss = &__ebss as *const _ as u32; + + if stack_start > ebss { + // No flip-link usage, check the MSP for overflow. + if rtic::export::msp::read() <= ebss { + panic!("Stack overflow after allocating executors"); + } + } + )] +} + #[cfg(feature = "cortex-m-source-masking")] mod source_masking { use super::*; diff --git a/rtic-macros/src/codegen/bindings/esp32c3.rs b/rtic-macros/src/codegen/bindings/esp32c3.rs index a8d58b7..4b14cae 100644 --- a/rtic-macros/src/codegen/bindings/esp32c3.rs +++ b/rtic-macros/src/codegen/bindings/esp32c3.rs @@ -72,7 +72,7 @@ mod esp32c3 { } pub fn pre_init_enable_interrupts(app: &App, analysis: &CodegenAnalysis) -> Vec { let mut stmts = vec![]; - let mut curr_cpu_id:u8 = 1; //cpu interrupt id 0 is reserved + let mut curr_cpu_id: u8 = 1; //cpu interrupt id 0 is reserved let rt_err = util::rt_err_ident(); let max_prio: usize = 15; //unfortunately this is not part of pac, but we know that max prio is 15. let interrupt_ids = analysis.interrupts.iter().map(|(p, (id, _))| (p, id)); @@ -158,6 +158,29 @@ mod esp32c3 { vec![] } + pub fn check_stack_overflow_before_init( + _app: &App, + _analysis: &CodegenAnalysis, + ) -> Vec { + vec![quote!( + // Check for stack overflow using symbols from `risc-v-rt`. + extern "C" { + pub static _stack_start: u32; + pub static __ebss: u32; + } + + let stack_start = &_stack_start as *const _ as u32; + let ebss = &__ebss as *const _ as u32; + + if stack_start > ebss { + // No flip-link usage, check the SP for overflow. + if rtic::export::read_sp() <= ebss { + panic!("Stack overflow after allocating executors"); + } + } + )] + } + pub fn async_entry( _app: &App, _analysis: &CodegenAnalysis, @@ -199,11 +222,9 @@ mod esp32c3 { .values() .filter_map(|task| Some((&task.args.priority, &task.args.binds))), ) { - if *name == dispatcher_name{ - let ret = &("cpu_int_".to_owned()+&curr_cpu_id.to_string()+"_handler"); - stmts.push( - quote!(#[export_name = #ret]) - ); + if *name == dispatcher_name { + let ret = &("cpu_int_".to_owned() + &curr_cpu_id.to_string() + "_handler"); + stmts.push(quote!(#[export_name = #ret])); } curr_cpu_id += 1; } diff --git a/rtic-macros/src/codegen/bindings/template.rs b/rtic-macros/src/codegen/bindings/template.rs index d929dd8..b5488b7 100644 --- a/rtic-macros/src/codegen/bindings/template.rs +++ b/rtic-macros/src/codegen/bindings/template.rs @@ -43,6 +43,13 @@ pub fn interrupt_exit(_app: &App, _analysis: &CodegenAnalysis) -> Vec Vec { + vec![] +} + pub fn async_entry( _app: &App, _analysis: &CodegenAnalysis, -- cgit v1.2.3