-
Notifications
You must be signed in to change notification settings - Fork 79
asm: Add function to get stack size #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I was about to try testing this on real hardware, then I noticed we first have to move I'll leave this PR open until that work has concluded, I don't want to merge it without evidence that it produces the expected results. Apart from that I think it's ready — thanks @LuigiPiucco for again picking up the work! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay, managed to test this and it seems to work as expected! 🎉
For reference, I used the following code:
#![no_std]
#![no_main]
use arduino_hal::prelude::*;
use panic_halt as _;
#[inline(never)]
fn stack_grower(level: usize, serial: &mut impl ufmt::uWrite) -> usize {
let stack_size = avr_device::asm::get_stack_size();
let next_size = if level < 16 {
stack_grower(level + 1, serial)
} else {
stack_size
};
let _ = ufmt::uwriteln!(
serial,
"Stack At Level {}: {} (diff {})\r",
level,
stack_size,
next_size - stack_size
);
stack_size
}
#[arduino_hal::entry]
fn main() -> ! {
let dp = arduino_hal::Peripherals::take().unwrap();
let pins = arduino_hal::pins!(dp);
let mut serial = arduino_hal::default_serial!(dp, pins, 57600);
ufmt::uwriteln!(&mut serial, "Hello from Arduino!\r").unwrap_infallible();
stack_grower(1, &mut serial);
loop {}
}
which produces the following output
Hello from Arduino!
Stack At Level 16: 133 (diff 0)
Stack At Level 15: 125 (diff 8)
Stack At Level 14: 117 (diff 8)
Stack At Level 13: 109 (diff 8)
Stack At Level 12: 101 (diff 8)
Stack At Level 11: 93 (diff 8)
Stack At Level 10: 85 (diff 8)
Stack At Level 9: 77 (diff 8)
Stack At Level 8: 69 (diff 8)
Stack At Level 7: 61 (diff 8)
Stack At Level 6: 53 (diff 8)
Stack At Level 5: 45 (diff 8)
Stack At Level 4: 37 (diff 8)
Stack At Level 3: 29 (diff 8)
Stack At Level 2: 21 (diff 8)
Stack At Level 1: 13 (diff 8)
Fix #175.
The check with
u8::MAX
is there because some MCUs (the really small ones whose data space address fits in au8
) may not haveSPH
. Becauseextern static
s are not as optimizable asconst
s, the resulting binary can't eliminate the opposing branch, something that could be worked on.