Skip to content

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

Merged
merged 1 commit into from
Apr 30, 2025
Merged

Conversation

LuigiPiucco
Copy link
Contributor

Fix #175.

The check with u8::MAX is there because some MCUs (the really small ones whose data space address fits in a u8) may not have SPH. Because extern statics are not as optimizable as consts, the resulting binary can't eliminate the opposing branch, something that could be worked on.

@Rahix
Copy link
Owner

Rahix commented Apr 29, 2025

I was about to try testing this on real hardware, then I noticed we first have to move avr-hal to the new avr-device after #157 ... (For reference, I had already started on this in Rahix/avr-hal#545 a while ago)

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!

Copy link
Owner

@Rahix Rahix left a 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)

@Rahix Rahix merged commit 66a0b80 into Rahix:main Apr 30, 2025
2 checks passed
@LuigiPiucco LuigiPiucco deleted the stack-size branch May 3, 2025 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add function to get stack size
3 participants