esp-hal/examples/src/bin/multicore.rs
Jesse Braham 4d2ab5bc8f
Create virtual peripherals for CPU control and radio clocks (#1428)
* Create a virtual `CPU_CTRL` peripheral for ESP32/S3

* Create a virtual `RADIO_CLK` peripherals for all devices with radios

* Use `PeripheralRef` for the `CpuControl` constructors

* Update `CHANGELOG.md`
2024-04-22 15:21:08 +00:00

57 lines
1.5 KiB
Rust

//! This shows how to spawn a task on the second core.
//!
//! The first core will print the value of a counter which is incremented by the
//! second core.
//% CHIPS: esp32 esp32s3
#![no_std]
#![no_main]
use core::{cell::RefCell, ptr::addr_of_mut};
use critical_section::Mutex;
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
cpu_control::{CpuControl, Stack},
delay::Delay,
peripherals::Peripherals,
prelude::*,
};
use esp_println::println;
static mut APP_CORE_STACK: Stack<8192> = Stack::new();
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let delay = Delay::new(&clocks);
let counter = Mutex::new(RefCell::new(0u32));
let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);
let _guard = cpu_control
.start_app_core(unsafe { &mut *addr_of_mut!(APP_CORE_STACK) }, || {
println!("Hello World - Core 1!");
loop {
delay.delay(500.millis());
critical_section::with(|cs| {
let mut val = counter.borrow_ref_mut(cs);
*val = val.wrapping_add(1);
});
}
})
.unwrap();
loop {
delay.delay(1.secs());
let count = critical_section::with(|cs| *counter.borrow_ref(cs));
println!("Hello World - Core 0! Counter is {}", count);
}
}