From f26eef646a4096dcae2402d53c7f0ed3e1e498de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Tue, 8 Oct 2024 16:04:59 +0200 Subject: [PATCH] Remove current_millis (#2304) --- esp-hal/CHANGELOG.md | 1 + esp-hal/src/time.rs | 14 +++++- esp-wifi/CHANGELOG.md | 1 + esp-wifi/MIGRATING-0.9.md | 13 ++++- esp-wifi/src/ble/npl.rs | 2 +- esp-wifi/src/lib.rs | 11 +++-- esp-wifi/src/wifi/os_adapter.rs | 2 +- esp-wifi/src/wifi/utils.rs | 9 +--- esp-wifi/src/wifi_interface.rs | 7 +-- examples/src/bin/wifi_access_point.rs | 38 +++++++------- .../src/bin/wifi_access_point_with_sta.rs | 25 ++++++---- examples/src/bin/wifi_bench.rs | 48 +++++++++--------- examples/src/bin/wifi_ble.rs | 4 +- examples/src/bin/wifi_coex.rs | 44 ++++++++--------- examples/src/bin/wifi_dhcp.rs | 41 +++++++--------- examples/src/bin/wifi_embassy_ble.rs | 5 +- examples/src/bin/wifi_esp_now.rs | 14 ++++-- examples/src/bin/wifi_static_ip.rs | 49 +++++++++---------- 18 files changed, 174 insertions(+), 154 deletions(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index f129e7506..8feed559c 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added `Efuse::read_bit` (#2259) - Limited SPI slave support for ESP32 (Modes 1 and 3 only) (#2278) - Added `Rtc::disable_rom_message_printing` (S3 and H2 only) (#2280) +- Added `esp_hal::time::{Duration, Instant}` (#2304) ### Changed diff --git a/esp-hal/src/time.rs b/esp-hal/src/time.rs index 6e8b16ef5..89b45ed5d 100644 --- a/esp-hal/src/time.rs +++ b/esp-hal/src/time.rs @@ -2,6 +2,16 @@ //! //! The `time` module offers a way to get the system now. +/// Represents a duration of time. +/// +/// The resolution is 1 microsecond, represented as a 64-bit unsigned integer. +pub type Duration = fugit::Duration; + +/// Represents an instant in time. +/// +/// The resolution is 1 microsecond, represented as a 64-bit unsigned integer. +pub type Instant = fugit::Instant; + /// Provides time since system start in microseconds precision. /// /// The counter won’t measure time in sleep-mode. @@ -10,7 +20,7 @@ #[cfg_attr(esp32, doc = "36_558 years")] #[cfg_attr(esp32s2, doc = "7_311 years")] #[cfg_attr(not(any(esp32, esp32s2)), doc = "more than 7 years")] -pub fn now() -> fugit::Instant { +pub fn now() -> Instant { #[cfg(esp32)] let (ticks, div) = { // on ESP32 use LACT @@ -45,7 +55,7 @@ pub fn now() -> fugit::Instant { ) }; - fugit::Instant::::from_ticks(ticks / div) + Instant::from_ticks(ticks / div) } #[cfg(esp32)] diff --git a/esp-wifi/CHANGELOG.md b/esp-wifi/CHANGELOG.md index 19a7d38df..5979e5896 100644 --- a/esp-wifi/CHANGELOG.md +++ b/esp-wifi/CHANGELOG.md @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed the `embedded-svc` traits and feature (#2235) - Removed the `log` feature from default features (#2253) - Removed the `enumset` feature (#2297) +- Removed `esp_wifi::current_millis` (#2304) ## 0.9.1 - 2024-09-03 diff --git a/esp-wifi/MIGRATING-0.9.md b/esp-wifi/MIGRATING-0.9.md index 84fbd0ba8..c3e1f9e96 100644 --- a/esp-wifi/MIGRATING-0.9.md +++ b/esp-wifi/MIGRATING-0.9.md @@ -93,7 +93,7 @@ pub extern "C" fn esp_wifi_allocate_from_internal_ram(size: usize) -> *mut u8 { It's important to allocate from internal memory (i.e. not PSRAM) -### Tunable parameters are now set via esp-config +## Tunable parameters are now set via esp-config We've replaced usage of `cfg_toml` with [esp-config](https://docs.rs/esp-config). Please remove any esp-wifi entries from `cfg.toml` and migrate the key value pairs to the `[env]` section of `.cargo/config.toml`. @@ -104,9 +104,18 @@ We've replaced usage of `cfg_toml` with [esp-config](https://docs.rs/esp-config) + ESP_WIFI_RX_QUEUE_SIZE=40 ``` -### `wifi-logs` feature renamed to `sys-logs` +## `wifi-logs` feature renamed to `sys-logs` ```diff - features = ["wifi-logs"] + features = ["sys-logs"] ``` + +## Removed `esp_wifi::current_millis` + +You can use `esp_hal::time::now()` instead. + +```diff +- let now = esp_wifi::current_millis(); ++ let now = time::now().duration_since_epoch().to_millis(); +``` diff --git a/esp-wifi/src/ble/npl.rs b/esp-wifi/src/ble/npl.rs index e4a5547eb..8deaadc5b 100644 --- a/esp-wifi/src/ble/npl.rs +++ b/esp-wifi/src/ble/npl.rs @@ -671,7 +671,7 @@ unsafe extern "C" fn ble_npl_time_ms_to_ticks( unsafe extern "C" fn ble_npl_time_get() -> u32 { trace!("ble_npl_time_get"); - crate::current_millis() as u32 + esp_hal::time::now().duration_since_epoch().to_millis() as u32 } unsafe extern "C" fn ble_npl_callout_set_arg( diff --git a/esp-wifi/src/lib.rs b/esp-wifi/src/lib.rs index e7fa2c202..546159fbc 100644 --- a/esp-wifi/src/lib.rs +++ b/esp-wifi/src/lib.rs @@ -163,14 +163,15 @@ pub mod tasks; pub(crate) mod memory_fence; -use timer::{get_systimer_count, ticks_to_millis}; - #[cfg(all(feature = "wifi", any(feature = "tcp", feature = "udp")))] pub mod wifi_interface; -/// Return the current systimer time in milliseconds -pub fn current_millis() -> u64 { - ticks_to_millis(get_systimer_count()) +// [esp_hal::time::now()] as a smoltcp [`Instant]` +#[cfg(feature = "smoltcp")] +fn timestamp() -> smoltcp::time::Instant { + smoltcp::time::Instant::from_micros( + esp_hal::time::now().duration_since_epoch().to_micros() as i64 + ) } // this is just to verify that we use the correct defaults in `build.rs` diff --git a/esp-wifi/src/wifi/os_adapter.rs b/esp-wifi/src/wifi/os_adapter.rs index 6bf59c8bd..cbed1db74 100644 --- a/esp-wifi/src/wifi/os_adapter.rs +++ b/esp-wifi/src/wifi/os_adapter.rs @@ -1497,7 +1497,7 @@ pub unsafe extern "C" fn log_writev( /// /// ************************************************************************* pub unsafe extern "C" fn log_timestamp() -> u32 { - crate::current_millis() as u32 + esp_hal::time::now().duration_since_epoch().to_millis() as u32 } /// ************************************************************************** diff --git a/esp-wifi/src/wifi/utils.rs b/esp-wifi/src/wifi/utils.rs index a417454bd..1eb5da3a2 100644 --- a/esp-wifi/src/wifi/utils.rs +++ b/esp-wifi/src/wifi/utils.rs @@ -4,12 +4,11 @@ use smoltcp::socket::dhcpv4::Socket as Dhcpv4Socket; use smoltcp::{ iface::{Config, Interface, SocketSet, SocketStorage}, - time::Instant, wire::{EthernetAddress, HardwareAddress}, }; use super::{WifiApDevice, WifiController, WifiDevice, WifiDeviceMode, WifiError, WifiStaDevice}; -use crate::{current_millis, EspWifiInitialization}; +use crate::{timestamp, EspWifiInitialization}; fn setup_iface<'a, MODE: WifiDeviceMode>( device: &mut WifiDevice<'_, MODE>, @@ -20,11 +19,7 @@ fn setup_iface<'a, MODE: WifiDeviceMode>( let hw_address = HardwareAddress::Ethernet(EthernetAddress::from_bytes(&mac)); let config = Config::new(hw_address); - let iface = Interface::new( - config, - device, - Instant::from_millis(current_millis() as i64), - ); + let iface = Interface::new(config, device, timestamp()); #[allow(unused_mut)] let mut socket_set = SocketSet::new(storage); diff --git a/esp-wifi/src/wifi_interface.rs b/esp-wifi/src/wifi_interface.rs index 7f8d33d0f..e4935604b 100644 --- a/esp-wifi/src/wifi_interface.rs +++ b/esp-wifi/src/wifi_interface.rs @@ -17,7 +17,7 @@ use smoltcp::{ }; use crate::{ - current_millis, + timestamp, wifi::{ipv4, WifiDevice, WifiDeviceMode}, }; @@ -511,11 +511,6 @@ impl Display for WifiStackError { } } -/// [current_millis] as an Instant -pub fn timestamp() -> Instant { - Instant::from_millis(current_millis() as i64) -} - impl WifiStack<'_, MODE> { pub fn get_iface_configuration(&self) -> Result { Ok(self.network_config.borrow().clone()) diff --git a/examples/src/bin/wifi_access_point.rs b/examples/src/bin/wifi_access_point.rs index cf92e4d5a..2dfd7d7e9 100644 --- a/examples/src/bin/wifi_access_point.rs +++ b/examples/src/bin/wifi_access_point.rs @@ -16,10 +16,14 @@ use embedded_io::*; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_hal::{ + prelude::*, + rng::Rng, + time::{self, Duration}, + timer::timg::TimerGroup, +}; use esp_println::{print, println}; use esp_wifi::{ - current_millis, init, wifi::{ utils::create_network_interface, @@ -57,7 +61,8 @@ fn main() -> ! { let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let (iface, device, mut controller, sockets) = create_network_interface(&init, &mut wifi, WifiApDevice, &mut socket_set_entries).unwrap(); - let mut wifi_stack = WifiStack::new(iface, device, sockets, current_millis); + let now = || time::now().duration_since_epoch().to_millis(); + let mut wifi_stack = WifiStack::new(iface, device, sockets, now); let client_config = Configuration::AccessPoint(AccessPointConfiguration { ssid: "esp-wifi".try_into().unwrap(), @@ -107,26 +112,21 @@ fn main() -> ! { println!("Connected"); let mut time_out = false; - let wait_end = current_millis() + 20 * 1000; + let deadline = time::now() + Duration::secs(20); let mut buffer = [0u8; 1024]; let mut pos = 0; - loop { - if let Ok(len) = socket.read(&mut buffer[pos..]) { - let to_print = - unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) }; + while let Ok(len) = socket.read(&mut buffer[pos..]) { + let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) }; - if to_print.contains("\r\n\r\n") { - print!("{}", to_print); - println!(); - break; - } - - pos += len; - } else { + if to_print.contains("\r\n\r\n") { + print!("{}", to_print); + println!(); break; } - if current_millis() > wait_end { + pos += len; + + if time::now() > deadline { println!("Timeout"); time_out = true; break; @@ -155,8 +155,8 @@ fn main() -> ! { println!(); } - let wait_end = current_millis() + 5 * 1000; - while current_millis() < wait_end { + let deadline = time::now() + Duration::secs(5); + while time::now() < deadline { socket.work(); } } diff --git a/examples/src/bin/wifi_access_point_with_sta.rs b/examples/src/bin/wifi_access_point_with_sta.rs index 08fa8f6c1..4817cc54d 100644 --- a/examples/src/bin/wifi_access_point_with_sta.rs +++ b/examples/src/bin/wifi_access_point_with_sta.rs @@ -17,10 +17,14 @@ use embedded_io::*; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_hal::{ + prelude::*, + rng::Rng, + time::{self, Duration}, + timer::timg::TimerGroup, +}; use esp_println::{print, println}; use esp_wifi::{ - current_millis, init, wifi::{ utils::{create_ap_sta_network_interface, ApStaInterface}, @@ -81,8 +85,9 @@ fn main() -> ! { ) .unwrap(); - let mut wifi_ap_stack = WifiStack::new(ap_interface, ap_device, ap_socket_set, current_millis); - let wifi_sta_stack = WifiStack::new(sta_interface, sta_device, sta_socket_set, current_millis); + let now = || time::now().duration_since_epoch().to_millis(); + let mut wifi_ap_stack = WifiStack::new(ap_interface, ap_device, ap_socket_set, now); + let wifi_sta_stack = WifiStack::new(sta_interface, sta_device, sta_socket_set, now); let client_config = Configuration::Mixed( ClientConfiguration { @@ -156,7 +161,7 @@ fn main() -> ! { println!("Connected"); let mut time_out = false; - let wait_end = current_millis() + 20 * 1000; + let deadline = time::now() + Duration::secs(20); let mut buffer = [0u8; 1024]; let mut pos = 0; loop { @@ -175,7 +180,7 @@ fn main() -> ! { break; } - if current_millis() > wait_end { + if time::now() > deadline { println!("Timeout"); time_out = true; break; @@ -195,7 +200,7 @@ fn main() -> ! { .unwrap(); sta_socket.flush().unwrap(); - let wait_end = current_millis() + 20 * 1000; + let deadline = time::now() + Duration::secs(20); loop { let mut buffer = [0u8; 512]; if let Ok(len) = sta_socket.read(&mut buffer) { @@ -205,7 +210,7 @@ fn main() -> ! { break; } - if current_millis() > wait_end { + if time::now() > deadline { println!("Timeout"); break; } @@ -221,8 +226,8 @@ fn main() -> ! { println!(); } - let wait_end = current_millis() + 5 * 1000; - while current_millis() < wait_end { + let deadline = time::now() + Duration::secs(5); + while time::now() < deadline { ap_socket.work(); } } diff --git a/examples/src/bin/wifi_bench.rs b/examples/src/bin/wifi_bench.rs index 7d2638b5c..761235975 100644 --- a/examples/src/bin/wifi_bench.rs +++ b/examples/src/bin/wifi_bench.rs @@ -16,10 +16,15 @@ use embedded_io::*; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{delay::Delay, prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_hal::{ + delay::Delay, + prelude::*, + rng::Rng, + time::{self, Duration}, + timer::timg::TimerGroup, +}; use esp_println::println; use esp_wifi::{ - current_millis, init, wifi::{ utils::create_network_interface, @@ -41,7 +46,7 @@ const SSID: &str = env!("SSID"); const PASSWORD: &str = env!("PASSWORD"); const HOST_IP: &str = env!("HOST_IP"); -const TEST_DURATION: usize = 15; +const TEST_DURATION: Duration = Duration::secs(15); const RX_BUFFER_SIZE: usize = 16384; const TX_BUFFER_SIZE: usize = 16384; const IO_BUFFER_SIZE: usize = 1024; @@ -76,7 +81,8 @@ fn main() -> ! { let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let (iface, device, mut controller, sockets) = create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); - let wifi_stack = WifiStack::new(iface, device, sockets, current_millis); + let now = || time::now().duration_since_epoch().to_millis(); + let wifi_stack = WifiStack::new(iface, device, sockets, now); let client_config = Configuration::Client(ClientConfiguration { ssid: SSID.try_into().unwrap(), @@ -103,13 +109,9 @@ fn main() -> ! { // wait to get connected println!("Wait to get connected"); loop { - let res = controller.is_connected(); - match res { - Ok(connected) => { - if connected { - break; - } - } + match controller.is_connected() { + Ok(true) => break, + Ok(false) => {} Err(err) => { println!("{:?}", err); loop {} @@ -162,21 +164,21 @@ fn test_download<'a>( let mut buf = [0; IO_BUFFER_SIZE]; let mut total = 0; - let wait_end = current_millis() + (TEST_DURATION as u64 * 1000); + let deadline = time::now() + TEST_DURATION; loop { socket.work(); if let Ok(len) = socket.read(&mut buf) { - total += len; + total += len as u64; } else { break; } - if current_millis() > wait_end { + if time::now() > deadline { break; } } - let kbps = (total + 512) / 1024 / TEST_DURATION; + let kbps = (total + 512) / 1024 / TEST_DURATION.to_secs(); println!("download: {} kB/s", kbps); socket.disconnect(); @@ -196,21 +198,21 @@ fn test_upload<'a>( let buf = [0; IO_BUFFER_SIZE]; let mut total = 0; - let wait_end = current_millis() + (TEST_DURATION as u64 * 1000); + let deadline = time::now() + TEST_DURATION; loop { socket.work(); if let Ok(len) = socket.write(&buf) { - total += len; + total += len as u64; } else { break; } - if current_millis() > wait_end { + if time::now() > deadline { break; } } - let kbps = (total + 512) / 1024 / TEST_DURATION; + let kbps = (total + 512) / 1024 / TEST_DURATION.to_secs(); println!("upload: {} kB/s", kbps); socket.disconnect(); @@ -231,7 +233,7 @@ fn test_upload_download<'a>( let mut rx_buf = [0; IO_BUFFER_SIZE]; let mut total = 0; - let wait_end = current_millis() + (TEST_DURATION as u64 * 1000); + let deadline = time::now() + TEST_DURATION; loop { socket.work(); if let Err(_) = socket.write(&tx_buf) { @@ -241,17 +243,17 @@ fn test_upload_download<'a>( socket.work(); if let Ok(len) = socket.read(&mut rx_buf) { - total += len; + total += len as u64; } else { break; } - if current_millis() > wait_end { + if time::now() > deadline { break; } } - let kbps = (total + 512) / 1024 / TEST_DURATION; + let kbps = (total + 512) / 1024 / TEST_DURATION.to_secs(); println!("upload+download: {} kB/s", kbps); socket.disconnect(); diff --git a/examples/src/bin/wifi_ble.rs b/examples/src/bin/wifi_ble.rs index 1b6e1d088..4ba170f33 100644 --- a/examples/src/bin/wifi_ble.rs +++ b/examples/src/bin/wifi_ble.rs @@ -28,6 +28,7 @@ use esp_hal::{ gpio::{Input, Io, Pull}, prelude::*, rng::Rng, + time, timer::timg::TimerGroup, }; use esp_println::println; @@ -68,9 +69,10 @@ fn main() -> ! { let mut bluetooth = peripherals.BT; + let now = || time::now().duration_since_epoch().to_millis(); loop { let connector = BleConnector::new(&init, &mut bluetooth); - let hci = HciConnector::new(connector, esp_wifi::current_millis); + let hci = HciConnector::new(connector, now); let mut ble = Ble::new(&hci); println!("{:?}", ble.init()); diff --git a/examples/src/bin/wifi_coex.rs b/examples/src/bin/wifi_coex.rs index 33002a188..647f484a1 100644 --- a/examples/src/bin/wifi_coex.rs +++ b/examples/src/bin/wifi_coex.rs @@ -27,11 +27,15 @@ use bleps::{ use embedded_io::*; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_hal::{ + prelude::*, + rng::Rng, + time::{self, Duration}, + timer::timg::TimerGroup, +}; use esp_println::{print, println}; use esp_wifi::{ ble::controller::BleConnector, - current_millis, init, wifi::{utils::create_network_interface, ClientConfiguration, Configuration, WifiStaDevice}, wifi_interface::WifiStack, @@ -90,7 +94,9 @@ fn main() -> ! { let mut socket_set_entries: [SocketStorage; 2] = Default::default(); let (iface, device, mut controller, sockets) = create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); - let wifi_stack = WifiStack::new(iface, device, sockets, current_millis); + + let now = || time::now().duration_since_epoch().to_millis(); + let wifi_stack = WifiStack::new(iface, device, sockets, now); let client_config = Configuration::Client(ClientConfiguration { ssid: SSID.try_into().unwrap(), @@ -108,13 +114,9 @@ fn main() -> ! { // wait to get connected println!("Wait to get connected"); loop { - let res = controller.is_connected(); - match res { - Ok(connected) => { - if connected { - break; - } - } + match controller.is_connected() { + Ok(true) => break, + Ok(false) => {} Err(err) => { println!("{:?}", err); loop {} @@ -135,7 +137,7 @@ fn main() -> ! { } let connector = BleConnector::new(&init, bluetooth); - let hci = HciConnector::new(connector, esp_wifi::current_millis); + let hci = HciConnector::new(connector, now); let mut ble = Ble::new(&hci); println!("{:?}", ble.init()); @@ -174,17 +176,13 @@ fn main() -> ! { .unwrap(); socket.flush().unwrap(); - let wait_end = current_millis() + 20 * 1000; - loop { - let mut buffer = [0u8; 128]; - if let Ok(len) = socket.read(&mut buffer) { - let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) }; - print!("{}", to_print); - } else { - break; - } + let deadline = time::now() + Duration::secs(20); + let mut buffer = [0u8; 128]; + while let Ok(len) = socket.read(&mut buffer) { + let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) }; + print!("{}", to_print); - if current_millis() > wait_end { + if time::now() > deadline { println!("Timeout"); break; } @@ -193,8 +191,8 @@ fn main() -> ! { socket.disconnect(); - let wait_end = current_millis() + 5 * 1000; - while current_millis() < wait_end { + let deadline = time::now() + Duration::secs(5); + while time::now() < deadline { socket.work(); } } diff --git a/examples/src/bin/wifi_dhcp.rs b/examples/src/bin/wifi_dhcp.rs index 9103f7b16..368c44530 100644 --- a/examples/src/bin/wifi_dhcp.rs +++ b/examples/src/bin/wifi_dhcp.rs @@ -16,10 +16,14 @@ extern crate alloc; use embedded_io::*; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_hal::{ + prelude::*, + rng::Rng, + time::{self, Duration}, + timer::timg::TimerGroup, +}; use esp_println::{print, println}; use esp_wifi::{ - current_millis, init, wifi::{ utils::create_network_interface, @@ -65,7 +69,8 @@ fn main() -> ! { let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let (iface, device, mut controller, sockets) = create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); - let wifi_stack = WifiStack::new(iface, device, sockets, current_millis); + let now = || time::now().duration_since_epoch().to_millis(); + let wifi_stack = WifiStack::new(iface, device, sockets, now); let client_config = Configuration::Client(ClientConfiguration { ssid: SSID.try_into().unwrap(), @@ -92,13 +97,9 @@ fn main() -> ! { // wait to get connected println!("Wait to get connected"); loop { - let res = controller.is_connected(); - match res { - Ok(connected) => { - if connected { - break; - } - } + match controller.is_connected() { + Ok(true) => break, + Ok(false) => {} Err(err) => { println!("{:?}", err); loop {} @@ -137,17 +138,13 @@ fn main() -> ! { .unwrap(); socket.flush().unwrap(); - let wait_end = current_millis() + 20 * 1000; - loop { - let mut buffer = [0u8; 512]; - if let Ok(len) = socket.read(&mut buffer) { - let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) }; - print!("{}", to_print); - } else { - break; - } + let deadline = time::now() + Duration::secs(20); + let mut buffer = [0u8; 512]; + while let Ok(len) = socket.read(&mut buffer) { + let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..len]) }; + print!("{}", to_print); - if current_millis() > wait_end { + if time::now() > deadline { println!("Timeout"); break; } @@ -156,8 +153,8 @@ fn main() -> ! { socket.disconnect(); - let wait_end = current_millis() + 5 * 1000; - while current_millis() < wait_end { + let deadline = time::now() + Duration::secs(5); + while time::now() < deadline { socket.work(); } } diff --git a/examples/src/bin/wifi_embassy_ble.rs b/examples/src/bin/wifi_embassy_ble.rs index 6604c15b2..f9ab2b322 100644 --- a/examples/src/bin/wifi_embassy_ble.rs +++ b/examples/src/bin/wifi_embassy_ble.rs @@ -31,6 +31,7 @@ use esp_hal::{ gpio::{Input, Io, Pull}, prelude::*, rng::Rng, + time, timer::timg::TimerGroup, }; use esp_println::println; @@ -80,7 +81,9 @@ async fn main(_spawner: Spawner) -> ! { let mut bluetooth = peripherals.BT; let connector = BleConnector::new(&init, &mut bluetooth); - let mut ble = Ble::new(connector, esp_wifi::current_millis); + + let now = || time::now().duration_since_epoch().to_millis(); + let mut ble = Ble::new(connector, now); println!("Connector created"); let pin_ref = RefCell::new(button); diff --git a/examples/src/bin/wifi_esp_now.rs b/examples/src/bin/wifi_esp_now.rs index 0d8208ac7..294bddc1e 100644 --- a/examples/src/bin/wifi_esp_now.rs +++ b/examples/src/bin/wifi_esp_now.rs @@ -10,10 +10,14 @@ use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_hal::{ + prelude::*, + rng::Rng, + time::{self, Duration}, + timer::timg::TimerGroup, +}; use esp_println::println; use esp_wifi::{ - current_millis, esp_now::{PeerInfo, BROADCAST_ADDRESS}, init, EspWifiInitFor, @@ -45,7 +49,7 @@ fn main() -> ! { println!("esp-now version {}", esp_now.get_version().unwrap()); - let mut next_send_time = current_millis() + 5 * 1000; + let mut next_send_time = time::now() + Duration::secs(5); loop { let r = esp_now.receive(); if let Some(r) = r { @@ -70,8 +74,8 @@ fn main() -> ! { } } - if current_millis() >= next_send_time { - next_send_time = current_millis() + 5 * 1000; + if time::now() >= next_send_time { + next_send_time = time::now() + Duration::secs(5); println!("Send"); let status = esp_now .send(&BROADCAST_ADDRESS, b"0123456789") diff --git a/examples/src/bin/wifi_static_ip.rs b/examples/src/bin/wifi_static_ip.rs index e23bd7858..f3f1f1926 100644 --- a/examples/src/bin/wifi_static_ip.rs +++ b/examples/src/bin/wifi_static_ip.rs @@ -15,10 +15,14 @@ use embedded_io::*; use esp_alloc as _; use esp_backtrace as _; -use esp_hal::{prelude::*, rng::Rng, timer::timg::TimerGroup}; +use esp_hal::{ + prelude::*, + rng::Rng, + time::{self, Duration}, + timer::timg::TimerGroup, +}; use esp_println::{print, println}; use esp_wifi::{ - current_millis, init, wifi::{ utils::create_network_interface, @@ -63,7 +67,9 @@ fn main() -> ! { let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let (iface, device, mut controller, sockets) = create_network_interface(&init, &mut wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); - let mut wifi_stack = WifiStack::new(iface, device, sockets, current_millis); + + let now = || time::now().duration_since_epoch().to_millis(); + let mut wifi_stack = WifiStack::new(iface, device, sockets, now); let client_config = Configuration::Client(ClientConfiguration { ssid: SSID.try_into().unwrap(), @@ -90,13 +96,9 @@ fn main() -> ! { // wait to get connected println!("Wait to get connected"); loop { - let res = controller.is_connected(); - match res { - Ok(connected) => { - if connected { - break; - } - } + match controller.is_connected() { + Ok(true) => break, + Ok(false) => {} Err(err) => { println!("{:?}", err); loop {} @@ -145,26 +147,21 @@ fn main() -> ! { println!("Connected"); let mut time_out = false; - let wait_end = current_millis() + 20 * 1000; + let deadline = time::now() + Duration::secs(20); let mut buffer = [0u8; 1024]; let mut pos = 0; - loop { - if let Ok(len) = socket.read(&mut buffer[pos..]) { - let to_print = - unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) }; + while let Ok(len) = socket.read(&mut buffer[pos..]) { + let to_print = unsafe { core::str::from_utf8_unchecked(&buffer[..(pos + len)]) }; - if to_print.contains("\r\n\r\n") { - print!("{}", to_print); - println!(); - break; - } - - pos += len; - } else { + if to_print.contains("\r\n\r\n") { + print!("{}", to_print); + println!(); break; } - if current_millis() > wait_end { + pos += len; + + if time::now() > deadline { println!("Timeout"); time_out = true; break; @@ -192,8 +189,8 @@ fn main() -> ! { println!(); } - let wait_end = current_millis() + 5 * 1000; - while current_millis() < wait_end { + let deadline = time::now() + Duration::secs(5); + while time::now() < deadline { socket.work(); } }