| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
/* |
| 4 |
Adapted from https://github.com/rust-embedded/msp430. |
| 5 |
|
| 6 |
See also src/imp/msp430.rs. |
| 7 |
|
| 8 |
Refs: |
| 9 |
- MSP430x5xx and MSP430x6xx Family User's Guide, Rev. Q |
| 10 |
https://www.ti.com/lit/ug/slau208q/slau208q.pdf |
| 11 |
|
| 12 |
See tests/asm-test/asm/portable-atomic for generated assembly. |
| 13 |
*/ |
| 14 |
|
| 15 |
#[cfg(not(portable_atomic_no_asm))] |
| 16 |
use core::arch::asm; |
| 17 |
|
| 18 |
#[cfg_attr( |
| 19 |
portable_atomic_no_cfg_target_has_atomic, |
| 20 |
cfg(any(test, portable_atomic_no_atomic_cas)) |
| 21 |
)] |
| 22 |
#[cfg_attr( |
| 23 |
not(portable_atomic_no_cfg_target_has_atomic), |
| 24 |
cfg(any(test, not(target_has_atomic = "ptr"))) |
| 25 |
)] |
| 26 |
pub(super) use super::super::msp430 as atomic; |
| 27 |
|
| 28 |
pub(crate) type State = u16; |
| 29 |
|
| 30 |
/// Disables interrupts and returns the previous interrupt state. |
| 31 |
#[inline(always)] |
| 32 |
pub(crate) fn disable() -> State { |
| 33 |
let sr: State; |
| 34 |
// SAFETY: reading the status register and disabling interrupts are safe. |
| 35 |
// (see module-level comments of interrupt/mod.rs on the safety of using privileged instructions) |
| 36 |
// |
| 37 |
// See "NOTE: Enable and Disable Interrupt" of User's Guide for NOP: https://www.ti.com/lit/ug/slau208q/slau208q.pdf#page=60 |
| 38 |
unsafe { |
| 39 |
#[cfg(not(portable_atomic_no_asm))] |
| 40 |
asm!( |
| 41 |
"mov r2, {sr}", // sr = SR |
| 42 |
"dint {{ nop", // SR.GIE = 0 |
| 43 |
sr = out(reg) sr, |
| 44 |
// Do not use `nomem` and `readonly` because prevent subsequent memory accesses from being reordered before interrupts are disabled. |
| 45 |
// Do not use `preserves_flags` because DINT modifies the GIE (global interrupt enable) bit of the status register. |
| 46 |
options(nostack), |
| 47 |
); |
| 48 |
#[cfg(portable_atomic_no_asm)] |
| 49 |
{ |
| 50 |
llvm_asm!("mov r2, $0" : "=r"(sr) ::: "volatile"); |
| 51 |
llvm_asm!("dint { nop" ::: "memory" : "volatile"); |
| 52 |
} |
| 53 |
} |
| 54 |
sr |
| 55 |
} |
| 56 |
|
| 57 |
/// Restores the previous interrupt state. |
| 58 |
/// |
| 59 |
/// # Safety |
| 60 |
/// |
| 61 |
/// The state must be the one retrieved by the previous `disable`. |
| 62 |
#[inline(always)] |
| 63 |
pub(crate) unsafe fn restore(prev_sr: State) { |
| 64 |
// SAFETY: the caller must guarantee that the state was retrieved by the previous `disable`, |
| 65 |
// |
| 66 |
// This clobbers the entire status register, but we never explicitly modify |
| 67 |
// flags within a critical session, and the only flags that may be changed |
| 68 |
// within a critical session are the arithmetic flags that are changed as |
| 69 |
// a side effect of arithmetic operations, etc., which LLVM recognizes unless |
| 70 |
// preserves_flags is set, so it is safe to clobber them here. |
| 71 |
// See also the discussion at https://github.com/taiki-e/portable-atomic/pull/40. |
| 72 |
// |
| 73 |
// See "NOTE: Enable and Disable Interrupt" of User's Guide for NOP: https://www.ti.com/lit/ug/slau208q/slau208q.pdf#page=60 |
| 74 |
unsafe { |
| 75 |
#[cfg(not(portable_atomic_no_asm))] |
| 76 |
asm!( |
| 77 |
"nop {{ mov {prev_sr}, r2 {{ nop", // SR = prev_sr |
| 78 |
prev_sr = in(reg) prev_sr, |
| 79 |
// Do not use `nomem` and `readonly` because prevent preceding memory accesses from being reordered after interrupts are enabled. |
| 80 |
// Do not use `preserves_flags` because MOV modifies the status register. |
| 81 |
options(nostack), |
| 82 |
); |
| 83 |
#[cfg(portable_atomic_no_asm)] |
| 84 |
llvm_asm!("nop { mov $0, r2 { nop" :: "r"(prev_sr) : "memory" : "volatile"); |
| 85 |
} |
| 86 |
} |