mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-27 14:04:56 +01:00
pub interface to logical2hw and hw2logical
This commit is contained in:
parent
a94de6bafc
commit
dad3a1f520
1 changed files with 57 additions and 93 deletions
150
src/lib.rs
150
src/lib.rs
|
@ -47,8 +47,7 @@ pub struct Resource<T, CEILING> {
|
|||
impl<T, C> Resource<T, C> {
|
||||
/// 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<T, CEILING> Resource<T, C<CEILING>> {
|
|||
/// section
|
||||
///
|
||||
/// This operation is zero cost and doesn't impose any additional blocking
|
||||
pub fn borrow<'cs, PRIORITY, SCEILING>(
|
||||
&'static self,
|
||||
_priority: &P<PRIORITY>,
|
||||
_system_ceiling: &'cs C<SCEILING>,
|
||||
) -> Ref<'cs, T>
|
||||
where
|
||||
SCEILING: GreaterThanOrEqual<CEILING>,
|
||||
CEILING: GreaterThanOrEqual<PRIORITY>,
|
||||
pub fn borrow<'cs, PRIORITY, SCEILING>(&'static self,
|
||||
_priority: &P<PRIORITY>,
|
||||
_system_ceiling: &'cs C<SCEILING>)
|
||||
-> Ref<'cs, T>
|
||||
where SCEILING: GreaterThanOrEqual<CEILING>,
|
||||
CEILING: GreaterThanOrEqual<PRIORITY>
|
||||
{
|
||||
unsafe { Ref::new(&*self.data.get()) }
|
||||
}
|
||||
|
@ -77,24 +74,18 @@ impl<T, CEILING> Resource<T, C<CEILING>> {
|
|||
/// 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<PRIORITY>,
|
||||
) -> Ref<'task, T>
|
||||
where
|
||||
CEILING: Cmp<PRIORITY, Output = Equal>,
|
||||
pub fn claim<'task, PRIORITY>(&'static self, _priority: &'task P<PRIORITY>) -> Ref<'task, T>
|
||||
where CEILING: Cmp<PRIORITY, Output = Equal>
|
||||
{
|
||||
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<PRIORITY>,
|
||||
) -> RefMut<'task, T>
|
||||
where
|
||||
CEILING: Cmp<PRIORITY, Output = Equal>,
|
||||
pub fn claim_mut<'task, PRIORITY>(&'static self,
|
||||
_priority: &'task mut P<PRIORITY>)
|
||||
-> RefMut<'task, T>
|
||||
where CEILING: Cmp<PRIORITY, Output = Equal>
|
||||
{
|
||||
unsafe { RefMut::new(&mut *self.data.get()) }
|
||||
}
|
||||
|
@ -109,22 +100,15 @@ impl<T, CEILING> Resource<T, C<CEILING>> {
|
|||
/// than `CEILING` can be borrowed at zero cost. See
|
||||
/// [Resource.borrow](struct.Resource.html#method.borrow).
|
||||
#[cfg(not(thumbv6m))]
|
||||
pub fn lock<R, PRIORITY, F>(
|
||||
&'static self,
|
||||
_priority: &P<PRIORITY>,
|
||||
f: F,
|
||||
) -> R
|
||||
where
|
||||
F: FnOnce(Ref<T>, C<CEILING>) -> R,
|
||||
CEILING: Cmp<PRIORITY, Output = Greater> + Cmp<UMAX, Output = Less>
|
||||
+ Level,
|
||||
pub fn lock<R, PRIORITY, F>(&'static self, _priority: &P<PRIORITY>, f: F) -> R
|
||||
where F: FnOnce(Ref<T>, C<CEILING>) -> R,
|
||||
CEILING: Cmp<PRIORITY, Output = Greater> + Cmp<UMAX, Output = Less> + Level
|
||||
{
|
||||
unsafe {
|
||||
let old_basepri = basepri::read();
|
||||
basepri_max::write(<CEILING>::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<T, CEILING> Resource<T, C<CEILING>> {
|
|||
/// resource that has ceiling equal `CEILING`. This constraint is required
|
||||
/// to preserve Rust aliasing rules.
|
||||
#[cfg(not(thumbv6m))]
|
||||
pub fn lock_mut<R, PRIORITY, F>(
|
||||
&'static self,
|
||||
_priority: &mut P<PRIORITY>,
|
||||
f: F,
|
||||
) -> R
|
||||
where
|
||||
F: FnOnce(RefMut<T>) -> R,
|
||||
CEILING: Cmp<PRIORITY, Output = Greater> + Cmp<UMAX, Output = Less>
|
||||
+ Level,
|
||||
pub fn lock_mut<R, PRIORITY, F>(&'static self, _priority: &mut P<PRIORITY>, f: F) -> R
|
||||
where F: FnOnce(RefMut<T>) -> R,
|
||||
CEILING: Cmp<PRIORITY, Output = Greater> + Cmp<UMAX, Output = Less> + Level
|
||||
{
|
||||
unsafe {
|
||||
let old_basepri = basepri::read();
|
||||
|
@ -160,32 +138,25 @@ 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
|
||||
pub struct Peripheral<P, CEILING>
|
||||
where
|
||||
P: 'static,
|
||||
where P: 'static
|
||||
{
|
||||
peripheral: cortex_m::peripheral::Peripheral<P>,
|
||||
_ceiling: PhantomData<CEILING>,
|
||||
}
|
||||
|
||||
impl<P, C> Peripheral<P, C>
|
||||
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<P>,)
|
||||
-> Self {
|
||||
pub const unsafe fn new(peripheral: cortex_m::peripheral::Peripheral<P>) -> Self {
|
||||
Peripheral {
|
||||
_ceiling: PhantomData,
|
||||
peripheral: peripheral,
|
||||
|
@ -195,49 +166,37 @@ where
|
|||
|
||||
impl<Periph, CEILING> Peripheral<Periph, C<CEILING>> {
|
||||
/// See [Resource.borrow](./struct.Resource.html#method.borrow)
|
||||
pub fn borrow<'cs, PRIORITY, SCEILING>(
|
||||
&'static self,
|
||||
_priority: &P<PRIORITY>,
|
||||
_system_ceiling: &'cs C<SCEILING>,
|
||||
) -> Ref<'cs, Periph>
|
||||
where
|
||||
SCEILING: GreaterThanOrEqual<CEILING>,
|
||||
CEILING: GreaterThanOrEqual<PRIORITY>,
|
||||
pub fn borrow<'cs, PRIORITY, SCEILING>(&'static self,
|
||||
_priority: &P<PRIORITY>,
|
||||
_system_ceiling: &'cs C<SCEILING>)
|
||||
-> Ref<'cs, Periph>
|
||||
where SCEILING: GreaterThanOrEqual<CEILING>,
|
||||
CEILING: GreaterThanOrEqual<PRIORITY>
|
||||
{
|
||||
unsafe { Ref::new(&*self.peripheral.get()) }
|
||||
}
|
||||
|
||||
/// See [Resource.claim](./struct.Resource.html#method.claim)
|
||||
pub fn claim<'task, PRIORITY>(
|
||||
&'static self,
|
||||
_priority: &'task P<PRIORITY>,
|
||||
) -> Ref<'task, Periph>
|
||||
where
|
||||
CEILING: Cmp<PRIORITY, Output = Equal>,
|
||||
pub fn claim<'task, PRIORITY>(&'static self,
|
||||
_priority: &'task P<PRIORITY>)
|
||||
-> Ref<'task, Periph>
|
||||
where CEILING: Cmp<PRIORITY, Output = Equal>
|
||||
{
|
||||
unsafe { Ref::new(&*self.peripheral.get()) }
|
||||
}
|
||||
|
||||
/// See [Resource.lock](./struct.Resource.html#method.lock)
|
||||
#[cfg(not(thumbv6m))]
|
||||
pub fn lock<R, PRIORITY, F>(
|
||||
&'static self,
|
||||
_priority: &P<PRIORITY>,
|
||||
f: F,
|
||||
) -> R
|
||||
where
|
||||
F: FnOnce(Ref<Periph>, C<CEILING>) -> R,
|
||||
CEILING: Cmp<PRIORITY, Output = Greater> + Cmp<UMAX, Output = Less>
|
||||
+ Level,
|
||||
pub fn lock<R, PRIORITY, F>(&'static self, _priority: &P<PRIORITY>, f: F) -> R
|
||||
where F: FnOnce(Ref<Periph>, C<CEILING>) -> R,
|
||||
CEILING: Cmp<PRIORITY, Output = Greater> + Cmp<UMAX, Output = Less> + Level
|
||||
{
|
||||
unsafe {
|
||||
let old_basepri = basepri::read();
|
||||
basepri_max::write(<CEILING>::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<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
|
||||
///
|
||||
/// No task can preempt this critical section
|
||||
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();
|
||||
::cortex_m::interrupt::disable();
|
||||
|
@ -274,9 +228,8 @@ where
|
|||
|
||||
/// Requests the execution of a `task`
|
||||
pub fn request<T, P>(_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<T> {
|
|||
}
|
||||
|
||||
impl<T> P<T>
|
||||
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>;
|
||||
|
||||
|
|
Loading…
Reference in a new issue