aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2019-06-24 14:09:12 +0200
committerGravatar Jorge Aparicio <jorge@japaric.io> 2019-06-24 14:09:12 +0200
commit596cf585ea8dc278d88e0652dffbacbc75de04c6 (patch)
tree147bad178f15a7e7a91d847f39d501ecc1051821
parent4e51bb68b976c6bb6a9a989dc560d2a8123a84ca (diff)
downloadrtic-596cf585ea8dc278d88e0652dffbacbc75de04c6.tar.gz
rtic-596cf585ea8dc278d88e0652dffbacbc75de04c6.tar.zst
rtic-596cf585ea8dc278d88e0652dffbacbc75de04c6.zip
Monotonic trait is safe; add MultiCore trait
Diffstat (limited to '')
-rw-r--r--heterogeneous/src/lib.rs6
-rw-r--r--homogeneous/src/lib.rs6
-rw-r--r--macros/src/codegen.rs2
-rw-r--r--macros/src/codegen/assertions.rs11
-rw-r--r--src/cyccnt.rs7
-rw-r--r--src/export.rs7
-rw-r--r--src/lib.rs5
7 files changed, 35 insertions, 9 deletions
diff --git a/heterogeneous/src/lib.rs b/heterogeneous/src/lib.rs
index a4f0ec57..3288bfe0 100644
--- a/heterogeneous/src/lib.rs
+++ b/heterogeneous/src/lib.rs
@@ -8,7 +8,7 @@ use core::{
};
use bare_metal::Nr;
-use rtfm::Monotonic;
+use rtfm::{Monotonic, MultiCore};
// both cores have the exact same interrupts
pub use Interrupt_0 as Interrupt_1;
@@ -21,7 +21,7 @@ pub fn xpend(_core: u8, _interrupt: impl Nr) {}
/// Fake monotonic timer
pub struct MT;
-unsafe impl Monotonic for MT {
+impl Monotonic for MT {
type Instant = Instant;
fn ratio() -> u32 {
@@ -41,6 +41,8 @@ unsafe impl Monotonic for MT {
}
}
+impl MultiCore for MT {}
+
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Instant(i32);
diff --git a/homogeneous/src/lib.rs b/homogeneous/src/lib.rs
index a4f0ec57..3288bfe0 100644
--- a/homogeneous/src/lib.rs
+++ b/homogeneous/src/lib.rs
@@ -8,7 +8,7 @@ use core::{
};
use bare_metal::Nr;
-use rtfm::Monotonic;
+use rtfm::{Monotonic, MultiCore};
// both cores have the exact same interrupts
pub use Interrupt_0 as Interrupt_1;
@@ -21,7 +21,7 @@ pub fn xpend(_core: u8, _interrupt: impl Nr) {}
/// Fake monotonic timer
pub struct MT;
-unsafe impl Monotonic for MT {
+impl Monotonic for MT {
type Instant = Instant;
fn ratio() -> u32 {
@@ -41,6 +41,8 @@ unsafe impl Monotonic for MT {
}
}
+impl MultiCore for MT {}
+
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct Instant(i32);
diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs
index 92766260..a3515994 100644
--- a/macros/src/codegen.rs
+++ b/macros/src/codegen.rs
@@ -32,7 +32,7 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 {
// generate a `main` function for each core
for core in 0..app.args.cores {
- let assertion_stmts = assertions::codegen(core, analysis);
+ let assertion_stmts = assertions::codegen(core, analysis, extra);
let (const_app_pre_init, pre_init_stmts) = pre_init::codegen(core, &app, analysis, extra);
diff --git a/macros/src/codegen/assertions.rs b/macros/src/codegen/assertions.rs
index 95268a2c..4a77352f 100644
--- a/macros/src/codegen/assertions.rs
+++ b/macros/src/codegen/assertions.rs
@@ -1,10 +1,10 @@
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
-use crate::analyze::Analysis;
+use crate::{analyze::Analysis, check::Extra};
/// Generates compile-time assertions that check that types implement the `Send` / `Sync` traits
-pub fn codegen(core: u8, analysis: &Analysis) -> Vec<TokenStream2> {
+pub fn codegen(core: u8, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
let mut stmts = vec![];
// we don't generate *all* assertions on all cores because the user could conditionally import a
@@ -22,5 +22,12 @@ pub fn codegen(core: u8, analysis: &Analysis) -> Vec<TokenStream2> {
}
}
+ // if the `schedule` API is used in more than one core then we need to check that the
+ // `monotonic` timer can be used in multi-core context
+ if analysis.timer_queues.len() > 1 && analysis.timer_queues.contains_key(&core) {
+ let monotonic = extra.monotonic();
+ stmts.push(quote!(rtfm::export::assert_multicore::<#monotonic>();));
+ }
+
stmts
}
diff --git a/src/cyccnt.rs b/src/cyccnt.rs
index a2b216c1..468aa712 100644
--- a/src/cyccnt.rs
+++ b/src/cyccnt.rs
@@ -116,6 +116,11 @@ pub struct Duration {
}
impl Duration {
+ /// Creates a new `Duration` from the specified number of clock cycles
+ pub fn from_cycles(cycles: u32) -> Self {
+ Duration { inner: cycles }
+ }
+
/// Returns the total number of clock cycles contained by this `Duration`
pub fn as_cycles(&self) -> u32 {
self.inner
@@ -181,7 +186,7 @@ impl U32Ext for u32 {
pub struct CYCCNT;
#[cfg(not(feature = "heterogeneous"))]
-unsafe impl crate::Monotonic for CYCCNT {
+impl crate::Monotonic for CYCCNT {
type Instant = Instant;
fn ratio() -> u32 {
diff --git a/src/export.rs b/src/export.rs
index 7646e3c5..572068ce 100644
--- a/src/export.rs
+++ b/src/export.rs
@@ -108,6 +108,13 @@ where
{
}
+#[inline(always)]
+pub fn assert_multicore<T>()
+where
+ T: super::MultiCore,
+{
+}
+
#[cfg(armv7m)]
#[inline(always)]
pub unsafe fn lock<T, R>(
diff --git a/src/lib.rs b/src/lib.rs
index acb3a63d..decd2da1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -117,7 +117,7 @@ impl From<cortex_m::Peripherals> for Peripherals {
}
/// A monotonic clock / counter
-pub unsafe trait Monotonic {
+pub trait Monotonic {
/// A measurement of this clock
type Instant: Copy + Ord + Sub;
@@ -134,6 +134,9 @@ pub unsafe trait Monotonic {
fn zero() -> Self::Instant;
}
+/// A marker trait that indicates that it is correct to use this type in multi-core context
+pub trait MultiCore {}
+
/// Sets the given `interrupt` as pending
///
/// This is a convenience function around