Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from esp-rs/asm-fixes
Browse files Browse the repository at this point in the history
asm fixups, avoid using a0, doc sp usage
  • Loading branch information
MabezDev authored Aug 11, 2021
2 parents c2074c4 + 5a27323 commit 1da638a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xtensa-lx"
version = "0.3.0"
version = "0.4.0"
description = "Low level access for xtensa lx processors and peripherals"
categories = ["embedded", "hardware-support", "no-std"]
keywords = ["xtensa", "lx", "register", "peripheral"]
Expand Down
26 changes: 16 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,34 @@ pub fn get_stack_pointer() -> *const u32 {
/// `stack` pointer to the non-inclusive end of the stack (must be 16-byte aligned)
#[inline(always)]
pub unsafe fn set_stack_pointer(stack: *mut u32) {
asm!("
movi a0, 0
mov sp, {0}
",
in(reg) stack, out("a0") _, options(nostack)
// FIXME: this function relies on it getting inlined - if it doesn't inline it will try and return from this function using the adress in `a0` which has just been trashed...
// According to https://nnethercote.github.io/perf-book/inlining.html:
// "Inline attributes do not guarantee that a function is inlined or not inlined, but in practice, #[inline(always)] will cause inlining in all but the most exceptional cases."
// Is this good enough? Should we rewrite these as a macro to guarentee inlining?


// NOTE: modification of the `sp` & `a0` is not typically allowed inside inline asm!,
// but because we *need* to modify it we can do so by ommiting it from the clobber
asm!(
"movi a0, 0", // trash return register
"mov sp, {0}", // move stack pointer
in(reg) stack, options(nostack)
);
}

/// Get the core current program counter
#[inline(always)]
pub fn get_program_counter() -> *const u32 {
let x: *const u32;
let _y: u32;
unsafe {
asm!("
mov {1}, a0
mov {1}, {2}
call0 1f
.align 4
1:
mov {0}, a0
mov a0, {1}
", out(reg) x, out(reg) _y, out("a0") _, options(nostack))
mov {0}, {2}
mov {2}, {1}
", out(reg) x, out(reg) _, out(reg) _, options(nostack))
};
x
}
Expand Down

0 comments on commit 1da638a

Please sign in to comment.