diff --git a/Cargo.toml b/Cargo.toml index 9a10ec276f..679dcc0425 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,7 +57,8 @@ required-features = ["__v7"] [dependencies] cortex-m = "0.6.2" cortex-m-rtic-macros = { path = "macros", version = "0.6.0-alpha.0" } -rtic-core = "0.3.1" +rtic-core = { git = "https://github.com/rtic-rs/rtic-core", branch = "new_monotonic" } +#rtic-core = "0.3.1" heapless = "0.5.0" bare-metal = "1.0.0" diff --git a/examples/test_new_monotonic.rs b/examples/test_new_monotonic.rs new file mode 100644 index 0000000000..5aac48ba56 --- /dev/null +++ b/examples/test_new_monotonic.rs @@ -0,0 +1,21 @@ +//! examples/test_new_monotonic.rs + +#![no_main] +#![no_std] + +use panic_semihosting as _; // panic handler +use rtic::app; + +#[app(device = lm3s6965)] +mod app { + #[monotonic(binds = SomeISR1)] + type Mono1 = hal::Mono1; + + #[monotonic(binds = SomeISR2)] + type Mono2 = hal::Mono2; + + #[init] + fn init(cx: init::Context) -> (init::LateResources, init::Monotonics) { + } +} + diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 3af48c7684..6996bef4a1 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -22,5 +22,5 @@ proc-macro2 = "1" proc-macro-error = "1" quote = "1" syn = "1" -rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.5.0-alpha.0" } +rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "new_monotonic", version = "0.5.0-alpha.0" } diff --git a/macros/src/check.rs b/macros/src/check.rs index e3161cb950..42bd90db47 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -62,18 +62,6 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result { for (name, task) in &app.hardware_tasks { let name_s = task.args.binds.to_string(); match &*name_s { - "SysTick" => { - // If the timer queue is used, then SysTick is unavailable - if app.args.monotonic.is_some() { - return Err(parse::Error::new( - name.span(), - "this exception can't be used because it's being used by the runtime", - )); - } else { - // OK - } - } - "NonMaskableInt" | "HardFault" => { return Err(parse::Error::new( name.span(), @@ -88,7 +76,7 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result { if let Some(device) = app.args.device.clone() { Ok(Extra { device, - monotonic: app.args.monotonic.clone(), + monotonic: None, peripherals: app.args.peripherals, }) } else { diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 3cddf57018..52940bc3e0 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -61,8 +61,6 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { use super::*; #[no_mangle] unsafe extern "C" fn #main() -> ! { - let _TODO: () = (); - #(#assertion_stmts)* #(#pre_init_stmts)* diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 6376ce31c4..6b57add1b2 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -58,6 +58,24 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { } )); + let monotonic_types: Vec<_> = app + .monotonics + .iter() + .map(|(_, monotonic)| { + let mono = &monotonic.ty; + quote! {#mono} + }) + .collect(); + let monotonics = util::monotonics_ident(&name); + + root_init.push(quote!( + /// Monotonics used by the system + #[allow(non_snake_case)] + pub struct #monotonics( + #(#monotonic_types),* + ); + )); + let mut locals_pat = None; let mut locals_new = None; if !init.locals.is_empty() { @@ -72,10 +90,16 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { let attrs = &init.attrs; let stmts = &init.stmts; let locals_pat = locals_pat.iter(); + + let mut user_init_return = vec![quote! {#name::LateResources}]; + if !app.monotonics.is_empty() { + user_init_return.push(quote! {#name::Monotonics}); + } + let user_init = Some(quote!( #(#attrs)* #[allow(non_snake_case)] - fn #name(#(#locals_pat,)* #context: #name::Context) -> #name::LateResources { + fn #name(#(#locals_pat,)* #context: #name::Context) -> (#(#user_init_return,)*) { #(#stmts)* } )); @@ -92,7 +116,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> CodegenResult { let app_path = quote! {crate::#app_name}; let locals_new = locals_new.iter(); let call_init = Some( - quote!(let late = #app_path::#name(#(#locals_new,)* #name::Context::new(core.into()));), + quote!(let (late, monotonics) = #app_path::#name(#(#locals_new,)* #name::Context::new(core.into()));), ); root_init.push(module::codegen( diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs index 2ff4801e43..d398a1a860 100644 --- a/macros/src/codegen/module.rs +++ b/macros/src/codegen/module.rs @@ -131,11 +131,17 @@ pub fn codegen( if let Context::Init = ctxt { let init = &app.inits.first().unwrap(); let late_resources = util::late_resources_ident(&init.name); + let monotonics = util::monotonics_ident(&init.name); items.push(quote!( #[doc(inline)] pub use super::#late_resources as LateResources; )); + + items.push(quote!( + #[doc(inline)] + pub use super::#monotonics as Monotonics; + )); } let doc = match ctxt { diff --git a/macros/src/codegen/post_init.rs b/macros/src/codegen/post_init.rs index 5545944d18..9174daebcb 100644 --- a/macros/src/codegen/post_init.rs +++ b/macros/src/codegen/post_init.rs @@ -25,6 +25,9 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec { } } + // Forget the monotonics so they won't be dropped. + stmts.push(quote!(core::mem::forget(monotonics);)); + // Enable the interrupts -- this completes the `init`-ialization phase stmts.push(quote!(rtic::export::interrupt::enable();)); diff --git a/macros/src/codegen/util.rs b/macros/src/codegen/util.rs index fb8f1a8450..4273ee2c42 100644 --- a/macros/src/codegen/util.rs +++ b/macros/src/codegen/util.rs @@ -111,6 +111,14 @@ pub fn late_resources_ident(init: &Ident) -> Ident { ) } +/// 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 pub fn mangle_ident(ident: &Ident) -> Ident { Ident::new( diff --git a/macros/src/lib.rs b/macros/src/lib.rs index dc37eaea3c..c9136e5552 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -1,4 +1,4 @@ -#![deny(warnings)] +// #![deny(warnings)] extern crate proc_macro; diff --git a/src/export.rs b/src/export.rs index 72d954ab14..46793aa64e 100644 --- a/src/export.rs +++ b/src/export.rs @@ -3,7 +3,7 @@ use core::{ sync::atomic::{AtomicBool, Ordering}, }; -pub use crate::tq::{NotReady, TimerQueue}; +//pub use crate::tq::{NotReady, TimerQueue}; pub use bare_metal::CriticalSection; #[cfg(armv7m)] pub use cortex_m::register::basepri; diff --git a/src/lib.rs b/src/lib.rs index 385b5ea3da..c1930b034e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,82 +32,20 @@ #![deny(missing_docs)] #![deny(rust_2018_compatibility)] #![deny(rust_2018_idioms)] -#![deny(warnings)] +// #![deny(warnings)] #![no_std] use core::ops::Sub; -use cortex_m::{ - interrupt::Nr, - peripheral::{CBP, CPUID, DCB, DWT, FPB, FPU, ITM, MPU, NVIC, SCB, TPIU}, -}; +use cortex_m::{interrupt::Nr, peripheral::NVIC}; pub use cortex_m_rtic_macros::app; -pub use rtic_core::{prelude as mutex_prelude, Exclusive, Mutex}; +pub use rtic_core::{prelude as mutex_prelude, Exclusive, monotonic::Monotonic, Mutex}; -#[cfg(armv7m)] -pub mod cyccnt; #[doc(hidden)] pub mod export; #[doc(hidden)] mod tq; -/// `cortex_m::Peripherals` minus `SYST` -#[allow(non_snake_case)] -pub struct Peripherals { - /// Cache and branch predictor maintenance operations (not present on Cortex-M0 variants) - pub CBP: CBP, - - /// CPUID - pub CPUID: CPUID, - - /// Debug Control Block - pub DCB: DCB, - - /// Data Watchpoint and Trace unit - pub DWT: DWT, - - /// Flash Patch and Breakpoint unit (not present on Cortex-M0 variants) - pub FPB: FPB, - - /// Floating Point Unit (only present on `thumbv7em-none-eabihf`) - pub FPU: FPU, - - /// Instrumentation Trace Macrocell (not present on Cortex-M0 variants) - pub ITM: ITM, - - /// Memory Protection Unit - pub MPU: MPU, - - /// Nested Vector Interrupt Controller - pub NVIC: NVIC, - - /// System Control Block - pub SCB: SCB, - - // SysTick: System Timer - // pub SYST: SYST, - /// Trace Port Interface Unit (not present on Cortex-M0 variants) - pub TPIU: TPIU, -} - -impl From for Peripherals { - fn from(p: cortex_m::Peripherals) -> Self { - Self { - CBP: p.CBP, - CPUID: p.CPUID, - DCB: p.DCB, - DWT: p.DWT, - FPB: p.FPB, - FPU: p.FPU, - ITM: p.ITM, - MPU: p.MPU, - NVIC: p.NVIC, - SCB: p.SCB, - TPIU: p.TPIU, - } - } -} - /// Sets the given `interrupt` as pending /// /// This is a convenience function around diff --git a/src/tq.rs b/src/tq.rs index b2a84c8593..f2539a991a 100644 --- a/src/tq.rs +++ b/src/tq.rs @@ -1,156 +1,156 @@ -use core::{ - cmp::{self, Ordering}, - convert::TryInto, - mem, - ops::Sub, -}; - -use cortex_m::peripheral::{SCB, SYST}; -use heapless::{binary_heap::Min, ArrayLength, BinaryHeap}; - -use crate::Monotonic; - -pub struct TimerQueue(pub BinaryHeap, N, Min>) -where - M: Monotonic, - ::Output: TryInto, - N: ArrayLength>, - T: Copy; - -impl TimerQueue -where - M: Monotonic, - ::Output: TryInto, - N: ArrayLength>, - T: Copy, -{ - /// # Safety - /// - /// Writing to memory with a transmute in order to enable - /// interrupts of the SysTick timer - /// - /// Enqueue a task without checking if it is full - #[inline] - pub unsafe fn enqueue_unchecked(&mut self, nr: NotReady) { - let mut is_empty = true; - // Check if the top contains a non-empty element and if that element is - // greater than nr - let if_heap_max_greater_than_nr = self - .0 - .peek() - .map(|head| { - is_empty = false; - nr.instant < head.instant - }) - .unwrap_or(true); - if if_heap_max_greater_than_nr { - if is_empty { - mem::transmute::<_, SYST>(()).enable_interrupt(); - } - - // Set SysTick pending - SCB::set_pendst(); - } - - self.0.push_unchecked(nr); - } - - /// Dequeue a task from the TimerQueue - #[inline] - pub fn dequeue(&mut self) -> Option<(T, u8)> { - unsafe { - if let Some(instant) = self.0.peek().map(|p| p.instant) { - let now = M::now(); - - if instant < now { - // task became ready - let nr = self.0.pop_unchecked(); - - Some((nr.task, nr.index)) - } else { - // set a new timeout - const MAX: u32 = 0x00ffffff; - - let ratio = M::ratio(); - let dur = match (instant - now).try_into().ok().and_then(|x| { - x.checked_mul(ratio.numerator) - .map(|x| x / ratio.denominator) - }) { - None => MAX, - - // ARM Architecture Reference Manual says: - // "Setting SYST_RVR to zero has the effect of - // disabling the SysTick counter independently - // of the counter enable bit." - Some(0) => 1, - - Some(x) => cmp::min(MAX, x), - }; - mem::transmute::<_, SYST>(()).set_reload(dur); - - // Start counting down from the new reload - mem::transmute::<_, SYST>(()).clear_current(); - - None - } - } else { - // The queue is empty - mem::transmute::<_, SYST>(()).disable_interrupt(); - - None - } - } - } -} - -pub struct NotReady -where - T: Copy, - M: Monotonic, - ::Output: TryInto, -{ - pub index: u8, - pub instant: M::Instant, - pub task: T, -} - -impl Eq for NotReady -where - T: Copy, - M: Monotonic, - ::Output: TryInto, -{ -} - -impl Ord for NotReady -where - T: Copy, - M: Monotonic, - ::Output: TryInto, -{ - fn cmp(&self, other: &Self) -> Ordering { - self.instant.cmp(&other.instant) - } -} - -impl PartialEq for NotReady -where - T: Copy, - M: Monotonic, - ::Output: TryInto, -{ - fn eq(&self, other: &Self) -> bool { - self.instant == other.instant - } -} - -impl PartialOrd for NotReady -where - T: Copy, - M: Monotonic, - ::Output: TryInto, -{ - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(&other)) - } -} +// use core::{ +// cmp::{self, Ordering}, +// convert::TryInto, +// mem, +// ops::Sub, +// }; +// +// use cortex_m::peripheral::{SCB, SYST}; +// use heapless::{binary_heap::Min, ArrayLength, BinaryHeap}; +// +// use crate::Monotonic; +// +// pub struct TimerQueue(pub BinaryHeap, N, Min>) +// where +// M: Monotonic, +// ::Output: TryInto, +// N: ArrayLength>, +// T: Copy; +// +// impl TimerQueue +// where +// M: Monotonic, +// ::Output: TryInto, +// N: ArrayLength>, +// T: Copy, +// { +// /// # Safety +// /// +// /// Writing to memory with a transmute in order to enable +// /// interrupts of the SysTick timer +// /// +// /// Enqueue a task without checking if it is full +// #[inline] +// pub unsafe fn enqueue_unchecked(&mut self, nr: NotReady) { +// let mut is_empty = true; +// // Check if the top contains a non-empty element and if that element is +// // greater than nr +// let if_heap_max_greater_than_nr = self +// .0 +// .peek() +// .map(|head| { +// is_empty = false; +// nr.instant < head.instant +// }) +// .unwrap_or(true); +// if if_heap_max_greater_than_nr { +// if is_empty { +// mem::transmute::<_, SYST>(()).enable_interrupt(); +// } +// +// // Set SysTick pending +// SCB::set_pendst(); +// } +// +// self.0.push_unchecked(nr); +// } +// +// /// Dequeue a task from the TimerQueue +// #[inline] +// pub fn dequeue(&mut self) -> Option<(T, u8)> { +// unsafe { +// if let Some(instant) = self.0.peek().map(|p| p.instant) { +// let now = M::now(); +// +// if instant < now { +// // task became ready +// let nr = self.0.pop_unchecked(); +// +// Some((nr.task, nr.index)) +// } else { +// // set a new timeout +// const MAX: u32 = 0x00ffffff; +// +// let ratio = M::ratio(); +// let dur = match (instant - now).try_into().ok().and_then(|x| { +// x.checked_mul(ratio.numerator) +// .map(|x| x / ratio.denominator) +// }) { +// None => MAX, +// +// // ARM Architecture Reference Manual says: +// // "Setting SYST_RVR to zero has the effect of +// // disabling the SysTick counter independently +// // of the counter enable bit." +// Some(0) => 1, +// +// Some(x) => cmp::min(MAX, x), +// }; +// mem::transmute::<_, SYST>(()).set_reload(dur); +// +// // Start counting down from the new reload +// mem::transmute::<_, SYST>(()).clear_current(); +// +// None +// } +// } else { +// // The queue is empty +// mem::transmute::<_, SYST>(()).disable_interrupt(); +// +// None +// } +// } +// } +// } +// +// pub struct NotReady +// where +// T: Copy, +// M: Monotonic, +// ::Output: TryInto, +// { +// pub index: u8, +// pub instant: M::Instant, +// pub task: T, +// } +// +// impl Eq for NotReady +// where +// T: Copy, +// M: Monotonic, +// ::Output: TryInto, +// { +// } +// +// impl Ord for NotReady +// where +// T: Copy, +// M: Monotonic, +// ::Output: TryInto, +// { +// fn cmp(&self, other: &Self) -> Ordering { +// self.instant.cmp(&other.instant) +// } +// } +// +// impl PartialEq for NotReady +// where +// T: Copy, +// M: Monotonic, +// ::Output: TryInto, +// { +// fn eq(&self, other: &Self) -> bool { +// self.instant == other.instant +// } +// } +// +// impl PartialOrd for NotReady +// where +// T: Copy, +// M: Monotonic, +// ::Output: TryInto, +// { +// fn partial_cmp(&self, other: &Self) -> Option { +// Some(self.cmp(&other)) +// } +// }