diff --git a/esp-wifi/Cargo.toml b/esp-wifi/Cargo.toml index c9edb2c1700..ee435249703 100644 --- a/esp-wifi/Cargo.toml +++ b/esp-wifi/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" license = "MIT OR Apache-2.0" [dependencies] +defmt = { workspace = true, optional = true } embedded-hal.workspace = true esp32c3-hal = { workspace = true, optional = true } esp32c2-hal = { workspace = true, optional = true } @@ -74,3 +75,4 @@ ps-min-modem = [] esp-now = [ "wifi" ] big-heap = [] ipv6 = ["smoltcp?/proto-ipv6"] +defmt = ["dep:defmt", "smoltcp?/defmt"] diff --git a/esp-wifi/src/ble/btdm.rs b/esp-wifi/src/ble/btdm.rs index dab53dd1ad4..4a2804862e8 100644 --- a/esp-wifi/src/ble/btdm.rs +++ b/esp-wifi/src/ble/btdm.rs @@ -31,6 +31,7 @@ static BT_RECEIVE_QUEUE: Mutex>> = Mutex::new(RefCell::new(SimpleQueue::new())); #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct ReceivedPacket { pub len: u8, pub data: [u8; 256], diff --git a/esp-wifi/src/ble/npl.rs b/esp-wifi/src/ble/npl.rs index 205c0ef2634..8ee4c118b00 100644 --- a/esp-wifi/src/ble/npl.rs +++ b/esp-wifi/src/ble/npl.rs @@ -63,6 +63,7 @@ static BT_RECEIVE_QUEUE: Mutex>> = type OsMembufT = u32; #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct ReceivedPacket { pub len: u8, pub data: [u8; 256], diff --git a/esp-wifi/src/esp_now/mod.rs b/esp-wifi/src/esp_now/mod.rs index df61b33b453..bd755339da9 100644 --- a/esp-wifi/src/esp_now/mod.rs +++ b/esp-wifi/src/esp_now/mod.rs @@ -8,8 +8,8 @@ use core::{cell::RefCell, fmt::Debug}; +use atomic_polyfill::{AtomicBool, Ordering}; use critical_section::Mutex; -use atomic_polyfill::{Ordering, AtomicBool}; use esp_hal_common::peripheral::{Peripheral, PeripheralRef}; use crate::compat::queue::SimpleQueue; @@ -27,7 +27,7 @@ static RECEIVE_QUEUE: Mutex>> = Mutex::new(RefCell::new(SimpleQueue::new())); /// This atomic behaves like a guard, so we need strict memory ordering when /// operating it. -/// +/// /// This flag indicates whether the send callback has been called after a sending. static ESP_NOW_SEND_CB_INVOKED: AtomicBool = AtomicBool::new(false); /// Status of esp now send, true for success, false for failure @@ -37,13 +37,14 @@ macro_rules! check_error { ($block:block) => { match unsafe { $block } { 0 => Ok(()), - res => Err(EspNowError::Error(Error::from_code(res as u32))) + res => Err(EspNowError::Error(Error::from_code(res as u32))), } }; } #[repr(u32)] #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { NotInitialized = 12389, InvalidArgument = 12390, @@ -73,17 +74,20 @@ impl Error { } #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum EspNowError { Error(Error), } #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct PeerCount { pub total_count: i32, pub encrypted_count: i32, } #[repr(u32)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum WifiPhyRate { /// < 1 Mbps with long preamble Rate1mL = 0, @@ -156,6 +160,7 @@ pub enum WifiPhyRate { } #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct PeerInfo { pub peer_address: [u8; 6], pub lmk: Option<[u8; 16]>, @@ -166,6 +171,7 @@ pub struct PeerInfo { #[cfg(not(any(esp32c6)))] #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct RxControlInfo { pub rssi: i32, pub rate: u32, @@ -190,6 +196,7 @@ pub struct RxControlInfo { #[cfg(any(esp32c6))] #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct RxControlInfo { pub rssi: i32, pub rate: u32, @@ -214,6 +221,7 @@ pub struct RxControlInfo { } #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct ReceiveInfo { pub src_address: [u8; 6], pub dst_address: [u8; 6], @@ -221,6 +229,7 @@ pub struct ReceiveInfo { } #[derive(Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct ReceivedData { pub len: u8, pub data: [u8; 256], @@ -448,11 +457,11 @@ impl<'d> EspNow<'d> { /// Send data to peer /// /// The peer needs to be added to the peer list first. - /// - /// This method returns a `SendWaiter` on success. ESP-NOW protocol provides guaranteed - /// delivery on MAC layer. If you need this guatantee, call `wait` method of the returned - /// `SendWaiter` and make sure it returns `SendStatus::Success`. - /// However, this method will block current task for milliseconds. + /// + /// This method returns a `SendWaiter` on success. ESP-NOW protocol provides guaranteed + /// delivery on MAC layer. If you need this guarantee, call `wait` method of the returned + /// `SendWaiter` and make sure it returns `SendStatus::Success`. + /// However, this method will block current task for milliseconds. /// So you can just drop the waiter if you want high frequency sending. pub fn send(&self, dst_addr: &[u8; 6], data: &[u8]) -> Result { let mut addr = [0u8; 6]; @@ -483,20 +492,21 @@ impl Drop for EspNow<'_> { /// This is essentially [esp_now_send_status_t](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html#_CPPv421esp_now_send_status_t) #[derive(Debug, Clone, Copy, PartialEq)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum SendStatus { Success, - Failed + Failed, } /// This struct is returned by a sync esp now send. Invoking `wait` method of this /// struct will block current task until the callback function of esp now send is called -/// and return the status of previous sending. +/// and return the status of previous sending. pub struct SendWaiter(()); impl SendWaiter { - /// Wait for the previous sending to complete, i.e. the send callback is invoked with + /// Wait for the previous sending to complete, i.e. the send callback is invoked with /// status of the sending. - /// + /// /// Note: if you firstly dropped waiter of a sending and then wait for a following sending, /// you probably get unreliable status because we cannot determine which sending the waited status /// belongs to. @@ -511,14 +521,11 @@ impl SendWaiter { } } -unsafe extern "C" fn send_cb( - _mac_addr: *const u8, - status: esp_now_send_status_t -) { +unsafe extern "C" fn send_cb(_mac_addr: *const u8, status: esp_now_send_status_t) { critical_section::with(|_| { let is_success = status == esp_now_send_status_t_ESP_NOW_SEND_SUCCESS; ESP_NOW_SEND_STATUS.store(is_success, Ordering::Relaxed); - + ESP_NOW_SEND_CB_INVOKED.store(true, Ordering::Release); #[cfg(feature = "async")] @@ -642,7 +649,11 @@ mod asynch { ReceiveFuture.await } - pub fn send_async(&self, dst_addr: &[u8; 6], data: &[u8]) -> Result { + pub fn send_async( + &self, + dst_addr: &[u8; 6], + data: &[u8], + ) -> Result { let mut addr = [0u8; 6]; addr.copy_from_slice(dst_addr); ESP_NOW_SEND_CB_INVOKED.store(false, Ordering::Release); diff --git a/esp-wifi/src/lib.rs b/esp-wifi/src/lib.rs index 591bf58ef6b..776780db6ba 100644 --- a/esp-wifi/src/lib.rs +++ b/esp-wifi/src/lib.rs @@ -168,10 +168,12 @@ pub type EspWifiTimer = Alarm; pub type EspWifiTimer = hal::timer::Timer>; #[derive(Debug, PartialEq, PartialOrd)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[non_exhaustive] pub struct EspWifiInitializationInternal; #[derive(Debug, PartialEq, PartialOrd)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum EspWifiInitialization { #[cfg(feature = "wifi")] Wifi(EspWifiInitializationInternal), @@ -202,6 +204,7 @@ impl EspWifiInitialization { } #[derive(Debug, PartialEq, PartialOrd)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum EspWifiInitFor { #[cfg(feature = "wifi")] Wifi, @@ -336,6 +339,7 @@ pub fn initialize( } #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum InitializationError { General(i32), #[cfg(feature = "wifi")] diff --git a/esp-wifi/src/wifi/mod.rs b/esp-wifi/src/wifi/mod.rs index b403be175b0..196b4df1549 100644 --- a/esp-wifi/src/wifi/mod.rs +++ b/esp-wifi/src/wifi/mod.rs @@ -84,6 +84,7 @@ use crate::{ use log::debug; #[derive(Debug, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum WifiMode { Sta, Ap, @@ -177,6 +178,7 @@ pub(crate) static DATA_QUEUE_TX: Mutex WifiStack<'a> { } #[derive(Debug, Copy, Clone)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum WifiStackError { Unknown(i32), InitializationError(crate::InitializationError), @@ -605,6 +606,7 @@ impl<'s, 'n: 's> Drop for Socket<'s, 'n> { } #[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum IoError { SocketClosed, MultiCastError(smoltcp::iface::MulticastError),