diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cf43880f89..5a2ba0e2c0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added sleep support for ESP32-C3 with timer and GPIO wakeups (#795) - Support for ULP-RISCV including Delay and GPIO (#840) - Add bare-bones SPI slave support, DMA only (#580) +- Embassy `#[main]` convenience macro ### Changed diff --git a/esp-hal-common/Cargo.toml b/esp-hal-common/Cargo.toml index 31531fe0a4d..fa0a95820fd 100644 --- a/esp-hal-common/Cargo.toml +++ b/esp-hal-common/Cargo.toml @@ -115,7 +115,7 @@ ufmt = ["ufmt-write"] async = ["embedded-hal-async", "eh1", "embassy-sync", "embassy-futures", "embedded-io-async"] # Embassy support -embassy = ["embassy-time"] +embassy = ["embassy-time","procmacros/embassy"] embassy-executor-interrupt = ["embassy", "embassy-executor"] embassy-executor-thread = ["embassy", "embassy-executor"] diff --git a/esp-hal-procmacros/Cargo.toml b/esp-hal-procmacros/Cargo.toml index 3bd4f3f55b9..1319dbf3200 100644 --- a/esp-hal-procmacros/Cargo.toml +++ b/esp-hal-procmacros/Cargo.toml @@ -40,3 +40,4 @@ esp32s3 = ["dep:object"] interrupt = [] rtc_slow = [] +embassy = [] diff --git a/esp-hal-procmacros/src/embassy_xtensa.rs b/esp-hal-procmacros/src/embassy_xtensa.rs new file mode 100644 index 00000000000..d7f0ebf7813 --- /dev/null +++ b/esp-hal-procmacros/src/embassy_xtensa.rs @@ -0,0 +1,191 @@ +use darling::ast::NestedMeta; +use syn::{ + parse::{Parse, ParseBuffer}, + punctuated::Punctuated, + Token, +}; + +pub(crate) struct Args { + pub(crate) meta: Vec, +} + +impl Parse for Args { + fn parse(input: &ParseBuffer) -> syn::Result { + let meta = Punctuated::::parse_terminated(input)?; + Ok(Args { + meta: meta.into_iter().collect(), + }) + } +} + +pub(crate) mod main { + use std::{cell::RefCell, fmt::Display, thread}; + + use darling::{export::NestedMeta, FromMeta}; + use proc_macro2::{Ident, Span, TokenStream}; + use proc_macro_crate::FoundCrate; + use quote::{quote, ToTokens}; + use syn::{ReturnType, Type}; + + #[derive(Debug, FromMeta)] + struct Args {} + + pub fn run( + args: &[NestedMeta], + f: syn::ItemFn, + main: TokenStream, + ) -> Result { + #[allow(unused_variables)] + let args = Args::from_list(args).map_err(|e| e.write_errors())?; + + let fargs = f.sig.inputs.clone(); + + let ctxt = Ctxt::new(); + + if f.sig.asyncness.is_none() { + ctxt.error_spanned_by(&f.sig, "main function must be async"); + } + if !f.sig.generics.params.is_empty() { + ctxt.error_spanned_by(&f.sig, "main function must not be generic"); + } + if !f.sig.generics.where_clause.is_none() { + ctxt.error_spanned_by(&f.sig, "main function must not have `where` clauses"); + } + if !f.sig.abi.is_none() { + ctxt.error_spanned_by(&f.sig, "main function must not have an ABI qualifier"); + } + if !f.sig.variadic.is_none() { + ctxt.error_spanned_by(&f.sig, "main function must not be variadic"); + } + match &f.sig.output { + ReturnType::Default => {} + ReturnType::Type(_, ty) => match &**ty { + Type::Tuple(tuple) if tuple.elems.is_empty() => {} + Type::Never(_) => {} + _ => ctxt.error_spanned_by( + &f.sig, + "main function must either not return a value, return `()` or return `!`", + ), + }, + } + + if fargs.len() != 1 { + ctxt.error_spanned_by(&f.sig, "main function must have 1 argument: the spawner."); + } + + ctxt.check()?; + + let f_body = f.block; + let out = &f.sig.output; + + let result = quote! { + #[::embassy_executor::task()] + async fn __embassy_main(#fargs) #out { + #f_body + } + + unsafe fn __make_static(t: &mut T) -> &'static mut T { + ::core::mem::transmute(t) + } + + #main + }; + + Ok(result) + } + + /// A type to collect errors together and format them. + /// + /// Dropping this object will cause a panic. It must be consumed using + /// `check`. + /// + /// References can be shared since this type uses run-time exclusive mut + /// checking. + #[derive(Default)] + pub struct Ctxt { + // The contents will be set to `None` during checking. This is so that checking can be + // enforced. + errors: RefCell>>, + } + + impl Ctxt { + /// Create a new context object. + /// + /// This object contains no errors, but will still trigger a panic if it + /// is not `check`ed. + pub fn new() -> Self { + Ctxt { + errors: RefCell::new(Some(Vec::new())), + } + } + + /// Add an error to the context object with a tokenenizable object. + /// + /// The object is used for spanning in error messages. + pub fn error_spanned_by(&self, obj: A, msg: T) { + self.errors + .borrow_mut() + .as_mut() + .unwrap() + // Curb monomorphization from generating too many identical methods. + .push(syn::Error::new_spanned(obj.into_token_stream(), msg)); + } + + /// Add one of Syn's parse errors. + #[allow(unused)] + pub fn syn_error(&self, err: syn::Error) { + self.errors.borrow_mut().as_mut().unwrap().push(err); + } + + /// Consume this object, producing a formatted error string if there are + /// errors. + pub fn check(self) -> Result<(), TokenStream> { + let errors = self.errors.borrow_mut().take().unwrap(); + match errors.len() { + 0 => Ok(()), + _ => Err(to_compile_errors(errors)), + } + } + } + + fn to_compile_errors(errors: Vec) -> proc_macro2::TokenStream { + let compile_errors = errors.iter().map(syn::Error::to_compile_error); + quote!(#(#compile_errors)*) + } + + impl Drop for Ctxt { + fn drop(&mut self) { + if !thread::panicking() && self.errors.borrow().is_some() { + panic!("forgot to check for errors"); + } + } + } + + pub fn main() -> TokenStream { + let (hal_crate, hal_crate_name) = crate::get_hal_crate(); + + let executor = match hal_crate { + Ok(FoundCrate::Itself) => { + quote!( #hal_crate_name::embassy::executor::Executor ) + } + Ok(FoundCrate::Name(ref name)) => { + let ident = Ident::new(&name, Span::call_site().into()); + quote!( #ident::embassy::executor::Executor ) + } + Err(_) => { + quote!(crate::embassy::executor::Executor) + } + }; + + quote! { + #[entry] + fn main() -> ! { + let mut executor = #executor::new(); + let executor = unsafe { __make_static(&mut executor) }; + executor.run(|spawner| { + spawner.must_spawn(__embassy_main(spawner)); + }) + } + } + } +} diff --git a/esp-hal-procmacros/src/lib.rs b/esp-hal-procmacros/src/lib.rs index dd7cecdd67f..10427a80bcf 100644 --- a/esp-hal-procmacros/src/lib.rs +++ b/esp-hal-procmacros/src/lib.rs @@ -49,6 +49,7 @@ use darling::{ast::NestedMeta, FromMeta}; use proc_macro::{self, Span, TokenStream}; +use proc_macro2::Ident; use proc_macro_error::{abort, proc_macro_error}; use quote::quote; #[cfg(feature = "interrupt")] @@ -57,7 +58,6 @@ use syn::{ spanned::Spanned, AttrStyle, Attribute, - Ident, ItemFn, Meta::Path, ReturnType, @@ -69,6 +69,12 @@ use syn::{ parse_macro_input, }; +#[cfg(all( + feature = "embassy", + any(feature = "esp32", feature = "esp32s2", feature = "esp32s3") +))] +mod embassy_xtensa; + #[derive(Debug, Default, FromMeta)] #[darling(default)] struct RamArgs { @@ -78,6 +84,45 @@ struct RamArgs { zeroed: bool, } +fn get_hal_crate() -> ( + Result, + proc_macro2::Ident, +) { + use proc_macro_crate::crate_name; + + #[cfg(feature = "esp32")] + let hal_crate = crate_name("esp32-hal"); + #[cfg(feature = "esp32s2")] + let hal_crate = crate_name("esp32s2-hal"); + #[cfg(feature = "esp32s3")] + let hal_crate = crate_name("esp32s3-hal"); + #[cfg(feature = "esp32c2")] + let hal_crate = crate_name("esp32c2-hal"); + #[cfg(feature = "esp32c3")] + let hal_crate = crate_name("esp32c3-hal"); + #[cfg(feature = "esp32c6")] + let hal_crate = crate_name("esp32c6-hal"); + #[cfg(feature = "esp32h2")] + let hal_crate = crate_name("esp32h2-hal"); + + #[cfg(feature = "esp32")] + let hal_crate_name = Ident::new("esp32_hal", Span::call_site().into()); + #[cfg(feature = "esp32s2")] + let hal_crate_name = Ident::new("esp32s2_hal", Span::call_site().into()); + #[cfg(feature = "esp32s3")] + let hal_crate_name = Ident::new("esp32s3_hal", Span::call_site().into()); + #[cfg(feature = "esp32c2")] + let hal_crate_name = Ident::new("esp32c2_hal", Span::call_site().into()); + #[cfg(feature = "esp32c3")] + let hal_crate_name = Ident::new("esp32c3_hal", Span::call_site().into()); + #[cfg(feature = "esp32c6")] + let hal_crate_name = Ident::new("esp32c6_hal", Span::call_site().into()); + #[cfg(feature = "esp32h2")] + let hal_crate_name = Ident::new("esp32h2_hal", Span::call_site().into()); + + (hal_crate, hal_crate_name) +} + /// This attribute allows placing statics and functions into ram. /// /// Options that can be specified are rtc_slow or rtc_fast to use the @@ -186,7 +231,7 @@ pub fn ram(args: TokenStream, input: TokenStream) -> TokenStream { #[cfg(feature = "interrupt")] #[proc_macro_attribute] pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { - use proc_macro_crate::{crate_name, FoundCrate}; + use proc_macro_crate::FoundCrate; let mut f: ItemFn = syn::parse(input).expect("`#[interrupt]` must be applied to a function"); @@ -260,35 +305,7 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream { proc_macro2::Span::call_site(), ); - #[cfg(feature = "esp32")] - let hal_crate = crate_name("esp32-hal"); - #[cfg(feature = "esp32s2")] - let hal_crate = crate_name("esp32s2-hal"); - #[cfg(feature = "esp32s3")] - let hal_crate = crate_name("esp32s3-hal"); - #[cfg(feature = "esp32c2")] - let hal_crate = crate_name("esp32c2-hal"); - #[cfg(feature = "esp32c3")] - let hal_crate = crate_name("esp32c3-hal"); - #[cfg(feature = "esp32c6")] - let hal_crate = crate_name("esp32c6-hal"); - #[cfg(feature = "esp32h2")] - let hal_crate = crate_name("esp32h2-hal"); - - #[cfg(feature = "esp32")] - let hal_crate_name = Ident::new("esp32_hal", Span::call_site().into()); - #[cfg(feature = "esp32s2")] - let hal_crate_name = Ident::new("esp32s2_hal", Span::call_site().into()); - #[cfg(feature = "esp32s3")] - let hal_crate_name = Ident::new("esp32s3_hal", Span::call_site().into()); - #[cfg(feature = "esp32c2")] - let hal_crate_name = Ident::new("esp32c2_hal", Span::call_site().into()); - #[cfg(feature = "esp32c3")] - let hal_crate_name = Ident::new("esp32c3_hal", Span::call_site().into()); - #[cfg(feature = "esp32c6")] - let hal_crate_name = Ident::new("esp32c6_hal", Span::call_site().into()); - #[cfg(feature = "esp32h2")] - let hal_crate_name = Ident::new("esp32h2_hal", Span::call_site().into()); + let (hal_crate, hal_crate_name) = get_hal_crate(); let interrupt_in_hal_crate = match hal_crate { Ok(FoundCrate::Itself) => { @@ -470,6 +487,8 @@ impl Parse for MakeGpioEnumDispatchMacro { } /// Create an enum for erased GPIO pins, using the enum-dispatch pattern +/// +/// Only used internally #[proc_macro] pub fn make_gpio_enum_dispatch_macro(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as MakeGpioEnumDispatchMacro); @@ -515,6 +534,13 @@ pub fn make_gpio_enum_dispatch_macro(input: TokenStream) -> TokenStream { } #[cfg(any(feature = "esp32c6", feature = "esp32s2", feature = "esp32s3"))] +/// Load code to be run on the LP/ULP core. +/// +/// ## Example +/// ```no_run +/// let lp_core_code = load_lp_code!("path.elf"); +/// lp_core_code.run(&mut lp_core, lp_core::LpCoreWakeupSource::HpCpu, lp_pin); +/// ```` #[proc_macro] pub fn load_lp_code(input: TokenStream) -> TokenStream { use object::{Object, ObjectSection, ObjectSymbol}; @@ -691,3 +717,80 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream { } .into() } + +// just delegates to embassy's macro for RISC-V +#[cfg(all( + feature = "embassy", + not(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3")) +))] +/// Creates a new `executor` instance and declares an application entry point +/// spawning the corresponding function body as an async task. +/// +/// The following restrictions apply: +/// +/// * The function must accept exactly 1 parameter, an +/// `embassy_executor::Spawner` handle that it can use to spawn additional +/// tasks. +/// * The function must be declared `async`. +/// * The function must not use generics. +/// * Only a single `main` task may be declared. +/// +/// ## Examples +/// Spawning a task: +/// +/// ``` rust +/// #[embassy_executor::main] +/// async fn main(_s: embassy_executor::Spawner) { +/// // Function body +/// } +/// ``` +#[proc_macro_attribute] +pub fn main(_args: TokenStream, input: TokenStream) -> TokenStream { + let f = parse_macro_input!(input as ItemFn); + + let asyncness = f.sig.asyncness; + let args = f.sig.inputs; + let stmts = f.block.stmts; + + quote!( + #[embassy_executor::main(entry = "entry")] + #asyncness fn main(#args) { + #(#stmts)* + } + ) + .into() +} + +#[cfg(all( + feature = "embassy", + any(feature = "esp32", feature = "esp32s2", feature = "esp32s3") +))] +/// Creates a new `executor` instance and declares an application entry point +/// spawning the corresponding function body as an async task. +/// +/// The following restrictions apply: +/// +/// * The function must accept exactly 1 parameter, an +/// `embassy_executor::Spawner` handle that it can use to spawn additional +/// tasks. +/// * The function must be declared `async`. +/// * The function must not use generics. +/// * Only a single `main` task may be declared. +/// +/// ## Examples +/// Spawning a task: +/// +/// ``` rust +/// #[embassy_executor::main] +/// async fn main(_s: embassy_executor::Spawner) { +/// // Function body +/// } +/// ``` +#[proc_macro_attribute] +pub fn main(args: TokenStream, item: TokenStream) -> TokenStream { + let args = syn::parse_macro_input!(args as embassy_xtensa::Args); + let f = syn::parse_macro_input!(item as syn::ItemFn); + embassy_xtensa::main::run(&args.meta, f, embassy_xtensa::main::main()) + .unwrap_or_else(|x| x) + .into() +} diff --git a/esp32-hal/examples/embassy_hello_world.rs b/esp32-hal/examples/embassy_hello_world.rs index 6371a885335..eed486f0e00 100644 --- a/esp32-hal/examples/embassy_hello_world.rs +++ b/esp32-hal/examples/embassy_hello_world.rs @@ -7,35 +7,27 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, timer::TimerGroup, }; use esp_backtrace as _; -use static_cell::make_static; #[embassy_executor::task] -async fn run1() { +async fn run() { loop { esp_println::println!("Hello world from embassy using esp-hal-async!"); Timer::after(Duration::from_millis(1_000)).await; } } -#[embassy_executor::task] -async fn run2() { - loop { - esp_println::println!("Bing!"); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -44,9 +36,10 @@ fn main() -> ! { let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); embassy::init(&clocks, timer_group0.timer0); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run1()).ok(); - spawner.spawn(run2()).ok(); - }); + spawner.spawn(run()).ok(); + + loop { + esp_println::println!("Bing!"); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32-hal/examples/embassy_i2c.rs b/esp32-hal/examples/embassy_i2c.rs index 12f2d13c2c9..f13b1a6a74e 100644 --- a/esp32-hal/examples/embassy_i2c.rs +++ b/esp32-hal/examples/embassy_i2c.rs @@ -14,10 +14,11 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, i2c::I2C, interrupt, peripherals::{Interrupt, Peripherals, I2C0}, @@ -27,23 +28,9 @@ use esp32_hal::{ }; use esp_backtrace as _; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; -use static_cell::make_static; -#[embassy_executor::task] -async fn run(i2c: I2C<'static, I2C0>) { - let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).await.unwrap(); - lis3dh.set_range(Range::G8).await.unwrap(); - - loop { - let norm = lis3dh.accel_norm().await.unwrap(); - esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); - - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -63,8 +50,13 @@ fn main() -> ! { interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run(i2c0)).ok(); - }); + let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); + lis3dh.set_range(Range::G8).await.unwrap(); + + loop { + let norm = lis3dh.accel_norm().await.unwrap(); + esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); + + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32-hal/examples/embassy_i2s_read.rs b/esp32-hal/examples/embassy_i2s_read.rs index b25a77aacdc..6e5101b786e 100644 --- a/esp32-hal/examples/embassy_i2s_read.rs +++ b/esp32-hal/examples/embassy_i2s_read.rs @@ -14,13 +14,12 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sRx, NoMclk, PinsBclkWsDin, Standard}, + embassy::{self}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, NoMclk, PinsBclkWsDin, Standard}, pdma::Dma, peripherals::Peripherals, prelude::*, @@ -29,43 +28,9 @@ use esp32_hal::{ }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn i2s_task( - i2s_rx: I2sRx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDin< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32_hal::pdma::I2s0DmaChannel, - >, -) { - let buffer = dma_buffer(); - println!("Start"); - - let mut data = [0u8; 5000]; - let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); - loop { - let avail = transaction.available().await; - println!("available {}", avail); - - let count = transaction.pop(&mut data).await.unwrap(); - println!( - "got {} bytes, {:x?}..{:x?}", - count, - &data[..10], - &data[count - 10..count] - ); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -81,8 +46,8 @@ fn main() -> ! { let dma = Dma::new(system.dma); let dma_channel = dma.i2s0channel; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -92,14 +57,14 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, ); - let i2s_tx = i2s.i2s_rx.with_pins(PinsBclkWsDin::new( + let i2s_rx = i2s.i2s_rx.with_pins(PinsBclkWsDin::new( io.pins.gpio12, io.pins.gpio13, io.pins.gpio14, @@ -112,10 +77,23 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let buffer = dma_buffer(); + println!("Start"); + + let mut data = [0u8; 5000]; + let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); + loop { + let avail = transaction.available().await; + println!("available {}", avail); + + let count = transaction.pop(&mut data).await.unwrap(); + println!( + "got {} bytes, {:x?}..{:x?}", + count, + &data[..10], + &data[count - 10..count] + ); + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32-hal/examples/embassy_i2s_sound.rs b/esp32-hal/examples/embassy_i2s_sound.rs index 8defca1c1a0..3c118dd1498 100644 --- a/esp32-hal/examples/embassy_i2s_sound.rs +++ b/esp32-hal/examples/embassy_i2s_sound.rs @@ -30,13 +30,12 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sTx, NoMclk, PinsBclkWsDout, Standard}, + embassy::{self}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, NoMclk, PinsBclkWsDout, Standard}, pdma::Dma, peripherals::Peripherals, prelude::*, @@ -45,7 +44,6 @@ use esp32_hal::{ }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; const SINE: [i16; 64] = [ 0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356, @@ -55,55 +53,8 @@ const SINE: [i16; 64] = [ -28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211, ]; -#[embassy_executor::task] -async fn i2s_task( - i2s_tx: I2sTx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDout< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32_hal::pdma::I2s0DmaChannel, - >, -) { - let data = - unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; - - let buffer = dma_buffer(); - let mut idx = 0; - for i in 0..usize::min(data.len(), buffer.len()) { - buffer[i] = data[idx]; - - idx += 1; - - if idx >= data.len() { - idx = 0; - } - } - - let mut filler = [0u8; 10000]; - let mut idx = 32000 % data.len(); - - println!("Start"); - let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); - - loop { - for i in 0..filler.len() { - filler[i] = data[(idx + i) % data.len()]; - } - println!("Next"); - - let written = transaction.push(&filler).await.unwrap(); - idx = (idx + written) % data.len(); - println!("written {}", written); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -119,8 +70,8 @@ fn main() -> ! { let dma = Dma::new(system.dma); let dma_channel = dma.i2s0channel; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -130,8 +81,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -150,10 +101,37 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let data = + unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; + + let buffer = dma_buffer(); + let mut idx = 0; + for i in 0..usize::min(data.len(), buffer.len()) { + buffer[i] = data[idx]; + + idx += 1; + + if idx >= data.len() { + idx = 0; + } + } + + let mut filler = [0u8; 10000]; + let mut idx = 32000 % data.len(); + + println!("Start"); + let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); + + loop { + for i in 0..filler.len() { + filler[i] = data[(idx + i) % data.len()]; + } + println!("Next"); + + let written = transaction.push(&filler).await.unwrap(); + idx = (idx + written) % data.len(); + println!("written {}", written); + } } fn dma_buffer() -> &'static mut [u8; 32000] { diff --git a/esp32-hal/examples/embassy_multicore.rs b/esp32-hal/examples/embassy_multicore.rs index f89f89cc284..236b2c0a9a4 100644 --- a/esp32-hal/examples/embassy_multicore.rs +++ b/esp32-hal/examples/embassy_multicore.rs @@ -6,6 +6,7 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal}; use embassy_time::{Duration, Ticker}; use esp32_hal::{ @@ -43,27 +44,8 @@ async fn control_led( } } -/// Sends periodic messages to control_led, enabling or disabling it. -#[embassy_executor::task] -async fn enable_disable_led(control: &'static Signal) { - println!( - "Starting enable_disable_led() on core {}", - get_core() as usize - ); - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - esp_println::println!("Sending LED on"); - control.signal(true); - ticker.next().await; - - esp_println::println!("Sending LED off"); - control.signal(false); - ticker.next().await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -89,8 +71,19 @@ fn main() -> ! { .start_app_core(unsafe { &mut APP_CORE_STACK }, cpu1_fnctn) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(enable_disable_led(led_ctrl_signal)).ok(); - }); + // Sends periodic messages to control_led, enabling or disabling it. + println!( + "Starting enable_disable_led() on core {}", + get_core() 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; + } } diff --git a/esp32-hal/examples/embassy_rmt_rx.rs b/esp32-hal/examples/embassy_rmt_rx.rs index 20dd592c374..bcb7b9e9d13 100644 --- a/esp32-hal/examples/embassy_rmt_rx.rs +++ b/esp32-hal/examples/embassy_rmt_rx.rs @@ -5,20 +5,20 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::RxChannelAsync, Channel2, PulseCode, RxChannelConfig, RxChannelCreator}, + rmt::{asynch::RxChannelAsync, PulseCode, RxChannelConfig, RxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_hal_common::gpio::{Gpio15, Output, PushPull}; use esp_println::{print, println}; -use static_cell::make_static; const WIDTH: usize = 80; @@ -26,7 +26,58 @@ const WIDTH: usize = 80; compile_error!("Run this example in release mode"); #[embassy_executor::task] -async fn rmt_task(mut channel: Channel2<2>) { +async fn signal_task(mut pin: Gpio15>) { + loop { + for _ in 0..10 { + pin.toggle().unwrap(); + Timer::after(Duration::from_micros(10)).await; + } + Timer::after(Duration::from_millis(1000)).await; + } +} + +#[main] +async fn main(spawner: Spawner) -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger_from_env(); + println!("Init!"); + let peripherals = Peripherals::take(); + let system = peripherals.SYSTEM.split(); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + #[cfg(feature = "embassy-time-timg0")] + { + let timer_group0 = esp32_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks); + embassy::init(&clocks, timer_group0.timer0); + } + + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + + let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap(); + + let mut channel = rmt + .channel2 + .configure( + io.pins.gpio4, + RxChannelConfig { + clk_divider: 1, + idle_threshold: 0b111_1111_1111_1111, + ..RxChannelConfig::default() + }, + ) + .unwrap(); + + // you have to enable the interrupt for async to work + esp32_hal::interrupt::enable( + esp32_hal::peripherals::Interrupt::RMT, + esp32_hal::interrupt::Priority::Priority1, + ) + .unwrap(); + + spawner + .spawn(signal_task(io.pins.gpio15.into_push_pull_output())) + .unwrap(); + let mut data = [PulseCode { level1: true, length1: 1, @@ -75,61 +126,3 @@ async fn rmt_task(mut channel: Channel2<2>) { println!(); } } - -#[embassy_executor::task] -async fn signal_task(mut pin: Gpio15>) { - loop { - for _ in 0..10 { - pin.toggle().unwrap(); - Timer::after(Duration::from_micros(10)).await; - } - Timer::after(Duration::from_millis(1000)).await; - } -} - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger_from_env(); - println!("Init!"); - let peripherals = Peripherals::take(); - let system = peripherals.SYSTEM.split(); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - - #[cfg(feature = "embassy-time-timg0")] - { - let timer_group0 = esp32_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks); - embassy::init(&clocks, timer_group0.timer0); - } - - let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - - let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap(); - - let channel = rmt - .channel2 - .configure( - io.pins.gpio4, - RxChannelConfig { - clk_divider: 1, - idle_threshold: 0b111_1111_1111_1111, - ..RxChannelConfig::default() - }, - ) - .unwrap(); - - // you have to enable the interrupt for async to work - esp32_hal::interrupt::enable( - esp32_hal::peripherals::Interrupt::RMT, - esp32_hal::interrupt::Priority::Priority1, - ) - .unwrap(); - - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - spawner - .spawn(signal_task(io.pins.gpio15.into_push_pull_output())) - .ok(); - }); -} diff --git a/esp32-hal/examples/embassy_rmt_tx.rs b/esp32-hal/examples/embassy_rmt_tx.rs index 701a103e246..2b3899d250e 100644 --- a/esp32-hal/examples/embassy_rmt_tx.rs +++ b/esp32-hal/examples/embassy_rmt_tx.rs @@ -5,47 +5,22 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::TxChannelAsync, Channel0, PulseCode, TxChannelConfig, TxChannelCreator}, + rmt::{asynch::TxChannelAsync, PulseCode, TxChannelConfig, TxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel0<0>) { - let mut data = [PulseCode { - level1: true, - length1: 200, - level2: false, - length2: 50, - }; 20]; - - data[data.len() - 2] = PulseCode { - level1: true, - length1: 3000, - level2: false, - length2: 500, - }; - data[data.len() - 1] = PulseCode::default(); - - loop { - println!("transmit"); - channel.transmit(&data).await.unwrap(); - println!("transmitted\n"); - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -63,7 +38,7 @@ fn main() -> ! { let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap(); - let channel = rmt + let mut channel = rmt .channel0 .configure( io.pins.gpio4.into_push_pull_output(), @@ -81,8 +56,25 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); + let mut data = [PulseCode { + level1: true, + length1: 200, + level2: false, + length2: 50, + }; 20]; + + data[data.len() - 2] = PulseCode { + level1: true, + length1: 3000, + level2: false, + length2: 500, + }; + data[data.len() - 1] = PulseCode::default(); + + loop { + println!("transmit"); + channel.transmit(&data).await.unwrap(); + println!("transmitted\n"); + Timer::after(Duration::from_millis(500)).await; + } } diff --git a/esp32-hal/examples/embassy_serial.rs b/esp32-hal/examples/embassy_serial.rs index 315454894dd..40d1031b405 100644 --- a/esp32-hal/examples/embassy_serial.rs +++ b/esp32-hal/examples/embassy_serial.rs @@ -7,9 +7,10 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, interrupt, peripherals::{Interrupt, Peripherals, UART0}, prelude::*, @@ -19,7 +20,6 @@ use esp32_hal::{ use esp_backtrace as _; use esp_hal_common::uart::{config::AtCmdConfig, UartRx, UartTx}; use heapless::Vec; -use static_cell::make_static; // rx_fifo_full_threshold const READ_BUF_SIZE: usize = 64; @@ -61,8 +61,8 @@ async fn reader(mut rx: UartRx<'static, UART0>) { } } -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -80,9 +80,6 @@ fn main() -> ! { interrupt::enable(Interrupt::UART0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(reader(rx)).ok(); - spawner.spawn(writer(tx)).ok(); - }); + spawner.spawn(reader(rx)).ok(); + spawner.spawn(writer(tx)).ok(); } diff --git a/esp32-hal/examples/embassy_spi.rs b/esp32-hal/examples/embassy_spi.rs index 4c638acaf2b..4b1c2471cd0 100644 --- a/esp32-hal/examples/embassy_spi.rs +++ b/esp32-hal/examples/embassy_spi.rs @@ -18,39 +18,23 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, + embassy::{self}, pdma::*, peripherals::Peripherals, prelude::*, - spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode}, + spi::{Spi, SpiMode}, timer::TimerGroup, IO, }; use esp_backtrace as _; -use static_cell::make_static; -pub type SpiType<'d> = SpiDma<'d, esp32_hal::peripherals::SPI2, Spi2DmaChannel, FullDuplexMode>; - -#[embassy_executor::task] -async fn spi_task(spi: &'static mut SpiType<'static>) { - let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; - loop { - let mut buffer = [0; 8]; - esp_println::println!("Sending bytes"); - embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer) - .await - .unwrap(); - esp_println::println!("Bytes recieved: {:?}", buffer); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -74,10 +58,10 @@ fn main() -> ! { let dma = Dma::new(system.dma); let dma_channel = dma.spi2channel; - let descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; - let spi = make_static!(Spi::new( + let mut spi = Spi::new( peripherals.SPI2, sclk, mosi, @@ -89,13 +73,19 @@ fn main() -> ! { ) .with_dma(dma_channel.configure( false, - descriptors, - rx_descriptors, + &mut descriptors, + &mut rx_descriptors, DmaPriority::Priority0, - ))); + )); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(spi_task(spi)).ok(); - }); + let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; + loop { + let mut buffer = [0; 8]; + esp_println::println!("Sending bytes"); + embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer) + .await + .unwrap(); + esp_println::println!("Bytes recieved: {:?}", buffer); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32-hal/examples/embassy_wait.rs b/esp32-hal/examples/embassy_wait.rs index 43e93afedde..22fee2f8484 100644 --- a/esp32-hal/examples/embassy_wait.rs +++ b/esp32-hal/examples/embassy_wait.rs @@ -6,32 +6,21 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use embedded_hal_async::digital::Wait; use esp32_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, - gpio::{Gpio0, Input, PullDown}, + embassy::{self}, peripherals::Peripherals, prelude::*, timer::TimerGroup, IO, }; use esp_backtrace as _; -use static_cell::make_static; -#[embassy_executor::task] -async fn ping(mut pin: Gpio0>) { - loop { - esp_println::println!("Waiting..."); - pin.wait_for_rising_edge().await.unwrap(); - esp_println::println!("Ping!"); - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -42,7 +31,7 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // GPIO 0 as input - let input = io.pins.gpio0.into_pull_down_input(); + let mut input = io.pins.gpio0.into_pull_down_input(); // Async requires the GPIO interrupt to wake futures esp32_hal::interrupt::enable( @@ -51,8 +40,10 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(ping(input)).ok(); - }); + loop { + esp_println::println!("Waiting..."); + input.wait_for_rising_edge().await.unwrap(); + esp_println::println!("Ping!"); + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32c2-hal/examples/embassy_hello_world.rs b/esp32c2-hal/examples/embassy_hello_world.rs index 53fbc487787..53b17dd6c2e 100644 --- a/esp32c2-hal/examples/embassy_hello_world.rs +++ b/esp32c2-hal/examples/embassy_hello_world.rs @@ -7,30 +7,21 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c2_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*}; use esp_backtrace as _; -use static_cell::make_static; #[embassy_executor::task] -async fn run1() { +async fn run() { loop { esp_println::println!("Hello world from embassy using esp-hal-async!"); Timer::after(Duration::from_millis(1_000)).await; } } -#[embassy_executor::task] -async fn run2() { - loop { - esp_println::println!("Bing!"); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -48,9 +39,10 @@ fn main() -> ! { esp32c2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, ); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run1()).ok(); - spawner.spawn(run2()).ok(); - }); + spawner.spawn(run()).ok(); + + loop { + esp_println::println!("Bing!"); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32c2-hal/examples/embassy_i2c.rs b/esp32c2-hal/examples/embassy_i2c.rs index 74fa205a05c..9e3243ca763 100644 --- a/esp32c2-hal/examples/embassy_i2c.rs +++ b/esp32c2-hal/examples/embassy_i2c.rs @@ -14,36 +14,22 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c2_hal::{ clock::ClockControl, embassy, i2c::I2C, interrupt, - peripherals::{Interrupt, Peripherals, I2C0}, + peripherals::{Interrupt, Peripherals}, prelude::*, IO, }; use esp_backtrace as _; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; -use static_cell::make_static; -#[embassy_executor::task] -async fn run(i2c: I2C<'static, I2C0>) { - let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).await.unwrap(); - lis3dh.set_range(Range::G8).await.unwrap(); - - loop { - let norm = lis3dh.accel_norm().await.unwrap(); - esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); - - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -72,8 +58,13 @@ fn main() -> ! { interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run(i2c0)).ok(); - }); + let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); + lis3dh.set_range(Range::G8).await.unwrap(); + + loop { + let norm = lis3dh.accel_norm().await.unwrap(); + esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); + + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32c2-hal/examples/embassy_serial.rs b/esp32c2-hal/examples/embassy_serial.rs index a0754b75b4d..910d3104634 100644 --- a/esp32c2-hal/examples/embassy_serial.rs +++ b/esp32c2-hal/examples/embassy_serial.rs @@ -7,7 +7,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c2_hal::{ clock::ClockControl, embassy, @@ -19,7 +19,6 @@ use esp32c2_hal::{ use esp_backtrace as _; use esp_hal_common::uart::{config::AtCmdConfig, UartRx, UartTx}; use heapless::Vec; -use static_cell::make_static; // rx_fifo_full_threshold const READ_BUF_SIZE: usize = 64; @@ -61,8 +60,8 @@ async fn reader(mut rx: UartRx<'static, UART0>) { } } -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -89,9 +88,6 @@ fn main() -> ! { interrupt::enable(Interrupt::UART0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(reader(rx)).ok(); - spawner.spawn(writer(tx)).ok(); - }); + spawner.spawn(reader(rx)).ok(); + spawner.spawn(writer(tx)).ok(); } diff --git a/esp32c2-hal/examples/embassy_spi.rs b/esp32c2-hal/examples/embassy_spi.rs index 720b71ea1fe..b1c8d59b8bb 100644 --- a/esp32c2-hal/examples/embassy_spi.rs +++ b/esp32c2-hal/examples/embassy_spi.rs @@ -18,7 +18,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c2_hal::{ clock::ClockControl, @@ -27,31 +27,13 @@ use esp32c2_hal::{ gdma::*, peripherals::Peripherals, prelude::*, - spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode}, + spi::{Spi, SpiMode}, IO, }; use esp_backtrace as _; -use static_cell::make_static; -pub type SpiType<'d> = - SpiDma<'d, esp32c2_hal::peripherals::SPI2, esp32c2_hal::gdma::Channel0, FullDuplexMode>; - -#[embassy_executor::task] -async fn spi_task(spi: &'static mut SpiType<'static>) { - let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; - loop { - let mut buffer = [0; 8]; - esp_println::println!("Sending bytes"); - embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer) - .await - .unwrap(); - esp_println::println!("Bytes recieved: {:?}", buffer); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -84,10 +66,10 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; - let spi = make_static!(Spi::new( + let mut spi = Spi::new( peripherals.SPI2, sclk, mosi, @@ -99,13 +81,19 @@ fn main() -> ! { ) .with_dma(dma_channel.configure( false, - descriptors, - rx_descriptors, + &mut descriptors, + &mut rx_descriptors, DmaPriority::Priority0, - ))); + )); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(spi_task(spi)).ok(); - }); + let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; + loop { + let mut buffer = [0; 8]; + esp_println::println!("Sending bytes"); + embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer) + .await + .unwrap(); + esp_println::println!("Bytes recieved: {:?}", buffer); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32c2-hal/examples/embassy_wait.rs b/esp32c2-hal/examples/embassy_wait.rs index 0ba712750e1..a9e39fd1f6d 100644 --- a/esp32c2-hal/examples/embassy_wait.rs +++ b/esp32c2-hal/examples/embassy_wait.rs @@ -6,32 +6,14 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use embedded_hal_async::digital::Wait; -use esp32c2_hal::{ - clock::ClockControl, - embassy, - gpio::{Gpio9, Input, PullDown}, - peripherals::Peripherals, - prelude::*, - IO, -}; +use esp32c2_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, IO}; use esp_backtrace as _; -use static_cell::make_static; -#[embassy_executor::task] -async fn ping(mut pin: Gpio9>) { - loop { - esp_println::println!("Waiting..."); - pin.wait_for_rising_edge().await.unwrap(); - esp_println::println!("Ping!"); - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -51,7 +33,7 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // GPIO 9 as input - let input = io.pins.gpio9.into_pull_down_input(); + let mut input = io.pins.gpio9.into_pull_down_input(); // Async requires the GPIO interrupt to wake futures esp32c2_hal::interrupt::enable( @@ -60,8 +42,10 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(ping(input)).ok(); - }); + loop { + esp_println::println!("Waiting..."); + input.wait_for_rising_edge().await.unwrap(); + esp_println::println!("Ping!"); + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32c3-hal/examples/embassy_hello_world.rs b/esp32c3-hal/examples/embassy_hello_world.rs index d96aefe762b..b372af6cb43 100644 --- a/esp32c3-hal/examples/embassy_hello_world.rs +++ b/esp32c3-hal/examples/embassy_hello_world.rs @@ -7,30 +7,22 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c3_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*}; use esp_backtrace as _; use static_cell::make_static; #[embassy_executor::task] -async fn run1() { +async fn run() { loop { esp_println::println!("Hello world from embassy using esp-hal-async!"); Timer::after(Duration::from_millis(1_000)).await; } } -#[embassy_executor::task] -async fn run2() { - loop { - esp_println::println!("Bing!"); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -48,9 +40,10 @@ fn main() -> ! { esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, ); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run1()).ok(); - spawner.spawn(run2()).ok(); - }); + spawner.spawn(run()).ok(); + + loop { + esp_println::println!("Bing!"); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32c3-hal/examples/embassy_i2c.rs b/esp32c3-hal/examples/embassy_i2c.rs index c496e85a2dd..893dcff5056 100644 --- a/esp32c3-hal/examples/embassy_i2c.rs +++ b/esp32c3-hal/examples/embassy_i2c.rs @@ -14,36 +14,22 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c3_hal::{ clock::ClockControl, embassy, i2c::I2C, interrupt, - peripherals::{Interrupt, Peripherals, I2C0}, + peripherals::{Interrupt, Peripherals}, prelude::*, IO, }; use esp_backtrace as _; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; -use static_cell::make_static; -#[embassy_executor::task] -async fn run(i2c: I2C<'static, I2C0>) { - let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).await.unwrap(); - lis3dh.set_range(Range::G8).await.unwrap(); - - loop { - let norm = lis3dh.accel_norm().await.unwrap(); - esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); - - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -72,8 +58,13 @@ fn main() -> ! { interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run(i2c0)).ok(); - }); + let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); + lis3dh.set_range(Range::G8).await.unwrap(); + + loop { + let norm = lis3dh.accel_norm().await.unwrap(); + esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); + + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32c3-hal/examples/embassy_i2s_read.rs b/esp32c3-hal/examples/embassy_i2s_read.rs index 784c2b9c417..af65a000513 100644 --- a/esp32c3-hal/examples/embassy_i2s_read.rs +++ b/esp32c3-hal/examples/embassy_i2s_read.rs @@ -15,63 +15,27 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c3_hal::{ clock::ClockControl, dma::DmaPriority, embassy, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sRx, MclkPin, PinsBclkWsDin, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDin, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn i2s_task( - i2s_rx: I2sRx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDin< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32c3_hal::gdma::Channel0, - >, -) { - let buffer = dma_buffer(); - println!("Start"); - - let mut data = [0u8; 5000]; - let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); - loop { - let avail = transaction.available().await; - println!("available {}", avail); - - let count = transaction.pop(&mut data).await.unwrap(); - println!( - "got {} bytes, {:x?}..{:x?}", - count, - &data[..10], - &data[count - 10..count] - ); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -91,8 +55,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -102,8 +66,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -122,10 +86,23 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_rx)).ok(); - }); + let buffer = dma_buffer(); + println!("Start"); + + let mut data = [0u8; 5000]; + let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); + loop { + let avail = transaction.available().await; + println!("available {}", avail); + + let count = transaction.pop(&mut data).await.unwrap(); + println!( + "got {} bytes, {:x?}..{:x?}", + count, + &data[..10], + &data[count - 10..count] + ); + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32c3-hal/examples/embassy_i2s_sound.rs b/esp32c3-hal/examples/embassy_i2s_sound.rs index 1ab0b92aca4..35f59262f51 100644 --- a/esp32c3-hal/examples/embassy_i2s_sound.rs +++ b/esp32c3-hal/examples/embassy_i2s_sound.rs @@ -31,22 +31,19 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c3_hal::{ clock::ClockControl, dma::DmaPriority, embassy, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sTx, MclkPin, PinsBclkWsDout, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDout, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; const SINE: [i16; 64] = [ 0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356, @@ -56,59 +53,13 @@ const SINE: [i16; 64] = [ -28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211, ]; -#[embassy_executor::task] -async fn i2s_task( - i2s_tx: I2sTx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDout< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32c3_hal::gdma::Channel0, - >, -) { - let data = - unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; - - let buffer = dma_buffer(); - let mut idx = 0; - for i in 0..usize::min(data.len(), buffer.len()) { - buffer[i] = data[idx]; - - idx += 1; - - if idx >= data.len() { - idx = 0; - } - } - - let mut filler = [0u8; 10000]; - let mut idx = 32000 % data.len(); - - println!("Start"); - let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); - loop { - for i in 0..filler.len() { - filler[i] = data[(idx + i) % data.len()]; - } - println!("Next"); - - let written = transaction.push(&filler).await.unwrap(); - idx = (idx + written) % data.len(); - println!("written {}", written); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -128,8 +79,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -139,8 +90,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -159,10 +110,36 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let data = + unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; + + let buffer = dma_buffer(); + let mut idx = 0; + for i in 0..usize::min(data.len(), buffer.len()) { + buffer[i] = data[idx]; + + idx += 1; + + if idx >= data.len() { + idx = 0; + } + } + + let mut filler = [0u8; 10000]; + let mut idx = 32000 % data.len(); + + println!("Start"); + let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); + loop { + for i in 0..filler.len() { + filler[i] = data[(idx + i) % data.len()]; + } + println!("Next"); + + let written = transaction.push(&filler).await.unwrap(); + idx = (idx + written) % data.len(); + println!("written {}", written); + } } fn dma_buffer() -> &'static mut [u8; 32000] { diff --git a/esp32c3-hal/examples/embassy_rmt_rx.rs b/esp32c3-hal/examples/embassy_rmt_rx.rs index 6afcb916709..f0ad3e81ddf 100644 --- a/esp32c3-hal/examples/embassy_rmt_rx.rs +++ b/esp32c3-hal/examples/embassy_rmt_rx.rs @@ -7,24 +7,65 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c3_hal::{ clock::ClockControl, embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::RxChannelAsync, Channel2, PulseCode, RxChannelConfig, RxChannelCreator}, + rmt::{asynch::RxChannelAsync, PulseCode, RxChannelConfig, RxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::{print, println}; -use static_cell::make_static; const WIDTH: usize = 80; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel2<2>) { +#[main] +async fn main(_spawner: Spawner) -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger_from_env(); + println!("Init!"); + let peripherals = Peripherals::take(); + let system = peripherals.SYSTEM.split(); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + #[cfg(feature = "embassy-time-systick")] + embassy::init( + &clocks, + esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), + ); + + #[cfg(feature = "embassy-time-timg0")] + embassy::init( + &clocks, + esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, + ); + + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + + let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); + + let mut channel = rmt + .channel2 + .configure( + io.pins.gpio9, + RxChannelConfig { + clk_divider: 255, + idle_threshold: 10000, + ..RxChannelConfig::default() + }, + ) + .unwrap(); + + // you have to enable the interrupt for async to work + esp32c3_hal::interrupt::enable( + esp32c3_hal::peripherals::Interrupt::RMT, + esp32c3_hal::interrupt::Priority::Priority1, + ) + .unwrap(); + let mut data = [PulseCode { level1: true, length1: 1, @@ -74,54 +115,3 @@ async fn rmt_task(mut channel: Channel2<2>) { println!(); } } - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger_from_env(); - println!("Init!"); - let peripherals = Peripherals::take(); - let system = peripherals.SYSTEM.split(); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let mut clock_control = system.peripheral_clock_control; - - #[cfg(feature = "embassy-time-systick")] - embassy::init( - &clocks, - esp32c3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), - ); - - #[cfg(feature = "embassy-time-timg0")] - embassy::init( - &clocks, - esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks, &mut clock_control).timer0, - ); - - let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - - let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &mut clock_control, &clocks).unwrap(); - - let channel = rmt - .channel2 - .configure( - io.pins.gpio9, - RxChannelConfig { - clk_divider: 255, - idle_threshold: 10000, - ..RxChannelConfig::default() - }, - ) - .unwrap(); - - // you have to enable the interrupt for async to work - esp32c3_hal::interrupt::enable( - esp32c3_hal::peripherals::Interrupt::RMT, - esp32c3_hal::interrupt::Priority::Priority1, - ) - .unwrap(); - - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); -} diff --git a/esp32c3-hal/examples/embassy_rmt_tx.rs b/esp32c3-hal/examples/embassy_rmt_tx.rs index 930fbea475d..3d413ba4407 100644 --- a/esp32c3-hal/examples/embassy_rmt_tx.rs +++ b/esp32c3-hal/examples/embassy_rmt_tx.rs @@ -5,55 +5,28 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c3_hal::{ clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, - rmt::{asynch::TxChannelAsync, Channel0, PulseCode, TxChannelConfig, TxChannelCreator}, + rmt::{asynch::TxChannelAsync, PulseCode, TxChannelConfig, TxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel0<0>) { - let mut data = [PulseCode { - level1: true, - length1: 200, - level2: false, - length2: 50, - }; 20]; - - data[data.len() - 2] = PulseCode { - level1: true, - length1: 3000, - level2: false, - length2: 500, - }; - data[data.len() - 1] = PulseCode::default(); - - loop { - println!("transmit"); - channel.transmit(&data).await.unwrap(); - println!("transmitted\n"); - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let mut clock_control = system.peripheral_clock_control; #[cfg(feature = "embassy-time-systick")] embassy::init( @@ -64,14 +37,14 @@ fn main() -> ! { #[cfg(feature = "embassy-time-timg0")] embassy::init( &clocks, - esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks, &mut clock_control).timer0, + esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, ); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &mut clock_control, &clocks).unwrap(); + let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); - let channel = rmt + let mut channel = rmt .channel0 .configure( io.pins.gpio1.into_push_pull_output(), @@ -89,8 +62,25 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); + let mut data = [PulseCode { + level1: true, + length1: 200, + level2: false, + length2: 50, + }; 20]; + + data[data.len() - 2] = PulseCode { + level1: true, + length1: 3000, + level2: false, + length2: 500, + }; + data[data.len() - 1] = PulseCode::default(); + + loop { + println!("transmit"); + channel.transmit(&data).await.unwrap(); + println!("transmitted\n"); + Timer::after(Duration::from_millis(500)).await; + } } diff --git a/esp32c3-hal/examples/embassy_serial.rs b/esp32c3-hal/examples/embassy_serial.rs index 429fe8fe20b..cb52e208fc0 100644 --- a/esp32c3-hal/examples/embassy_serial.rs +++ b/esp32c3-hal/examples/embassy_serial.rs @@ -7,7 +7,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c3_hal::{ clock::ClockControl, embassy, @@ -19,7 +19,6 @@ use esp32c3_hal::{ use esp_backtrace as _; use esp_hal_common::uart::{config::AtCmdConfig, UartRx, UartTx}; use heapless::Vec; -use static_cell::make_static; // rx_fifo_full_threshold const READ_BUF_SIZE: usize = 64; @@ -61,8 +60,8 @@ async fn reader(mut rx: UartRx<'static, UART0>) { } } -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -89,9 +88,6 @@ fn main() -> ! { interrupt::enable(Interrupt::UART0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(reader(rx)).ok(); - spawner.spawn(writer(tx)).ok(); - }); + spawner.spawn(reader(rx)).ok(); + spawner.spawn(writer(tx)).ok(); } diff --git a/esp32c3-hal/examples/embassy_spi.rs b/esp32c3-hal/examples/embassy_spi.rs index 61ad469a170..b891e88acd6 100644 --- a/esp32c3-hal/examples/embassy_spi.rs +++ b/esp32c3-hal/examples/embassy_spi.rs @@ -18,7 +18,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c3_hal::{ clock::ClockControl, @@ -27,31 +27,13 @@ use esp32c3_hal::{ gdma::*, peripherals::Peripherals, prelude::*, - spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode}, + spi::{Spi, SpiMode}, IO, }; use esp_backtrace as _; -use static_cell::make_static; -pub type SpiType<'d> = - SpiDma<'d, esp32c3_hal::peripherals::SPI2, esp32c3_hal::gdma::Channel0, FullDuplexMode>; - -#[embassy_executor::task] -async fn spi_task(spi: &'static mut SpiType<'static>) { - let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; - loop { - let mut buffer = [0; 8]; - esp_println::println!("Sending bytes"); - embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer) - .await - .unwrap(); - esp_println::println!("Bytes recieved: {:?}", buffer); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -84,10 +66,10 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; - let spi = make_static!(Spi::new( + let mut spi = Spi::new( peripherals.SPI2, sclk, mosi, @@ -99,13 +81,19 @@ fn main() -> ! { ) .with_dma(dma_channel.configure( false, - descriptors, - rx_descriptors, + &mut descriptors, + &mut rx_descriptors, DmaPriority::Priority0, - ))); + )); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(spi_task(spi)).ok(); - }); + let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; + loop { + let mut buffer = [0; 8]; + esp_println::println!("Sending bytes"); + embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer) + .await + .unwrap(); + esp_println::println!("Bytes recieved: {:?}", buffer); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32c3-hal/examples/embassy_wait.rs b/esp32c3-hal/examples/embassy_wait.rs index 20114a1c858..97564d7f483 100644 --- a/esp32c3-hal/examples/embassy_wait.rs +++ b/esp32c3-hal/examples/embassy_wait.rs @@ -6,32 +6,14 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use embedded_hal_async::digital::Wait; -use esp32c3_hal::{ - clock::ClockControl, - embassy, - gpio::{Gpio9, Input, PullDown}, - peripherals::Peripherals, - prelude::*, - IO, -}; +use esp32c3_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, IO}; use esp_backtrace as _; -use static_cell::make_static; -#[embassy_executor::task] -async fn ping(mut pin: Gpio9>) { - loop { - esp_println::println!("Waiting..."); - pin.wait_for_rising_edge().await.unwrap(); - esp_println::println!("Ping!"); - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -51,7 +33,7 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // GPIO 9 as input - let input = io.pins.gpio9.into_pull_down_input(); + let mut input = io.pins.gpio9.into_pull_down_input(); // Async requires the GPIO interrupt to wake futures esp32c3_hal::interrupt::enable( @@ -60,8 +42,10 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(ping(input)).ok(); - }); + loop { + esp_println::println!("Waiting..."); + input.wait_for_rising_edge().await.unwrap(); + esp_println::println!("Ping!"); + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32c6-hal/examples/embassy_hello_world.rs b/esp32c6-hal/examples/embassy_hello_world.rs index 0636191063d..6c9955f061c 100644 --- a/esp32c6-hal/examples/embassy_hello_world.rs +++ b/esp32c6-hal/examples/embassy_hello_world.rs @@ -7,30 +7,21 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c6_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*}; use esp_backtrace as _; -use static_cell::make_static; #[embassy_executor::task] -async fn run1() { +async fn run() { loop { esp_println::println!("Hello world from embassy using esp-hal-async!"); Timer::after(Duration::from_millis(1_000)).await; } } -#[embassy_executor::task] -async fn run2() { - loop { - esp_println::println!("Bing!"); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -48,9 +39,10 @@ fn main() -> ! { embassy::init(&clocks, timer_group0.timer0); } - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run1()).ok(); - spawner.spawn(run2()).ok(); - }); + spawner.spawn(run()).ok(); + + loop { + esp_println::println!("Bing!"); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32c6-hal/examples/embassy_i2c.rs b/esp32c6-hal/examples/embassy_i2c.rs index 13f6d6b48ea..078ff05752c 100644 --- a/esp32c6-hal/examples/embassy_i2c.rs +++ b/esp32c6-hal/examples/embassy_i2c.rs @@ -14,36 +14,22 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c6_hal::{ clock::ClockControl, embassy, i2c::I2C, interrupt, - peripherals::{Interrupt, Peripherals, I2C0}, + peripherals::{Interrupt, Peripherals}, prelude::*, IO, }; use esp_backtrace as _; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; -use static_cell::make_static; -#[embassy_executor::task] -async fn run(i2c: I2C<'static, I2C0>) { - let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).await.unwrap(); - lis3dh.set_range(Range::G8).await.unwrap(); - - loop { - let norm = lis3dh.accel_norm().await.unwrap(); - esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); - - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -72,8 +58,13 @@ fn main() -> ! { interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run(i2c0)).ok(); - }); + let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); + lis3dh.set_range(Range::G8).await.unwrap(); + + loop { + let norm = lis3dh.accel_norm().await.unwrap(); + esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); + + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32c6-hal/examples/embassy_i2s_read.rs b/esp32c6-hal/examples/embassy_i2s_read.rs index 1d499fb65b2..54b2c6907b2 100644 --- a/esp32c6-hal/examples/embassy_i2s_read.rs +++ b/esp32c6-hal/examples/embassy_i2s_read.rs @@ -15,63 +15,27 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c6_hal::{ clock::ClockControl, dma::DmaPriority, embassy, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sRx, MclkPin, PinsBclkWsDin, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDin, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn i2s_task( - i2s_rx: I2sRx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDin< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32c6_hal::gdma::Channel0, - >, -) { - let buffer = dma_buffer(); - println!("Start"); - - let mut data = [0u8; 5000]; - let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); - loop { - let avail = transaction.available().await; - println!("available {}", avail); - - let count = transaction.pop(&mut data).await.unwrap(); - println!( - "got {} bytes, {:x?}..{:x?}", - count, - &data[..10], - &data[count - 10..count] - ); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -91,8 +55,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -102,8 +66,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -122,10 +86,23 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_rx)).ok(); - }); + let buffer = dma_buffer(); + println!("Start"); + + let mut data = [0u8; 5000]; + let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); + loop { + let avail = transaction.available().await; + println!("available {}", avail); + + let count = transaction.pop(&mut data).await.unwrap(); + println!( + "got {} bytes, {:x?}..{:x?}", + count, + &data[..10], + &data[count - 10..count] + ); + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32c6-hal/examples/embassy_i2s_sound.rs b/esp32c6-hal/examples/embassy_i2s_sound.rs index 83910d40749..ae68712f334 100644 --- a/esp32c6-hal/examples/embassy_i2s_sound.rs +++ b/esp32c6-hal/examples/embassy_i2s_sound.rs @@ -31,22 +31,19 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c6_hal::{ clock::ClockControl, dma::DmaPriority, embassy, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sTx, MclkPin, PinsBclkWsDout, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDout, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; const SINE: [i16; 64] = [ 0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356, @@ -56,59 +53,13 @@ const SINE: [i16; 64] = [ -28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211, ]; -#[embassy_executor::task] -async fn i2s_task( - i2s_tx: I2sTx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDout< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32c6_hal::gdma::Channel0, - >, -) { - let data = - unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; - - let buffer = dma_buffer(); - let mut idx = 0; - for i in 0..usize::min(data.len(), buffer.len()) { - buffer[i] = data[idx]; - - idx += 1; - - if idx >= data.len() { - idx = 0; - } - } - - let mut filler = [0u8; 10000]; - let mut idx = 32000 % data.len(); - - println!("Start"); - let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); - loop { - for i in 0..filler.len() { - filler[i] = data[(idx + i) % data.len()]; - } - println!("Next"); - - let written = transaction.push(&filler).await.unwrap(); - idx = (idx + written) % data.len(); - println!("written {}", written); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -128,8 +79,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -139,8 +90,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -159,10 +110,36 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let data = + unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; + + let buffer = dma_buffer(); + let mut idx = 0; + for i in 0..usize::min(data.len(), buffer.len()) { + buffer[i] = data[idx]; + + idx += 1; + + if idx >= data.len() { + idx = 0; + } + } + + let mut filler = [0u8; 10000]; + let mut idx = 32000 % data.len(); + + println!("Start"); + let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); + loop { + for i in 0..filler.len() { + filler[i] = data[(idx + i) % data.len()]; + } + println!("Next"); + + let written = transaction.push(&filler).await.unwrap(); + idx = (idx + written) % data.len(); + println!("written {}", written); + } } fn dma_buffer() -> &'static mut [u8; 32000] { diff --git a/esp32c6-hal/examples/embassy_parl_io_rx.rs b/esp32c6-hal/examples/embassy_parl_io_rx.rs index d771eef1875..fa1d60202f9 100644 --- a/esp32c6-hal/examples/embassy_parl_io_rx.rs +++ b/esp32c6-hal/examples/embassy_parl_io_rx.rs @@ -7,53 +7,28 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c6_hal::{ clock::ClockControl, dma::DmaPriority, embassy, - gdma::{self, Gdma}, - gpio::{GpioPin, Unknown, IO}, + gdma::Gdma, + gpio::IO, interrupt, - parl_io::{BitPackOrder, NoClkPin, ParlIoRx, ParlIoRxOnly, RxFourBits}, + parl_io::{BitPackOrder, NoClkPin, ParlIoRxOnly, RxFourBits}, peripherals, peripherals::Peripherals, prelude::*, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn parl_io_task( - mut parl_io_rx: ParlIoRx< - 'static, - gdma::Channel0, - RxFourBits< - 'static, - GpioPin, - GpioPin, - GpioPin, - GpioPin, - >, - NoClkPin, - >, -) { - let buffer = dma_buffer(); - loop { - parl_io_rx.read_dma_async(buffer).await.unwrap(); - println!("Received: {:02x?} ...", &buffer[..30]); - - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -70,8 +45,8 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let tx_descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; @@ -82,8 +57,8 @@ fn main() -> ! { peripherals.PARL_IO, dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), 1u32.MHz(), @@ -91,7 +66,7 @@ fn main() -> ! { ) .unwrap(); - let parl_io_rx = parl_io + let mut parl_io_rx = parl_io .rx .with_config(rx_pins, NoClkPin, BitPackOrder::Msb, Some(0xfff)) .unwrap(); @@ -103,10 +78,13 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(parl_io_task(parl_io_rx)).ok(); - }); + let buffer = dma_buffer(); + loop { + parl_io_rx.read_dma_async(buffer).await.unwrap(); + println!("Received: {:02x?} ...", &buffer[..30]); + + Timer::after(Duration::from_millis(500)).await; + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32c6-hal/examples/embassy_parl_io_tx.rs b/esp32c6-hal/examples/embassy_parl_io_tx.rs index ff44ecc835c..d85ffe21a6e 100644 --- a/esp32c6-hal/examples/embassy_parl_io_tx.rs +++ b/esp32c6-hal/examples/embassy_parl_io_tx.rs @@ -11,19 +11,18 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c6_hal::{ clock::ClockControl, dma::DmaPriority, embassy, - gdma::{self, Gdma}, - gpio::{GpioPin, Unknown, IO}, + gdma::Gdma, + gpio::IO, interrupt, parl_io::{ BitPackOrder, ClkOutPin, - ParlIoTx, ParlIoTxOnly, SampleEdge, TxFourBits, @@ -35,45 +34,12 @@ use esp32c6_hal::{ }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; - -#[embassy_executor::task] -async fn parl_io_task( - mut parl_io_tx: ParlIoTx< - 'static, - gdma::Channel0, - TxPinConfigWithValidPin< - 'static, - TxFourBits< - 'static, - GpioPin, - GpioPin, - GpioPin, - GpioPin, - >, - GpioPin, - >, - ClkOutPin<'static, GpioPin>, - >, -) { - let buffer = dma_buffer(); - for i in 0..buffer.len() { - buffer[i] = (i % 255) as u8; - } - - loop { - parl_io_tx.write_dma_async(buffer).await.unwrap(); - println!("Transferred {} bytes", buffer.len()); - - Timer::after(Duration::from_millis(500)).await; - } -} -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -90,8 +56,8 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let tx_descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; @@ -104,8 +70,8 @@ fn main() -> ! { peripherals.PARL_IO, dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), 1u32.MHz(), @@ -115,7 +81,7 @@ fn main() -> ! { let clock_pin = ClkOutPin::new(io.pins.gpio6); - let parl_io_tx = parl_io + let mut parl_io_tx = parl_io .tx .with_config( pin_conf, @@ -133,10 +99,17 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(parl_io_task(parl_io_tx)).ok(); - }); + let buffer = dma_buffer(); + for i in 0..buffer.len() { + buffer[i] = (i % 255) as u8; + } + + loop { + parl_io_tx.write_dma_async(buffer).await.unwrap(); + println!("Transferred {} bytes", buffer.len()); + + Timer::after(Duration::from_millis(500)).await; + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32c6-hal/examples/embassy_rmt_rx.rs b/esp32c6-hal/examples/embassy_rmt_rx.rs index 5a96acd6866..5c9ad9c9ce4 100644 --- a/esp32c6-hal/examples/embassy_rmt_rx.rs +++ b/esp32c6-hal/examples/embassy_rmt_rx.rs @@ -7,24 +7,65 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c6_hal::{ clock::ClockControl, embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::RxChannelAsync, Channel2, PulseCode, RxChannelConfig, RxChannelCreator}, + rmt::{asynch::RxChannelAsync, PulseCode, RxChannelConfig, RxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::{print, println}; -use static_cell::make_static; const WIDTH: usize = 80; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel2<2>) { +#[main] +async fn main(_spawner: Spawner) -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger_from_env(); + println!("Init!"); + let peripherals = Peripherals::take(); + let system = peripherals.SYSTEM.split(); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + #[cfg(feature = "embassy-time-systick")] + embassy::init( + &clocks, + esp32c6_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), + ); + + #[cfg(feature = "embassy-time-timg0")] + embassy::init( + &clocks, + esp32c6_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, + ); + + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + + let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); + + let mut channel = rmt + .channel2 + .configure( + io.pins.gpio9, + RxChannelConfig { + clk_divider: 255, + idle_threshold: 10000, + ..RxChannelConfig::default() + }, + ) + .unwrap(); + + // you have to enable the interrupt for async to work + esp32c6_hal::interrupt::enable( + esp32c6_hal::peripherals::Interrupt::RMT, + esp32c6_hal::interrupt::Priority::Priority1, + ) + .unwrap(); + let mut data = [PulseCode { level1: true, length1: 1, @@ -74,54 +115,3 @@ async fn rmt_task(mut channel: Channel2<2>) { println!(); } } - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger_from_env(); - println!("Init!"); - let peripherals = Peripherals::take(); - let system = peripherals.SYSTEM.split(); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let mut clock_control = system.peripheral_clock_control; - - #[cfg(feature = "embassy-time-systick")] - embassy::init( - &clocks, - esp32c6_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), - ); - - #[cfg(feature = "embassy-time-timg0")] - embassy::init( - &clocks, - esp32c6_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks, &mut clock_control).timer0, - ); - - let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - - let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &mut clock_control, &clocks).unwrap(); - - let channel = rmt - .channel2 - .configure( - io.pins.gpio9, - RxChannelConfig { - clk_divider: 255, - idle_threshold: 10000, - ..RxChannelConfig::default() - }, - ) - .unwrap(); - - // you have to enable the interrupt for async to work - esp32c6_hal::interrupt::enable( - esp32c6_hal::peripherals::Interrupt::RMT, - esp32c6_hal::interrupt::Priority::Priority1, - ) - .unwrap(); - - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); -} diff --git a/esp32c6-hal/examples/embassy_rmt_tx.rs b/esp32c6-hal/examples/embassy_rmt_tx.rs index e956c8aaf35..ab76340dd23 100644 --- a/esp32c6-hal/examples/embassy_rmt_tx.rs +++ b/esp32c6-hal/examples/embassy_rmt_tx.rs @@ -5,55 +5,28 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c6_hal::{ clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, - rmt::{asynch::TxChannelAsync, Channel0, PulseCode, TxChannelConfig, TxChannelCreator}, + rmt::{asynch::TxChannelAsync, PulseCode, TxChannelConfig, TxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel0<0>) { - let mut data = [PulseCode { - level1: true, - length1: 200, - level2: false, - length2: 50, - }; 20]; - - data[data.len() - 2] = PulseCode { - level1: true, - length1: 3000, - level2: false, - length2: 500, - }; - data[data.len() - 1] = PulseCode::default(); - - loop { - println!("transmit"); - channel.transmit(&data).await.unwrap(); - println!("transmitted\n"); - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let mut clock_control = system.peripheral_clock_control; #[cfg(feature = "embassy-time-systick")] embassy::init( @@ -64,14 +37,14 @@ fn main() -> ! { #[cfg(feature = "embassy-time-timg0")] embassy::init( &clocks, - esp32c6_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks, &mut clock_control).timer0, + esp32c6_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, ); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &mut clock_control, &clocks).unwrap(); + let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); - let channel = rmt + let mut channel = rmt .channel0 .configure( io.pins.gpio1.into_push_pull_output(), @@ -89,8 +62,25 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); + let mut data = [PulseCode { + level1: true, + length1: 200, + level2: false, + length2: 50, + }; 20]; + + data[data.len() - 2] = PulseCode { + level1: true, + length1: 3000, + level2: false, + length2: 500, + }; + data[data.len() - 1] = PulseCode::default(); + + loop { + println!("transmit"); + channel.transmit(&data).await.unwrap(); + println!("transmitted\n"); + Timer::after(Duration::from_millis(500)).await; + } } diff --git a/esp32c6-hal/examples/embassy_serial.rs b/esp32c6-hal/examples/embassy_serial.rs index 431352cac09..d2080e4cd20 100644 --- a/esp32c6-hal/examples/embassy_serial.rs +++ b/esp32c6-hal/examples/embassy_serial.rs @@ -7,7 +7,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32c6_hal::{ clock::ClockControl, embassy, @@ -19,7 +19,6 @@ use esp32c6_hal::{ use esp_backtrace as _; use esp_hal_common::uart::{config::AtCmdConfig, UartRx, UartTx}; use heapless::Vec; -use static_cell::make_static; // rx_fifo_full_threshold const READ_BUF_SIZE: usize = 64; @@ -61,8 +60,8 @@ async fn reader(mut rx: UartRx<'static, UART0>) { } } -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -89,9 +88,6 @@ fn main() -> ! { interrupt::enable(Interrupt::UART0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(reader(rx)).ok(); - spawner.spawn(writer(tx)).ok(); - }); + spawner.spawn(reader(rx)).ok(); + spawner.spawn(writer(tx)).ok(); } diff --git a/esp32c6-hal/examples/embassy_spi.rs b/esp32c6-hal/examples/embassy_spi.rs index 33b29f774cc..5c751acef79 100644 --- a/esp32c6-hal/examples/embassy_spi.rs +++ b/esp32c6-hal/examples/embassy_spi.rs @@ -18,7 +18,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32c6_hal::{ clock::ClockControl, @@ -27,31 +27,13 @@ use esp32c6_hal::{ gdma::*, peripherals::Peripherals, prelude::*, - spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode}, + spi::{Spi, SpiMode}, IO, }; use esp_backtrace as _; -use static_cell::make_static; -pub type SpiType<'d> = - SpiDma<'d, esp32c6_hal::peripherals::SPI2, esp32c6_hal::gdma::Channel0, FullDuplexMode>; - -#[embassy_executor::task] -async fn spi_task(spi: &'static mut SpiType<'static>) { - let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; - loop { - let mut buffer = [0; 8]; - esp_println::println!("Sending bytes"); - embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer) - .await - .unwrap(); - esp_println::println!("Bytes recieved: {:?}", buffer); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -89,10 +71,10 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; - let spi = make_static!(Spi::new( + let mut spi = Spi::new( peripherals.SPI2, sclk, mosi, @@ -104,13 +86,19 @@ fn main() -> ! { ) .with_dma(dma_channel.configure( false, - descriptors, - rx_descriptors, + &mut descriptors, + &mut rx_descriptors, DmaPriority::Priority0, - ))); + )); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(spi_task(spi)).ok(); - }); + let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; + loop { + let mut buffer = [0; 8]; + esp_println::println!("Sending bytes"); + embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer) + .await + .unwrap(); + esp_println::println!("Bytes recieved: {:?}", buffer); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32c6-hal/examples/embassy_wait.rs b/esp32c6-hal/examples/embassy_wait.rs index faf0aac6bd6..dfd2d86a2ba 100644 --- a/esp32c6-hal/examples/embassy_wait.rs +++ b/esp32c6-hal/examples/embassy_wait.rs @@ -6,32 +6,14 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use embedded_hal_async::digital::Wait; -use esp32c6_hal::{ - clock::ClockControl, - embassy, - gpio::{Gpio9, Input, PullDown}, - peripherals::Peripherals, - prelude::*, - IO, -}; +use esp32c6_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, IO}; use esp_backtrace as _; -use static_cell::make_static; -#[embassy_executor::task] -async fn ping(mut pin: Gpio9>) { - loop { - esp_println::println!("Waiting..."); - pin.wait_for_rising_edge().await.unwrap(); - esp_println::println!("Ping!"); - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -51,7 +33,7 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // GPIO 9 as input - let input = io.pins.gpio9.into_pull_down_input(); + let mut input = io.pins.gpio9.into_pull_down_input(); // Async requires the GPIO interrupt to wake futures esp32c6_hal::interrupt::enable( @@ -60,8 +42,10 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(ping(input)).ok(); - }); + loop { + esp_println::println!("Waiting..."); + input.wait_for_rising_edge().await.unwrap(); + esp_println::println!("Ping!"); + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32h2-hal/examples/embassy_hello_world.rs b/esp32h2-hal/examples/embassy_hello_world.rs index 30e612bd76c..5e07ab3c7d3 100644 --- a/esp32h2-hal/examples/embassy_hello_world.rs +++ b/esp32h2-hal/examples/embassy_hello_world.rs @@ -7,30 +7,21 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32h2_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*}; use esp_backtrace as _; -use static_cell::make_static; #[embassy_executor::task] -async fn run1() { +async fn run() { loop { esp_println::println!("Hello world from embassy using esp-hal-async!"); Timer::after(Duration::from_millis(1_000)).await; } } -#[embassy_executor::task] -async fn run2() { - loop { - esp_println::println!("Bing!"); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -48,9 +39,10 @@ fn main() -> ! { embassy::init(&clocks, timer_group0.timer0); } - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run1()).ok(); - spawner.spawn(run2()).ok(); - }); + spawner.spawn(run()).ok(); + + loop { + esp_println::println!("Bing!"); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32h2-hal/examples/embassy_i2c.rs b/esp32h2-hal/examples/embassy_i2c.rs index 3a613239b1e..2e74fe299eb 100644 --- a/esp32h2-hal/examples/embassy_i2c.rs +++ b/esp32h2-hal/examples/embassy_i2c.rs @@ -14,36 +14,22 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32h2_hal::{ clock::ClockControl, embassy, i2c::I2C, interrupt, - peripherals::{Interrupt, Peripherals, I2C0}, + peripherals::{Interrupt, Peripherals}, prelude::*, IO, }; use esp_backtrace as _; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; -use static_cell::make_static; -#[embassy_executor::task] -async fn run(i2c: I2C<'static, I2C0>) { - let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).await.unwrap(); - lis3dh.set_range(Range::G8).await.unwrap(); - - loop { - let norm = lis3dh.accel_norm().await.unwrap(); - esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); - - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -72,8 +58,13 @@ fn main() -> ! { interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run(i2c0)).ok(); - }); + let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); + lis3dh.set_range(Range::G8).await.unwrap(); + + loop { + let norm = lis3dh.accel_norm().await.unwrap(); + esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); + + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32h2-hal/examples/embassy_i2s_read.rs b/esp32h2-hal/examples/embassy_i2s_read.rs index b66959903f5..9c71ff6cc65 100644 --- a/esp32h2-hal/examples/embassy_i2s_read.rs +++ b/esp32h2-hal/examples/embassy_i2s_read.rs @@ -15,63 +15,27 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32h2_hal::{ clock::ClockControl, dma::DmaPriority, embassy, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sRx, MclkPin, PinsBclkWsDin, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDin, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn i2s_task( - i2s_rx: I2sRx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDin< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32h2_hal::gdma::Channel0, - >, -) { - let buffer = dma_buffer(); - println!("Start"); - - let mut data = [0u8; 5000]; - let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); - loop { - let avail = transaction.available().await; - println!("available {}", avail); - - let count = transaction.pop(&mut data).await.unwrap(); - println!( - "got {} bytes, {:x?}..{:x?}", - count, - &data[..10], - &data[count - 10..count] - ); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -91,8 +55,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -102,8 +66,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -122,10 +86,23 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_rx)).ok(); - }); + let buffer = dma_buffer(); + println!("Start"); + + let mut data = [0u8; 5000]; + let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); + loop { + let avail = transaction.available().await; + println!("available {}", avail); + + let count = transaction.pop(&mut data).await.unwrap(); + println!( + "got {} bytes, {:x?}..{:x?}", + count, + &data[..10], + &data[count - 10..count] + ); + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32h2-hal/examples/embassy_i2s_sound.rs b/esp32h2-hal/examples/embassy_i2s_sound.rs index 7fd36603d80..21b24ac0196 100644 --- a/esp32h2-hal/examples/embassy_i2s_sound.rs +++ b/esp32h2-hal/examples/embassy_i2s_sound.rs @@ -31,22 +31,19 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32h2_hal::{ clock::ClockControl, dma::DmaPriority, embassy, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sTx, MclkPin, PinsBclkWsDout, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDout, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; const SINE: [i16; 64] = [ 0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356, @@ -56,59 +53,13 @@ const SINE: [i16; 64] = [ -28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211, ]; -#[embassy_executor::task] -async fn i2s_task( - i2s_tx: I2sTx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDout< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32h2_hal::gdma::Channel0, - >, -) { - let data = - unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; - - let buffer = dma_buffer(); - let mut idx = 0; - for i in 0..usize::min(data.len(), buffer.len()) { - buffer[i] = data[idx]; - - idx += 1; - - if idx >= data.len() { - idx = 0; - } - } - - let mut filler = [0u8; 10000]; - let mut idx = 32000 % data.len(); - - println!("Start"); - let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); - loop { - for i in 0..filler.len() { - filler[i] = data[(idx + i) % data.len()]; - } - println!("Next"); - - let written = transaction.push(&filler).await.unwrap(); - idx = (idx + written) % data.len(); - println!("written {}", written); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -128,8 +79,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -139,8 +90,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -159,10 +110,36 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let data = + unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; + + let buffer = dma_buffer(); + let mut idx = 0; + for i in 0..usize::min(data.len(), buffer.len()) { + buffer[i] = data[idx]; + + idx += 1; + + if idx >= data.len() { + idx = 0; + } + } + + let mut filler = [0u8; 10000]; + let mut idx = 32000 % data.len(); + + println!("Start"); + let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); + loop { + for i in 0..filler.len() { + filler[i] = data[(idx + i) % data.len()]; + } + println!("Next"); + + let written = transaction.push(&filler).await.unwrap(); + idx = (idx + written) % data.len(); + println!("written {}", written); + } } fn dma_buffer() -> &'static mut [u8; 32000] { diff --git a/esp32h2-hal/examples/embassy_parl_io_rx.rs b/esp32h2-hal/examples/embassy_parl_io_rx.rs index b67580850cd..96ec6226787 100644 --- a/esp32h2-hal/examples/embassy_parl_io_rx.rs +++ b/esp32h2-hal/examples/embassy_parl_io_rx.rs @@ -7,53 +7,28 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32h2_hal::{ clock::ClockControl, dma::DmaPriority, embassy, - gdma::{self, Gdma}, - gpio::{GpioPin, Unknown, IO}, + gdma::Gdma, + gpio::IO, interrupt, - parl_io::{BitPackOrder, NoClkPin, ParlIoRx, ParlIoRxOnly, RxFourBits}, + parl_io::{BitPackOrder, NoClkPin, ParlIoRxOnly, RxFourBits}, peripherals, peripherals::Peripherals, prelude::*, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn parl_io_task( - mut parl_io_rx: ParlIoRx< - 'static, - gdma::Channel0, - RxFourBits< - 'static, - GpioPin, - GpioPin, - GpioPin, - GpioPin, - >, - NoClkPin, - >, -) { - let buffer = dma_buffer(); - loop { - parl_io_rx.read_dma_async(buffer).await.unwrap(); - println!("Received: {:02x?} ...", &buffer[..30]); - - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -70,8 +45,8 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let tx_descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; @@ -82,8 +57,8 @@ fn main() -> ! { peripherals.PARL_IO, dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), 1u32.MHz(), @@ -91,7 +66,7 @@ fn main() -> ! { ) .unwrap(); - let parl_io_rx = parl_io + let mut parl_io_rx = parl_io .rx .with_config(rx_pins, NoClkPin, BitPackOrder::Msb, Some(0xfff)) .unwrap(); @@ -103,10 +78,13 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(parl_io_task(parl_io_rx)).ok(); - }); + let buffer = dma_buffer(); + loop { + parl_io_rx.read_dma_async(buffer).await.unwrap(); + println!("Received: {:02x?} ...", &buffer[..30]); + + Timer::after(Duration::from_millis(500)).await; + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32h2-hal/examples/embassy_parl_io_tx.rs b/esp32h2-hal/examples/embassy_parl_io_tx.rs index 1d07eca1b95..3cc372e00a2 100644 --- a/esp32h2-hal/examples/embassy_parl_io_tx.rs +++ b/esp32h2-hal/examples/embassy_parl_io_tx.rs @@ -11,19 +11,18 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32h2_hal::{ clock::ClockControl, dma::DmaPriority, embassy, - gdma::{self, Gdma}, - gpio::{GpioPin, Unknown, IO}, + gdma::Gdma, + gpio::IO, interrupt, parl_io::{ BitPackOrder, ClkOutPin, - ParlIoTx, ParlIoTxOnly, SampleEdge, TxFourBits, @@ -35,45 +34,12 @@ use esp32h2_hal::{ }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; - -#[embassy_executor::task] -async fn parl_io_task( - mut parl_io_tx: ParlIoTx< - 'static, - gdma::Channel0, - TxPinConfigWithValidPin< - 'static, - TxFourBits< - 'static, - GpioPin, - GpioPin, - GpioPin, - GpioPin, - >, - GpioPin, - >, - ClkOutPin<'static, GpioPin>, - >, -) { - let buffer = dma_buffer(); - for i in 0..buffer.len() { - buffer[i] = (i % 255) as u8; - } - - loop { - parl_io_tx.write_dma_async(buffer).await.unwrap(); - println!("Transferred {} bytes", buffer.len()); - - Timer::after(Duration::from_millis(500)).await; - } -} -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); - let mut system = peripherals.SYSTEM.split(); + let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); #[cfg(feature = "embassy-time-systick")] @@ -90,8 +56,8 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let tx_descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; @@ -104,8 +70,8 @@ fn main() -> ! { peripherals.PARL_IO, dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), 1u32.MHz(), @@ -115,7 +81,7 @@ fn main() -> ! { let clock_pin = ClkOutPin::new(io.pins.gpio6); - let parl_io_tx = parl_io + let mut parl_io_tx = parl_io .tx .with_config( pin_conf, @@ -133,10 +99,17 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(parl_io_task(parl_io_tx)).ok(); - }); + let buffer = dma_buffer(); + for i in 0..buffer.len() { + buffer[i] = (i % 255) as u8; + } + + loop { + parl_io_tx.write_dma_async(buffer).await.unwrap(); + println!("Transferred {} bytes", buffer.len()); + + Timer::after(Duration::from_millis(500)).await; + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32h2-hal/examples/embassy_rmt_rx.rs b/esp32h2-hal/examples/embassy_rmt_rx.rs index 1c62003c52b..6320c308bde 100644 --- a/esp32h2-hal/examples/embassy_rmt_rx.rs +++ b/esp32h2-hal/examples/embassy_rmt_rx.rs @@ -7,24 +7,65 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32h2_hal::{ clock::ClockControl, embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::RxChannelAsync, Channel2, PulseCode, RxChannelConfig, RxChannelCreator}, + rmt::{asynch::RxChannelAsync, PulseCode, RxChannelConfig, RxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::{print, println}; -use static_cell::make_static; const WIDTH: usize = 80; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel2<2>) { +#[main] +async fn main(_spawner: Spawner) -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger_from_env(); + println!("Init!"); + let peripherals = Peripherals::take(); + let system = peripherals.SYSTEM.split(); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + #[cfg(feature = "embassy-time-systick")] + embassy::init( + &clocks, + esp32h2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), + ); + + #[cfg(feature = "embassy-time-timg0")] + embassy::init( + &clocks, + esp32h2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, + ); + + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + + let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); + + let mut channel = rmt + .channel2 + .configure( + io.pins.gpio9, + RxChannelConfig { + clk_divider: 255, + idle_threshold: 10000, + ..RxChannelConfig::default() + }, + ) + .unwrap(); + + // you have to enable the interrupt for async to work + esp32h2_hal::interrupt::enable( + esp32h2_hal::peripherals::Interrupt::RMT, + esp32h2_hal::interrupt::Priority::Priority1, + ) + .unwrap(); + let mut data = [PulseCode { level1: true, length1: 1, @@ -74,54 +115,3 @@ async fn rmt_task(mut channel: Channel2<2>) { println!(); } } - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger_from_env(); - println!("Init!"); - let peripherals = Peripherals::take(); - let system = peripherals.SYSTEM.split(); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let mut clock_control = system.peripheral_clock_control; - - #[cfg(feature = "embassy-time-systick")] - embassy::init( - &clocks, - esp32h2_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), - ); - - #[cfg(feature = "embassy-time-timg0")] - embassy::init( - &clocks, - esp32h2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks, &mut clock_control).timer0, - ); - - let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - - let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &mut clock_control, &clocks).unwrap(); - - let channel = rmt - .channel2 - .configure( - io.pins.gpio9, - RxChannelConfig { - clk_divider: 255, - idle_threshold: 10000, - ..RxChannelConfig::default() - }, - ) - .unwrap(); - - // you have to enable the interrupt for async to work - esp32h2_hal::interrupt::enable( - esp32h2_hal::peripherals::Interrupt::RMT, - esp32h2_hal::interrupt::Priority::Priority1, - ) - .unwrap(); - - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); -} diff --git a/esp32h2-hal/examples/embassy_rmt_tx.rs b/esp32h2-hal/examples/embassy_rmt_tx.rs index e7b5369e04b..17bb227e6af 100644 --- a/esp32h2-hal/examples/embassy_rmt_tx.rs +++ b/esp32h2-hal/examples/embassy_rmt_tx.rs @@ -5,55 +5,28 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32h2_hal::{ clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, - rmt::{asynch::TxChannelAsync, Channel0, PulseCode, TxChannelConfig, TxChannelCreator}, + rmt::{asynch::TxChannelAsync, PulseCode, TxChannelConfig, TxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel0<0>) { - let mut data = [PulseCode { - level1: true, - length1: 200, - level2: false, - length2: 50, - }; 20]; - - data[data.len() - 2] = PulseCode { - level1: true, - length1: 3000, - level2: false, - length2: 500, - }; - data[data.len() - 1] = PulseCode::default(); - - loop { - println!("transmit"); - channel.transmit(&data).await.unwrap(); - println!("transmitted\n"); - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - let mut clock_control = system.peripheral_clock_control; #[cfg(feature = "embassy-time-systick")] embassy::init( @@ -64,14 +37,14 @@ fn main() -> ! { #[cfg(feature = "embassy-time-timg0")] embassy::init( &clocks, - esp32h2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks, &mut clock_control).timer0, + esp32h2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, ); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &mut clock_control, &clocks).unwrap(); + let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); - let channel = rmt + let mut channel = rmt .channel0 .configure( io.pins.gpio1.into_push_pull_output(), @@ -89,8 +62,25 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); + let mut data = [PulseCode { + level1: true, + length1: 200, + level2: false, + length2: 50, + }; 20]; + + data[data.len() - 2] = PulseCode { + level1: true, + length1: 3000, + level2: false, + length2: 500, + }; + data[data.len() - 1] = PulseCode::default(); + + loop { + println!("transmit"); + channel.transmit(&data).await.unwrap(); + println!("transmitted\n"); + Timer::after(Duration::from_millis(500)).await; + } } diff --git a/esp32h2-hal/examples/embassy_serial.rs b/esp32h2-hal/examples/embassy_serial.rs index d562c6e96e9..7522dd7ad58 100644 --- a/esp32h2-hal/examples/embassy_serial.rs +++ b/esp32h2-hal/examples/embassy_serial.rs @@ -7,7 +7,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use esp32h2_hal::{ clock::ClockControl, embassy, @@ -19,7 +19,6 @@ use esp32h2_hal::{ use esp_backtrace as _; use esp_hal_common::uart::{config::AtCmdConfig, UartRx, UartTx}; use heapless::Vec; -use static_cell::make_static; // rx_fifo_full_threshold const READ_BUF_SIZE: usize = 64; @@ -61,8 +60,8 @@ async fn reader(mut rx: UartRx<'static, UART0>) { } } -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -89,9 +88,6 @@ fn main() -> ! { interrupt::enable(Interrupt::UART0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(reader(rx)).ok(); - spawner.spawn(writer(tx)).ok(); - }); + spawner.spawn(reader(rx)).ok(); + spawner.spawn(writer(tx)).ok(); } diff --git a/esp32h2-hal/examples/embassy_spi.rs b/esp32h2-hal/examples/embassy_spi.rs index 606f6b02939..a695c6a8c01 100644 --- a/esp32h2-hal/examples/embassy_spi.rs +++ b/esp32h2-hal/examples/embassy_spi.rs @@ -18,7 +18,7 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32h2_hal::{ clock::ClockControl, @@ -27,31 +27,13 @@ use esp32h2_hal::{ gdma::*, peripherals::Peripherals, prelude::*, - spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode}, + spi::{Spi, SpiMode}, IO, }; use esp_backtrace as _; -use static_cell::make_static; -pub type SpiType<'d> = - SpiDma<'d, esp32h2_hal::peripherals::SPI2, esp32h2_hal::gdma::Channel0, FullDuplexMode>; - -#[embassy_executor::task] -async fn spi_task(spi: &'static mut SpiType<'static>) { - let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; - loop { - let mut buffer = [0; 8]; - esp_println::println!("Sending bytes"); - embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer) - .await - .unwrap(); - esp_println::println!("Bytes recieved: {:?}", buffer); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -89,10 +71,10 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; - let spi = make_static!(Spi::new( + let mut spi = Spi::new( peripherals.SPI2, sclk, mosi, @@ -104,13 +86,19 @@ fn main() -> ! { ) .with_dma(dma_channel.configure( false, - descriptors, - rx_descriptors, + &mut descriptors, + &mut rx_descriptors, DmaPriority::Priority0, - ))); + )); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(spi_task(spi)).ok(); - }); + let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; + loop { + let mut buffer = [0; 8]; + esp_println::println!("Sending bytes"); + embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer) + .await + .unwrap(); + esp_println::println!("Bytes recieved: {:?}", buffer); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32h2-hal/examples/embassy_wait.rs b/esp32h2-hal/examples/embassy_wait.rs index 26cfddef016..54492bb6f10 100644 --- a/esp32h2-hal/examples/embassy_wait.rs +++ b/esp32h2-hal/examples/embassy_wait.rs @@ -6,32 +6,14 @@ #![no_main] #![feature(type_alias_impl_trait)] -use embassy_executor::Executor; +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use embedded_hal_async::digital::Wait; -use esp32h2_hal::{ - clock::ClockControl, - embassy, - gpio::{Gpio9, Input, PullDown}, - peripherals::Peripherals, - prelude::*, - IO, -}; +use esp32h2_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*, IO}; use esp_backtrace as _; -use static_cell::make_static; -#[embassy_executor::task] -async fn ping(mut pin: Gpio9>) { - loop { - esp_println::println!("Waiting..."); - pin.wait_for_rising_edge().await.unwrap(); - esp_println::println!("Ping!"); - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -51,7 +33,7 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // GPIO 9 as input - let input = io.pins.gpio9.into_pull_down_input(); + let mut input = io.pins.gpio9.into_pull_down_input(); // Async requires the GPIO interrupt to wake futures esp32h2_hal::interrupt::enable( @@ -60,8 +42,10 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(ping(input)).ok(); - }); + loop { + esp_println::println!("Waiting..."); + input.wait_for_rising_edge().await.unwrap(); + esp_println::println!("Ping!"); + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32s2-hal/examples/embassy_hello_world.rs b/esp32s2-hal/examples/embassy_hello_world.rs index d607e983932..ba3fad4e3fd 100644 --- a/esp32s2-hal/examples/embassy_hello_world.rs +++ b/esp32s2-hal/examples/embassy_hello_world.rs @@ -7,35 +7,27 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s2_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, }; use esp_backtrace as _; -use static_cell::make_static; use xtensa_atomic_emulation_trap as _; #[embassy_executor::task] -async fn run1() { +async fn run() { loop { esp_println::println!("Hello world from embassy using esp-hal-async!"); Timer::after(Duration::from_millis(1_000)).await; } } -#[embassy_executor::task] -async fn run2() { - loop { - esp_println::println!("Bing!"); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -47,9 +39,10 @@ fn main() -> ! { embassy::init(&clocks, timer_group0.timer0); } - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run1()).ok(); - spawner.spawn(run2()).ok(); - }); + spawner.spawn(run()).ok(); + + loop { + esp_println::println!("Bing!"); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32s2-hal/examples/embassy_i2c.rs b/esp32s2-hal/examples/embassy_i2c.rs index 50162ea2f23..92e0225d0ba 100644 --- a/esp32s2-hal/examples/embassy_i2c.rs +++ b/esp32s2-hal/examples/embassy_i2c.rs @@ -14,35 +14,22 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s2_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, i2c::I2C, interrupt, - peripherals::{Interrupt, Peripherals, I2C0}, + peripherals::{Interrupt, Peripherals}, prelude::*, IO, }; use esp_backtrace as _; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; -use static_cell::make_static; -#[embassy_executor::task] -async fn run(i2c: I2C<'static, I2C0>) { - let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).await.unwrap(); - lis3dh.set_range(Range::G8).await.unwrap(); - - loop { - let norm = lis3dh.accel_norm().await.unwrap(); - esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); - - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -65,8 +52,13 @@ fn main() -> ! { interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run(i2c0)).ok(); - }); + let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); + lis3dh.set_range(Range::G8).await.unwrap(); + + loop { + let norm = lis3dh.accel_norm().await.unwrap(); + esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); + + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32s2-hal/examples/embassy_i2s_read.rs b/esp32s2-hal/examples/embassy_i2s_read.rs index 73e0286a4c5..54041a74f64 100644 --- a/esp32s2-hal/examples/embassy_i2s_read.rs +++ b/esp32s2-hal/examples/embassy_i2s_read.rs @@ -14,13 +14,12 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32s2_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sRx, NoMclk, PinsBclkWsDin, Standard}, + embassy::{self}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, NoMclk, PinsBclkWsDin, Standard}, pdma::Dma, peripherals::Peripherals, prelude::*, @@ -29,43 +28,9 @@ use esp32s2_hal::{ }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn i2s_task( - i2s_rx: I2sRx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDin< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32s2_hal::pdma::I2s0DmaChannel, - >, -) { - let buffer = dma_buffer(); - println!("Start"); - - let mut data = [0u8; 5000]; - let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); - loop { - let avail = transaction.available().await; - println!("available {}", avail); - - let count = transaction.pop(&mut data).await.unwrap(); - println!( - "got {} bytes, {:x?}..{:x?}", - count, - &data[..10], - &data[count - 10..count] - ); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -81,8 +46,8 @@ fn main() -> ! { let dma = Dma::new(system.dma); let dma_channel = dma.i2s0channel; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -92,14 +57,14 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, ); - let i2s_tx = i2s.i2s_rx.with_pins(PinsBclkWsDin::new( + let i2s_rx = i2s.i2s_rx.with_pins(PinsBclkWsDin::new( io.pins.gpio1, io.pins.gpio2, io.pins.gpio3, @@ -112,10 +77,23 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let buffer = dma_buffer(); + println!("Start"); + + let mut data = [0u8; 5000]; + let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); + loop { + let avail = transaction.available().await; + println!("available {}", avail); + + let count = transaction.pop(&mut data).await.unwrap(); + println!( + "got {} bytes, {:x?}..{:x?}", + count, + &data[..10], + &data[count - 10..count] + ); + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32s2-hal/examples/embassy_i2s_sound.rs b/esp32s2-hal/examples/embassy_i2s_sound.rs index 402aedc3edd..f0e1ff846b9 100644 --- a/esp32s2-hal/examples/embassy_i2s_sound.rs +++ b/esp32s2-hal/examples/embassy_i2s_sound.rs @@ -31,13 +31,12 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32s2_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sTx, NoMclk, PinsBclkWsDout, Standard}, + embassy::{self}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, NoMclk, PinsBclkWsDout, Standard}, pdma::Dma, peripherals::Peripherals, prelude::*, @@ -46,7 +45,6 @@ use esp32s2_hal::{ }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; const SINE: [i16; 64] = [ 0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356, @@ -56,55 +54,8 @@ const SINE: [i16; 64] = [ -28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211, ]; -#[embassy_executor::task] -async fn i2s_task( - i2s_tx: I2sTx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDout< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32s2_hal::pdma::I2s0DmaChannel, - >, -) { - let data = - unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; - - let buffer = dma_buffer(); - let mut idx = 0; - for i in 0..usize::min(data.len(), buffer.len()) { - buffer[i] = data[idx]; - - idx += 1; - - if idx >= data.len() { - idx = 0; - } - } - - let mut filler = [0u8; 10000]; - let mut idx = 32000 % data.len(); - - println!("Start"); - let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); - - loop { - for i in 0..filler.len() { - filler[i] = data[(idx + i) % data.len()]; - } - println!("Next"); - - let written = transaction.push(&filler).await.unwrap(); - idx = (idx + written) % data.len(); - println!("written {}", written); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -120,8 +71,8 @@ fn main() -> ! { let dma = Dma::new(system.dma); let dma_channel = dma.i2s0channel; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -131,8 +82,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -151,10 +102,37 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let data = + unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; + + let buffer = dma_buffer(); + let mut idx = 0; + for i in 0..usize::min(data.len(), buffer.len()) { + buffer[i] = data[idx]; + + idx += 1; + + if idx >= data.len() { + idx = 0; + } + } + + let mut filler = [0u8; 10000]; + let mut idx = 32000 % data.len(); + + println!("Start"); + let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); + + loop { + for i in 0..filler.len() { + filler[i] = data[(idx + i) % data.len()]; + } + println!("Next"); + + let written = transaction.push(&filler).await.unwrap(); + idx = (idx + written) % data.len(); + println!("written {}", written); + } } fn dma_buffer() -> &'static mut [u8; 32000] { diff --git a/esp32s2-hal/examples/embassy_rmt_rx.rs b/esp32s2-hal/examples/embassy_rmt_rx.rs index a685cc779e2..b9687b34fc2 100644 --- a/esp32s2-hal/examples/embassy_rmt_rx.rs +++ b/esp32s2-hal/examples/embassy_rmt_rx.rs @@ -5,20 +5,20 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s2_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::RxChannelAsync, Channel2, PulseCode, RxChannelConfig, RxChannelCreator}, + rmt::{asynch::RxChannelAsync, PulseCode, RxChannelConfig, RxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_hal_common::gpio::{Gpio15, Output, PushPull}; use esp_println::{print, println}; -use static_cell::make_static; use xtensa_atomic_emulation_trap as _; const WIDTH: usize = 80; @@ -27,7 +27,57 @@ const WIDTH: usize = 80; compile_error!("Run this example in release mode"); #[embassy_executor::task] -async fn rmt_task(mut channel: Channel2<2>) { +async fn signal_task(mut pin: Gpio15>) { + loop { + for _ in 0..10 { + pin.toggle().unwrap(); + Timer::after(Duration::from_micros(10)).await; + } + Timer::after(Duration::from_millis(1000)).await; + } +} + +#[main] +async fn main(spawner: Spawner) -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger_from_env(); + println!("Init!"); + let peripherals = Peripherals::take(); + let system = peripherals.SYSTEM.split(); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + #[cfg(feature = "embassy-time-timg0")] + { + let timer_group0 = esp32s2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks); + embassy::init(&clocks, timer_group0.timer0); + } + + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + + let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap(); + + let mut channel = rmt + .channel2 + .configure( + io.pins.gpio4, + RxChannelConfig { + clk_divider: 1, + idle_threshold: 0b111_1111_1111_1111, + ..RxChannelConfig::default() + }, + ) + .unwrap(); + + // you have to enable the interrupt for async to work + esp32s2_hal::interrupt::enable( + esp32s2_hal::peripherals::Interrupt::RMT, + esp32s2_hal::interrupt::Priority::Priority1, + ) + .unwrap(); + + spawner + .spawn(signal_task(io.pins.gpio15.into_push_pull_output())) + .ok(); let mut data = [PulseCode { level1: true, length1: 1, @@ -76,61 +126,3 @@ async fn rmt_task(mut channel: Channel2<2>) { println!(); } } - -#[embassy_executor::task] -async fn signal_task(mut pin: Gpio15>) { - loop { - for _ in 0..10 { - pin.toggle().unwrap(); - Timer::after(Duration::from_micros(10)).await; - } - Timer::after(Duration::from_millis(1000)).await; - } -} - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger_from_env(); - println!("Init!"); - let peripherals = Peripherals::take(); - let system = peripherals.SYSTEM.split(); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - - #[cfg(feature = "embassy-time-timg0")] - { - let timer_group0 = esp32s2_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks); - embassy::init(&clocks, timer_group0.timer0); - } - - let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - - let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap(); - - let channel = rmt - .channel2 - .configure( - io.pins.gpio4, - RxChannelConfig { - clk_divider: 1, - idle_threshold: 0b111_1111_1111_1111, - ..RxChannelConfig::default() - }, - ) - .unwrap(); - - // you have to enable the interrupt for async to work - esp32s2_hal::interrupt::enable( - esp32s2_hal::peripherals::Interrupt::RMT, - esp32s2_hal::interrupt::Priority::Priority1, - ) - .unwrap(); - - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - spawner - .spawn(signal_task(io.pins.gpio15.into_push_pull_output())) - .ok(); - }); -} diff --git a/esp32s2-hal/examples/embassy_rmt_tx.rs b/esp32s2-hal/examples/embassy_rmt_tx.rs index 2aff40258fc..671591482cc 100644 --- a/esp32s2-hal/examples/embassy_rmt_tx.rs +++ b/esp32s2-hal/examples/embassy_rmt_tx.rs @@ -5,48 +5,23 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s2_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::TxChannelAsync, Channel0, PulseCode, TxChannelConfig, TxChannelCreator}, + rmt::{asynch::TxChannelAsync, PulseCode, TxChannelConfig, TxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; use xtensa_atomic_emulation_trap as _; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel0<0>) { - let mut data = [PulseCode { - level1: true, - length1: 200, - level2: false, - length2: 50, - }; 20]; - - data[data.len() - 2] = PulseCode { - level1: true, - length1: 3000, - level2: false, - length2: 500, - }; - data[data.len() - 1] = PulseCode::default(); - - loop { - println!("transmit"); - channel.transmit(&data).await.unwrap(); - println!("transmitted\n"); - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -64,7 +39,7 @@ fn main() -> ! { let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).unwrap(); - let channel = rmt + let mut channel = rmt .channel0 .configure( io.pins.gpio4.into_push_pull_output(), @@ -82,8 +57,25 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); + let mut data = [PulseCode { + level1: true, + length1: 200, + level2: false, + length2: 50, + }; 20]; + + data[data.len() - 2] = PulseCode { + level1: true, + length1: 3000, + level2: false, + length2: 500, + }; + data[data.len() - 1] = PulseCode::default(); + + loop { + println!("transmit"); + channel.transmit(&data).await.unwrap(); + println!("transmitted\n"); + Timer::after(Duration::from_millis(500)).await; + } } diff --git a/esp32s2-hal/examples/embassy_serial.rs b/esp32s2-hal/examples/embassy_serial.rs index 253c2f7c882..fbc0a7df6af 100644 --- a/esp32s2-hal/examples/embassy_serial.rs +++ b/esp32s2-hal/examples/embassy_serial.rs @@ -7,9 +7,10 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32s2_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, interrupt, peripherals::{Interrupt, Peripherals, UART0}, prelude::*, @@ -18,7 +19,6 @@ use esp32s2_hal::{ use esp_backtrace as _; use esp_hal_common::uart::{config::AtCmdConfig, UartRx, UartTx}; use heapless::Vec; -use static_cell::make_static; // rx_fifo_full_threshold const READ_BUF_SIZE: usize = 64; @@ -60,8 +60,8 @@ async fn reader(mut rx: UartRx<'static, UART0>) { } } -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -82,9 +82,6 @@ fn main() -> ! { interrupt::enable(Interrupt::UART0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(reader(rx)).ok(); - spawner.spawn(writer(tx)).ok(); - }); + spawner.spawn(reader(rx)).ok(); + spawner.spawn(writer(tx)).ok(); } diff --git a/esp32s2-hal/examples/embassy_spi.rs b/esp32s2-hal/examples/embassy_spi.rs index f30835e4d50..e984f712d4e 100644 --- a/esp32s2-hal/examples/embassy_spi.rs +++ b/esp32s2-hal/examples/embassy_spi.rs @@ -18,38 +18,22 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s2_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, + embassy::{self}, pdma::*, peripherals::Peripherals, prelude::*, - spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode}, + spi::{Spi, SpiMode}, IO, }; use esp_backtrace as _; -use static_cell::make_static; -pub type SpiType<'d> = SpiDma<'d, esp32s2_hal::peripherals::SPI2, Spi2DmaChannel, FullDuplexMode>; - -#[embassy_executor::task] -async fn spi_task(spi: &'static mut SpiType<'static>) { - let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; - loop { - let mut buffer = [0; 8]; - esp_println::println!("Sending bytes"); - embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer) - .await - .unwrap(); - esp_println::println!("Bytes recieved: {:?}", buffer); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -76,10 +60,10 @@ fn main() -> ! { let dma = Dma::new(system.dma); let dma_channel = dma.spi2channel; - let descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; - let spi = make_static!(Spi::new( + let mut spi = Spi::new( peripherals.SPI2, sclk, mosi, @@ -91,13 +75,19 @@ fn main() -> ! { ) .with_dma(dma_channel.configure( false, - descriptors, - rx_descriptors, + &mut descriptors, + &mut rx_descriptors, DmaPriority::Priority0, - ))); + )); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(spi_task(spi)).ok(); - }); + let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; + loop { + let mut buffer = [0; 8]; + esp_println::println!("Sending bytes"); + embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer) + .await + .unwrap(); + esp_println::println!("Bytes recieved: {:?}", buffer); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32s2-hal/examples/embassy_wait.rs b/esp32s2-hal/examples/embassy_wait.rs index fee21c9d2f6..6d5ec439e4e 100644 --- a/esp32s2-hal/examples/embassy_wait.rs +++ b/esp32s2-hal/examples/embassy_wait.rs @@ -6,31 +6,20 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use embedded_hal_async::digital::Wait; use esp32s2_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, - gpio::{Gpio0, Input, PullDown}, + embassy::{self}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; -use static_cell::make_static; -#[embassy_executor::task] -async fn ping(mut pin: Gpio0>) { - loop { - esp_println::println!("Waiting..."); - pin.wait_for_rising_edge().await.unwrap(); - esp_println::println!("Ping!"); - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -44,7 +33,7 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // GPIO 0 as input - let input = io.pins.gpio0.into_pull_down_input(); + let mut input = io.pins.gpio0.into_pull_down_input(); // Async requires the GPIO interrupt to wake futures esp32s2_hal::interrupt::enable( @@ -53,8 +42,10 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(ping(input)).ok(); - }); + loop { + esp_println::println!("Waiting..."); + input.wait_for_rising_edge().await.unwrap(); + esp_println::println!("Ping!"); + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32s3-hal/examples/embassy_hello_world.rs b/esp32s3-hal/examples/embassy_hello_world.rs index 2d8b5f0bdf1..1244470c305 100644 --- a/esp32s3-hal/examples/embassy_hello_world.rs +++ b/esp32s3-hal/examples/embassy_hello_world.rs @@ -7,34 +7,26 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s3_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, }; use esp_backtrace as _; -use static_cell::make_static; #[embassy_executor::task] -async fn run1() { +async fn run() { loop { esp_println::println!("Hello world from embassy using esp-hal-async!"); Timer::after(Duration::from_millis(1_000)).await; } } -#[embassy_executor::task] -async fn run2() { - loop { - esp_println::println!("Bing!"); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -52,9 +44,10 @@ fn main() -> ! { embassy::init(&clocks, timer_group0.timer0); } - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run1()).ok(); - spawner.spawn(run2()).ok(); - }); + spawner.spawn(run()).ok(); + + loop { + esp_println::println!("Bing!"); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32s3-hal/examples/embassy_i2c.rs b/esp32s3-hal/examples/embassy_i2c.rs index c4511d70cd2..60002c5a444 100644 --- a/esp32s3-hal/examples/embassy_i2c.rs +++ b/esp32s3-hal/examples/embassy_i2c.rs @@ -14,35 +14,22 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s3_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, i2c::I2C, interrupt, - peripherals::{Interrupt, Peripherals, I2C0}, + peripherals::{Interrupt, Peripherals}, prelude::*, IO, }; use esp_backtrace as _; use lis3dh_async::{Lis3dh, Range, SlaveAddr}; -use static_cell::make_static; -#[embassy_executor::task] -async fn run(i2c: I2C<'static, I2C0>) { - let mut lis3dh = Lis3dh::new_i2c(i2c, SlaveAddr::Alternate).await.unwrap(); - lis3dh.set_range(Range::G8).await.unwrap(); - - loop { - let norm = lis3dh.accel_norm().await.unwrap(); - esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); - - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -71,8 +58,13 @@ fn main() -> ! { interrupt::enable(Interrupt::I2C_EXT0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(run(i2c0)).ok(); - }); + let mut lis3dh = Lis3dh::new_i2c(i2c0, SlaveAddr::Alternate).await.unwrap(); + lis3dh.set_range(Range::G8).await.unwrap(); + + loop { + let norm = lis3dh.accel_norm().await.unwrap(); + esp_println::println!("X: {:+.5} Y: {:+.5} Z: {:+.5}", norm.x, norm.y, norm.z); + + Timer::after(Duration::from_millis(100)).await; + } } diff --git a/esp32s3-hal/examples/embassy_i2s_read.rs b/esp32s3-hal/examples/embassy_i2s_read.rs index 111287aac78..1e92099126a 100644 --- a/esp32s3-hal/examples/embassy_i2s_read.rs +++ b/esp32s3-hal/examples/embassy_i2s_read.rs @@ -15,57 +15,22 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32s3_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, + embassy::{self}, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sRx, MclkPin, PinsBclkWsDin, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDin, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn i2s_task( - i2s_rx: I2sRx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDin< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32s3_hal::gdma::Channel0, - >, -) { - let buffer = dma_buffer(); - println!("Start"); - - let mut data = [0u8; 5000]; - let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); - loop { - let avail = transaction.available().await; - println!("available {}", avail); - - let count = transaction.pop(&mut data).await.unwrap(); - println!( - "got {} bytes, {:x?}..{:x?}", - count, - &data[..10], - &data[count - 10..count] - ); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -90,8 +55,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -101,8 +66,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -121,10 +86,23 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_rx)).ok(); - }); + let buffer = dma_buffer(); + println!("Start"); + + let mut data = [0u8; 5000]; + let mut transaction = i2s_rx.read_dma_circular_async(buffer).unwrap(); + loop { + let avail = transaction.available().await; + println!("available {}", avail); + + let count = transaction.pop(&mut data).await.unwrap(); + println!( + "got {} bytes, {:x?}..{:x?}", + count, + &data[..10], + &data[count - 10..count] + ); + } } fn dma_buffer() -> &'static mut [u8; 4092 * 4] { diff --git a/esp32s3-hal/examples/embassy_i2s_sound.rs b/esp32s3-hal/examples/embassy_i2s_sound.rs index 59e676fef64..5112f03ad50 100644 --- a/esp32s3-hal/examples/embassy_i2s_sound.rs +++ b/esp32s3-hal/examples/embassy_i2s_sound.rs @@ -31,21 +31,19 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32s3_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, + embassy::{self}, gdma::Gdma, - gpio::GpioPin, - i2s, - i2s::{asynch::*, DataFormat, I2s, I2s0New, I2sTx, MclkPin, PinsBclkWsDout, Standard}, + i2s::{asynch::*, DataFormat, I2s, I2s0New, MclkPin, PinsBclkWsDout, Standard}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; const SINE: [i16; 64] = [ 0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356, @@ -55,54 +53,8 @@ const SINE: [i16; 64] = [ -28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211, ]; -#[embassy_executor::task] -async fn i2s_task( - i2s_tx: I2sTx< - 'static, - i2s::I2sPeripheral0, - PinsBclkWsDout< - 'static, - GpioPin, - GpioPin, - GpioPin, - >, - esp32s3_hal::gdma::Channel0, - >, -) { - let data = - unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; - - let buffer = dma_buffer(); - let mut idx = 0; - for i in 0..usize::min(data.len(), buffer.len()) { - buffer[i] = data[idx]; - - idx += 1; - - if idx >= data.len() { - idx = 0; - } - } - - let mut filler = [0u8; 10000]; - let mut idx = 32000 % data.len(); - - println!("Start"); - let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); - loop { - for i in 0..filler.len() { - filler[i] = data[(idx + i) % data.len()]; - } - println!("Next"); - - let written = transaction.push(&filler).await.unwrap(); - idx = (idx + written) % data.len(); - println!("written {}", written); - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -127,8 +79,8 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let tx_descriptors = make_static!([0u32; 20 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut tx_descriptors = [0u32; 20 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; let i2s = I2s::new( peripherals.I2S0, @@ -138,8 +90,8 @@ fn main() -> ! { 44100u32.Hz(), dma_channel.configure( false, - tx_descriptors, - rx_descriptors, + &mut tx_descriptors, + &mut rx_descriptors, DmaPriority::Priority0, ), &clocks, @@ -158,10 +110,36 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(i2s_task(i2s_tx)).ok(); - }); + let data = + unsafe { core::slice::from_raw_parts(&SINE as *const _ as *const u8, SINE.len() * 2) }; + + let buffer = dma_buffer(); + let mut idx = 0; + for i in 0..usize::min(data.len(), buffer.len()) { + buffer[i] = data[idx]; + + idx += 1; + + if idx >= data.len() { + idx = 0; + } + } + + let mut filler = [0u8; 10000]; + let mut idx = 32000 % data.len(); + + println!("Start"); + let mut transaction = i2s_tx.write_dma_circular_async(buffer).unwrap(); + loop { + for i in 0..filler.len() { + filler[i] = data[(idx + i) % data.len()]; + } + println!("Next"); + + let written = transaction.push(&filler).await.unwrap(); + idx = (idx + written) % data.len(); + println!("written {}", written); + } } fn dma_buffer() -> &'static mut [u8; 32000] { diff --git a/esp32s3-hal/examples/embassy_multicore.rs b/esp32s3-hal/examples/embassy_multicore.rs index aaef5964456..03e57485d3e 100644 --- a/esp32s3-hal/examples/embassy_multicore.rs +++ b/esp32s3-hal/examples/embassy_multicore.rs @@ -6,6 +6,7 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, signal::Signal}; use embassy_time::{Duration, Ticker}; use esp32s3_hal::{ @@ -42,27 +43,8 @@ async fn control_led( } } -/// Sends periodic messages to control_led, enabling or disabling it. -#[embassy_executor::task] -async fn enable_disable_led(control: &'static Signal) { - println!( - "Starting enable_disable_led() on core {}", - get_core() as usize - ); - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - esp_println::println!("Sending LED on"); - control.signal(true); - ticker.next().await; - - esp_println::println!("Sending LED off"); - control.signal(false); - ticker.next().await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); @@ -97,8 +79,19 @@ fn main() -> ! { .start_app_core(unsafe { &mut APP_CORE_STACK }, cpu1_fnctn) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(enable_disable_led(led_ctrl_signal)).ok(); - }); + // Sends periodic messages to control_led, enabling or disabling it. + println!( + "Starting enable_disable_led() on core {}", + get_core() 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; + } } diff --git a/esp32s3-hal/examples/embassy_rmt_rx.rs b/esp32s3-hal/examples/embassy_rmt_rx.rs index f3d04517b03..72e1051cc3f 100644 --- a/esp32s3-hal/examples/embassy_rmt_rx.rs +++ b/esp32s3-hal/examples/embassy_rmt_rx.rs @@ -7,23 +7,65 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32s3_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::RxChannelAsync, Channel4, PulseCode, RxChannelConfig, RxChannelCreator}, + rmt::{asynch::RxChannelAsync, PulseCode, RxChannelConfig, RxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::{print, println}; -use static_cell::make_static; const WIDTH: usize = 80; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel4<4>) { +#[main] +async fn main(_spawner: Spawner) -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger_from_env(); + println!("Init!"); + let peripherals = Peripherals::take(); + let system = peripherals.SYSTEM.split(); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + #[cfg(feature = "embassy-time-systick")] + embassy::init( + &clocks, + esp32s3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), + ); + + #[cfg(feature = "embassy-time-timg0")] + { + let timer_group0 = esp32s3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks); + embassy::init(&clocks, timer_group0.timer0); + } + + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); + + let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); + + let mut channel = rmt + .channel4 + .configure( + io.pins.gpio0, + RxChannelConfig { + clk_divider: 255, + idle_threshold: 10000, + ..RxChannelConfig::default() + }, + ) + .unwrap(); + + // you have to enable the interrupt for async to work + esp32s3_hal::interrupt::enable( + esp32s3_hal::peripherals::Interrupt::RMT, + esp32s3_hal::interrupt::Priority::Priority1, + ) + .unwrap(); + let mut data = [PulseCode { level1: true, length1: 1, @@ -73,53 +115,3 @@ async fn rmt_task(mut channel: Channel4<4>) { println!(); } } - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger_from_env(); - println!("Init!"); - let peripherals = Peripherals::take(); - let system = peripherals.SYSTEM.split(); - let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); - - #[cfg(feature = "embassy-time-systick")] - embassy::init( - &clocks, - esp32s3_hal::systimer::SystemTimer::new(peripherals.SYSTIMER), - ); - - #[cfg(feature = "embassy-time-timg0")] - { - let timer_group0 = esp32s3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks); - embassy::init(&clocks, timer_group0.timer0); - } - - let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - - let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); - - let channel = rmt - .channel4 - .configure( - io.pins.gpio0, - RxChannelConfig { - clk_divider: 255, - idle_threshold: 10000, - ..RxChannelConfig::default() - }, - ) - .unwrap(); - - // you have to enable the interrupt for async to work - esp32s3_hal::interrupt::enable( - esp32s3_hal::peripherals::Interrupt::RMT, - esp32s3_hal::interrupt::Priority::Priority1, - ) - .unwrap(); - - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); -} diff --git a/esp32s3-hal/examples/embassy_rmt_tx.rs b/esp32s3-hal/examples/embassy_rmt_tx.rs index 99f03721b84..2d30c97edbc 100644 --- a/esp32s3-hal/examples/embassy_rmt_tx.rs +++ b/esp32s3-hal/examples/embassy_rmt_tx.rs @@ -5,47 +5,22 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s3_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, peripherals::Peripherals, prelude::*, - rmt::{asynch::TxChannelAsync, Channel0, PulseCode, TxChannelConfig, TxChannelCreator}, + rmt::{asynch::TxChannelAsync, PulseCode, TxChannelConfig, TxChannelCreator}, Rmt, IO, }; use esp_backtrace as _; use esp_println::println; -use static_cell::make_static; -#[embassy_executor::task] -async fn rmt_task(mut channel: Channel0<0>) { - let mut data = [PulseCode { - level1: true, - length1: 200, - level2: false, - length2: 50, - }; 20]; - - data[data.len() - 2] = PulseCode { - level1: true, - length1: 3000, - level2: false, - length2: 500, - }; - data[data.len() - 1] = PulseCode::default(); - - loop { - println!("transmit"); - channel.transmit(&data).await.unwrap(); - println!("transmitted\n"); - Timer::after(Duration::from_millis(500)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { #[cfg(feature = "log")] esp_println::logger::init_logger_from_env(); println!("Init!"); @@ -69,7 +44,7 @@ fn main() -> ! { let rmt = Rmt::new(peripherals.RMT, 8u32.MHz(), &clocks).unwrap(); - let channel = rmt + let mut channel = rmt .channel0 .configure( io.pins.gpio1.into_push_pull_output(), @@ -87,8 +62,25 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(rmt_task(channel)).ok(); - }); + let mut data = [PulseCode { + level1: true, + length1: 200, + level2: false, + length2: 50, + }; 20]; + + data[data.len() - 2] = PulseCode { + level1: true, + length1: 3000, + level2: false, + length2: 500, + }; + data[data.len() - 1] = PulseCode::default(); + + loop { + println!("transmit"); + channel.transmit(&data).await.unwrap(); + println!("transmitted\n"); + Timer::after(Duration::from_millis(500)).await; + } } diff --git a/esp32s3-hal/examples/embassy_serial.rs b/esp32s3-hal/examples/embassy_serial.rs index 000fff25e84..788774dd3f4 100644 --- a/esp32s3-hal/examples/embassy_serial.rs +++ b/esp32s3-hal/examples/embassy_serial.rs @@ -7,9 +7,10 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use esp32s3_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, + embassy::{self}, interrupt, peripherals::{Interrupt, Peripherals, UART0}, prelude::*, @@ -18,7 +19,6 @@ use esp32s3_hal::{ use esp_backtrace as _; use esp_hal_common::uart::{config::AtCmdConfig, UartRx, UartTx}; use heapless::Vec; -use static_cell::make_static; // rx_fifo_full_threshold const READ_BUF_SIZE: usize = 64; @@ -60,8 +60,8 @@ async fn reader(mut rx: UartRx<'static, UART0>) { } } -#[entry] -fn main() -> ! { +#[main] +async fn main(spawner: Spawner) { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -88,9 +88,6 @@ fn main() -> ! { interrupt::enable(Interrupt::UART0, interrupt::Priority::Priority1).unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(reader(rx)).ok(); - spawner.spawn(writer(tx)).ok(); - }); + spawner.spawn(reader(rx)).ok(); + spawner.spawn(writer(tx)).ok(); } diff --git a/esp32s3-hal/examples/embassy_spi.rs b/esp32s3-hal/examples/embassy_spi.rs index 5f71bb765cd..1e3fbecb3f4 100644 --- a/esp32s3-hal/examples/embassy_spi.rs +++ b/esp32s3-hal/examples/embassy_spi.rs @@ -18,40 +18,22 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use esp32s3_hal::{ clock::ClockControl, dma::DmaPriority, - embassy::{self, executor::Executor}, + embassy::{self}, gdma::*, peripherals::Peripherals, prelude::*, - spi::{dma::SpiDma, FullDuplexMode, Spi, SpiMode}, + spi::{Spi, SpiMode}, IO, }; use esp_backtrace as _; -use static_cell::make_static; -// This example uses SPI3 to test that WithDmaSpi3 is included in the prelude. -pub type SpiType<'d> = - SpiDma<'d, esp32s3_hal::peripherals::SPI3, esp32s3_hal::gdma::Channel0, FullDuplexMode>; - -#[embassy_executor::task] -async fn spi_task(spi: &'static mut SpiType<'static>) { - let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; - loop { - let mut buffer = [0; 8]; - esp_println::println!("Sending bytes"); - embedded_hal_async::spi::SpiBus::transfer(spi, &mut buffer, &send_buffer) - .await - .unwrap(); - esp_println::println!("Bytes recieved: {:?}", buffer); - Timer::after(Duration::from_millis(5_000)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -89,10 +71,10 @@ fn main() -> ! { let dma = Gdma::new(peripherals.DMA); let dma_channel = dma.channel0; - let descriptors = make_static!([0u32; 8 * 3]); - let rx_descriptors = make_static!([0u32; 8 * 3]); + let mut descriptors = [0u32; 8 * 3]; + let mut rx_descriptors = [0u32; 8 * 3]; - let spi = make_static!(Spi::new( + let mut spi = Spi::new( peripherals.SPI3, sclk, mosi, @@ -104,13 +86,19 @@ fn main() -> ! { ) .with_dma(dma_channel.configure( false, - descriptors, - rx_descriptors, + &mut descriptors, + &mut rx_descriptors, DmaPriority::Priority0, - ))); + )); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(spi_task(spi)).ok(); - }); + let send_buffer = [0, 1, 2, 3, 4, 5, 6, 7]; + loop { + let mut buffer = [0; 8]; + esp_println::println!("Sending bytes"); + embedded_hal_async::spi::SpiBus::transfer(&mut spi, &mut buffer, &send_buffer) + .await + .unwrap(); + esp_println::println!("Bytes recieved: {:?}", buffer); + Timer::after(Duration::from_millis(5_000)).await; + } } diff --git a/esp32s3-hal/examples/embassy_wait.rs b/esp32s3-hal/examples/embassy_wait.rs index 5d4351c81a3..59ab653a2d8 100644 --- a/esp32s3-hal/examples/embassy_wait.rs +++ b/esp32s3-hal/examples/embassy_wait.rs @@ -6,31 +6,20 @@ #![no_main] #![feature(type_alias_impl_trait)] +use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use embedded_hal_async::digital::Wait; use esp32s3_hal::{ clock::ClockControl, - embassy::{self, executor::Executor}, - gpio::{Gpio0, Input, PullDown}, + embassy::{self}, peripherals::Peripherals, prelude::*, IO, }; use esp_backtrace as _; -use static_cell::make_static; -#[embassy_executor::task] -async fn ping(mut pin: Gpio0>) { - loop { - esp_println::println!("Waiting..."); - pin.wait_for_rising_edge().await.unwrap(); - esp_println::println!("Ping!"); - Timer::after(Duration::from_millis(100)).await; - } -} - -#[entry] -fn main() -> ! { +#[main] +async fn main(_spawner: Spawner) -> ! { esp_println::println!("Init!"); let peripherals = Peripherals::take(); let system = peripherals.SYSTEM.split(); @@ -50,7 +39,7 @@ fn main() -> ! { let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); // GPIO 0 as input - let input = io.pins.gpio0.into_pull_down_input(); + let mut input = io.pins.gpio0.into_pull_down_input(); // Async requires the GPIO interrupt to wake futures esp32s3_hal::interrupt::enable( @@ -59,8 +48,10 @@ fn main() -> ! { ) .unwrap(); - let executor = make_static!(Executor::new()); - executor.run(|spawner| { - spawner.spawn(ping(input)).ok(); - }); + loop { + esp_println::println!("Waiting..."); + input.wait_for_rising_edge().await.unwrap(); + esp_println::println!("Ping!"); + Timer::after(Duration::from_millis(100)).await; + } }