-
Notifications
You must be signed in to change notification settings - Fork 6
/
interrupt.rs
61 lines (52 loc) · 1.45 KB
/
interrupt.rs
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
//! Interrupts
use crate::asm;
pub use bare_metal::{CriticalSection, Mutex};
/// Disables all interrupts
#[inline(always)]
pub fn disable() {
match () {
#[cfg(target_arch = "msp430")]
() => unsafe {
asm!("dint {{ nop");
},
#[cfg(not(target_arch = "msp430"))]
() => {}
}
}
/// Enables all the interrupts
///
/// # Safety
///
/// - In any function `f()` that calls `enable`, `CriticalSection` or `&CriticalSection` tokens cannot be used in `f()`'s body after the
/// call to `enable`. If `f()` owns `CriticalSection` tokens, it is recommended to [`drop`](https://doc.rust-lang.org/nightly/core/mem/fn.drop.html)
/// these tokens before calling `enable`.
#[inline(always)]
pub unsafe fn enable() {
match () {
#[cfg(target_arch = "msp430")]
() => {
asm!("nop {{ eint {{ nop");
}
#[cfg(not(target_arch = "msp430"))]
() => {}
}
}
/// Execute closure `f` in an interrupt-free context.
///
/// This as also known as a "critical section".
pub fn free<F, R>(f: F) -> R
where
F: for<'a> FnOnce(&'a CriticalSection<'a>) -> R,
{
let status = ::register::sr::read();
// disable interrupts
disable();
let cs = unsafe { CriticalSection::new() };
let r = f(&cs);
// If the interrupts were active before our `disable` call, then re-enable
// them. Otherwise, keep them disabled
if status.gie() {
unsafe { enable() }
}
r
}