Skip to content

Commit a409f40

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

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/asm.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,51 @@ 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+
50+
cfg_if::cfg_if! {
51+
if #[cfg(target_arch = "avr")] {
52+
unsafe {
53+
let data_region_end = __DATA_REGION_ORIGIN__ + __DATA_REGION_LENGTH__;
54+
let sp: usize;
55+
if data_region_end > u8::MAX as usize {
56+
// Here the stack pointer is 2 bytes.
57+
58+
let spl: u8;
59+
let sph: u8;
60+
core::arch::asm!(
61+
"in {}, 0x3d",
62+
"in {}, 0x3e",
63+
out(reg) spl,
64+
out(reg) sph,
65+
options(nostack, nomem, pure),
66+
);
67+
sp = usize::from_le_bytes([spl, sph]);
68+
} else {
69+
// Here the stack pointer is 1 byte.
70+
71+
let spl: u8;
72+
core::arch::asm!(
73+
"in {}, 0x3d",
74+
out(reg) spl,
75+
options(nostack, nomem, pure),
76+
);
77+
sp = spl as usize;
78+
}
79+
data_region_end - sp
80+
}
81+
} else {
82+
unimplemented!()
83+
}
84+
}
85+
}
86+
4287
/// Blocks the program for at least `cycles` CPU cycles.
4388
///
4489
/// This is intended for very simple delays in low-level drivers, but it

0 commit comments

Comments
 (0)