xtensa_lx/lib.rs
1//! Low-level access to Xtensa LX processors and peripherals.
2//!
3//! ## Minimum Supported Rust Version (MSRV)
4//!
5//! This crate is guaranteed to compile on stable Rust 1.65 and up. It might
6//! compile with older versions but that may change in any new patch release.
7//!
8//! ## Feature Flags
9#![doc = document_features::document_features!()]
10#![doc(html_logo_url = "https://avatars.githubusercontent.com/u/46717278")]
11#![allow(asm_sub_register)]
12#![feature(asm_experimental_arch)]
13#![no_std]
14
15use core::arch::asm;
16
17pub mod interrupt;
18pub mod timer;
19
20#[macro_use]
21mod macros;
22
23const DCR_ENABLEOCD: u32 = 0x01;
24const XDM_OCD_DCR_SET: u32 = 0x10200C;
25
26/// Move the vector base
27///
28/// # Safety
29///
30/// *This is highly unsafe!*
31/// It should be used with care, `base` MUST be a valid pointer
32#[inline(always)]
33pub unsafe fn set_vecbase(base: *const u32) {
34 asm!("wsr.vecbase {0}", in(reg) base, options(nostack));
35}
36
37/// Get the core stack pointer
38#[inline(always)]
39pub fn get_stack_pointer() -> *const u32 {
40 let x: *const u32;
41 unsafe { asm!("mov {0}, sp", out(reg) x, options(nostack)) };
42 x
43}
44
45/// Set the core stack pointer
46///
47/// `stack` pointer to the non-inclusive end of the stack (must be 16-byte
48/// aligned)
49///
50/// # Safety
51///
52/// *This is highly unsafe!*
53/// It should be used with care at e.g. program start or when building a task
54/// scheduler
55#[inline(always)]
56pub unsafe fn set_stack_pointer(stack: *mut u32) {
57 // FIXME: this function relies on it getting inlined - if it doesn't inline it
58 // will try and return from this function using the adress in `a0` which has
59 // just been trashed... According to https://nnethercote.github.io/perf-book/inlining.html:
60 // "Inline attributes do not guarantee that a function is inlined or not
61 // inlined, but in practice, #[inline(always)] will cause inlining in all but
62 // the most exceptional cases." Is this good enough? Should we rewrite these
63 // as a macro to guarentee inlining?
64
65 // NOTE: modification of the `sp` & `a0` is not typically allowed inside inline
66 // asm!, but because we *need* to modify it we can do so by ommiting it from
67 // the clobber
68 asm!(
69 "movi a0, 0", // trash return register
70 "mov sp, {0}", // move stack pointer
71 in(reg) stack, options(nostack)
72 );
73}
74
75/// Get the core current program counter
76#[inline(always)]
77pub fn get_program_counter() -> *const u32 {
78 let x: *const u32;
79 unsafe {
80 asm!("
81 mov {1}, {2}
82 call0 2f
83 .align 4
84 2:
85 mov {0}, {2}
86 mov {2}, {1}
87 ", out(reg) x, out(reg) _, out(reg) _, options(nostack))
88 };
89 x
90}
91
92/// Get the id of the current core
93#[inline(always)]
94pub fn get_processor_id() -> u32 {
95 let mut x: u32;
96 unsafe { asm!("rsr.prid {0}", out(reg) x, options(nostack)) };
97 x
98}
99
100/// Returns true if a debugger is attached
101#[inline(always)]
102pub fn is_debugger_attached() -> bool {
103 let mut x: u32;
104 unsafe { asm!("rer {0}, {1}", out(reg) x, in(reg) XDM_OCD_DCR_SET, options(nostack)) };
105 (x & DCR_ENABLEOCD) != 0
106}
107
108/// Insert debug breakpoint
109#[inline(always)]
110pub fn debug_break() {
111 unsafe { asm!("break 1, 15", options(nostack)) };
112}
113
114/// Used to reexport items for use in macros. Do not use directly.
115/// Not covered by semver guarantees.
116#[doc(hidden)]
117pub mod _export {
118 pub use critical_section;
119}