diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index d0bff3e76b..db454a58ca 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::quote; +use quote::{quote, format_ident}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -23,6 +23,8 @@ pub fn codegen( Vec, // user_idle Option, + // user_idle_imports + Vec, // call_idle TokenStream2, ) { @@ -34,15 +36,25 @@ pub fn codegen( let mut locals_pat = None; let mut locals_new = None; + let mut user_idle_imports = vec![]; + + let name = &idle.name; + if !idle.args.resources.is_empty() { let (item, constructor) = resources_struct::codegen(Context::Idle, 0, &mut needs_lt, app, analysis); root_idle.push(item); const_app = Some(constructor); + + let name_resource = format_ident!("{}Resources", name); + user_idle_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_resource; + )); + } - let name = &idle.name; if !idle.locals.is_empty() { let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app); @@ -66,6 +78,12 @@ pub fn codegen( #(#stmts)* } )); + user_idle_imports.push(quote!( + #(#attrs)* + #[allow(non_snake_case)] + #cfg_core + use super::#name; + )); let locals_new = locals_new.iter(); let call_idle = quote!(crate::#name( @@ -73,12 +91,13 @@ pub fn codegen( #name::Context::new(&rtic::export::Priority::new(0)) )); - (const_app, root_idle, user_idle, call_idle) + (const_app, root_idle, user_idle, user_idle_imports, call_idle) } else { ( None, vec![], None, + vec![], quote!(loop { rtic::export::wfi() }), diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index e0b7d69972..88a7a23b14 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::quote; +use quote::{quote, format_ident}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -24,6 +24,8 @@ pub fn codegen( Vec, // user_init -- the `#[init]` function written by the user Option, + // user_init_imports -- the imports for `#[init]` functio written by the user + Vec, // call_init -- the call to the user `#[init]` if there's one Option, ) { @@ -34,6 +36,8 @@ pub fn codegen( let mut root_init = vec![]; + let mut user_init_imports = vec![]; + let ret = { let late_fields = analysis .late_resources @@ -62,6 +66,12 @@ pub fn codegen( } )); + let name_late = format_ident!("{}LateResources", name); + user_init_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_late; + )); + Some(quote!(-> #name::LateResources)) } else { None @@ -89,6 +99,12 @@ pub fn codegen( #(#stmts)* } )); + user_init_imports.push(quote!( + #(#attrs)* + #cfg_core + #[allow(non_snake_case)] + use super::#name; + )); let mut const_app = None; if !init.args.resources.is_empty() { @@ -97,6 +113,12 @@ pub fn codegen( root_init.push(item); const_app = Some(constructor); + + let name_late = format_ident!("{}Resources", name); + user_init_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_late; + )); } let locals_new = locals_new.iter(); @@ -106,8 +128,8 @@ pub fn codegen( root_init.push(module::codegen(Context::Init, needs_lt, app, extra)); - (const_app, root_init, user_init, call_init) + (const_app, root_init, user_init, user_init_imports, call_init) } else { - (None, vec![], None, None) + (None, vec![], None, vec![], None) } } diff --git a/macros/src/codegen/resources.rs b/macros/src/codegen/resources.rs index 80e63c79f1..a326e68c54 100644 --- a/macros/src/codegen/resources.rs +++ b/macros/src/codegen/resources.rs @@ -89,7 +89,7 @@ pub fn codegen( #[allow(non_camel_case_types)] #(#cfgs)* #cfg_core - use super::#name; + use super::resources::#name; )); const_app.push(util::impl_mutex( @@ -107,6 +107,11 @@ pub fn codegen( let mod_resources = if mod_resources.is_empty() { quote!() } else { + // Also import the resource module + mod_resources_imports.push(quote!( + use super::resources; + )); + quote!(mod resources { use rtic::export::Priority; diff --git a/macros/src/codegen/resources_struct.rs b/macros/src/codegen/resources_struct.rs index dbbba30e0f..92d5b66633 100644 --- a/macros/src/codegen/resources_struct.rs +++ b/macros/src/codegen/resources_struct.rs @@ -165,7 +165,7 @@ pub fn codegen( let constructor = quote!( impl<#lt> #ident<#lt> { #[inline(always)] - unsafe fn new(#arg) -> Self { + pub unsafe fn new(#arg) -> Self { #ident { #(#values,)* } diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 07edd1db86..f3a0db16ff 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -145,14 +145,13 @@ pub fn codegen( #(#attrs)* #(#cfgs)* #[allow(non_snake_case)] - fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) { + pub fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) { use rtic::Mutex as _; #(#stmts)* } )); software_tasks_imports.push(quote!( - #(#attrs)* #(#cfgs)* #[allow(non_snake_case)] use super::#name; diff --git a/macros/src/codegen/spawn.rs b/macros/src/codegen/spawn.rs index 4b824f56ab..da2815160b 100644 --- a/macros/src/codegen/spawn.rs +++ b/macros/src/codegen/spawn.rs @@ -40,7 +40,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec Result<(), #ty> { + pub fn #name(&self #(,#args)*) -> Result<(), #ty> { #let_instant #body } @@ -92,7 +92,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec Result<(), #ty> { + pub fn #name(&self #(,#args)*) -> Result<(), #ty> { unsafe { #let_instant #spawn(self.priority() #instant #(,#untupled)*)