aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: f5c8b992b0efb17807e704715c9102688a8f9d73 (plain) (blame)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#![feature(asm)]
#![feature(const_fn)]
#![feature(optin_builtin_traits)]
#![no_std]

extern crate cortex_m;
extern crate static_ref;

use core::cell::UnsafeCell;

pub use cortex_m::asm::{bkpt, wfi};
pub use cortex_m::interrupt::CriticalSection;
pub use cortex_m::interrupt::free as atomic;
pub use static_ref::Static;
#[cfg(not(armv6m))]
use cortex_m::register::{basepri_max, basepri};

#[cfg(not(armv6m))]
macro_rules! barrier {
    () => {
        asm!("" ::: "memory" : "volatile");
    }
}

#[inline(always)]
unsafe fn claim<T, U, R, F, G>(
    data: T,
    ceiling: u8,
    nvic_prio_bits: u8,
    t: &mut Threshold,
    f: F,
    g: G,
) -> R
where
    F: FnOnce(U, &mut Threshold) -> R,
    G: FnOnce(T) -> U,
{
    let max_priority = 1 << nvic_prio_bits;
    if ceiling > t.0 {
        match () {
            #[cfg(armv6m)]
            () => {
                atomic(|_| f(g(data), &mut Threshold::new(max_priority)))
            }
            #[cfg(not(armv6m))]
            () => {
                if ceiling == max_priority {
                    atomic(|_| f(g(data), &mut Threshold::new(max_priority)))
                } else {
                    let old = basepri::read();
                    let hw = (max_priority - ceiling) << (8 - nvic_prio_bits);
                    basepri_max::write(hw);
                    barrier!();
                    let ret = f(g(data), &mut Threshold(ceiling));
                    barrier!();
                    basepri::write(old);
                    ret
                }
            }
        }
    } else {
        f(g(data), t)
    }
}

pub struct Peripheral<P>
where
    P: 'static,
{
    // FIXME(rustc/LLVM bug?) storing the ceiling in the resource de-optimizes
    // claims (the ceiling value gets loaded at runtime rather than inlined)
    // ceiling: u8,
    peripheral: cortex_m::peripheral::Peripheral<P>,
}

impl<P> Peripheral<P> {
    pub const fn new(peripheral: cortex_m::peripheral::Peripheral<P>) -> Self {
        Peripheral { peripheral }
    }

    #[inline(always)]
    pub unsafe fn claim<R, F>(
        &'static self,
        ceiling: u8,
        nvic_prio_bits: u8,
        t: &mut Threshold,
        f: F,
    ) -> R
    where
        F: FnOnce(&P, &mut Threshold) -> R,
    {
        claim(
            &self.peripheral,
            ceiling,
            nvic_prio_bits,
            t,
            f,
            |peripheral| &*peripheral.get(),
        )
    }

    pub fn get(&self) -> *mut P {
        self.peripheral.get()
    }
}

unsafe impl<P> Sync for Peripheral<P>
where
    P: Send,
{
}

pub struct Resource<T> {
    // FIXME(rustc/LLVM bug?) storing the ceiling in the resource de-optimizes
    // claims (the ceiling value gets loaded at runtime rather than inlined)
    // ceiling: u8,
    data: UnsafeCell<T>,
}

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

    #[inline(always)]
    pub unsafe fn claim<R, F>(
        &'static self,
        ceiling: u8,
        nvic_prio_bits: u8,
        t: &mut Threshold,
        f: F,
    ) -> R
    where
        F: FnOnce(&Static<T>, &mut Threshold) -> R,
    {
        claim(&self.data, ceiling, nvic_prio_bits, t, f, |data| {
            Static::ref_(&*data.get())
        })
    }

    #[inline(always)]
    pub unsafe fn claim_mut<R, F>(
        &'static self,
        ceiling: u8,
        nvic_prio_bits: u8,
        t: &mut Threshold,
        f: F,
    ) -> R
    where
        F: FnOnce(&mut Static<T>, &mut Threshold) -> R,
    {
        claim(&self.data, ceiling, nvic_prio_bits, t, f, |data| {
            Static::ref_mut(&mut *data.get())
        })
    }

    pub fn get(&self) -> *mut T {
        self.data.get()
    }
}

unsafe impl<T> Sync for Resource<T>
where
    T: Send,
{
}

pub struct Threshold(u8);

impl Threshold {
    pub unsafe fn new(value: u8) -> Self {
        Threshold(value)
    }
}

impl !Send for Threshold {}

#[macro_export]
macro_rules! task {
    ($NAME:ident, $body:path) => {
        #[allow(non_snake_case)]
        #[no_mangle]
        pub unsafe extern "C" fn $NAME() {
            let f: fn($crate::Threshold, ::$NAME::Resources) = $body;

            f(
                $crate::Threshold::new(::$NAME::$NAME),
                ::$NAME::Resources::new(),
            );
        }
    };
    ($NAME:ident, $body:path, $local:ident {
        $($var:ident: $ty:ty = $expr:expr;)+
    }) => {
        struct $local {
            $($var: $ty,)+
        }

        #[allow(non_snake_case)]
        #[no_mangle]
        pub unsafe extern "C" fn $NAME() {
            let f: fn(
                $crate::Threshold,
                &mut $local,
                ::$NAME::Resources,
            ) = $body;

            static mut LOCAL: $local = $local {
                $($var: $expr,)+
            };

            f(
                $crate::Threshold::new(::$NAME::$NAME),
                &mut LOCAL,
                ::$NAME::Resources::new(),
            );
        }
    };
}

#[allow(non_camel_case_types)]
#[doc(hidden)]
pub enum Exception {
    /// System service call via SWI instruction
    SVCALL,
    /// Pendable request for system service
    PENDSV,
    /// System tick timer
    SYS_TICK,
}

impl Exception {
    #[doc(hidden)]
    pub fn nr(&self) -> usize {
        match *self {
            Exception::SVCALL => 11,
            Exception::PENDSV => 14,
            Exception::SYS_TICK => 15,
        }
    }
}