esp-hal/examples/src/bin/embassy_multicore.rs
Dániel Buga 571760884b
Update our time driver for upcoming embassy changes (#2701)
* Add a timer-driven task

* Spawn another timer

* Log

* foo

* Do not access current time on each schedule

* Update generic queue

* Minimize alarm priorities

* Point to github with patches

* Fix build without any queue impl selected

* Remove explicit generic-queue features

* Define cfgs, fix calling something uninitialized

* Clean up RefCell+generic queue

* Fix arg order

* Feature

* Fix single integrated-timer queue

* Fix next expiration when arming

* Add note

* Adjust impl to latest changes

* Local patch

* Refactor the refactor refactor

* Track the timer item's owner

* Clear owner on dequeue

* Clean up

* Point at the right branch

* Fix panic message

* Hide private function

* Remove integrated-timer references

* Point at upstream embassy

* Configure via esp-config

* Document, clean up, fix

* Hack

* Remove patches

* Update config separator, test the complex variant

* Undo esp-config hack

* Remove trouble example, update edge-net

* Update test deps

* Document

* Update bt-hci.

* Fix generic queue

* Fix panic message

* Fix UB

* Fix rebase

* Resolve UB

* Avoid mutable reference in interrupt executor

---------

Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
2025-01-14 16:17:58 +00:00

94 lines
2.8 KiB
Rust

//! This example shows how to spawn async tasks on the second core.
//!
//! The second core runs a simple LED blinking task, that is controlled by a
//! signal set by the task running on the other core.
//!
//! The following wiring is assumed:
//! - LED => GPIO0
//% CHIPS: esp32 esp32s3
//% FEATURES: embassy esp-hal/unstable
#![no_std]
#![no_main]
use core::ptr::addr_of_mut;
use embassy_executor::Spawner;
use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal};
use embassy_time::{Duration, Ticker};
use esp_backtrace as _;
use esp_hal::{
cpu_control::{CpuControl, Stack},
gpio::{Level, Output},
timer::{timg::TimerGroup, AnyTimer},
Cpu,
};
use esp_hal_embassy::Executor;
use esp_println::println;
use static_cell::StaticCell;
static mut APP_CORE_STACK: Stack<8192> = Stack::new();
/// Waits for a message that contains a duration, then flashes a led for that
/// duration of time.
#[embassy_executor::task]
async fn control_led(
mut led: Output<'static>,
control: &'static Signal<CriticalSectionRawMutex, bool>,
) {
println!("Starting control_led() on core {}", Cpu::current() as usize);
loop {
if control.wait().await {
esp_println::println!("LED on");
led.set_low();
} else {
esp_println::println!("LED off");
led.set_high();
}
}
}
#[esp_hal_embassy::main]
async fn main(_spawner: Spawner) {
let peripherals = esp_hal::init(esp_hal::Config::default());
let timg0 = TimerGroup::new(peripherals.TIMG0);
let timer0: AnyTimer = timg0.timer0.into();
let timer1: AnyTimer = timg0.timer1.into();
esp_hal_embassy::init([timer0, timer1]);
let mut cpu_control = CpuControl::new(peripherals.CPU_CTRL);
static LED_CTRL: StaticCell<Signal<CriticalSectionRawMutex, bool>> = StaticCell::new();
let led_ctrl_signal = &*LED_CTRL.init(Signal::new());
let led = Output::new(peripherals.GPIO0, Level::Low);
let _guard = cpu_control
.start_app_core(unsafe { &mut *addr_of_mut!(APP_CORE_STACK) }, move || {
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(control_led(led, led_ctrl_signal)).ok();
});
})
.unwrap();
// Sends periodic messages to control_led, enabling or disabling it.
println!(
"Starting enable_disable_led() on core {}",
Cpu::current() as usize
);
let mut ticker = Ticker::every(Duration::from_secs(1));
loop {
esp_println::println!("Sending LED on");
led_ctrl_signal.signal(true);
ticker.next().await;
esp_println::println!("Sending LED off");
led_ctrl_signal.signal(false);
ticker.next().await;
}
}