aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jorge Aparicio <jorge@japaric.io> 2017-07-04 22:14:13 -0500
committerGravatar Jorge Aparicio <jorge@japaric.io> 2017-07-04 22:14:42 -0500
commitd383d40459d1b6cba7ec2fd9f45b10439a6dbac4 (patch)
tree36ecbde73ee3d0173c793d3e921010a93519d922
parent9828d7c4f3eddde66526df183acfb9360ee2bece (diff)
downloadcortex-m-d383d40459d1b6cba7ec2fd9f45b10439a6dbac4.tar.gz
cortex-m-d383d40459d1b6cba7ec2fd9f45b10439a6dbac4.tar.zst
cortex-m-d383d40459d1b6cba7ec2fd9f45b10439a6dbac4.zip
move non Cortex-M specific bits into a crate
closes #50
-rw-r--r--Cargo.toml1
-rw-r--r--src/interrupt.rs45
-rw-r--r--src/lib.rs1
-rw-r--r--src/peripheral/mod.rs36
4 files changed, 6 insertions, 77 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 6dee988..37338a2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,5 +10,6 @@ repository = "https://github.com/japaric/cortex-m"
version = "0.3.0"
[dependencies]
+mcu = { git = "https://github.com/japaric/mcu" }
aligned = "0.1.1"
volatile-register = "0.2.0" \ No newline at end of file
diff --git a/src/interrupt.rs b/src/interrupt.rs
index 2e43e85..6d7e6ce 100644
--- a/src/interrupt.rs
+++ b/src/interrupt.rs
@@ -1,40 +1,6 @@
//! Interrupts
-use core::cell::UnsafeCell;
-
-/// A "mutex" based on critical sections
-pub struct Mutex<T> {
- inner: UnsafeCell<T>,
-}
-
-impl<T> Mutex<T> {
- /// Creates a new mutex
- pub const fn new(value: T) -> Self {
- Mutex { inner: UnsafeCell::new(value) }
- }
-}
-
-impl<T> Mutex<T> {
- /// Borrows the data for the duration of the critical section
- pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
- unsafe { &*self.inner.get() }
- }
-}
-
-/// Interrupt number
-pub unsafe trait Nr {
- /// Returns the number associated with this interrupt
- fn nr(&self) -> u8;
-}
-
-// NOTE `Mutex` can be used as a channel so, the protected data must be `Send`
-// to prevent sending non-Sendable stuff (e.g. interrupt tokens) across
-// different execution contexts (e.g. interrupts)
-unsafe impl<T> Sync for Mutex<T>
-where
- T: Send,
-{
-}
+pub use mcu::{CriticalSection, Mutex, Nr};
/// Disables all interrupts
#[inline(always)]
@@ -74,13 +40,6 @@ pub unsafe fn enable() {
}
}
-/// Critical section context
-///
-/// Indicates that you are executing code within a critical section
-pub struct CriticalSection {
- _0: (),
-}
-
/// Execute closure `f` in an interrupt-free context.
///
/// This as also known as a "critical section".
@@ -93,7 +52,7 @@ where
// disable interrupts
disable();
- let r = f(&CriticalSection { _0: () });
+ let r = f(unsafe { &CriticalSection::new() });
// If the interrupts were active before our `disable` call, then re-enable
// them. Otherwise, keep them disabled
diff --git a/src/lib.rs b/src/lib.rs
index 5480599..d4d019e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -15,6 +15,7 @@
#![no_std]
extern crate aligned;
+extern crate mcu;
extern crate volatile_register;
#[macro_use]
diff --git a/src/peripheral/mod.rs b/src/peripheral/mod.rs
index f297ab8..97c1762 100644
--- a/src/peripheral/mod.rs
+++ b/src/peripheral/mod.rs
@@ -5,12 +5,12 @@
//! - ARMv7-M Architecture Reference Manual (Issue E.b) - Chapter B3
use core::cell::UnsafeCell;
-use core::marker::PhantomData;
use core::ptr;
+pub use mcu::Peripheral;
use volatile_register::{RO, RW, WO};
-use interrupt::{CriticalSection, Nr};
+use interrupt::Nr;
#[cfg(test)]
mod test;
@@ -54,38 +54,6 @@ pub const CBP: Peripheral<CBP> = unsafe { Peripheral::new(0xE000_EF50) };
// TODO stand-alone registers: ICTR, ACTLR and STIR
-/// A peripheral
-#[derive(Debug)]
-pub struct Peripheral<T>
-where
- T: 'static,
-{
- address: usize,
- _marker: PhantomData<&'static mut T>,
-}
-
-impl<T> Peripheral<T> {
- /// Creates a new peripheral
- ///
- /// `address` is the base address of the register block
- pub const unsafe fn new(address: usize) -> Self {
- Peripheral {
- address: address,
- _marker: PhantomData,
- }
- }
-
- /// Borrows the peripheral for the duration of a critical section
- pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
- unsafe { &*self.get() }
- }
-
- /// Returns a pointer to the register block
- pub fn get(&self) -> *mut T {
- self.address as *mut T
- }
-}
-
/// CPUID register block
#[repr(C)]
pub struct CPUID {