with new formatting (perhaps)

This commit is contained in:
pln 2017-04-17 18:59:56 +02:00
parent dad3a1f520
commit 70f573a6c4

View file

@ -47,7 +47,8 @@ pub struct Resource<T, CEILING> {
impl<T, C> Resource<T, C> { impl<T, C> Resource<T, C> {
/// Creates a new resource with ceiling `C` /// Creates a new resource with ceiling `C`
pub const fn new(data: T) -> Self pub const fn new(data: T) -> Self
where C: Ceiling where
C: Ceiling,
{ {
Resource { Resource {
_ceiling: PhantomData, _ceiling: PhantomData,
@ -61,12 +62,14 @@ impl<T, CEILING> Resource<T, C<CEILING>> {
/// section /// section
/// ///
/// This operation is zero cost and doesn't impose any additional blocking /// This operation is zero cost and doesn't impose any additional blocking
pub fn borrow<'cs, PRIORITY, SCEILING>(&'static self, pub fn borrow<'cs, PRIORITY, SCEILING>(
_priority: &P<PRIORITY>, &'static self,
_system_ceiling: &'cs C<SCEILING>) _priority: &P<PRIORITY>,
-> Ref<'cs, T> _system_ceiling: &'cs C<SCEILING>,
where SCEILING: GreaterThanOrEqual<CEILING>, ) -> Ref<'cs, T>
CEILING: GreaterThanOrEqual<PRIORITY> where
SCEILING: GreaterThanOrEqual<CEILING>,
CEILING: GreaterThanOrEqual<PRIORITY>,
{ {
unsafe { Ref::new(&*self.data.get()) } unsafe { Ref::new(&*self.data.get()) }
} }
@ -74,18 +77,24 @@ impl<T, CEILING> Resource<T, C<CEILING>> {
/// Claims the resource at the task with highest priority /// Claims the resource at the task with highest priority
/// ///
/// This operation is zero cost and doesn't impose any additional blocking /// This operation is zero cost and doesn't impose any additional blocking
pub fn claim<'task, PRIORITY>(&'static self, _priority: &'task P<PRIORITY>) -> Ref<'task, T> pub fn claim<'task, PRIORITY>(
where CEILING: Cmp<PRIORITY, Output = Equal> &'static self,
_priority: &'task P<PRIORITY>,
) -> Ref<'task, T>
where
CEILING: Cmp<PRIORITY, Output = Equal>,
{ {
unsafe { Ref::new(&*self.data.get()) } unsafe { Ref::new(&*self.data.get()) }
} }
/// Like [Resource.claim](struct.Resource.html#method.claim) but returns a /// Like [Resource.claim](struct.Resource.html#method.claim) but returns a
/// `&mut-` reference /// `&mut-` reference
pub fn claim_mut<'task, PRIORITY>(&'static self, pub fn claim_mut<'task, PRIORITY>(
_priority: &'task mut P<PRIORITY>) &'static self,
-> RefMut<'task, T> _priority: &'task mut P<PRIORITY>,
where CEILING: Cmp<PRIORITY, Output = Equal> ) -> RefMut<'task, T>
where
CEILING: Cmp<PRIORITY, Output = Equal>,
{ {
unsafe { RefMut::new(&mut *self.data.get()) } unsafe { RefMut::new(&mut *self.data.get()) }
} }
@ -108,7 +117,8 @@ impl<T, CEILING> Resource<T, C<CEILING>> {
let old_basepri = basepri::read(); let old_basepri = basepri::read();
basepri_max::write(<CEILING>::hw()); basepri_max::write(<CEILING>::hw());
barrier!(); barrier!();
let ret = f(Ref::new(&*self.data.get()), C { _marker: PhantomData }); let ret =
f(Ref::new(&*self.data.get()), C { _marker: PhantomData });
barrier!(); barrier!();
basepri::write(old_basepri); basepri::write(old_basepri);
ret ret
@ -138,25 +148,32 @@ impl<T, CEILING> Resource<T, C<CEILING>> {
} }
} }
unsafe impl<T, C> Sync for Resource<T, C> where C: Ceiling {} unsafe impl<T, C> Sync for Resource<T, C>
where
C: Ceiling,
{
}
/// A hardware peripheral as a resource /// A hardware peripheral as a resource
pub struct Peripheral<P, CEILING> pub struct Peripheral<P, CEILING>
where P: 'static where
P: 'static,
{ {
peripheral: cortex_m::peripheral::Peripheral<P>, peripheral: cortex_m::peripheral::Peripheral<P>,
_ceiling: PhantomData<CEILING>, _ceiling: PhantomData<CEILING>,
} }
impl<P, C> Peripheral<P, C> impl<P, C> Peripheral<P, C>
where C: Ceiling where
C: Ceiling,
{ {
/// Assigns a ceiling `C` to the `peripheral` /// Assigns a ceiling `C` to the `peripheral`
/// ///
/// # Safety /// # Safety
/// ///
/// You MUST not create two resources that point to the same peripheral /// You MUST not create two resources that point to the same peripheral
pub const unsafe fn new(peripheral: cortex_m::peripheral::Peripheral<P>) -> Self { pub const unsafe fn new(peripheral: cortex_m::peripheral::Peripheral<P>,)
-> Self {
Peripheral { Peripheral {
_ceiling: PhantomData, _ceiling: PhantomData,
peripheral: peripheral, peripheral: peripheral,
@ -166,21 +183,25 @@ impl<P, C> Peripheral<P, C>
impl<Periph, CEILING> Peripheral<Periph, C<CEILING>> { impl<Periph, CEILING> Peripheral<Periph, C<CEILING>> {
/// See [Resource.borrow](./struct.Resource.html#method.borrow) /// See [Resource.borrow](./struct.Resource.html#method.borrow)
pub fn borrow<'cs, PRIORITY, SCEILING>(&'static self, pub fn borrow<'cs, PRIORITY, SCEILING>(
_priority: &P<PRIORITY>, &'static self,
_system_ceiling: &'cs C<SCEILING>) _priority: &P<PRIORITY>,
-> Ref<'cs, Periph> _system_ceiling: &'cs C<SCEILING>,
where SCEILING: GreaterThanOrEqual<CEILING>, ) -> Ref<'cs, Periph>
CEILING: GreaterThanOrEqual<PRIORITY> where
SCEILING: GreaterThanOrEqual<CEILING>,
CEILING: GreaterThanOrEqual<PRIORITY>,
{ {
unsafe { Ref::new(&*self.peripheral.get()) } unsafe { Ref::new(&*self.peripheral.get()) }
} }
/// See [Resource.claim](./struct.Resource.html#method.claim) /// See [Resource.claim](./struct.Resource.html#method.claim)
pub fn claim<'task, PRIORITY>(&'static self, pub fn claim<'task, PRIORITY>(
_priority: &'task P<PRIORITY>) &'static self,
-> Ref<'task, Periph> _priority: &'task P<PRIORITY>,
where CEILING: Cmp<PRIORITY, Output = Equal> ) -> Ref<'task, Periph>
where
CEILING: Cmp<PRIORITY, Output = Equal>,
{ {
unsafe { Ref::new(&*self.peripheral.get()) } unsafe { Ref::new(&*self.peripheral.get()) }
} }
@ -195,8 +216,10 @@ impl<Periph, CEILING> Peripheral<Periph, C<CEILING>> {
let old_basepri = basepri::read(); let old_basepri = basepri::read();
basepri_max::write(<CEILING>::hw()); basepri_max::write(<CEILING>::hw());
barrier!(); barrier!();
let ret = f(Ref::new(&*self.peripheral.get()), let ret = f(
C { _marker: PhantomData }); Ref::new(&*self.peripheral.get()),
C { _marker: PhantomData },
);
barrier!(); barrier!();
basepri::write(old_basepri); basepri::write(old_basepri);
ret ret
@ -204,13 +227,18 @@ impl<Periph, CEILING> Peripheral<Periph, C<CEILING>> {
} }
} }
unsafe impl<T, C> Sync for Peripheral<T, C> where C: Ceiling {} unsafe impl<T, C> Sync for Peripheral<T, C>
where
C: Ceiling,
{
}
/// A global critical section /// A global critical section
/// ///
/// No task can preempt this critical section /// No task can preempt this critical section
pub fn critical<R, F>(f: F) -> R pub fn critical<R, F>(f: F) -> R
where F: FnOnce(CMAX) -> R where
F: FnOnce(CMAX) -> R,
{ {
let primask = ::cortex_m::register::primask::read(); let primask = ::cortex_m::register::primask::read();
::cortex_m::interrupt::disable(); ::cortex_m::interrupt::disable();
@ -228,8 +256,9 @@ pub fn critical<R, F>(f: F) -> R
/// Requests the execution of a `task` /// Requests the execution of a `task`
pub fn request<T, P>(_task: fn(T, P)) pub fn request<T, P>(_task: fn(T, P))
where T: Context + Nr, where
P: Priority T: Context + Nr,
P: Priority,
{ {
let nvic = unsafe { &*NVIC.get() }; let nvic = unsafe { &*NVIC.get() };
@ -263,7 +292,8 @@ pub struct P<T> {
} }
impl<T> P<T> impl<T> P<T>
where T: Level where
T: Level,
{ {
#[doc(hidden)] #[doc(hidden)]
pub fn hw() -> u8 { pub fn hw() -> u8 {