aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/rp235x/src/bin/multicore_stack_overflow.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/multicore_stack_overflow.rs b/examples/rp235x/src/bin/multicore_stack_overflow.rs
new file mode 100644
index 000000000..dba44aa23
--- /dev/null
+++ b/examples/rp235x/src/bin/multicore_stack_overflow.rs
@@ -0,0 +1,72 @@
1//! This example tests stack overflow handling on core1 of the RP235x chip.
2
3#![no_std]
4#![no_main]
5
6use defmt::*;
7use embassy_executor::Executor;
8use embassy_rp::gpio::{Level, Output};
9use embassy_rp::multicore::{spawn_core1, Stack};
10use embassy_time::Timer;
11use static_cell::StaticCell;
12use {defmt_rtt as _, panic_probe as _};
13
14const CORE1_STACK_LENGTH: usize = 4096;
15
16static mut CORE1_STACK: Stack<CORE1_STACK_LENGTH> = Stack::new();
17static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
18static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
19
20#[cortex_m_rt::entry]
21fn main() -> ! {
22 let p = embassy_rp::init(Default::default());
23 let led = Output::new(p.PIN_25, Level::Low);
24
25 spawn_core1(
26 p.CORE1,
27 unsafe { &mut *core::ptr::addr_of_mut!(CORE1_STACK) },
28 move || {
29 let executor1 = EXECUTOR1.init(Executor::new());
30 executor1.run(|spawner| spawner.spawn(unwrap!(core1_task())));
31 },
32 );
33
34 let executor0 = EXECUTOR0.init(Executor::new());
35 executor0.run(|spawner| spawner.spawn(unwrap!(core0_task(led))));
36}
37
38#[embassy_executor::task]
39async fn core0_task(mut led: Output<'static>) {
40 info!("Hello from core 0");
41 loop {
42 info!("core 0 still alive");
43 led.set_high();
44 Timer::after_millis(500).await;
45 led.set_low();
46 Timer::after_millis(500).await;
47 }
48}
49
50fn blow_my_stack() {
51 // Allocating an array a little larger than our stack should ensure a stack overflow when it is used.
52 let t = [0u8; CORE1_STACK_LENGTH + 64];
53
54 info!("Array initialised without error");
55 // We need to use black_box to otherwise the compiler is too smart and will optimise all of this away.
56 // We shouldn't get to this code - the initialisation above will touch the stack guard.
57 for ref i in t {
58 let _data = core::hint::black_box(*i) + 1;
59 }
60}
61
62#[embassy_executor::task]
63async fn core1_task() {
64 info!("Hello from core 1");
65
66 blow_my_stack();
67
68 loop {
69 info!("core 1 still alive");
70 Timer::after_millis(1000).await;
71 }
72}