From dad3a1f5207ac78dd23b0a5cfc58f5f64625dc21 Mon Sep 17 00:00:00 2001 From: pln Date: Mon, 17 Apr 2017 18:40:56 +0200 Subject: [PATCH] pub interface to logical2hw and hw2logical --- src/lib.rs | 150 ++++++++++++++++++++--------------------------------- 1 file changed, 57 insertions(+), 93 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ba5e0dc7c2..a208786068 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -47,8 +47,7 @@ pub struct Resource { impl Resource { /// Creates a new resource with ceiling `C` pub const fn new(data: T) -> Self - where - C: Ceiling, + where C: Ceiling { Resource { _ceiling: PhantomData, @@ -62,14 +61,12 @@ impl Resource> { /// section /// /// This operation is zero cost and doesn't impose any additional blocking - pub fn borrow<'cs, PRIORITY, SCEILING>( - &'static self, - _priority: &P, - _system_ceiling: &'cs C, - ) -> Ref<'cs, T> - where - SCEILING: GreaterThanOrEqual, - CEILING: GreaterThanOrEqual, + pub fn borrow<'cs, PRIORITY, SCEILING>(&'static self, + _priority: &P, + _system_ceiling: &'cs C) + -> Ref<'cs, T> + where SCEILING: GreaterThanOrEqual, + CEILING: GreaterThanOrEqual { unsafe { Ref::new(&*self.data.get()) } } @@ -77,24 +74,18 @@ impl Resource> { /// Claims the resource at the task with highest priority /// /// This operation is zero cost and doesn't impose any additional blocking - pub fn claim<'task, PRIORITY>( - &'static self, - _priority: &'task P, - ) -> Ref<'task, T> - where - CEILING: Cmp, + pub fn claim<'task, PRIORITY>(&'static self, _priority: &'task P) -> Ref<'task, T> + where CEILING: Cmp { unsafe { Ref::new(&*self.data.get()) } } /// Like [Resource.claim](struct.Resource.html#method.claim) but returns a /// `&mut-` reference - pub fn claim_mut<'task, PRIORITY>( - &'static self, - _priority: &'task mut P, - ) -> RefMut<'task, T> - where - CEILING: Cmp, + pub fn claim_mut<'task, PRIORITY>(&'static self, + _priority: &'task mut P) + -> RefMut<'task, T> + where CEILING: Cmp { unsafe { RefMut::new(&mut *self.data.get()) } } @@ -109,22 +100,15 @@ impl Resource> { /// than `CEILING` can be borrowed at zero cost. See /// [Resource.borrow](struct.Resource.html#method.borrow). #[cfg(not(thumbv6m))] - pub fn lock( - &'static self, - _priority: &P, - f: F, - ) -> R - where - F: FnOnce(Ref, C) -> R, - CEILING: Cmp + Cmp - + Level, + pub fn lock(&'static self, _priority: &P, f: F) -> R + where F: FnOnce(Ref, C) -> R, + CEILING: Cmp + Cmp + Level { unsafe { let old_basepri = basepri::read(); basepri_max::write(::hw()); barrier!(); - let ret = - f(Ref::new(&*self.data.get()), C { _marker: PhantomData }); + let ret = f(Ref::new(&*self.data.get()), C { _marker: PhantomData }); barrier!(); basepri::write(old_basepri); ret @@ -138,15 +122,9 @@ impl Resource> { /// resource that has ceiling equal `CEILING`. This constraint is required /// to preserve Rust aliasing rules. #[cfg(not(thumbv6m))] - pub fn lock_mut( - &'static self, - _priority: &mut P, - f: F, - ) -> R - where - F: FnOnce(RefMut) -> R, - CEILING: Cmp + Cmp - + Level, + pub fn lock_mut(&'static self, _priority: &mut P, f: F) -> R + where F: FnOnce(RefMut) -> R, + CEILING: Cmp + Cmp + Level { unsafe { let old_basepri = basepri::read(); @@ -160,32 +138,25 @@ impl Resource> { } } -unsafe impl Sync for Resource -where - C: Ceiling, -{ -} +unsafe impl Sync for Resource where C: Ceiling {} /// A hardware peripheral as a resource pub struct Peripheral -where - P: 'static, + where P: 'static { peripheral: cortex_m::peripheral::Peripheral

, _ceiling: PhantomData, } impl Peripheral -where - C: Ceiling, + where C: Ceiling { /// Assigns a ceiling `C` to the `peripheral` /// /// # Safety /// /// You MUST not create two resources that point to the same peripheral - pub const unsafe fn new(peripheral: cortex_m::peripheral::Peripheral

,) - -> Self { + pub const unsafe fn new(peripheral: cortex_m::peripheral::Peripheral

) -> Self { Peripheral { _ceiling: PhantomData, peripheral: peripheral, @@ -195,49 +166,37 @@ where impl Peripheral> { /// See [Resource.borrow](./struct.Resource.html#method.borrow) - pub fn borrow<'cs, PRIORITY, SCEILING>( - &'static self, - _priority: &P, - _system_ceiling: &'cs C, - ) -> Ref<'cs, Periph> - where - SCEILING: GreaterThanOrEqual, - CEILING: GreaterThanOrEqual, + pub fn borrow<'cs, PRIORITY, SCEILING>(&'static self, + _priority: &P, + _system_ceiling: &'cs C) + -> Ref<'cs, Periph> + where SCEILING: GreaterThanOrEqual, + CEILING: GreaterThanOrEqual { unsafe { Ref::new(&*self.peripheral.get()) } } /// See [Resource.claim](./struct.Resource.html#method.claim) - pub fn claim<'task, PRIORITY>( - &'static self, - _priority: &'task P, - ) -> Ref<'task, Periph> - where - CEILING: Cmp, + pub fn claim<'task, PRIORITY>(&'static self, + _priority: &'task P) + -> Ref<'task, Periph> + where CEILING: Cmp { unsafe { Ref::new(&*self.peripheral.get()) } } /// See [Resource.lock](./struct.Resource.html#method.lock) #[cfg(not(thumbv6m))] - pub fn lock( - &'static self, - _priority: &P, - f: F, - ) -> R - where - F: FnOnce(Ref, C) -> R, - CEILING: Cmp + Cmp - + Level, + pub fn lock(&'static self, _priority: &P, f: F) -> R + where F: FnOnce(Ref, C) -> R, + CEILING: Cmp + Cmp + Level { unsafe { let old_basepri = basepri::read(); basepri_max::write(::hw()); barrier!(); - let ret = f( - Ref::new(&*self.peripheral.get()), - C { _marker: PhantomData }, - ); + let ret = f(Ref::new(&*self.peripheral.get()), + C { _marker: PhantomData }); barrier!(); basepri::write(old_basepri); ret @@ -245,18 +204,13 @@ impl Peripheral> { } } -unsafe impl Sync for Peripheral -where - C: Ceiling, -{ -} +unsafe impl Sync for Peripheral where C: Ceiling {} /// A global critical section /// /// No task can preempt this critical section pub fn critical(f: F) -> R -where - F: FnOnce(CMAX) -> R, + where F: FnOnce(CMAX) -> R { let primask = ::cortex_m::register::primask::read(); ::cortex_m::interrupt::disable(); @@ -274,9 +228,8 @@ where /// Requests the execution of a `task` pub fn request(_task: fn(T, P)) -where - T: Context + Nr, - P: Priority, + where T: Context + Nr, + P: Priority { let nvic = unsafe { &*NVIC.get() }; @@ -310,8 +263,7 @@ pub struct P { } impl P -where - T: Level, + where T: Level { #[doc(hidden)] pub fn hw() -> u8 { @@ -342,10 +294,22 @@ pub unsafe trait Level { /// DO NOT IMPLEMENT THIS TRAIT YOURSELF pub unsafe trait Priority {} -fn logical2hw(logical: u8) -> u8 { + +/// Convert a logical priority to a shifted hardware prio +/// as used by the NVIC and basepri registers +/// Notice, wrapping causes a panic due to u8 +pub fn logical2hw(logical: u8) -> u8 { ((1 << PRIORITY_BITS) - logical) << (8 - PRIORITY_BITS) } +/// Convert a shifted hardware prio to a logical priority +/// as used by the NVIC and basepri registers +/// Notice, wrapping causes a panic due to u8 +pub fn hw2logical(hw: u8) -> u8 { + (1 << PRIORITY_BITS) - (hw >> (8 - PRIORITY_BITS)) +} + + /// Priority 0, the lowest priority pub type P0 = P<::typenum::U0>;