diff options
| author | 9names <[email protected]> | 2025-09-21 13:27:54 +1000 |
|---|---|---|
| committer | 9names <[email protected]> | 2025-09-21 19:12:38 +1000 |
| commit | c69d17e5fb11852ba14ddc3369a0c2dbfff4e29d (patch) | |
| tree | a3e8c71ffbba9452b7b11cd8664f65b3fb1db271 /examples/rp235x/src/bin | |
| parent | 9bb77f36f9697dd301f6c68a649030e2b5487782 (diff) | |
examples/rp235x: Add multicore stack overflow example
Diffstat (limited to 'examples/rp235x/src/bin')
| -rw-r--r-- | examples/rp235x/src/bin/multicore_stack_overflow.rs | 72 |
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 | |||
| 6 | use defmt::*; | ||
| 7 | use embassy_executor::Executor; | ||
| 8 | use embassy_rp::gpio::{Level, Output}; | ||
| 9 | use embassy_rp::multicore::{spawn_core1, Stack}; | ||
| 10 | use embassy_time::Timer; | ||
| 11 | use static_cell::StaticCell; | ||
| 12 | use {defmt_rtt as _, panic_probe as _}; | ||
| 13 | |||
| 14 | const CORE1_STACK_LENGTH: usize = 4096; | ||
| 15 | |||
| 16 | static mut CORE1_STACK: Stack<CORE1_STACK_LENGTH> = Stack::new(); | ||
| 17 | static EXECUTOR0: StaticCell<Executor> = StaticCell::new(); | ||
| 18 | static EXECUTOR1: StaticCell<Executor> = StaticCell::new(); | ||
| 19 | |||
| 20 | #[cortex_m_rt::entry] | ||
| 21 | fn 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] | ||
| 39 | async 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 | |||
| 50 | fn 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] | ||
| 63 | async 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 | } | ||
