aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/module.rs
diff options
context:
space:
mode:
Diffstat (limited to 'macros/src/codegen/module.rs')
-rw-r--r--macros/src/codegen/module.rs220
1 files changed, 120 insertions, 100 deletions
diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs
index 2ff4801e..93fbeaef 100644
--- a/macros/src/codegen/module.rs
+++ b/macros/src/codegen/module.rs
@@ -18,29 +18,16 @@ pub fn codegen(
let mut task_cfgs = vec![];
let name = ctxt.ident(app);
+ let app_name = &app.name;
+ let app_path = quote! {crate::#app_name};
- let mut needs_instant = false;
let mut lt = None;
match ctxt {
Context::Init => {
- if let Some(m) = &extra.monotonic {
- fields.push(quote!(
- /// System start time = `Instant(0 /* cycles */)`
- pub start: <#m as rtic::Monotonic>::Instant
- ));
-
- values.push(quote!(start: <#m as rtic::Monotonic>::zero()));
-
- fields.push(quote!(
- /// Core (Cortex-M) peripherals minus the SysTick
- pub core: rtic::Peripherals
- ));
- } else {
- fields.push(quote!(
- /// Core (Cortex-M) peripherals
- pub core: rtic::export::Peripherals
- ));
- }
+ fields.push(quote!(
+ /// Core (Cortex-M) peripherals
+ pub core: rtic::export::Peripherals
+ ));
if extra.peripherals {
let device = &extra.device;
@@ -67,29 +54,11 @@ pub fn codegen(
Context::Idle => {}
Context::HardwareTask(..) => {
- if let Some(m) = &extra.monotonic {
- fields.push(quote!(
- /// Time at which this handler started executing
- pub start: <#m as rtic::Monotonic>::Instant
- ));
-
- values.push(quote!(start: instant));
-
- needs_instant = true;
- }
+ // None for now.
}
Context::SoftwareTask(..) => {
- if let Some(m) = &extra.monotonic {
- fields.push(quote!(
- /// The time at which this task was scheduled to run
- pub scheduled: <#m as rtic::Monotonic>::Instant
- ));
-
- values.push(quote!(scheduled: instant));
-
- needs_instant = true;
- }
+ // None for now.
}
}
@@ -129,12 +98,45 @@ pub fn codegen(
}
if let Context::Init = ctxt {
- let init = &app.inits.first().unwrap();
- let late_resources = util::late_resources_ident(&init.name);
+ let late_fields = analysis
+ .late_resources
+ .iter()
+ .flat_map(|resources| {
+ resources.iter().map(|name| {
+ let ty = &app.late_resources[name].ty;
+ let cfgs = &app.late_resources[name].cfgs;
+
+ quote!(
+ #(#cfgs)*
+ pub #name: #ty
+ )
+ })
+ })
+ .collect::<Vec<_>>();
items.push(quote!(
- #[doc(inline)]
- pub use super::#late_resources as LateResources;
+ /// Resources initialized at runtime
+ #[allow(non_snake_case)]
+ pub struct LateResources {
+ #(#late_fields),*
+ }
+ ));
+
+ let monotonic_types: Vec<_> = app
+ .monotonics
+ .iter()
+ .map(|(_, monotonic)| {
+ let mono = &monotonic.ident;
+ quote! {#app_path::#mono}
+ })
+ .collect();
+
+ items.push(quote!(
+ /// Monotonics used by the system
+ #[allow(non_snake_case)]
+ pub struct Monotonics(
+ #(pub #monotonic_types),*
+ );
));
}
@@ -146,11 +148,7 @@ pub fn codegen(
};
let core = if ctxt.is_init() {
- if extra.monotonic.is_some() {
- Some(quote!(core: rtic::Peripherals,))
- } else {
- Some(quote!(core: rtic::export::Peripherals,))
- }
+ Some(quote!(core: rtic::export::Peripherals,))
} else {
None
};
@@ -161,14 +159,6 @@ pub fn codegen(
Some(quote!(priority: &#lt rtic::export::Priority))
};
- let instant = if needs_instant {
- let m = extra.monotonic.clone().expect("RTIC-ICE: UNREACHABLE");
-
- Some(quote!(, instant: <#m as rtic::Monotonic>::Instant))
- } else {
- None
- };
-
items.push(quote!(
/// Execution context
pub struct Context<#lt> {
@@ -177,7 +167,7 @@ pub fn codegen(
impl<#lt> Context<#lt> {
#[inline(always)]
- pub unsafe fn new(#core #priority #instant) -> Self {
+ pub unsafe fn new(#core #priority) -> Self {
Context {
#(#values,)*
}
@@ -195,16 +185,13 @@ pub fn codegen(
let cfgs = &spawnee.cfgs;
// Store a copy of the task cfgs
task_cfgs = cfgs.clone();
- let (args, tupled, _untupled, ty) = util::regroup_inputs(&spawnee.inputs);
+ 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);
- let app_name = &app.name;
- let app_path = quote! {crate::#app_name};
-
let device = &extra.device;
let enum_ = util::interrupt_ident();
let interrupt = &analysis
@@ -217,10 +204,6 @@ pub fn codegen(
items.push(quote!(
#(#cfgs)*
pub fn spawn(#(#args,)*) -> Result<(), #ty> {
- // #let_instant // do we need it?
- use rtic::Mutex as _;
- use rtic::mutex_prelude::*;
-
let input = #tupled;
unsafe {
@@ -245,45 +228,82 @@ pub fn codegen(
}));
// Schedule caller
- if let Some(m) = &extra.monotonic {
- let instants = util::instants_ident(name);
+ for (_, monotonic) in &app.monotonics {
+ let instants = util::monotonic_instants_ident(name, &monotonic.ident);
- let tq = util::tq_ident();
+ let tq = util::tq_ident(&monotonic.ident.to_string());
let t = util::schedule_t_ident();
+ let m = &monotonic.ident;
+ let m_isr = &monotonic.args.binds;
+ let enum_ = util::interrupt_ident();
+
+ if monotonic.args.default {
+ items.push(quote!(pub use #m::spawn_after;));
+ items.push(quote!(pub use #m::spawn_at;));
+ }
+
+ let (unmask, pend) = if &*m_isr.to_string() == "SysTick" {
+ (
+ quote!(core::mem::transmute::<_, cortex_m::peripheral::SYST>(()).disable_interrupt()),
+ quote!(cortex_m::peripheral::SCB::set_pendst()),
+ )
+ } else {
+ let rt_err = util::rt_err_ident();
+ (
+ quote!(rtic::export::NVIC::unmask(#app_path::#rt_err::#enum_::#m_isr)),
+ quote!(rtic::pend(#app_path::#rt_err::#enum_::#m_isr)),
+ )
+ };
items.push(quote!(
- #(#cfgs)*
- pub fn schedule(
- instant: <#m as rtic::Monotonic>::Instant
- #(,#args)*
- ) -> Result<(), #ty> {
- unsafe {
- use rtic::Mutex as _;
- use rtic::mutex_prelude::*;
-
- let input = #tupled;
- if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.dequeue()) {
- #app_path::#inputs
- .get_unchecked_mut(usize::from(index))
- .as_mut_ptr()
- .write(input);
-
- #app_path::#instants
- .get_unchecked_mut(usize::from(index))
- .as_mut_ptr()
- .write(instant);
-
- let nr = rtic::export::NotReady {
- instant,
- index,
- task: #app_path::#t::#name,
- };
-
- rtic::export::interrupt::free(|_| #app_path::#tq.enqueue_unchecked(nr));
-
- Ok(())
- } else {
- Err(input)
+ pub mod #m {
+ #(#cfgs)*
+ pub fn spawn_after<D>(
+ duration: D,
+ #(,#args)*
+ ) -> Result<(), #ty>
+ where D: rtic::time::duration::Duration + rtic::time::fixed_point::FixedPoint,
+ D::T: Into<<#app_path::#m as rtic::time::Clock>::T>,
+ {
+ let instant = <#app_path::#m as rtic::Monotonic>::now();
+
+ spawn_at(instant + duration, #(,#untupled)*)
+ }
+
+ #(#cfgs)*
+ pub fn spawn_at(
+ instant: rtic::time::Instant<#app_path::#m>
+ #(,#args)*
+ ) -> Result<(), #ty> {
+ unsafe {
+ let input = #tupled;
+ if let Some(index) = rtic::export::interrupt::free(|_| #app_path::#fq.dequeue()) {
+ #app_path::#inputs
+ .get_unchecked_mut(usize::from(index))
+ .as_mut_ptr()
+ .write(input);
+
+ #app_path::#instants
+ .get_unchecked_mut(usize::from(index))
+ .as_mut_ptr()
+ .write(instant);
+
+ let nr = rtic::export::NotReady {
+ instant,
+ index,
+ task: #app_path::#t::#name,
+ };
+
+ rtic::export::interrupt::free(|_| #app_path::#tq.enqueue_unchecked(
+ nr,
+ || #unmask,
+ || #pend,
+ ));
+
+ Ok(())
+ } else {
+ Err(input)
+ }
}
}
}));