Skip to content

Commit 97cba0d

Browse files
committed
asm: Add function to get stack size
Fix #175
1 parent a7ca8e8 commit 97cba0d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/asm.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,52 @@ pub fn wdr() {
3939
}
4040
}
4141

42+
/// Get the stack size in bytes, for debbuging.
43+
#[inline(always)]
44+
pub fn get_stack_size() -> usize {
45+
extern "C" {
46+
static __DATA_REGION_ORIGIN__: usize;
47+
static __DATA_REGION_LENGTH__: usize;
48+
}
49+
use core::ptr::addr_of;
50+
51+
cfg_if::cfg_if! {
52+
if #[cfg(target_arch = "avr")] {
53+
unsafe {
54+
let data_region_end = addr_of!(__DATA_REGION_ORIGIN__) as usize + addr_of!(__DATA_REGION_LENGTH__) as usize;
55+
let sp: usize;
56+
if data_region_end > u8::MAX as usize {
57+
// Here the stack pointer is 2 bytes.
58+
59+
let spl: u8;
60+
let sph: u8;
61+
core::arch::asm!(
62+
"in {}, 0x3d",
63+
"in {}, 0x3e",
64+
out(reg) spl,
65+
out(reg) sph,
66+
options(nostack, nomem, pure),
67+
);
68+
sp = usize::from_le_bytes([spl, sph]);
69+
} else {
70+
// Here the stack pointer is 1 byte.
71+
72+
let spl: u8;
73+
core::arch::asm!(
74+
"in {}, 0x3d",
75+
out(reg) spl,
76+
options(nostack, nomem, pure),
77+
);
78+
sp = spl as usize;
79+
}
80+
data_region_end - sp
81+
}
82+
} else {
83+
unimplemented!()
84+
}
85+
}
86+
}
87+
4288
/// Blocks the program for at least `cycles` CPU cycles.
4389
///
4490
/// This is intended for very simple delays in low-level drivers, but it

0 commit comments

Comments
 (0)