Skip to content

Commit d383d40

Browse files
committed
move non Cortex-M specific bits into a crate
closes #50
1 parent 9828d7c commit d383d40

File tree

4 files changed

+6
-77
lines changed

4 files changed

+6
-77
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ repository = "https://github.com/japaric/cortex-m"
1010
version = "0.3.0"
1111

1212
[dependencies]
13+
mcu = { git = "https://github.com/japaric/mcu" }
1314
aligned = "0.1.1"
1415
volatile-register = "0.2.0"

src/interrupt.rs

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,6 @@
11
//! Interrupts
22
3-
use core::cell::UnsafeCell;
4-
5-
/// A "mutex" based on critical sections
6-
pub struct Mutex<T> {
7-
inner: UnsafeCell<T>,
8-
}
9-
10-
impl<T> Mutex<T> {
11-
/// Creates a new mutex
12-
pub const fn new(value: T) -> Self {
13-
Mutex { inner: UnsafeCell::new(value) }
14-
}
15-
}
16-
17-
impl<T> Mutex<T> {
18-
/// Borrows the data for the duration of the critical section
19-
pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
20-
unsafe { &*self.inner.get() }
21-
}
22-
}
23-
24-
/// Interrupt number
25-
pub unsafe trait Nr {
26-
/// Returns the number associated with this interrupt
27-
fn nr(&self) -> u8;
28-
}
29-
30-
// NOTE `Mutex` can be used as a channel so, the protected data must be `Send`
31-
// to prevent sending non-Sendable stuff (e.g. interrupt tokens) across
32-
// different execution contexts (e.g. interrupts)
33-
unsafe impl<T> Sync for Mutex<T>
34-
where
35-
T: Send,
36-
{
37-
}
3+
pub use mcu::{CriticalSection, Mutex, Nr};
384

395
/// Disables all interrupts
406
#[inline(always)]
@@ -74,13 +40,6 @@ pub unsafe fn enable() {
7440
}
7541
}
7642

77-
/// Critical section context
78-
///
79-
/// Indicates that you are executing code within a critical section
80-
pub struct CriticalSection {
81-
_0: (),
82-
}
83-
8443
/// Execute closure `f` in an interrupt-free context.
8544
///
8645
/// This as also known as a "critical section".
@@ -93,7 +52,7 @@ where
9352
// disable interrupts
9453
disable();
9554

96-
let r = f(&CriticalSection { _0: () });
55+
let r = f(unsafe { &CriticalSection::new() });
9756

9857
// If the interrupts were active before our `disable` call, then re-enable
9958
// them. Otherwise, keep them disabled

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#![no_std]
1616

1717
extern crate aligned;
18+
extern crate mcu;
1819
extern crate volatile_register;
1920

2021
#[macro_use]

src/peripheral/mod.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
//! - ARMv7-M Architecture Reference Manual (Issue E.b) - Chapter B3
66
77
use core::cell::UnsafeCell;
8-
use core::marker::PhantomData;
98
use core::ptr;
109

10+
pub use mcu::Peripheral;
1111
use volatile_register::{RO, RW, WO};
1212

13-
use interrupt::{CriticalSection, Nr};
13+
use interrupt::Nr;
1414

1515
#[cfg(test)]
1616
mod test;
@@ -54,38 +54,6 @@ pub const CBP: Peripheral<CBP> = unsafe { Peripheral::new(0xE000_EF50) };
5454

5555
// TODO stand-alone registers: ICTR, ACTLR and STIR
5656

57-
/// A peripheral
58-
#[derive(Debug)]
59-
pub struct Peripheral<T>
60-
where
61-
T: 'static,
62-
{
63-
address: usize,
64-
_marker: PhantomData<&'static mut T>,
65-
}
66-
67-
impl<T> Peripheral<T> {
68-
/// Creates a new peripheral
69-
///
70-
/// `address` is the base address of the register block
71-
pub const unsafe fn new(address: usize) -> Self {
72-
Peripheral {
73-
address: address,
74-
_marker: PhantomData,
75-
}
76-
}
77-
78-
/// Borrows the peripheral for the duration of a critical section
79-
pub fn borrow<'cs>(&self, _ctxt: &'cs CriticalSection) -> &'cs T {
80-
unsafe { &*self.get() }
81-
}
82-
83-
/// Returns a pointer to the register block
84-
pub fn get(&self) -> *mut T {
85-
self.address as *mut T
86-
}
87-
}
88-
8957
/// CPUID register block
9058
#[repr(C)]
9159
pub struct CPUID {

0 commit comments

Comments
 (0)