esp-hal/examples/src/bin/embassy_multiprio.rs
Jesse Braham 6a663f8b1a
Unify: Remove the chip-specific HAL packages, adapt esp-hal for direct use [1/?] (#1196)
* Remove the chip-specific HAL packages

* Update some doc comments which were missed, fix build script for ESP32/S2

* Refactor/update `esp-hal-procmacros`

* Create the `examples` package, add back all of the previously existing examples

* Use `xtask` automation package for checking examples and documentation in CI

* Combine the `rt-riscv` and `rt-xtensa` features into a single `rt` feature

* Bump MSRV to 1.76.0 (shocking!)

* Re-document the features for the HAL

* No need to re-export the `riscv` package like this

* Make clippy happy, improve CI clippy checks

* Update `CHANGELOG.md`

* riscv: zero bss

Co-authored-by: Björn Quentin <bjoernQ@users.noreply.github.com>

* Address a number of review comments

* Correct pin number in `hello_rgb` example for ESP32-C3

* Address the remaining review comments

* More small tweaks/improvements

* Fix RMT examples (#11)

* Fix RMT examples

* Remove logger-init

* Make I2S examples work on ESP32 (#12)

* Make I2S examples work on ESP32

* Remove logger init

* Fix the direct-vectoring examples on all RISCV chips (#10)

* Update GPIOs for some examples...

* Embassy timer example fixes (#13)

* Switch to generic queue instead of integrated for all examples

* changelog

* Update GPIO in another example, make `rustfmt` happy

* Fix ESP32-S2 PSRAM

* Avoid UART0 and SPI flash pins (#15)

* Avoid UART0 and SPI flash pins

* Fix spi_eh1_device_loopback for non-ESP32

* Update examples/src/bin/gpio_interrupt.rs

Co-authored-by: Juraj Sadel <jurajsadel@gmail.com>

---------

Co-authored-by: Juraj Sadel <jurajsadel@gmail.com>

---------

Co-authored-by: Scott Mabin <scott@mabez.dev>
Co-authored-by: Björn Quentin <bjoernQ@users.noreply.github.com>
Co-authored-by: bjoernQ <bjoern.quentin@mobile-j.de>
Co-authored-by: Juraj Sadel <jurajsadel@gmail.com>
2024-02-27 14:10:11 +00:00

97 lines
3.0 KiB
Rust

//! This example shows how to use the interrupt executors to prioritize some
//! tasks over others.
//!
//! The example creates three tasks:
//! - A low priority task that is not actually async, but simulates some
//! blocking work. This task will run for 5 seconds, then sleep for 5
//! seconds.
//! - A low priority task that is actually async, but will not be able to run
//! while the blocking task is running.
//! - A high priority task that prints something every second. The example
//! demonstrates that this task will continue to run even while the low
//! priority blocking task is running.
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
// FIXME: We should not need *two* executor features enabled here...
//% FEATURES: embassy embassy-executor-interrupt embassy-executor-thread embassy-time-timg0 embassy-generic-timers
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Spawner;
use embassy_time::{Duration, Instant, Ticker, Timer};
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
embassy::{
self,
executor::{FromCpu1, InterruptExecutor},
},
interrupt::Priority,
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
};
use esp_println::println;
static INT_EXECUTOR_0: InterruptExecutor<FromCpu1> = InterruptExecutor::new();
#[interrupt]
fn FROM_CPU_INTR1() {
unsafe { INT_EXECUTOR_0.on_interrupt() }
}
/// Periodically print something.
#[embassy_executor::task]
async fn high_prio() {
println!("Starting high_prio()");
let mut ticker = Ticker::every(Duration::from_secs(1));
loop {
println!("High priority ticks");
ticker.next().await;
}
}
/// Simulates some blocking (badly behaving) task.
#[embassy_executor::task]
async fn low_prio_blocking() {
println!("Starting low-priority task that isn't actually async");
loop {
println!("Doing some long and complicated calculation");
let start = Instant::now();
while start.elapsed() < Duration::from_secs(5) {}
println!("Calculation finished");
Timer::after(Duration::from_secs(5)).await;
}
}
/// A well-behaved, but starved async task.
#[embassy_executor::task]
async fn low_prio_async() {
println!("Starting low-priority task that will not be able to run while the blocking task is running");
let mut ticker = Ticker::every(Duration::from_secs(1));
loop {
println!("Low priority ticks");
ticker.next().await;
}
}
#[main]
async fn main(low_prio_spawner: Spawner) {
println!("Init!");
let peripherals = Peripherals::take();
let system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timg0 = TimerGroup::new(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);
let spawner = INT_EXECUTOR_0.start(Priority::Priority2);
spawner.must_spawn(high_prio());
println!("Spawning low-priority tasks");
low_prio_spawner.must_spawn(low_prio_async());
low_prio_spawner.must_spawn(low_prio_blocking());
}