diff options
-rw-r--r-- | examples/spawn.rs | 33 | ||||
-rw-r--r-- | examples/spawn2.rs | 39 | ||||
-rw-r--r-- | macros/src/codegen/dispatchers.rs | 4 | ||||
-rw-r--r-- | macros/src/codegen/hardware_tasks.rs | 1 | ||||
-rw-r--r-- | macros/src/codegen/idle.rs | 3 | ||||
-rw-r--r-- | macros/src/codegen/init.rs | 8 | ||||
-rw-r--r-- | macros/src/codegen/module.rs | 69 | ||||
-rw-r--r-- | macros/src/codegen/software_tasks.rs | 7 |
8 files changed, 154 insertions, 10 deletions
diff --git a/examples/spawn.rs b/examples/spawn.rs new file mode 100644 index 00000000..62635632 --- /dev/null +++ b/examples/spawn.rs @@ -0,0 +1,33 @@ +//! examples/message.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 { + #[init(spawn = [foo])] + fn init(_c: init::Context) { + foo::spawn(1, 2).unwrap(); + } + + #[task()] + fn foo(_c: foo::Context, x: i32, y: u32) { + hprintln!("foo {}, {}", x, y).unwrap(); + if x == 2 { + debug::exit(debug::EXIT_SUCCESS); + } + foo::spawn(2, 3).unwrap(); + } + + // RTIC requires that unused interrupts are declared in an extern block when + // using software tasks; these free interrupts will be used to dispatch the + // software tasks. + extern "C" { + fn SSI0(); + } +} diff --git a/examples/spawn2.rs b/examples/spawn2.rs new file mode 100644 index 00000000..07ed53a4 --- /dev/null +++ b/examples/spawn2.rs @@ -0,0 +1,39 @@ +//! examples/message.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 { + #[init(spawn = [foo, foo2])] + fn init(_c: init::Context) { + foo::spawn(1, 2).unwrap(); + } + + #[task()] + fn foo(_c: foo::Context, x: i32, y: u32) { + hprintln!("foo {}, {}", x, y).unwrap(); + if x == 2 { + debug::exit(debug::EXIT_SUCCESS); + } + foo2::spawn(2).unwrap(); + } + + #[task()] + fn foo2(_c: foo2::Context, x: i32) { + hprintln!("foo2 {}", x).unwrap(); + foo::spawn(x, 0).unwrap(); + } + + // RTIC requires that unused interrupts are declared in an extern block when + // using software tasks; these free interrupts will be used to dispatch the + // software tasks. + extern "C" { + fn SSI0(); + } +} diff --git a/macros/src/codegen/dispatchers.rs b/macros/src/codegen/dispatchers.rs index 300aa996..eac0371d 100644 --- a/macros/src/codegen/dispatchers.rs +++ b/macros/src/codegen/dispatchers.rs @@ -35,7 +35,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream #[allow(non_camel_case_types)] #[derive(Clone, Copy)] #[doc = #doc] - enum #t { + pub enum #t { #(#variants,)* } )); @@ -57,7 +57,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream ); items.push(quote!( #[doc = #doc] - static mut #rq: #rq_ty = #rq_expr; + pub static mut #rq: #rq_ty = #rq_expr; )); if let Some(ceiling) = channel.ceiling { diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index 25f1df41..b6365ac3 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -97,6 +97,7 @@ pub fn codegen( Context::HardwareTask(name), needs_lt, app, + analysis, extra, )); diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index 2e2932d7..78fac96b 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -62,7 +62,8 @@ pub fn codegen( root_idle.push(locals); } - root_idle.push(module::codegen(Context::Idle, needs_lt, app, extra)); + root_idle.push(module::codegen(Context::Idle, needs_lt, app,analysis, extra)); + let attrs = &idle.attrs; let context = &idle.context; diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 8942439b..465a927d 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -116,7 +116,13 @@ pub fn codegen( quote!(let late = crate::#name(#(#locals_new,)* #name::Context::new(core.into()));), ); - root_init.push(module::codegen(Context::Init, needs_lt, app, extra)); + root_init.push(module::codegen( + Context::Init, + needs_lt, + app, + analysis, + extra, + )); (mod_app, root_init, user_init, user_init_imports, call_init) } else { diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs index 2e51e7db..526bf491 100644 --- a/macros/src/codegen/module.rs +++ b/macros/src/codegen/module.rs @@ -2,9 +2,15 @@ use proc_macro2::TokenStream as TokenStream2; use quote::quote; use rtic_syntax::{ast::App, Context}; -use crate::{check::Extra, codegen::util}; - -pub fn codegen(ctxt: Context, resources_tick: bool, app: &App, extra: &Extra) -> TokenStream2 { +use crate::{analyze::Analysis, check::Extra, codegen::util}; + +pub fn codegen( + ctxt: Context, + resources_tick: bool, + app: &App, + analysis: &Analysis, + extra: &Extra, +) -> TokenStream2 { let mut items = vec![]; let mut fields = vec![]; let mut values = vec![]; @@ -316,6 +322,63 @@ pub fn codegen(ctxt: Context, resources_tick: bool, app: &App, extra: &Extra) -> } )); + // not sure if this is the right way, maybe its backwards, + // that spawn_module should put in in root + + if let Context::SoftwareTask(..) = ctxt { + let spawnee = &app.software_tasks[name]; + let priority = spawnee.args.priority; + let t = util::spawn_t_ident(priority); + let cfgs = &spawnee.cfgs; + let (args, tupled, _untupled, ty) = util::regroup_inputs(&spawnee.inputs); + let args = &args; + let tupled = &tupled; + let fq = util::fq_ident(name); + let rq = util::rq_ident(priority); + let inputs = util::inputs_ident(name); + + eprintln!("app name: {}", app.name); + eprintln!("inputs {}", &inputs); + eprintln!("task name: {}", name); + eprintln!("fq {}", fq); + eprintln!("rq {}", rq); + let app_name = &app.name; + let app_path = quote! {crate::#app_name}; + + let device = extra.device; + let enum_ = util::interrupt_ident(); + let interrupt = &analysis.interrupts.get(&priority); + + items.push(quote!( + #(#cfgs)* + pub fn spawn(#(#args,)*) -> Result<(), #ty> { + // #let_instant // do we need it? + use rtic::Mutex as _; + + let input = #tupled; + + if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.dequeue()) { + unsafe { + #app_path::#inputs + .get_unchecked_mut(usize::from(index)) + .as_mut_ptr() + .write(input); + } + + rtic::export::interrupt::free(|_| { + #app_path::#rq.enqueue_unchecked((#app_path::#t::#name, index)); + }); + + rtic::pend(#device::#enum_::#interrupt); + + Ok(()) + } else { + Err(input) + } + + })); + } + if !items.is_empty() { quote!( #[allow(non_snake_case)] diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 4ae37e4e..b240d7aa 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -54,7 +54,7 @@ pub fn codegen( mod_app.push(quote!( /// Queue version of a free-list that keeps track of empty slots in /// the following buffers - static mut #fq: #fq_ty = #fq_expr; + pub static mut #fq: #fq_ty = #fq_expr; )); // Generate a resource proxy if needed @@ -88,7 +88,7 @@ pub fn codegen( mod_app.push(quote!( #uninit /// Buffer that holds the instants associated to the inputs of a task - static mut #instants: + pub static mut #instants: [core::mem::MaybeUninit<<#m as rtic::Monotonic>::Instant>; #cap_lit] = [#(#elems,)*]; )); @@ -99,7 +99,7 @@ pub fn codegen( mod_app.push(quote!( #uninit /// Buffer that holds the inputs of a task - static mut #inputs: [core::mem::MaybeUninit<#input_ty>; #cap_lit] = + pub static mut #inputs: [core::mem::MaybeUninit<#input_ty>; #cap_lit] = [#(#elems,)*]; )); } @@ -161,6 +161,7 @@ pub fn codegen( Context::SoftwareTask(name), needs_lt, app, + analysis, extra, )); } |