From 5475bb6cf9d6b5f07aa68f99dcef8a52d83d3fb3 Mon Sep 17 00:00:00 2001 From: Sofia Date: Sun, 17 May 2026 03:34:32 +0300 Subject: [PATCH] Communicate between threads --- src/main.rs | 66 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index f259684..2679ae9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,13 @@ )] #![deny(clippy::large_stack_frames)] -use core::mem::MaybeUninit; +use core::{ + cell::{Cell, RefCell}, + mem::MaybeUninit, +}; -use alloc::boxed::Box; +use alloc::{borrow::ToOwned, boxed::Box, string::String, sync::Arc, vec::Vec}; +use critical_section::{CriticalSection, Mutex}; use embassy_executor::Spawner; use esp_hal::{ clock::{ClockConfig, CpuClock}, @@ -59,23 +63,6 @@ fn main() -> ! { let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); let peripherals = esp_hal::init(config); - static mut THREAD_2_STACK: Stack<1024> = esp_hal::system::Stack { - mem: MaybeUninit::new([0u8; 1024]), - }; - - let timg0 = TimerGroup::new(peripherals.TIMG0); - let software_interrupt = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); - esp_rtos::start(timg0.timer0, software_interrupt.software_interrupt0); - esp_rtos::start_second_core( - peripherals.CPU_CTRL, - software_interrupt.software_interrupt1, - unsafe { - #[allow(static_mut_refs)] - &mut THREAD_2_STACK - }, - || thread_2_main(), - ); - // The following pins are used to bootstrap the chip. They are available // for use, but check the datasheet of the module for more information on them. // - GPIO0 @@ -170,19 +157,52 @@ fn main() -> ! { // let input_6 = Input::new(peripherals.GPIO26, pull_up_cfg); // let input_7 = Input::new(peripherals.GPIO27, pull_up_cfg); + let messages = Arc::new(Mutex::new(RefCell::new(Vec::::new()))); + + static mut THREAD_2_STACK: Stack<1024> = esp_hal::system::Stack { + mem: MaybeUninit::new([0u8; 1024]), + }; + + let timg0 = TimerGroup::new(peripherals.TIMG0); + let software_interrupt = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); + esp_rtos::start(timg0.timer0, software_interrupt.software_interrupt0); + esp_rtos::start_second_core( + peripherals.CPU_CTRL, + software_interrupt.software_interrupt1, + unsafe { + #[allow(static_mut_refs)] + &mut THREAD_2_STACK + }, + { + let messages = messages.clone(); + || thread_2_main(messages) + }, + ); + let mut test_delay = Delay::new(); loop { - state_mgr.update(); - state_mgr.draw(); + critical_section::with(|cs| { + let mut borrowed = messages.borrow(cs).borrow_mut(); + for message in borrowed.iter() { + log::info!("Received: {}", message); + } + borrowed.clear(); + }) + // state_mgr.update(); + // state_mgr.draw(); } // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.1.0/examples } -fn thread_2_main() { +fn thread_2_main(messages: Arc>>>) { let delay = Delay::new(); loop { delay.delay_millis(1000); - log::info!("Hello there!") + log::info!("Sent message"); + critical_section::with(|cs| { + let mut borrowed = messages.borrow(cs).borrow_mut(); + borrowed.push("Message!".to_owned()); + }) } }