aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 62dd508747ad5c813ba817bb82cd983f497fa1f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! Stack Resource Policy for Cortex-M processors
//!
//! NOTE ARMv6-M is not supported at the moment.

#![feature(asm)]
#![feature(const_fn)]
#![no_std]

// NOTE Only the 4 highest bits of the priority byte (BASEPRI / NVIC.IPR) are
// considered when determining priorities.
const PRIORITY_BITS: u8 = 4;

extern crate cortex_m;

use cortex_m::interrupt::CsToken;
use cortex_m::register::{basepri, basepri_max};

use core::cell::UnsafeCell;
use core::marker::PhantomData;

// XXX why is this needed?
#[inline(always)]
fn compiler_barrier() {
    unsafe {
        asm!(""
             :
             :
             : "memory"
             : "volatile")
    }
}

// XXX why is this needed?
#[inline(always)]
fn memory_barrier() {
    unsafe {
        asm!("dsb
              isb"
             :
             :
             : "memory"
             : "volatile")
    }
}

pub struct Peripheral<T>
    where T: 'static
{
    _ty: PhantomData<&'static mut T>,
    address: usize,
    ceiling: u8,
}

impl<T> Peripheral<T> {
    pub const unsafe fn new(address: usize, ceiling: u8) -> Self {
        Peripheral {
            _ty: PhantomData,
            address: address,
            ceiling: ceiling,
        }
    }

    pub fn claim<F, R>(&self, f: F) -> R
        where F: FnOnce(&T) -> R
    {
        unsafe {
            let old_basepri = basepri::read();
            basepri_max::write(priority(self.ceiling));
            memory_barrier();
            let ret = f(&*(self.address as *const T));
            compiler_barrier();
            basepri::write(old_basepri);
            ret
        }
    }

    pub fn claim_mut<F, R>(&self, f: F) -> R
        where F: FnOnce(&mut T) -> R
    {
        unsafe {
            let old_basepri = basepri::read();
            basepri_max::write(priority(self.ceiling));
            memory_barrier();
            let ret = f(&mut *(self.address as *mut T));
            compiler_barrier();
            basepri::write(old_basepri);
            ret
        }
    }

    pub fn take<'a>(&self, _token: &'a CsToken) -> &'a T {
        unsafe {
            &*(self.address as *const T)
        }
    }

    pub fn take_mut<'a>(&self, _token: &'a CsToken) -> &'a mut T {
        unsafe {
            &mut *(self.address as *mut T)
        }
    }
}

pub struct Resource<T> {
    ceiling: u8,
    data: UnsafeCell<T>,
}

impl<T> Resource<T> {
    pub const fn new(data: T, ceiling: u8) -> Self {
        Resource {
            ceiling: ceiling,
            data: UnsafeCell::new(data),
        }
    }

    pub fn claim<F, R>(&self, f: F) -> R
        where F: FnOnce(&T) -> R
    {
        unsafe {
            let old_basepri = basepri::read();
            basepri_max::write(priority(self.ceiling));
            memory_barrier();
            let ret = f(&*self.data.get());
            compiler_barrier();
            basepri::write(old_basepri);
            ret
        }
    }

    pub fn claim_mut<F, R>(&self, f: F) -> R
        where F: FnOnce(&mut T) -> R
    {
        unsafe {
            let old_basepri = basepri::read();
            basepri_max::write(priority(self.ceiling));
            memory_barrier();
            let ret = f(&mut *self.data.get());
            compiler_barrier();
            basepri::write(old_basepri);
            ret
        }
    }

    pub fn take<'a>(&self, _token: &'a CsToken) -> &'a T {
        unsafe {
            &*self.data.get()
        }
    }

    pub fn take_mut<'a>(&self, _token: &'a CsToken) -> &'a mut T {
        unsafe {
            &mut *self.data.get()
        }
    }
}

unsafe impl<T> Sync for Resource<T> {}

/// Turns a `logical` priority into a NVIC-style priority
///
/// With `logical` priorities, `2` has HIGHER priority than `1`.
///
/// With NVIC priorities, `32` has LOWER priority than `16`. (Also, NVIC
/// priorities encode the actual priority in the highest bits of a byte so
/// priorities like `1` and `2` aren't actually different)
// TODO review the handling extreme value
pub fn priority(logical: u8) -> u8 {
    ((1 << PRIORITY_BITS) - logical) << (8 - PRIORITY_BITS)
}