embedded-trainings-2020/beginner/apps/src/bin/stack_overflow.rs

33 lines
636 B
Rust
Raw Normal View History

2021-01-22 14:01:48 +00:00
#![no_main]
#![no_std]
use cortex_m::asm;
use cortex_m_rt::entry;
2021-04-12 09:51:44 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
2021-01-22 14:01:48 +00:00
#[entry]
fn main() -> ! {
// board initialization
dk::init().unwrap();
2021-06-04 15:04:23 +00:00
fib(100);
2021-01-22 14:01:48 +00:00
loop {
asm::bkpt();
}
}
#[inline(never)]
fn fib(n: u32) -> u32 {
// allocate and initialize one kilobyte of stack memory to provoke stack overflow
2021-06-04 15:04:23 +00:00
let use_stack = [0xAA; 1024];
defmt::info!("allocating [{}; 1024]; round #{}", use_stack[1023], n);
2021-01-22 14:01:48 +00:00
if n < 2 {
1
} else {
fib(n - 1) + fib(n - 2) // recursion
}
}