Save, init generation fixed

This commit is contained in:
Emil Fresk 2020-12-03 21:04:06 +01:00
parent 3b4c10e790
commit ef50aeb2e8
13 changed files with 229 additions and 242 deletions

View file

@ -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"

View file

@ -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) {
}
}

View file

@ -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" }

View file

@ -62,18 +62,6 @@ pub fn app(app: &App, _analysis: &Analysis) -> parse::Result<Extra> {
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<Extra> {
if let Some(device) = app.args.device.clone() {
Ok(Extra {
device,
monotonic: app.args.monotonic.clone(),
monotonic: None,
peripherals: app.args.peripherals,
})
} else {

View file

@ -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)*

View file

@ -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(

View file

@ -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 {

View file

@ -25,6 +25,9 @@ pub fn codegen(app: &App, analysis: &Analysis) -> Vec<TokenStream2> {
}
}
// 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();));

View file

@ -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(

View file

@ -1,4 +1,4 @@
#![deny(warnings)]
// #![deny(warnings)]
extern crate proc_macro;

View file

@ -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;

View file

@ -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<cortex_m::Peripherals> 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

312
src/tq.rs
View file

@ -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<M, T, N>(pub BinaryHeap<NotReady<M, T>, N, Min>)
where
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
N: ArrayLength<NotReady<M, T>>,
T: Copy;
impl<M, T, N> TimerQueue<M, T, N>
where
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
N: ArrayLength<NotReady<M, T>>,
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<M, T>) {
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<M, T>
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
{
pub index: u8,
pub instant: M::Instant,
pub task: T,
}
impl<M, T> Eq for NotReady<M, T>
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
{
}
impl<M, T> Ord for NotReady<M, T>
where
T: Copy,
M: Monotonic,
<M::Instant as Sub>::Output: TryInto<u32>,
{
fn cmp(&self, other: &Self) -> Ordering {
self.instant.cmp(&other.instant)
}
}
impl<M, T> PartialEq for NotReady<M, T>
where
T: Copy,
M: Monotonic,
<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,
<M::Instant as Sub>::Output: TryInto<u32>,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
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<M, T, N>(pub BinaryHeap<NotReady<M, T>, N, Min>)
// where
// M: Monotonic,
// <M::Instant as Sub>::Output: TryInto<u32>,
// N: ArrayLength<NotReady<M, T>>,
// T: Copy;
//
// impl<M, T, N> TimerQueue<M, T, N>
// where
// M: Monotonic,
// <M::Instant as Sub>::Output: TryInto<u32>,
// N: ArrayLength<NotReady<M, T>>,
// 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<M, T>) {
// 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<M, T>
// where
// T: Copy,
// M: Monotonic,
// <M::Instant as Sub>::Output: TryInto<u32>,
// {
// pub index: u8,
// pub instant: M::Instant,
// pub task: T,
// }
//
// impl<M, T> Eq for NotReady<M, T>
// where
// T: Copy,
// M: Monotonic,
// <M::Instant as Sub>::Output: TryInto<u32>,
// {
// }
//
// impl<M, T> Ord for NotReady<M, T>
// where
// T: Copy,
// M: Monotonic,
// <M::Instant as Sub>::Output: TryInto<u32>,
// {
// fn cmp(&self, other: &Self) -> Ordering {
// self.instant.cmp(&other.instant)
// }
// }
//
// impl<M, T> PartialEq for NotReady<M, T>
// where
// T: Copy,
// M: Monotonic,
// <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,
// <M::Instant as Sub>::Output: TryInto<u32>,
// {
// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
// Some(self.cmp(&other))
// }
// }