Monotonic codegen now passing compile stage

This commit is contained in:
Emil Fresk 2020-12-12 23:24:54 +01:00
parent 97a48983d2
commit 8e8ec9b7b8
8 changed files with 189 additions and 219 deletions

View file

@ -70,22 +70,6 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
let inputs = util::inputs_ident(name); let inputs = util::inputs_ident(name);
let (_, tupled, pats, _) = util::regroup_inputs(&task.inputs); let (_, tupled, pats, _) = util::regroup_inputs(&task.inputs);
// TODO: Fix for new monotonics
// let (let_instant, instant) = if extra.monotonic.is_some() {
// let instants = util::instants_ident(name);
// (
// quote!(
// let instant =
// #instants.get_unchecked(usize::from(index)).as_ptr().read();
// ),
// quote!(, instant),
// )
// } else {
// (quote!(), quote!())
// };
let (let_instant, instant) = (quote!(), quote!());
let locals_new = if task.locals.is_empty() { let locals_new = if task.locals.is_empty() {
quote!() quote!()
} else { } else {
@ -99,12 +83,11 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
#t::#name => { #t::#name => {
let #tupled = let #tupled =
#inputs.get_unchecked(usize::from(index)).as_ptr().read(); #inputs.get_unchecked(usize::from(index)).as_ptr().read();
#let_instant
#fq.split().0.enqueue_unchecked(index); #fq.split().0.enqueue_unchecked(index);
let priority = &rtic::export::Priority::new(PRIORITY); let priority = &rtic::export::Priority::new(PRIORITY);
#app_path::#name( #app_path::#name(
#locals_new #locals_new
#name::Context::new(priority #instant) #name::Context::new(priority)
#(,#pats)* #(,#pats)*
) )
} }

View file

@ -18,6 +18,8 @@ pub fn codegen(
let mut task_cfgs = vec![]; let mut task_cfgs = vec![];
let name = ctxt.ident(app); let name = ctxt.ident(app);
let app_name = &app.name;
let app_path = quote! {crate::#app_name};
let mut lt = None; let mut lt = None;
match ctxt { match ctxt {
@ -125,7 +127,7 @@ pub fn codegen(
.iter() .iter()
.map(|(_, monotonic)| { .map(|(_, monotonic)| {
let mono = &monotonic.ident; let mono = &monotonic.ident;
quote! {#mono} quote! {#app_path::#mono}
}) })
.collect(); .collect();
@ -190,9 +192,6 @@ pub fn codegen(
let rq = util::rq_ident(priority); let rq = util::rq_ident(priority);
let inputs = util::inputs_ident(name); let inputs = util::inputs_ident(name);
let app_name = &app.name;
let app_path = quote! {crate::#app_name};
let device = &extra.device; let device = &extra.device;
let enum_ = util::interrupt_ident(); let enum_ = util::interrupt_ident();
let interrupt = &analysis let interrupt = &analysis
@ -234,11 +233,13 @@ pub fn codegen(
// Schedule caller // Schedule caller
for (_, monotonic) in &app.monotonics { for (_, monotonic) in &app.monotonics {
let instants = util::instants_ident(name); let instants = util::monotonic_instants_ident(name, &monotonic.ident);
let tq = util::tq_ident(&monotonic.ident.to_string()); let tq = util::tq_ident(&monotonic.ident.to_string());
let t = util::schedule_t_ident(); let t = util::schedule_t_ident();
let m = &monotonic.ident; let m = &monotonic.ident;
let m_isr = &monotonic.args.binds;
let enum_ = util::interrupt_ident();
if monotonic.args.default { if monotonic.args.default {
items.push(quote!(pub use #m::spawn_after;)); items.push(quote!(pub use #m::spawn_after;));
@ -259,7 +260,7 @@ pub fn codegen(
#(#cfgs)* #(#cfgs)*
pub fn spawn_at( pub fn spawn_at(
instant: Instant<#app_path::#m as rtic::Monotonic> instant: rtic::Instant<#app_path::#m>
#(,#args)* #(,#args)*
) -> Result<(), #ty> { ) -> Result<(), #ty> {
unsafe { unsafe {
@ -284,9 +285,11 @@ pub fn codegen(
task: #app_path::#t::#name, task: #app_path::#t::#name,
}; };
rtic::export::interrupt::free(|_| #app_path::#tq.enqueue_unchecked(nr)); rtic::export::interrupt::free(|_| #app_path::#tq.enqueue_unchecked(
nr,
// TODO: After adding the scheduled task, check and setup the timer. || rtic::export::NVIC::unmask(#app_path::you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#enum_::#m_isr),
|| rtic::pend(#app_path::you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#enum_::#m_isr),
));
Ok(()) Ok(())
} else { } else {

View file

@ -57,19 +57,22 @@ pub fn codegen(
.map(|_| quote!(core::mem::MaybeUninit::uninit())) .map(|_| quote!(core::mem::MaybeUninit::uninit()))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// TODO: Update for new monotonic let app_name = &app.name;
// if let Some(m) = &extra.monotonic { let app_path = quote! {crate::#app_name};
// let instants = util::instants_ident(name);
// let uninit = mk_uninit(); for (_, monotonic) in &app.monotonics {
// mod_app.push(quote!( let instants = util::monotonic_instants_ident(name, &monotonic.ident);
// #uninit let m = &monotonic.ident;
// /// Buffer that holds the instants associated to the inputs of a task
// static mut #instants: let uninit = mk_uninit();
// [core::mem::MaybeUninit<<#m as rtic::Monotonic>::Instant>; #cap_lit] = mod_app.push(quote!(
// [#(#elems,)*]; #uninit
// )); /// Buffer that holds the instants associated to the inputs of a task
// } static mut #instants:
[core::mem::MaybeUninit<rtic::Instant<#app_path::#m>>; #cap_lit] =
[#(#elems,)*];
));
}
let uninit = mk_uninit(); let uninit = mk_uninit();
let inputs_ident = util::inputs_ident(name); let inputs_ident = util::inputs_ident(name);

View file

@ -67,6 +67,8 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
// Timer queue handler // Timer queue handler
{ {
let enum_ = util::interrupt_ident();
let arms = app let arms = app
.software_tasks .software_tasks
.iter() .iter()
@ -75,7 +77,6 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
let priority = task.args.priority; let priority = task.args.priority;
let rq = util::rq_ident(priority); let rq = util::rq_ident(priority);
let rqt = util::spawn_t_ident(priority); let rqt = util::spawn_t_ident(priority);
let enum_ = util::interrupt_ident();
// The interrupt that runs the task dispatcher // The interrupt that runs the task dispatcher
let interrupt = &analysis.interrupts.get(&priority).expect("RTIC-ICE: interrupt not found").0; let interrupt = &analysis.interrupts.get(&priority).expect("RTIC-ICE: interrupt not found").0;
@ -98,12 +99,15 @@ pub fn codegen(app: &App, analysis: &Analysis, _extra: &Extra) -> Vec<TokenStrea
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let bound_interrupt = &monotonic.args.binds; let bound_interrupt = &monotonic.args.binds;
items.push(quote!( items.push(quote!(
#[no_mangle] #[no_mangle]
unsafe fn #bound_interrupt() { unsafe fn #bound_interrupt() {
use rtic::Mutex as _; use rtic::Mutex as _;
while let Some((task, index)) = rtic::export::interrupt::free(|_| #tq.dequeue()) while let Some((task, index)) = rtic::export::interrupt::free(|_| #tq.dequeue(
|| rtic::export::NVIC::unmask(you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml::#enum_::#bound_interrupt),
))
{ {
match task { match task {
#(#arms)* #(#arms)*

View file

@ -77,8 +77,8 @@ pub fn inputs_ident(task: &Ident) -> Ident {
} }
/// Generates an identifier for the `INSTANTS` buffer (`schedule` API) /// Generates an identifier for the `INSTANTS` buffer (`schedule` API)
pub fn instants_ident(task: &Ident) -> Ident { pub fn monotonic_instants_ident(task: &Ident, monotonic: &Ident) -> Ident {
Ident::new(&format!("{}_INSTANTS", task), Span::call_site()) Ident::new(&format!("{}_{}_INSTANTS", task, monotonic), Span::call_site())
} }
pub fn interrupt_ident() -> Ident { pub fn interrupt_ident() -> Ident {
@ -103,22 +103,6 @@ pub fn is_exception(name: &Ident) -> bool {
) )
} }
/// Generates a pre-reexport identifier for the "late resources" struct
pub fn late_resources_ident(init: &Ident) -> Ident {
Ident::new(
&format!("{}LateResources", init.to_string()),
Span::call_site(),
)
}
/// Generates a pre-reexport identifier for the "monotonics" struct
pub fn monotonics_ident(init: &Ident) -> Ident {
Ident::new(
&format!("{}Monotonics", init.to_string()),
Span::call_site(),
)
}
/// Mangle an ident /// Mangle an ident
pub fn mangle_ident(ident: &Ident) -> Ident { pub fn mangle_ident(ident: &Ident) -> Ident {
Ident::new( Ident::new(

View file

@ -3,7 +3,7 @@ use core::{
sync::atomic::{AtomicBool, Ordering}, sync::atomic::{AtomicBool, Ordering},
}; };
// pub use crate::tq::{NotReady, TimerQueue}; pub use crate::tq::{NotReady, TimerQueue};
pub use bare_metal::CriticalSection; pub use bare_metal::CriticalSection;
#[cfg(armv7m)] #[cfg(armv7m)]
pub use cortex_m::register::basepri; pub use cortex_m::register::basepri;

View file

@ -35,12 +35,10 @@
// #![deny(warnings)] // #![deny(warnings)]
#![no_std] #![no_std]
use core::ops::Sub;
use cortex_m::{interrupt::Nr, peripheral::NVIC}; use cortex_m::{interrupt::Nr, peripheral::NVIC};
pub use cortex_m_rtic_macros::app; pub use cortex_m_rtic_macros::app;
pub use rtic_core::{ pub use rtic_core::{
monotonic::{Clock, Instant, Monotonic}, monotonic::{self, Clock, Duration, Instant, Monotonic},
prelude as mutex_prelude, Exclusive, Mutex, prelude as mutex_prelude, Exclusive, Mutex,
}; };

307
src/tq.rs
View file

@ -1,156 +1,151 @@
// use core::{ use crate::{Instant, Monotonic};
// cmp::{self, Ordering}, use core::cmp::Ordering;
// convert::TryInto, use heapless::{binary_heap::Min, ArrayLength, BinaryHeap};
// mem,
// ops::Sub, pub struct TimerQueue<M, T, N>(pub BinaryHeap<NotReady<M, T>, N, Min>)
// }; where
// M: Monotonic,
// use cortex_m::peripheral::{SCB, SYST}; N: ArrayLength<NotReady<M, T>>,
// use heapless::{binary_heap::Min, ArrayLength, BinaryHeap}; T: Copy;
//
// use crate::Monotonic; impl<M, T, N> TimerQueue<M, T, N>
// where
// pub struct TimerQueue<M, T, N>(pub BinaryHeap<NotReady<M, T>, N, Min>) M: Monotonic,
// where N: ArrayLength<NotReady<M, T>>,
// M: Monotonic, T: Copy,
// <M::Instant as Sub>::Output: TryInto<u32>, {
// N: ArrayLength<NotReady<M, T>>, /// # Safety
// T: Copy; ///
// /// Writing to memory with a transmute in order to enable
// impl<M, T, N> TimerQueue<M, T, N> /// interrupts of the SysTick timer
// where ///
// M: Monotonic, /// Enqueue a task without checking if it is full
// <M::Instant as Sub>::Output: TryInto<u32>, #[inline]
// N: ArrayLength<NotReady<M, T>>, pub unsafe fn enqueue_unchecked<F1, F2>(
// T: Copy, &mut self,
// { nr: NotReady<M, T>,
// /// # Safety enable_interrupt: F1,
// /// pend_handler: F2,
// /// Writing to memory with a transmute in order to enable ) where
// /// interrupts of the SysTick timer F1: FnOnce(),
// /// F2: FnOnce(),
// /// Enqueue a task without checking if it is full {
// #[inline] let mut is_empty = true;
// pub unsafe fn enqueue_unchecked(&mut self, nr: NotReady<M, T>) { // Check if the top contains a non-empty element and if that element is
// let mut is_empty = true; // greater than nr
// // Check if the top contains a non-empty element and if that element is let if_heap_max_greater_than_nr = self
// // greater than nr .0
// let if_heap_max_greater_than_nr = self .peek()
// .0 .map(|head| {
// .peek() is_empty = false;
// .map(|head| { nr.instant < head.instant
// is_empty = false; })
// nr.instant < head.instant .unwrap_or(true);
// }) if if_heap_max_greater_than_nr {
// .unwrap_or(true); if is_empty {
// if if_heap_max_greater_than_nr { // mem::transmute::<_, SYST>(()).enable_interrupt();
// if is_empty { enable_interrupt();
// mem::transmute::<_, SYST>(()).enable_interrupt(); }
// }
// // Set SysTick pending
// // Set SysTick pending // SCB::set_pendst();
// SCB::set_pendst(); pend_handler();
// } }
//
// self.0.push_unchecked(nr); self.0.push_unchecked(nr);
// } }
//
// /// Dequeue a task from the TimerQueue /// Check if the timer queue is empty.
// #[inline] #[inline]
// pub fn dequeue(&mut self) -> Option<(T, u8)> { pub fn is_empty(&self) -> bool {
// unsafe { self.0.is_empty()
// if let Some(instant) = self.0.peek().map(|p| p.instant) { }
// let now = M::now();
// /// Dequeue a task from the TimerQueue
// if instant < now { #[inline]
// // task became ready pub fn dequeue<F>(&mut self, disable_interrupt: F) -> Option<(T, u8)>
// let nr = self.0.pop_unchecked(); where
// F: FnOnce(),
// Some((nr.task, nr.index)) {
// } else { unsafe {
// // set a new timeout M::clear_compare();
// const MAX: u32 = 0x00ffffff;
// if let Some(instant) = self.0.peek().map(|p| p.instant) {
// let ratio = M::ratio(); let now = M::now();
// let dur = match (instant - now).try_into().ok().and_then(|x| {
// x.checked_mul(ratio.numerator) match instant.checked_duration_since(&now) {
// .map(|x| x / ratio.denominator) None => {
// }) { // instant < now
// None => MAX, // task became ready
// let nr = self.0.pop_unchecked();
// // ARM Architecture Reference Manual says:
// // "Setting SYST_RVR to zero has the effect of Some((nr.task, nr.index))
// // disabling the SysTick counter independently }
// // of the counter enable bit." Some(dur) => {
// Some(0) => 1, // TODO: Fix this hack...
// let new_instant = *now.duration_since_epoch().integer() + *dur.integer();
// Some(x) => cmp::min(MAX, x), M::set_compare(new_instant);
// };
// mem::transmute::<_, SYST>(()).set_reload(dur); // Start counting down from the new reload
// // mem::transmute::<_, SYST>(()).clear_current();
// // Start counting down from the new reload
// mem::transmute::<_, SYST>(()).clear_current(); None
// }
// None }
// } } else {
// } else { // The queue is empty
// // The queue is empty // mem::transmute::<_, SYST>(()).disable_interrupt();
// mem::transmute::<_, SYST>(()).disable_interrupt(); disable_interrupt();
//
// None None
// } }
// } }
// } }
// } }
//
// pub struct NotReady<M, T> pub struct NotReady<M, T>
// where where
// T: Copy, T: Copy,
// M: Monotonic, M: Monotonic,
// <M::Instant as Sub>::Output: TryInto<u32>, {
// { pub index: u8,
// pub index: u8, pub instant: Instant<M>,
// pub instant: M::Instant, pub task: T,
// pub task: T, }
// }
// impl<M, T> Eq for NotReady<M, T>
// impl<M, T> Eq for NotReady<M, T> where
// where T: Copy,
// T: Copy, M: Monotonic,
// M: Monotonic, {
// <M::Instant as Sub>::Output: TryInto<u32>, }
// {
// } impl<M, T> Ord for NotReady<M, T>
// where
// impl<M, T> Ord for NotReady<M, T> T: Copy,
// where M: Monotonic,
// T: Copy, {
// M: Monotonic, fn cmp(&self, other: &Self) -> Ordering {
// <M::Instant as Sub>::Output: TryInto<u32>, self.instant.cmp(&other.instant)
// { }
// fn cmp(&self, other: &Self) -> Ordering { }
// self.instant.cmp(&other.instant)
// } impl<M, T> PartialEq for NotReady<M, T>
// } where
// T: Copy,
// impl<M, T> PartialEq for NotReady<M, T> M: Monotonic,
// where {
// T: Copy, fn eq(&self, other: &Self) -> bool {
// M: Monotonic, self.instant == other.instant
// <M::Instant as Sub>::Output: TryInto<u32>, }
// { }
// fn eq(&self, other: &Self) -> bool {
// self.instant == other.instant impl<M, T> PartialOrd for NotReady<M, T>
// } where
// } T: Copy,
// M: Monotonic,
// impl<M, T> PartialOrd for NotReady<M, T> {
// where fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
// T: Copy, Some(self.cmp(&other))
// M: Monotonic, }
// <M::Instant as Sub>::Output: TryInto<u32>, }
// {
// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
// Some(self.cmp(&other))
// }
// }