From a87830d0465d560ac613053c2589938ced9ad937 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 14 Sep 2024 11:46:58 +0000 Subject: [PATCH 1/4] Use embassy-executor in rp2040 examples --- rp2040-hal-examples/Cargo.toml | 5 ++++- rp2040-hal-examples/src/bin/i2c_async.rs | 11 ++++++----- rp2040-hal-examples/src/bin/i2c_async_cancelled.rs | 11 ++++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/rp2040-hal-examples/Cargo.toml b/rp2040-hal-examples/Cargo.toml index 7529eb2df..3660f9b82 100644 --- a/rp2040-hal-examples/Cargo.toml +++ b/rp2040-hal-examples/Cargo.toml @@ -29,7 +29,6 @@ fugit = "0.3.6" futures = {version = "0.3.30", default-features = false, features = ["async-await"]} hd44780-driver = "0.4.0" nb = "1.0" -nostd_async = {version = "0.6.1", features = ["cortex_m"]} panic-halt = "0.2.0" panic-probe = {version = "0.3.1", features = ["print-defmt"]} pio = "0.2.0" @@ -39,3 +38,7 @@ pio-proc = "0.2.0" portable-atomic = {version = "1.7.0", features = ["critical-section"]} rp2040-boot2 = "0.3.0" rp2040-hal = {path = "../rp2040-hal", version = "0.10.0", features = ["binary-info", "critical-section-impl", "rt", "defmt"]} +static_cell = "2.1.0" + +[target.'cfg( target_arch = "arm" )'.dependencies] +embassy-executor = {version = "0.6", features = ["arch-cortex-m", "executor-thread"]} diff --git a/rp2040-hal-examples/src/bin/i2c_async.rs b/rp2040-hal-examples/src/bin/i2c_async.rs index 79c4fd1ed..bd9ec051e 100644 --- a/rp2040-hal-examples/src/bin/i2c_async.rs +++ b/rp2040-hal-examples/src/bin/i2c_async.rs @@ -10,6 +10,7 @@ #![no_std] #![no_main] +use embassy_executor::Executor; // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_halt as _; @@ -32,6 +33,7 @@ use hal::{ pac::{self, interrupt}, Clock, }; +use static_cell::StaticCell; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. @@ -54,6 +56,7 @@ unsafe fn I2C0_IRQ() { /// The function configures the RP2040 peripherals, then performs a single I²C /// write to a fixed address. +#[embassy_executor::task] async fn demo() { let mut pac = pac::Peripherals::take().unwrap(); @@ -118,9 +121,7 @@ async fn demo() { /// Entry point to our bare-metal application. #[rp2040_hal::entry] fn main() -> ! { - let runtime = nostd_async::Runtime::new(); - let mut task = nostd_async::Task::new(demo()); - let handle = task.spawn(&runtime); - handle.join(); - unreachable!() + static EXECUTOR: StaticCell = StaticCell::new(); + let executor = EXECUTOR.init(Executor::new()); + executor.run(|spawner| spawner.spawn(demo()).unwrap()); } diff --git a/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs b/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs index 17092aa7d..6a4e55ef0 100644 --- a/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs +++ b/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs @@ -16,6 +16,7 @@ use core::task::Poll; // be linked) //use panic_halt as _; +use embassy_executor::Executor; use embedded_hal_async::i2c::I2c; use futures::FutureExt; @@ -39,6 +40,7 @@ use hal::{ use defmt_rtt as _; use panic_probe as _; +use static_cell::StaticCell; /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. @@ -60,6 +62,7 @@ unsafe fn I2C0_IRQ() { /// The function configures the RP2040 peripherals, then performs a single I²C /// write to a fixed address. +#[embassy_executor::task] async fn demo() { let mut pac = pac::Peripherals::take().unwrap(); @@ -142,9 +145,7 @@ async fn demo() { /// Entry point to our bare-metal application. #[rp2040_hal::entry] fn main() -> ! { - let runtime = nostd_async::Runtime::new(); - let mut task = nostd_async::Task::new(demo()); - let handle = task.spawn(&runtime); - handle.join(); - unreachable!() + static EXECUTOR: StaticCell = StaticCell::new(); + let executor = EXECUTOR.init(Executor::new()); + executor.run(|spawner| spawner.spawn(demo()).unwrap()); } From 9377c4a8e7df1fa520fed7b02152b54633d8cee3 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 14 Sep 2024 11:54:26 +0000 Subject: [PATCH 2/4] Use embassy-executor in rp235x examples --- rp235x-hal-examples/Cargo.toml | 8 +++++++- rp235x-hal-examples/src/bin/i2c_async.rs | 11 ++++++----- rp235x-hal-examples/src/bin/i2c_async_cancelled.rs | 11 ++++++----- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/rp235x-hal-examples/Cargo.toml b/rp235x-hal-examples/Cargo.toml index bb9f25f7e..baa8be660 100644 --- a/rp235x-hal-examples/Cargo.toml +++ b/rp235x-hal-examples/Cargo.toml @@ -29,10 +29,16 @@ futures = {version = "0.3.30", default-features = false, features = ["async-awai hd44780-driver = "0.4.0" heapless = "0.8.0" nb = "1.0" -nostd_async = {version = "0.6.1", features = ["cortex_m"]} panic-halt = "0.2.0" pio = "0.2.0" pio-proc = "0.2.0" rp235x-hal = {path = "../rp235x-hal", version = "0.2.0", features = ["binary-info", "critical-section-impl", "rt", "defmt"]} usb-device = "0.3.2" usbd-serial = "0.2.2" +static_cell = "2.1.0" + +[target.'cfg( target_arch = "arm" )'.dependencies] +embassy-executor = {version = "0.6", features = ["arch-cortex-m", "executor-thread"]} + +[target.'cfg( target_arch = "riscv32" )'.dependencies] +embassy-executor = {version = "0.6", features = ["arch-riscv32", "executor-thread"]} diff --git a/rp235x-hal-examples/src/bin/i2c_async.rs b/rp235x-hal-examples/src/bin/i2c_async.rs index 8b104bc99..c8f43975a 100644 --- a/rp235x-hal-examples/src/bin/i2c_async.rs +++ b/rp235x-hal-examples/src/bin/i2c_async.rs @@ -10,6 +10,7 @@ #![no_std] #![no_main] +use embassy_executor::Executor; // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_halt as _; @@ -29,6 +30,7 @@ use hal::{ // Import required types & traits. use embedded_hal_async::i2c::I2c; +use static_cell::StaticCell; /// Tell the Boot ROM about our application #[link_section = ".start_block"] @@ -48,6 +50,7 @@ unsafe fn I2C0_IRQ() { /// The function configures the RP235x peripherals, then performs a single I²C /// write to a fixed address. +#[embassy_executor::task] async fn demo() { let mut pac = hal::pac::Peripherals::take().unwrap(); @@ -112,11 +115,9 @@ async fn demo() { /// Entry point to our bare-metal application. #[hal::entry] fn main() -> ! { - let runtime = nostd_async::Runtime::new(); - let mut task = nostd_async::Task::new(demo()); - let handle = task.spawn(&runtime); - handle.join(); - unreachable!() + static EXECUTOR: StaticCell = StaticCell::new(); + let executor = EXECUTOR.init(Executor::new()); + executor.run(|spawner| spawner.spawn(demo()).unwrap()); } /// Program metadata for `picotool info` diff --git a/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs b/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs index f9b069389..76b276d59 100644 --- a/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs +++ b/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs @@ -15,6 +15,7 @@ use panic_halt as _; use core::task::Poll; +use embassy_executor::Executor; use embedded_hal_async::i2c::I2c; use futures::FutureExt; @@ -32,6 +33,7 @@ use hal::{ }; use defmt_rtt as _; +use static_cell::StaticCell; /// Tell the Boot ROM about our application #[link_section = ".start_block"] @@ -50,6 +52,7 @@ unsafe fn I2C0_IRQ() { /// The function configures the RP235x peripherals, then performs a single I²C /// write to a fixed address. +#[embassy_executor::task] async fn demo() { let mut pac = hal::pac::Peripherals::take().unwrap(); @@ -132,11 +135,9 @@ async fn demo() { /// Entry point to our bare-metal application. #[hal::entry] fn main() -> ! { - let runtime = nostd_async::Runtime::new(); - let mut task = nostd_async::Task::new(demo()); - let handle = task.spawn(&runtime); - handle.join(); - unreachable!() + static EXECUTOR: StaticCell = StaticCell::new(); + let executor = EXECUTOR.init(Executor::new()); + executor.run(|spawner| spawner.spawn(demo()).unwrap()); } /// Program metadata for `picotool info` From 1675e2ca5fd7cd52a50784c34d70b1cbbc8d3b6d Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 14 Sep 2024 13:01:55 +0000 Subject: [PATCH 3/4] Downgrade to embassy-executor 0.5 to avoid MSRV issues (We can also consider bumping our MSRV, I just didn't want to mix two different matters in a single merge request.) --- rp2040-hal-examples/Cargo.toml | 2 +- rp235x-hal-examples/Cargo.toml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rp2040-hal-examples/Cargo.toml b/rp2040-hal-examples/Cargo.toml index 3660f9b82..6e524fab9 100644 --- a/rp2040-hal-examples/Cargo.toml +++ b/rp2040-hal-examples/Cargo.toml @@ -41,4 +41,4 @@ rp2040-hal = {path = "../rp2040-hal", version = "0.10.0", features = ["binary-in static_cell = "2.1.0" [target.'cfg( target_arch = "arm" )'.dependencies] -embassy-executor = {version = "0.6", features = ["arch-cortex-m", "executor-thread"]} +embassy-executor = {version = "0.5", features = ["arch-cortex-m", "executor-thread"]} diff --git a/rp235x-hal-examples/Cargo.toml b/rp235x-hal-examples/Cargo.toml index baa8be660..f9a1295cb 100644 --- a/rp235x-hal-examples/Cargo.toml +++ b/rp235x-hal-examples/Cargo.toml @@ -38,7 +38,7 @@ usbd-serial = "0.2.2" static_cell = "2.1.0" [target.'cfg( target_arch = "arm" )'.dependencies] -embassy-executor = {version = "0.6", features = ["arch-cortex-m", "executor-thread"]} +embassy-executor = {version = "0.5", features = ["arch-cortex-m", "executor-thread"]} [target.'cfg( target_arch = "riscv32" )'.dependencies] -embassy-executor = {version = "0.6", features = ["arch-riscv32", "executor-thread"]} +embassy-executor = {version = "0.5", features = ["arch-riscv32", "executor-thread"]} From 2289a5b9da90ad28aa2a5bf75e2bb06ed66e4ac8 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 14 Sep 2024 20:46:07 +0000 Subject: [PATCH 4/4] Improve location of new imports --- rp2040-hal-examples/src/bin/i2c_async.rs | 2 +- rp2040-hal-examples/src/bin/i2c_async_cancelled.rs | 2 +- rp235x-hal-examples/src/bin/i2c_async.rs | 2 +- rp235x-hal-examples/src/bin/i2c_async_cancelled.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/rp2040-hal-examples/src/bin/i2c_async.rs b/rp2040-hal-examples/src/bin/i2c_async.rs index bd9ec051e..9e490e786 100644 --- a/rp2040-hal-examples/src/bin/i2c_async.rs +++ b/rp2040-hal-examples/src/bin/i2c_async.rs @@ -10,7 +10,6 @@ #![no_std] #![no_main] -use embassy_executor::Executor; // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_halt as _; @@ -27,6 +26,7 @@ use hal::{ }; // Import required types & traits. +use embassy_executor::Executor; use embedded_hal_async::i2c::I2c; use hal::{ gpio::{FunctionI2C, Pin, PullUp}, diff --git a/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs b/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs index 6a4e55ef0..90ac5a065 100644 --- a/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs +++ b/rp2040-hal-examples/src/bin/i2c_async_cancelled.rs @@ -16,7 +16,6 @@ use core::task::Poll; // be linked) //use panic_halt as _; -use embassy_executor::Executor; use embedded_hal_async::i2c::I2c; use futures::FutureExt; @@ -39,6 +38,7 @@ use hal::{ }; use defmt_rtt as _; +use embassy_executor::Executor; use panic_probe as _; use static_cell::StaticCell; diff --git a/rp235x-hal-examples/src/bin/i2c_async.rs b/rp235x-hal-examples/src/bin/i2c_async.rs index c8f43975a..291cc1439 100644 --- a/rp235x-hal-examples/src/bin/i2c_async.rs +++ b/rp235x-hal-examples/src/bin/i2c_async.rs @@ -10,7 +10,6 @@ #![no_std] #![no_main] -use embassy_executor::Executor; // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_halt as _; @@ -29,6 +28,7 @@ use hal::{ }; // Import required types & traits. +use embassy_executor::Executor; use embedded_hal_async::i2c::I2c; use static_cell::StaticCell; diff --git a/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs b/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs index 76b276d59..f85240e9c 100644 --- a/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs +++ b/rp235x-hal-examples/src/bin/i2c_async_cancelled.rs @@ -15,7 +15,6 @@ use panic_halt as _; use core::task::Poll; -use embassy_executor::Executor; use embedded_hal_async::i2c::I2c; use futures::FutureExt; @@ -33,6 +32,7 @@ use hal::{ }; use defmt_rtt as _; +use embassy_executor::Executor; use static_cell::StaticCell; /// Tell the Boot ROM about our application