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 unsafe {
35 asm!("wsr.vecbase {0}", in(reg) base, options(nostack));
36 }
37}
38
39/// Get the core stack pointer
40#[inline(always)]
41pub fn get_stack_pointer() -> *const u32 {
42 let x: *const u32;
43 unsafe { asm!("mov {0}, sp", out(reg) x, options(nostack)) };
44 x
45}
46
47/// Set the core stack pointer
48///
49/// `stack` pointer to the non-inclusive end of the stack (must be 16-byte
50/// aligned)
51///
52/// # Safety
53///
54/// *This is highly unsafe!*
55/// It should be used with care at e.g. program start or when building a task
56/// scheduler
57#[inline(always)]
58pub unsafe fn set_stack_pointer(stack: *mut u32) {
59 unsafe {
60 // FIXME: this function relies on it getting inlined - if it doesn't inline it
61 // will try and return from this function using the adress in `a0` which has
62 // just been trashed... According to https://nnethercote.github.io/perf-book/inlining.html:
63 // "Inline attributes do not guarantee that a function is inlined or not
64 // inlined, but in practice, #[inline(always)] will cause inlining in all but
65 // the most exceptional cases." Is this good enough? Should we rewrite these
66 // as a macro to guarentee inlining?
67
68 // NOTE: modification of the `sp` & `a0` is not typically allowed inside inline
69 // asm!, but because we *need* to modify it we can do so by ommiting it from
70 // the clobber
71 asm!(
72 "movi a0, 0", // trash return register
73 "mov sp, {0}", // move stack pointer
74 in(reg) stack, options(nostack)
75 );
76 }
77}
78
79/// Get the core current program counter
80#[inline(always)]
81pub fn get_program_counter() -> *const u32 {
82 let x: *const u32;
83 unsafe {
84 asm!("
85 mov {1}, {2}
86 call0 2f
87 .align 4
88 2:
89 mov {0}, {2}
90 mov {2}, {1}
91 ", out(reg) x, out(reg) _, out(reg) _, options(nostack))
92 };
93 x
94}
95
96/// Get the id of the current core
97#[inline(always)]
98pub fn get_processor_id() -> u32 {
99 let mut x: u32;
100 unsafe { asm!("rsr.prid {0}", out(reg) x, options(nostack)) };
101 x
102}
103
104/// Returns true if a debugger is attached
105#[inline(always)]
106pub fn is_debugger_attached() -> bool {
107 let mut x: u32;
108 unsafe { asm!("rer {0}, {1}", out(reg) x, in(reg) XDM_OCD_DCR_SET, options(nostack)) };
109 (x & DCR_ENABLEOCD) != 0
110}
111
112/// Insert debug breakpoint
113#[inline(always)]
114pub fn debug_break() {
115 unsafe { asm!("break 1, 15", options(nostack)) };
116}
117
118/// Used to reexport items for use in macros. Do not use directly.
119/// Not covered by semver guarantees.
120#[doc(hidden)]
121pub mod _export {
122 pub use critical_section;
123}