Skip to content

Commit 6194db6

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

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/asm.rs

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

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

0 commit comments

Comments
 (0)