diff --git a/README.md b/README.md index c364984e..aa84fb8f 100644 --- a/README.md +++ b/README.md @@ -207,17 +207,11 @@ To build these ensure you are in the `examples-esp32XXX` directory matching your `cargo run --example embassy_esp_now --release --features "async,esp-now"` -### esp_now_duplex - -- broadcasts, sends and asynchronously receives messages via esp-now in multiple embassy tasks - -`cargo run --example esp_now_duplex --release --features "async,esp-now"` - -### esp_now_async_duplex +### embassy_esp_now_duplex - asynchronously broadcasts, receives and sends messages via esp-now in multiple embassy tasks -`cargo run --example esp_now_async_duplex --release --features "async,esp-now"` +`cargo run --example embassy_esp_now_duplex --release --features "async,esp-now"` ### embassy_dhcp diff --git a/examples-esp32/examples/embassy_dhcp.rs b/examples-esp32/examples/embassy_dhcp.rs index 9ed962ed..0411adf9 100644 --- a/examples-esp32/examples/embassy_dhcp.rs +++ b/examples-esp32/examples/embassy_dhcp.rs @@ -20,9 +20,6 @@ use hal::clock::ClockControl; use hal::Rng; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); diff --git a/examples-esp32/examples/embassy_esp_now.rs b/examples-esp32/examples/embassy_esp_now.rs index 597270fd..60e427df 100644 --- a/examples-esp32/examples/embassy_esp_now.rs +++ b/examples-esp32/examples/embassy_esp_now.rs @@ -18,9 +18,6 @@ use hal::clock::ClockControl; use hal::Rng; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - #[embassy_executor::task] async fn run(mut esp_now: EspNow<'static>) { let mut ticker = Ticker::every(Duration::from_secs(5)); diff --git a/examples-esp32/examples/esp_now_async_duplex.rs b/examples-esp32/examples/embassy_esp_now_duplex.rs similarity index 85% rename from examples-esp32/examples/esp_now_async_duplex.rs rename to examples-esp32/examples/embassy_esp_now_duplex.rs index 6c545ee7..618055b4 100644 --- a/examples-esp32/examples/esp_now_async_duplex.rs +++ b/examples-esp32/examples/embassy_esp_now_duplex.rs @@ -5,6 +5,8 @@ use embassy_executor::_export::StaticCell; use embassy_sync::blocking_mutex::raw::NoopRawMutex; use embassy_sync::mutex::Mutex; +#[path = "../../examples-util/util.rs"] +mod examples_util; use examples_util::hal; use embassy_executor::Executor; @@ -14,12 +16,9 @@ use esp_backtrace as _; use esp_println::println; use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; +use hal::clock::ClockControl; use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; +use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; #[embassy_executor::task] async fn broadcaster(sender: &'static Mutex>) { @@ -92,12 +91,15 @@ fn main() -> ! { let peripherals = Peripherals::take(); - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); + let mut system = peripherals.DPORT.split(); + let clocks = ClockControl::max(system.clock_control).freeze(); - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); + let timer = hal::timer::TimerGroup::new( + peripherals.TIMG1, + &clocks, + &mut system.peripheral_clock_control, + ) + .timer0; let init = initialize( EspWifiInitFor::Wifi, timer, @@ -107,11 +109,15 @@ fn main() -> ! { ) .unwrap(); - let wifi = examples_util::get_wifi!(peripherals); + let (wifi, ..) = peripherals.RADIO.split(); let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); println!("esp-now version {}", esp_now.get_version().unwrap()); - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); + let timer_group0 = TimerGroup::new( + peripherals.TIMG0, + &clocks, + &mut system.peripheral_clock_control, + ); embassy::init(&clocks, timer_group0.timer0); let executor = EXECUTOR.init(Executor::new()); diff --git a/examples-esp32/examples/esp_now_duplex.rs b/examples-esp32/examples/esp_now_duplex.rs deleted file mode 100644 index 4b036717..00000000 --- a/examples-esp32/examples/esp_now_duplex.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use core::cell::RefCell; - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::NoopMutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static NoopMutex>>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&BROADCAST_ADDRESS, b"Hello.") - .unwrap() - .wait() - }); - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static NoopMutex>>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&peer.peer_address, b"Hello Peer.") - .unwrap() - .wait() - }); - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(NoopMutex::new(RefCell::new(sender))); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -} diff --git a/examples-esp32c2/examples/embassy_dhcp.rs b/examples-esp32c2/examples/embassy_dhcp.rs index f92079be..8ca6a23a 100644 --- a/examples-esp32c2/examples/embassy_dhcp.rs +++ b/examples-esp32c2/examples/embassy_dhcp.rs @@ -21,9 +21,6 @@ use hal::clock::ClockControl; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; use hal::{systimer::SystemTimer, Rng}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); diff --git a/examples-esp32c2/examples/embassy_esp_now.rs b/examples-esp32c2/examples/embassy_esp_now.rs index 801b6e9f..e15d07b4 100644 --- a/examples-esp32c2/examples/embassy_esp_now.rs +++ b/examples-esp32c2/examples/embassy_esp_now.rs @@ -19,9 +19,6 @@ use hal::clock::ClockControl; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; use hal::{systimer::SystemTimer, Rng}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - #[embassy_executor::task] async fn run(mut esp_now: EspNow<'static>) { let mut ticker = Ticker::every(Duration::from_secs(5)); diff --git a/examples-esp32c6/examples/esp_now_async_duplex.rs b/examples-esp32c2/examples/embassy_esp_now_duplex.rs similarity index 84% rename from examples-esp32c6/examples/esp_now_async_duplex.rs rename to examples-esp32c2/examples/embassy_esp_now_duplex.rs index 6c545ee7..60d1a9d9 100644 --- a/examples-esp32c6/examples/esp_now_async_duplex.rs +++ b/examples-esp32c2/examples/embassy_esp_now_duplex.rs @@ -5,6 +5,8 @@ use embassy_executor::_export::StaticCell; use embassy_sync::blocking_mutex::raw::NoopRawMutex; use embassy_sync::mutex::Mutex; +#[path = "../../examples-util/util.rs"] +mod examples_util; use examples_util::hal; use embassy_executor::Executor; @@ -14,12 +16,11 @@ use esp_backtrace as _; use esp_println::println; use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; +use hal::clock::ClockControl; use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; +use hal::{ + embassy, peripherals::Peripherals, prelude::*, systimer::SystemTimer, timer::TimerGroup, +}; #[embassy_executor::task] async fn broadcaster(sender: &'static Mutex>) { @@ -92,12 +93,10 @@ fn main() -> ! { let peripherals = Peripherals::take(); - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); + let mut system = peripherals.SYSTEM.split(); + let clocks = ClockControl::max(system.clock_control).freeze(); - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); + let timer = SystemTimer::new(peripherals.SYSTIMER).alarm0; let init = initialize( EspWifiInitFor::Wifi, timer, @@ -107,11 +106,15 @@ fn main() -> ! { ) .unwrap(); - let wifi = examples_util::get_wifi!(peripherals); + let (wifi, ..) = peripherals.RADIO.split(); let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); println!("esp-now version {}", esp_now.get_version().unwrap()); - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); + let timer_group0 = TimerGroup::new( + peripherals.TIMG0, + &clocks, + &mut system.peripheral_clock_control, + ); embassy::init(&clocks, timer_group0.timer0); let executor = EXECUTOR.init(Executor::new()); diff --git a/examples-esp32c2/examples/esp_now_duplex.rs b/examples-esp32c2/examples/esp_now_duplex.rs deleted file mode 100644 index 4b036717..00000000 --- a/examples-esp32c2/examples/esp_now_duplex.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use core::cell::RefCell; - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::NoopMutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static NoopMutex>>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&BROADCAST_ADDRESS, b"Hello.") - .unwrap() - .wait() - }); - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static NoopMutex>>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&peer.peer_address, b"Hello Peer.") - .unwrap() - .wait() - }); - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(NoopMutex::new(RefCell::new(sender))); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -} diff --git a/examples-esp32c3/examples/embassy_dhcp.rs b/examples-esp32c3/examples/embassy_dhcp.rs index f92079be..8ca6a23a 100644 --- a/examples-esp32c3/examples/embassy_dhcp.rs +++ b/examples-esp32c3/examples/embassy_dhcp.rs @@ -21,9 +21,6 @@ use hal::clock::ClockControl; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; use hal::{systimer::SystemTimer, Rng}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); diff --git a/examples-esp32c3/examples/embassy_esp_now.rs b/examples-esp32c3/examples/embassy_esp_now.rs index 1df5db17..74640f86 100644 --- a/examples-esp32c3/examples/embassy_esp_now.rs +++ b/examples-esp32c3/examples/embassy_esp_now.rs @@ -19,9 +19,6 @@ use hal::clock::ClockControl; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; use hal::{systimer::SystemTimer, Rng}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - #[embassy_executor::task] async fn run(mut esp_now: EspNow<'static>) { let mut ticker = Ticker::every(Duration::from_secs(5)); diff --git a/examples-esp32c3/examples/embassy_esp_now_duplex.rs b/examples-esp32c3/examples/embassy_esp_now_duplex.rs new file mode 100644 index 00000000..60d1a9d9 --- /dev/null +++ b/examples-esp32c3/examples/embassy_esp_now_duplex.rs @@ -0,0 +1,130 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy_executor::_export::StaticCell; +use embassy_sync::blocking_mutex::raw::NoopRawMutex; +use embassy_sync::mutex::Mutex; +#[path = "../../examples-util/util.rs"] +mod examples_util; +use examples_util::hal; + +use embassy_executor::Executor; +use embassy_time::{Duration, Ticker}; +use esp_backtrace as _; + +use esp_println::println; +use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; +use esp_wifi::{initialize, EspWifiInitFor}; +use hal::clock::ClockControl; +use hal::Rng; +use hal::{ + embassy, peripherals::Peripherals, prelude::*, systimer::SystemTimer, timer::TimerGroup, +}; + +#[embassy_executor::task] +async fn broadcaster(sender: &'static Mutex>) { + let mut ticker = Ticker::every(Duration::from_secs(1)); + loop { + ticker.next().await; + + println!("Send Broadcast..."); + let mut sender = sender.lock().await; + let status = sender.send_async(&BROADCAST_ADDRESS, b"Hello.").await; + println!("Send broadcast status: {:?}", status); + } +} + +#[embassy_executor::task] +async fn sayhello( + manager: &'static EspNowManager<'static>, + sender: &'static Mutex>, +) { + let mut ticker = Ticker::every(Duration::from_millis(500)); + loop { + ticker.next().await; + let peer = match manager.fetch_peer(false) { + Ok(peer) => peer, + Err(_) => { + if let Ok(peer) = manager.fetch_peer(true) { + peer + } else { + continue; + } + } + }; + + println!("Send hello to peer {:?}", peer.peer_address); + let mut sender = sender.lock().await; + let status = sender.send_async(&peer.peer_address, b"Hello Peer.").await; + println!("Send hello status: {:?}", status); + } +} + +#[embassy_executor::task] +async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { + loop { + let r = receiver.receive_async().await; + println!("Received {:?}", r.get_data()); + if r.info.dst_address == BROADCAST_ADDRESS { + if !manager.peer_exists(&r.info.src_address) { + manager + .add_peer(PeerInfo { + peer_address: r.info.src_address, + lmk: None, + channel: None, + encrypt: false, + }) + .unwrap(); + println!("Added peer {:?}", r.info.src_address); + } + } + } +} + +static EXECUTOR: StaticCell = StaticCell::new(); +static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); +static ESP_NOW_SENDER: StaticCell>> = StaticCell::new(); + +#[entry] +fn main() -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger(log::LevelFilter::Info); + + let peripherals = Peripherals::take(); + + let mut system = peripherals.SYSTEM.split(); + let clocks = ClockControl::max(system.clock_control).freeze(); + + let timer = SystemTimer::new(peripherals.SYSTIMER).alarm0; + let init = initialize( + EspWifiInitFor::Wifi, + timer, + Rng::new(peripherals.RNG), + system.radio_clock_control, + &clocks, + ) + .unwrap(); + + let (wifi, ..) = peripherals.RADIO.split(); + let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); + println!("esp-now version {}", esp_now.get_version().unwrap()); + + let timer_group0 = TimerGroup::new( + peripherals.TIMG0, + &clocks, + &mut system.peripheral_clock_control, + ); + embassy::init(&clocks, timer_group0.timer0); + let executor = EXECUTOR.init(Executor::new()); + + let (manager, sender, receiver) = esp_now.split(); + let manager = ESP_NOW_MANAGER.init(manager); + let sender: &'static _ = ESP_NOW_SENDER.init(Mutex::new(sender)); + + executor.run(|spawner| { + spawner.spawn(listener(manager, receiver)).ok(); + spawner.spawn(broadcaster(sender)).ok(); + spawner.spawn(sayhello(manager, sender)).ok(); + }) +} diff --git a/examples-esp32c3/examples/esp_now_duplex.rs b/examples-esp32c3/examples/esp_now_duplex.rs deleted file mode 100644 index 4b036717..00000000 --- a/examples-esp32c3/examples/esp_now_duplex.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use core::cell::RefCell; - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::NoopMutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static NoopMutex>>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&BROADCAST_ADDRESS, b"Hello.") - .unwrap() - .wait() - }); - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static NoopMutex>>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&peer.peer_address, b"Hello Peer.") - .unwrap() - .wait() - }); - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(NoopMutex::new(RefCell::new(sender))); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -} diff --git a/examples-esp32c6/examples/embassy_dhcp.rs b/examples-esp32c6/examples/embassy_dhcp.rs index 41817f28..f323bbc9 100644 --- a/examples-esp32c6/examples/embassy_dhcp.rs +++ b/examples-esp32c6/examples/embassy_dhcp.rs @@ -21,9 +21,6 @@ use hal::clock::ClockControl; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; use hal::{systimer::SystemTimer, Rng}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); diff --git a/examples-esp32c6/examples/embassy_esp_now.rs b/examples-esp32c6/examples/embassy_esp_now.rs index 43106614..fae89b14 100644 --- a/examples-esp32c6/examples/embassy_esp_now.rs +++ b/examples-esp32c6/examples/embassy_esp_now.rs @@ -19,9 +19,6 @@ use hal::clock::ClockControl; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; use hal::{systimer::SystemTimer, Rng}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - #[embassy_executor::task] async fn run(mut esp_now: EspNow<'static>) { let mut ticker = Ticker::every(Duration::from_secs(5)); diff --git a/examples-esp32c6/examples/embassy_esp_now_duplex.rs b/examples-esp32c6/examples/embassy_esp_now_duplex.rs new file mode 100644 index 00000000..7592c216 --- /dev/null +++ b/examples-esp32c6/examples/embassy_esp_now_duplex.rs @@ -0,0 +1,130 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy_executor::_export::StaticCell; +use embassy_sync::blocking_mutex::raw::NoopRawMutex; +use embassy_sync::mutex::Mutex; +#[path = "../../examples-util/util.rs"] +mod examples_util; +use examples_util::hal; + +use embassy_executor::Executor; +use embassy_time::{Duration, Ticker}; +use esp_backtrace as _; + +use esp_println::println; +use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; +use esp_wifi::{initialize, EspWifiInitFor}; +use hal::clock::ClockControl; +use hal::Rng; +use hal::{ + embassy, peripherals::Peripherals, prelude::*, systimer::SystemTimer, timer::TimerGroup, +}; + +#[embassy_executor::task] +async fn broadcaster(sender: &'static Mutex>) { + let mut ticker = Ticker::every(Duration::from_secs(1)); + loop { + ticker.next().await; + + println!("Send Broadcast..."); + let mut sender = sender.lock().await; + let status = sender.send_async(&BROADCAST_ADDRESS, b"Hello.").await; + println!("Send broadcast status: {:?}", status); + } +} + +#[embassy_executor::task] +async fn sayhello( + manager: &'static EspNowManager<'static>, + sender: &'static Mutex>, +) { + let mut ticker = Ticker::every(Duration::from_millis(500)); + loop { + ticker.next().await; + let peer = match manager.fetch_peer(false) { + Ok(peer) => peer, + Err(_) => { + if let Ok(peer) = manager.fetch_peer(true) { + peer + } else { + continue; + } + } + }; + + println!("Send hello to peer {:?}", peer.peer_address); + let mut sender = sender.lock().await; + let status = sender.send_async(&peer.peer_address, b"Hello Peer.").await; + println!("Send hello status: {:?}", status); + } +} + +#[embassy_executor::task] +async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { + loop { + let r = receiver.receive_async().await; + println!("Received {:?}", r.get_data()); + if r.info.dst_address == BROADCAST_ADDRESS { + if !manager.peer_exists(&r.info.src_address) { + manager + .add_peer(PeerInfo { + peer_address: r.info.src_address, + lmk: None, + channel: None, + encrypt: false, + }) + .unwrap(); + println!("Added peer {:?}", r.info.src_address); + } + } + } +} + +static EXECUTOR: StaticCell = StaticCell::new(); +static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); +static ESP_NOW_SENDER: StaticCell>> = StaticCell::new(); + +#[entry] +fn main() -> ! { + #[cfg(feature = "log")] + esp_println::logger::init_logger(log::LevelFilter::Info); + + let peripherals = Peripherals::take(); + + let mut system = peripherals.PCR.split(); + let clocks = ClockControl::max(system.clock_control).freeze(); + + let timer = SystemTimer::new(peripherals.SYSTIMER).alarm0; + let init = initialize( + EspWifiInitFor::Wifi, + timer, + Rng::new(peripherals.RNG), + system.radio_clock_control, + &clocks, + ) + .unwrap(); + + let (wifi, ..) = peripherals.RADIO.split(); + let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); + println!("esp-now version {}", esp_now.get_version().unwrap()); + + let timer_group0 = TimerGroup::new( + peripherals.TIMG0, + &clocks, + &mut system.peripheral_clock_control, + ); + embassy::init(&clocks, timer_group0.timer0); + let executor = EXECUTOR.init(Executor::new()); + + let (manager, sender, receiver) = esp_now.split(); + let manager = ESP_NOW_MANAGER.init(manager); + let sender: &'static _ = ESP_NOW_SENDER.init(Mutex::new(sender)); + + executor.run(|spawner| { + spawner.spawn(listener(manager, receiver)).ok(); + spawner.spawn(broadcaster(sender)).ok(); + spawner.spawn(sayhello(manager, sender)).ok(); + }) +} diff --git a/examples-esp32c6/examples/esp_now_duplex.rs b/examples-esp32c6/examples/esp_now_duplex.rs deleted file mode 100644 index 4b036717..00000000 --- a/examples-esp32c6/examples/esp_now_duplex.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use core::cell::RefCell; - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::NoopMutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static NoopMutex>>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&BROADCAST_ADDRESS, b"Hello.") - .unwrap() - .wait() - }); - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static NoopMutex>>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&peer.peer_address, b"Hello Peer.") - .unwrap() - .wait() - }); - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(NoopMutex::new(RefCell::new(sender))); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -} diff --git a/examples-esp32s2/examples/embassy_dhcp.rs b/examples-esp32s2/examples/embassy_dhcp.rs index 120d104c..921cded2 100644 --- a/examples-esp32s2/examples/embassy_dhcp.rs +++ b/examples-esp32s2/examples/embassy_dhcp.rs @@ -21,9 +21,6 @@ use hal::clock::ClockControl; use hal::Rng; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); diff --git a/examples-esp32s2/examples/embassy_esp_now.rs b/examples-esp32s2/examples/embassy_esp_now.rs index 0510e86f..d9109568 100644 --- a/examples-esp32s2/examples/embassy_esp_now.rs +++ b/examples-esp32s2/examples/embassy_esp_now.rs @@ -19,9 +19,6 @@ use hal::clock::ClockControl; use hal::Rng; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - #[embassy_executor::task] async fn run(mut esp_now: EspNow<'static>) { let mut ticker = Ticker::every(Duration::from_secs(5)); diff --git a/examples-esp32c3/examples/esp_now_async_duplex.rs b/examples-esp32s2/examples/embassy_esp_now_duplex.rs similarity index 85% rename from examples-esp32c3/examples/esp_now_async_duplex.rs rename to examples-esp32s2/examples/embassy_esp_now_duplex.rs index 6c545ee7..9572c57c 100644 --- a/examples-esp32c3/examples/esp_now_async_duplex.rs +++ b/examples-esp32s2/examples/embassy_esp_now_duplex.rs @@ -5,6 +5,8 @@ use embassy_executor::_export::StaticCell; use embassy_sync::blocking_mutex::raw::NoopRawMutex; use embassy_sync::mutex::Mutex; +#[path = "../../examples-util/util.rs"] +mod examples_util; use examples_util::hal; use embassy_executor::Executor; @@ -14,12 +16,9 @@ use esp_backtrace as _; use esp_println::println; use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; +use hal::clock::ClockControl; use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; +use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; #[embassy_executor::task] async fn broadcaster(sender: &'static Mutex>) { @@ -92,12 +91,15 @@ fn main() -> ! { let peripherals = Peripherals::take(); - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); + let mut system = peripherals.SYSTEM.split(); + let clocks = ClockControl::max(system.clock_control).freeze(); - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); + let timer = hal::timer::TimerGroup::new( + peripherals.TIMG1, + &clocks, + &mut system.peripheral_clock_control, + ) + .timer0; let init = initialize( EspWifiInitFor::Wifi, timer, @@ -107,11 +109,15 @@ fn main() -> ! { ) .unwrap(); - let wifi = examples_util::get_wifi!(peripherals); + let wifi = peripherals.RADIO.split(); let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); println!("esp-now version {}", esp_now.get_version().unwrap()); - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); + let timer_group0 = TimerGroup::new( + peripherals.TIMG0, + &clocks, + &mut system.peripheral_clock_control, + ); embassy::init(&clocks, timer_group0.timer0); let executor = EXECUTOR.init(Executor::new()); diff --git a/examples-esp32s2/examples/esp_now_async_duplex.rs b/examples-esp32s2/examples/esp_now_async_duplex.rs deleted file mode 100644 index 6c545ee7..00000000 --- a/examples-esp32s2/examples/esp_now_async_duplex.rs +++ /dev/null @@ -1,127 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::raw::NoopRawMutex; -use embassy_sync::mutex::Mutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static Mutex>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let mut sender = sender.lock().await; - let status = sender.send_async(&BROADCAST_ADDRESS, b"Hello.").await; - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static Mutex>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let mut sender = sender.lock().await; - let status = sender.send_async(&peer.peer_address, b"Hello Peer.").await; - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(Mutex::new(sender)); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -} diff --git a/examples-esp32s2/examples/esp_now_duplex.rs b/examples-esp32s2/examples/esp_now_duplex.rs deleted file mode 100644 index 4b036717..00000000 --- a/examples-esp32s2/examples/esp_now_duplex.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use core::cell::RefCell; - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::NoopMutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static NoopMutex>>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&BROADCAST_ADDRESS, b"Hello.") - .unwrap() - .wait() - }); - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static NoopMutex>>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&peer.peer_address, b"Hello Peer.") - .unwrap() - .wait() - }); - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(NoopMutex::new(RefCell::new(sender))); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -} diff --git a/examples-esp32s3/examples/embassy_dhcp.rs b/examples-esp32s3/examples/embassy_dhcp.rs index d1e59c75..4b139e2e 100644 --- a/examples-esp32s3/examples/embassy_dhcp.rs +++ b/examples-esp32s3/examples/embassy_dhcp.rs @@ -21,9 +21,6 @@ use hal::clock::ClockControl; use hal::Rng; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); diff --git a/examples-esp32s3/examples/embassy_esp_now.rs b/examples-esp32s3/examples/embassy_esp_now.rs index 8377ada5..9fb4dec9 100644 --- a/examples-esp32s3/examples/embassy_esp_now.rs +++ b/examples-esp32s3/examples/embassy_esp_now.rs @@ -19,9 +19,6 @@ use hal::clock::ClockControl; use hal::Rng; use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - #[embassy_executor::task] async fn run(mut esp_now: EspNow<'static>) { let mut ticker = Ticker::every(Duration::from_secs(5)); diff --git a/examples-esp32c2/examples/esp_now_async_duplex.rs b/examples-esp32s3/examples/embassy_esp_now_duplex.rs similarity index 85% rename from examples-esp32c2/examples/esp_now_async_duplex.rs rename to examples-esp32s3/examples/embassy_esp_now_duplex.rs index 6c545ee7..014aac29 100644 --- a/examples-esp32c2/examples/esp_now_async_duplex.rs +++ b/examples-esp32s3/examples/embassy_esp_now_duplex.rs @@ -5,6 +5,8 @@ use embassy_executor::_export::StaticCell; use embassy_sync::blocking_mutex::raw::NoopRawMutex; use embassy_sync::mutex::Mutex; +#[path = "../../examples-util/util.rs"] +mod examples_util; use examples_util::hal; use embassy_executor::Executor; @@ -14,12 +16,9 @@ use esp_backtrace as _; use esp_println::println; use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; +use hal::clock::ClockControl; use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; +use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup}; #[embassy_executor::task] async fn broadcaster(sender: &'static Mutex>) { @@ -92,12 +91,15 @@ fn main() -> ! { let peripherals = Peripherals::take(); - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); + let mut system = peripherals.SYSTEM.split(); + let clocks = ClockControl::max(system.clock_control).freeze(); - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); + let timer = hal::timer::TimerGroup::new( + peripherals.TIMG1, + &clocks, + &mut system.peripheral_clock_control, + ) + .timer0; let init = initialize( EspWifiInitFor::Wifi, timer, @@ -107,11 +109,15 @@ fn main() -> ! { ) .unwrap(); - let wifi = examples_util::get_wifi!(peripherals); + let (wifi, ..) = peripherals.RADIO.split(); let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); println!("esp-now version {}", esp_now.get_version().unwrap()); - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); + let timer_group0 = TimerGroup::new( + peripherals.TIMG0, + &clocks, + &mut system.peripheral_clock_control, + ); embassy::init(&clocks, timer_group0.timer0); let executor = EXECUTOR.init(Executor::new()); diff --git a/examples-esp32s3/examples/esp_now_async_duplex.rs b/examples-esp32s3/examples/esp_now_async_duplex.rs deleted file mode 100644 index 6c545ee7..00000000 --- a/examples-esp32s3/examples/esp_now_async_duplex.rs +++ /dev/null @@ -1,127 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::raw::NoopRawMutex; -use embassy_sync::mutex::Mutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static Mutex>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let mut sender = sender.lock().await; - let status = sender.send_async(&BROADCAST_ADDRESS, b"Hello.").await; - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static Mutex>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let mut sender = sender.lock().await; - let status = sender.send_async(&peer.peer_address, b"Hello Peer.").await; - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(Mutex::new(sender)); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -} diff --git a/examples-esp32s3/examples/esp_now_duplex.rs b/examples-esp32s3/examples/esp_now_duplex.rs deleted file mode 100644 index 4b036717..00000000 --- a/examples-esp32s3/examples/esp_now_duplex.rs +++ /dev/null @@ -1,138 +0,0 @@ -#![no_std] -#![no_main] -#![feature(type_alias_impl_trait)] - -use core::cell::RefCell; - -use embassy_executor::_export::StaticCell; -use embassy_sync::blocking_mutex::NoopMutex; -use examples_util::hal; - -use embassy_executor::Executor; -use embassy_time::{Duration, Ticker}; -use esp_backtrace as _; - -use esp_println::println; -use esp_wifi::esp_now::{EspNowManager, EspNowReceiver, EspNowSender, PeerInfo, BROADCAST_ADDRESS}; -use esp_wifi::{initialize, EspWifiInitFor}; -use hal::clock::{ClockControl, CpuClock}; -use hal::Rng; -use hal::{embassy, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc}; - -#[cfg(any(feature = "esp32c3", feature = "esp32c2", feature = "esp32c6"))] -use hal::system::SystemExt; - -#[embassy_executor::task] -async fn broadcaster(sender: &'static NoopMutex>>) { - let mut ticker = Ticker::every(Duration::from_secs(1)); - loop { - ticker.next().await; - - println!("Send Broadcast..."); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&BROADCAST_ADDRESS, b"Hello.") - .unwrap() - .wait() - }); - println!("Send broadcast status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn sayhello( - manager: &'static EspNowManager<'static>, - sender: &'static NoopMutex>>, -) { - let mut ticker = Ticker::every(Duration::from_millis(500)); - loop { - ticker.next().await; - let peer = match manager.fetch_peer(false) { - Ok(peer) => peer, - Err(_) => { - if let Ok(peer) = manager.fetch_peer(true) { - peer - } else { - continue; - } - } - }; - - println!("Send hello to peer {:?}", peer.peer_address); - let status = sender.lock(|sender| { - sender - .borrow_mut() - .send(&peer.peer_address, b"Hello Peer.") - .unwrap() - .wait() - }); - println!("Send hello status: {:?}", status); - } -} - -#[embassy_executor::task] -async fn listener(manager: &'static EspNowManager<'static>, mut receiver: EspNowReceiver<'static>) { - loop { - let r = receiver.receive_async().await; - println!("Received {:?}", r.get_data()); - if r.info.dst_address == BROADCAST_ADDRESS { - if !manager.peer_exists(&r.info.src_address) { - manager - .add_peer(PeerInfo { - peer_address: r.info.src_address, - lmk: None, - channel: None, - encrypt: false, - }) - .unwrap(); - println!("Added peer {:?}", r.info.src_address); - } - } - } -} - -static EXECUTOR: StaticCell = StaticCell::new(); -static ESP_NOW_MANAGER: StaticCell> = StaticCell::new(); -static ESP_NOW_SENDER: StaticCell>>> = StaticCell::new(); - -#[entry] -fn main() -> ! { - #[cfg(feature = "log")] - esp_println::logger::init_logger(log::LevelFilter::Info); - - let peripherals = Peripherals::take(); - - let system = examples_util::system!(peripherals); - let mut peripheral_clock_control = system.peripheral_clock_control; - let clocks = examples_util::clocks!(system); - examples_util::rtc!(peripherals); - - let timer = examples_util::timer!(peripherals, clocks, peripheral_clock_control); - let init = initialize( - EspWifiInitFor::Wifi, - timer, - Rng::new(peripherals.RNG), - system.radio_clock_control, - &clocks, - ) - .unwrap(); - - let wifi = examples_util::get_wifi!(peripherals); - let esp_now = esp_wifi::esp_now::EspNow::new(&init, wifi).unwrap(); - println!("esp-now version {}", esp_now.get_version().unwrap()); - - let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks, &mut peripheral_clock_control); - embassy::init(&clocks, timer_group0.timer0); - let executor = EXECUTOR.init(Executor::new()); - - let (manager, sender, receiver) = esp_now.split(); - let manager = ESP_NOW_MANAGER.init(manager); - let sender: &'static _ = ESP_NOW_SENDER.init(NoopMutex::new(RefCell::new(sender))); - - executor.run(|spawner| { - spawner.spawn(listener(manager, receiver)).ok(); - spawner.spawn(broadcaster(sender)).ok(); - spawner.spawn(sayhello(manager, sender)).ok(); - }) -}