diff options
author | 2017-07-04 22:14:13 -0500 | |
---|---|---|
committer | 2017-07-04 22:14:42 -0500 | |
commit | d383d40459d1b6cba7ec2fd9f45b10439a6dbac4 (patch) | |
tree | 36ecbde73ee3d0173c793d3e921010a93519d922 /src | |
parent | 9828d7c4f3eddde66526df183acfb9360ee2bece (diff) | |
download | cortex-m-d383d40459d1b6cba7ec2fd9f45b10439a6dbac4.tar.gz cortex-m-d383d40459d1b6cba7ec2fd9f45b10439a6dbac4.tar.zst cortex-m-d383d40459d1b6cba7ec2fd9f45b10439a6dbac4.zip |
move non Cortex-M specific bits into a crate
closes #50
Diffstat (limited to 'src')
-rw-r--r-- | src/interrupt.rs | 45 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/peripheral/mod.rs | 36 |
3 files changed, 5 insertions, 77 deletions
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 @@ -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 { |