cortexm/
support.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5use crate::scb;
6
7#[cfg(any(doc, all(target_arch = "arm", target_os = "none")))]
8#[inline(always)]
9/// NOP instruction
10pub fn nop() {
11    use core::arch::asm;
12    unsafe {
13        asm!("nop", options(nomem, nostack, preserves_flags));
14    }
15}
16
17#[cfg(any(doc, all(target_arch = "arm", target_os = "none")))]
18#[inline(always)]
19/// WFI instruction
20pub unsafe fn wfi() {
21    use core::arch::asm;
22    asm!("wfi", options(nomem, preserves_flags));
23}
24
25#[cfg(any(doc, all(target_arch = "arm", target_os = "none")))]
26pub unsafe fn atomic<F, R>(f: F) -> R
27where
28    F: FnOnce() -> R,
29{
30    use core::arch::asm;
31    // Set PRIMASK
32    asm!("cpsid i", options(nomem, nostack));
33
34    let res = f();
35
36    // Unset PRIMASK
37    asm!("cpsie i", options(nomem, nostack));
38    res
39}
40
41// Mock implementations for tests on Travis-CI.
42#[cfg(not(any(doc, all(target_arch = "arm", target_os = "none"))))]
43/// NOP instruction (mock)
44pub fn nop() {
45    unimplemented!()
46}
47
48#[cfg(not(any(doc, all(target_arch = "arm", target_os = "none"))))]
49/// WFI instruction (mock)
50pub unsafe fn wfi() {
51    unimplemented!()
52}
53
54#[cfg(not(any(doc, all(target_arch = "arm", target_os = "none"))))]
55pub unsafe fn atomic<F, R>(_f: F) -> R
56where
57    F: FnOnce() -> R,
58{
59    unimplemented!()
60}
61
62pub fn reset() -> ! {
63    unsafe {
64        scb::reset();
65    }
66    loop {
67        // This is required to avoid the empty loop clippy
68        // warning #[warn(clippy::empty_loop)]
69        nop();
70    }
71}