diff --git a/esp-hal-embassy/src/time_driver.rs b/esp-hal-embassy/src/time_driver.rs index 414a0f86c7..29fb71eb37 100644 --- a/esp-hal-embassy/src/time_driver.rs +++ b/esp-hal-embassy/src/time_driver.rs @@ -10,7 +10,7 @@ use embassy_time_driver::Driver; use esp_hal::{ interrupt::{InterruptHandler, Priority}, sync::Locked, - time::{now, Duration}, + time::{Duration, Instant}, timer::OneShotTimer, Blocking, }; @@ -203,7 +203,7 @@ impl EmbassyTimer { /// Returns `true` if the timer was armed, `false` if the timestamp is in /// the past. fn arm(timer: &mut Timer, timestamp: u64) -> bool { - let now = now().duration_since_epoch().as_micros(); + let now = Instant::now().duration_since_epoch().as_micros(); if timestamp > now { let timeout = Duration::from_micros(timestamp - now); @@ -294,7 +294,7 @@ impl EmbassyTimer { impl Driver for EmbassyTimer { fn now(&self) -> u64 { - now().duration_since_epoch().as_micros() + Instant::now().duration_since_epoch().as_micros() } fn schedule_wake(&self, at: u64, waker: &core::task::Waker) { diff --git a/esp-hal-embassy/src/timer_queue.rs b/esp-hal-embassy/src/timer_queue.rs index 387cae2f61..dbd01150e7 100644 --- a/esp-hal-embassy/src/timer_queue.rs +++ b/esp-hal-embassy/src/timer_queue.rs @@ -60,7 +60,9 @@ impl TimerQueue { } pub fn dispatch(&self) { - let now = esp_hal::time::now().duration_since_epoch().as_micros(); + let now = esp_hal::time::Instant::now() + .duration_since_epoch() + .as_micros(); self.arm_alarm(now); } diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index cd1e634f15..ac319cef4a 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -8,9 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added + - SPI: Added support for 3-wire SPI (#2919) - UART: Add separate config for Rx and Tx (#2965) - Added accessor methods to config structs (#3011) +- `esp_hal::time::{Rate, Duration, Instant}` (#3083) - Async support for ADC oneshot reads for ESP32C2, ESP32C3, ESP32C6 and ESP32H2 (#2925, #3082) ### Changed @@ -33,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `flip-link` feature is now a config option (`ESP_HAL_CONFIG_FLIP_LINK`) (#3001) - Migrate AES driver to DMA move API (#3084) - Removed features `psram-quad` and `psram-octal` - replaced by `psram` and the `ESP_HAL_CONFIG_PSRAM_MODE` (`quad`/`octal`) (#3001) +- The `esp_hal::time` module no longer reexports `fugit` types (#3083) - I2C: Async functions are postfixed with `_async`, non-async functions are available in async-mode (#3056) @@ -48,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - OutputOpenDrain has been removed (#3029) - The fields of config structs are no longer public (#3011) - Removed the dysfunctional `DmaChannel::set_priority` function (#3088) +- `esp_hal::time::now()`, which has been replaced by `esp_hal::time::Instant::now()` (#3083) ## [0.23.1] - 2025-01-15 diff --git a/esp-hal/MIGRATING-0.23.md b/esp-hal/MIGRATING-0.23.md index c0e94c58f7..d52addab10 100644 --- a/esp-hal/MIGRATING-0.23.md +++ b/esp-hal/MIGRATING-0.23.md @@ -266,6 +266,7 @@ All async functions now include the `_async` postfix. Additionally the non-async ```diff - let result = i2c.write_read(0x77, &[0xaa], &mut data).await; + let result = i2c.write_read_async(0x77, &[0xaa], &mut data).await; +``` ## ADC Changes @@ -277,3 +278,17 @@ NOTE: Async support is only supported in ESP32C3 and ESP32C6 for now - Adc<'d, ADC> + Adc<'d, ADC, Blocking> ``` + +## time API changes + +ESP-HAL no longer publicly exposes `fugit` and no longer exposes the concept of a `tick`. +This comes with a number of changes: + +- The `RateExtU32` and similar traits are no longer used, which means `.kHz()` and similar suffix + conversions are no longer available. A number of matching constructors are available. For example, + instead of `1.MHz()` you need to write `Rate::from_mhz(1)`. +- Methods on `esp_hal::time` types are named differently. + - Getters are prefixed with `as_`, e.g. `Duration::as_secs`. + - Constructors are prefixed with `from`, e.g. `Rate::from_mhz`. + - A number of functions that convert from a number into a time type (e.g. `Duration::from_ticks`) + are not available. Use conversions from physical units of time, like `Duration::from_millis`. diff --git a/esp-hal/src/delay.rs b/esp-hal/src/delay.rs index e7ddbee376..29a1895e32 100644 --- a/esp-hal/src/delay.rs +++ b/esp-hal/src/delay.rs @@ -3,7 +3,7 @@ //! ## Overview //! //! The Delay driver provides blocking delay functionalities using the -//! [now] function. +//! [`Instant`] struct. //! //! ## Configuration //! @@ -30,7 +30,6 @@ //! //! [DelayNs]: https://docs.rs/embedded-hal/1.0.0/embedded_hal/delay/trait.DelayNs.html //! [embedded-hal]: https://docs.rs/embedded-hal/1.0.0/embedded_hal/delay/index.html -//! [now]: crate::time::now use crate::time::{Duration, Instant}; diff --git a/esp-hal/src/time.rs b/esp-hal/src/time.rs index 215297f58d..c1f9a0996b 100644 --- a/esp-hal/src/time.rs +++ b/esp-hal/src/time.rs @@ -144,7 +144,17 @@ impl defmt::Format for Instant { } impl Instant { + /// Represents the moment the system booted. + pub const EPOCH: Instant = Instant(InnerInstant::from_ticks(0)); + /// Returns the current instant. + /// + /// The counter won’t measure time in sleep-mode. + /// + /// The timer has a 1 microsecond resolution and will wrap after + #[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() -> Self { now() } @@ -154,8 +164,8 @@ impl Instant { } /// Returns the elapsed time since boot. - pub const fn duration_since_epoch(&self) -> Duration { - Duration::from_micros(self.0.ticks()) + pub fn duration_since_epoch(&self) -> Duration { + Self::EPOCH.elapsed() } /// Returns the elapsed `Duration` since this instant was created. @@ -375,15 +385,7 @@ impl core::ops::Div for Duration { } } -/// Provides time since system start in microseconds precision. -/// -/// The counter won’t measure time in sleep-mode. -/// -/// The timer will wrap after -#[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() -> Instant { +fn now() -> Instant { #[cfg(esp32)] let (ticks, div) = { // on ESP32 use LACT diff --git a/esp-hal/src/timer/systimer.rs b/esp-hal/src/timer/systimer.rs index 867d6eec01..28a2f6478a 100644 --- a/esp-hal/src/timer/systimer.rs +++ b/esp-hal/src/timer/systimer.rs @@ -93,7 +93,7 @@ impl SystemTimer { /// Create a new instance. pub fn new(_systimer: SYSTIMER) -> Self { - // Don't reset Systimer as it will break `time::now`, only enable it + // Don't reset Systimer as it will break `time::Instant::now`, only enable it PeripheralClockControl::enable(PeripheralEnable::Systimer); #[cfg(soc_etm)] @@ -124,7 +124,7 @@ impl SystemTimer { /// /// - Disabling a `Unit` whilst [`Alarm`]s are using it will affect the /// [`Alarm`]s operation. - /// - Disabling Unit0 will affect [`now`](crate::time::now). + /// - Disabling Unit0 will affect [`Instant::now`]. pub unsafe fn configure_unit(unit: Unit, config: UnitConfig) { unit.configure(config) } @@ -139,8 +139,7 @@ impl SystemTimer { /// /// - Modifying a unit's count whilst [`Alarm`]s are using it may cause /// unexpected behaviour - /// - Any modification of the unit0 count will affect - /// [`now`](crate::time::now). + /// - Any modification of the unit0 count will affect [`Instant::now`] pub unsafe fn set_unit_value(unit: Unit, value: u64) { unit.set_count(value) } diff --git a/esp-hal/src/timer/timg.rs b/esp-hal/src/timer/timg.rs index 55a42f185c..edfe8b4100 100644 --- a/esp-hal/src/timer/timg.rs +++ b/esp-hal/src/timer/timg.rs @@ -151,7 +151,7 @@ impl TimerGroupInstance for TIMG0 { fn reset_peripheral() { // FIXME: for TIMG0 do nothing for now because the reset breaks - // `time::now` + // `time::Instant::now` } fn configure_wdt_src_clk() { diff --git a/esp-wifi/src/ble/npl.rs b/esp-wifi/src/ble/npl.rs index bfc4d29b7f..0ffff9887c 100644 --- a/esp-wifi/src/ble/npl.rs +++ b/esp-wifi/src/ble/npl.rs @@ -661,7 +661,9 @@ unsafe extern "C" fn ble_npl_time_ms_to_ticks( unsafe extern "C" fn ble_npl_time_get() -> u32 { trace!("ble_npl_time_get"); - esp_hal::time::now().duration_since_epoch().as_millis() as u32 + esp_hal::time::Instant::now() + .duration_since_epoch() + .as_millis() as u32 } unsafe extern "C" fn ble_npl_callout_set_arg( diff --git a/esp-wifi/src/timer/riscv.rs b/esp-wifi/src/timer/riscv.rs index b174d7dedc..b47e10c988 100644 --- a/esp-wifi/src/timer/riscv.rs +++ b/esp-wifi/src/timer/riscv.rs @@ -92,7 +92,9 @@ pub(crate) fn yield_task() { /// Current systimer count value /// A tick is 1 / 1_000_000 seconds pub(crate) fn systimer_count() -> u64 { - esp_hal::time::now().duration_since_epoch().as_micros() + esp_hal::time::Instant::now() + .duration_since_epoch() + .as_micros() } // TODO: use an Instance type instead... diff --git a/esp-wifi/src/timer/xtensa.rs b/esp-wifi/src/timer/xtensa.rs index de122abc31..7ca95afcda 100644 --- a/esp-wifi/src/timer/xtensa.rs +++ b/esp-wifi/src/timer/xtensa.rs @@ -17,7 +17,9 @@ pub const TICKS_PER_SECOND: u64 = 1_000_000; /// This function must not be called in a critical section. Doing so may return /// an incorrect value. pub(crate) fn systimer_count() -> u64 { - esp_hal::time::now().duration_since_epoch().as_micros() + esp_hal::time::Instant::now() + .duration_since_epoch() + .as_micros() } pub(crate) fn setup_timer(mut timer1: TimeBase) { diff --git a/esp-wifi/src/wifi/os_adapter.rs b/esp-wifi/src/wifi/os_adapter.rs index 558ebebf7b..8cd7a03e2a 100644 --- a/esp-wifi/src/wifi/os_adapter.rs +++ b/esp-wifi/src/wifi/os_adapter.rs @@ -1500,7 +1500,9 @@ pub unsafe extern "C" fn log_writev( /// /// ************************************************************************* pub unsafe extern "C" fn log_timestamp() -> u32 { - esp_hal::time::now().duration_since_epoch().as_millis() as u32 + esp_hal::time::Instant::now() + .duration_since_epoch() + .as_millis() as u32 } /// ************************************************************************** diff --git a/esp-wifi/src/wifi/utils.rs b/esp-wifi/src/wifi/utils.rs index 8e181ca21f..eca819f997 100644 --- a/esp-wifi/src/wifi/utils.rs +++ b/esp-wifi/src/wifi/utils.rs @@ -8,11 +8,13 @@ use smoltcp::{ use super::{WifiApDevice, WifiController, WifiDevice, WifiDeviceMode, WifiError, WifiStaDevice}; use crate::EspWifiController; -// [esp_hal::time::now()] as a smoltcp [`Instant`] +/// [esp_hal::time::Instant] as a smoltcp [`Instant`] #[cfg(feature = "smoltcp")] fn timestamp() -> smoltcp::time::Instant { smoltcp::time::Instant::from_micros( - esp_hal::time::now().duration_since_epoch().as_micros() as i64 + esp_hal::time::Instant::now() + .duration_since_epoch() + .as_micros() as i64, ) } diff --git a/examples/src/bin/hmac.rs b/examples/src/bin/hmac.rs index efec6fd47f..f7eefef488 100644 --- a/examples/src/bin/hmac.rs +++ b/examples/src/bin/hmac.rs @@ -94,19 +94,19 @@ fn main() -> ! { let mut remaining = nsrc; hw_hmac.init(); block!(hw_hmac.configure(HmacPurpose::ToUser, KeyId::Key0)).expect("Key purpose mismatch"); - let pre_hw_hmac = esp_hal::time::now(); + let pre_hw_hmac = esp_hal::time::Instant::now(); while remaining.len() > 0 { remaining = block!(hw_hmac.update(remaining)).unwrap(); } block!(hw_hmac.finalize(output.as_mut_slice())).unwrap(); - let post_hw_hmac = esp_hal::time::now(); - let hw_time = post_hw_hmac - pre_hw_hmac; + let hw_time = pre_hw_hmac.elapsed(); + let mut sw_hmac = HmacSha256::new_from_slice(key).expect("HMAC can take key of any size"); - let pre_sw_hash = esp_hal::time::now(); + let pre_sw_hash = esp_hal::time::Instant::now(); sw_hmac.update(nsrc); let soft_result = sw_hmac.finalize().into_bytes(); - let post_sw_hash = esp_hal::time::now(); - let soft_time = post_sw_hash - pre_sw_hash; + + let soft_time = pre_sw_hash.elapsed(); for (a, b) in output.iter().zip(soft_result) { assert_eq!(*a, b); } diff --git a/examples/src/bin/wifi_access_point.rs b/examples/src/bin/wifi_access_point.rs index ce82ad0a99..2718ebd472 100644 --- a/examples/src/bin/wifi_access_point.rs +++ b/examples/src/bin/wifi_access_point.rs @@ -73,7 +73,7 @@ fn main() -> ! { let mut wifi = peripherals.WIFI; let (iface, device, mut controller) = create_network_interface(&init, &mut wifi, WifiApDevice).unwrap(); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let socket_set = SocketSet::new(&mut socket_set_entries[..]); @@ -129,7 +129,7 @@ fn main() -> ! { println!("Connected"); let mut time_out = false; - let deadline = time::now() + Duration::from_secs(20); + let deadline = time::Instant::now() + Duration::from_secs(20); let mut buffer = [0u8; 1024]; let mut pos = 0; while let Ok(len) = socket.read(&mut buffer[pos..]) { @@ -143,7 +143,7 @@ fn main() -> ! { pos += len; - if time::now() > deadline { + if time::Instant::now() > deadline { println!("Timeout"); time_out = true; break; @@ -172,7 +172,7 @@ fn main() -> ! { println!(); } - let start = time::now(); + let start = time::Instant::now(); while start.elapsed() < Duration::from_secs(5) { 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 531c6d878d..fb496fcad9 100644 --- a/examples/src/bin/wifi_access_point_with_sta.rs +++ b/examples/src/bin/wifi_access_point_with_sta.rs @@ -70,7 +70,7 @@ fn main() -> ! { mut controller, } = create_ap_sta_network_interface(&init, wifi).unwrap(); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let mut ap_socket_set_entries: [SocketStorage; 3] = Default::default(); let ap_socket_set = SocketSet::new(&mut ap_socket_set_entries[..]); let mut ap_stack = Stack::new(ap_interface, ap_device, ap_socket_set, now, rng.random()); @@ -154,7 +154,7 @@ fn main() -> ! { println!("Connected"); let mut time_out = false; - let deadline = time::now() + Duration::from_secs(20); + let deadline = time::Instant::now() + Duration::from_secs(20); let mut buffer = [0u8; 1024]; let mut pos = 0; loop { @@ -173,7 +173,7 @@ fn main() -> ! { break; } - if time::now() > deadline { + if time::Instant::now() > deadline { println!("Timeout"); time_out = true; break; @@ -193,7 +193,7 @@ fn main() -> ! { .unwrap(); sta_socket.flush().unwrap(); - let deadline = time::now() + Duration::from_secs(20); + let deadline = time::Instant::now() + Duration::from_secs(20); loop { let mut buffer = [0u8; 512]; if let Ok(len) = sta_socket.read(&mut buffer) { @@ -203,7 +203,7 @@ fn main() -> ! { break; } - if time::now() > deadline { + if time::Instant::now() > deadline { println!("Timeout"); break; } @@ -219,7 +219,7 @@ fn main() -> ! { println!(); } - let start = time::now(); + let start = time::Instant::now(); while start.elapsed() < Duration::from_secs(5) { ap_socket.work(); } diff --git a/examples/src/bin/wifi_bench.rs b/examples/src/bin/wifi_bench.rs index 00ec535761..0d758c42e5 100644 --- a/examples/src/bin/wifi_bench.rs +++ b/examples/src/bin/wifi_bench.rs @@ -87,7 +87,7 @@ fn main() -> ! { }]); socket_set.add(dhcp_socket); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let stack = Stack::new(iface, device, socket_set, now, rng.random()); let client_config = Configuration::Client(ClientConfiguration { @@ -170,7 +170,7 @@ fn test_download<'a, D: smoltcp::phy::Device>( let mut buf = [0; IO_BUFFER_SIZE]; let mut total = 0; - let deadline = time::now() + TEST_DURATION; + let deadline = time::Instant::now() + TEST_DURATION; loop { socket.work(); if let Ok(len) = socket.read(&mut buf) { @@ -179,7 +179,7 @@ fn test_download<'a, D: smoltcp::phy::Device>( break; } - if time::now() > deadline { + if time::Instant::now() > deadline { break; } } @@ -204,7 +204,7 @@ fn test_upload<'a, D: smoltcp::phy::Device>( let buf = [0; IO_BUFFER_SIZE]; let mut total = 0; - let deadline = time::now() + TEST_DURATION; + let deadline = time::Instant::now() + TEST_DURATION; loop { socket.work(); if let Ok(len) = socket.write(&buf) { @@ -213,7 +213,7 @@ fn test_upload<'a, D: smoltcp::phy::Device>( break; } - if time::now() > deadline { + if time::Instant::now() > deadline { break; } } @@ -239,7 +239,7 @@ fn test_upload_download<'a, D: smoltcp::phy::Device>( let mut rx_buf = [0; IO_BUFFER_SIZE]; let mut total = 0; - let deadline = time::now() + TEST_DURATION; + let deadline = time::Instant::now() + TEST_DURATION; loop { socket.work(); if let Err(_) = socket.write(&tx_buf) { @@ -254,7 +254,7 @@ fn test_upload_download<'a, D: smoltcp::phy::Device>( break; } - if time::now() > deadline { + if time::Instant::now() > deadline { break; } } diff --git a/examples/src/bin/wifi_ble.rs b/examples/src/bin/wifi_ble.rs index fbbbb1ce69..0e257c4360 100644 --- a/examples/src/bin/wifi_ble.rs +++ b/examples/src/bin/wifi_ble.rs @@ -65,7 +65,7 @@ fn main() -> ! { let mut bluetooth = peripherals.BT; - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); loop { let connector = BleConnector::new(&init, &mut bluetooth); let hci = HciConnector::new(connector, now); diff --git a/examples/src/bin/wifi_coex.rs b/examples/src/bin/wifi_coex.rs index 4c6005647e..a7bea60382 100644 --- a/examples/src/bin/wifi_coex.rs +++ b/examples/src/bin/wifi_coex.rs @@ -104,7 +104,7 @@ fn main() -> ! { }]); socket_set.add(dhcp_socket); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let stack = Stack::new(iface, device, socket_set, now, rng.random()); let client_config = Configuration::Client(ClientConfiguration { @@ -185,13 +185,13 @@ fn main() -> ! { .unwrap(); socket.flush().unwrap(); - let deadline = time::now() + Duration::from_secs(20); + let deadline = time::Instant::now() + Duration::from_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 time::now() > deadline { + if time::Instant::now() > deadline { println!("Timeout"); break; } @@ -200,8 +200,8 @@ fn main() -> ! { socket.disconnect(); - let deadline = time::now() + Duration::from_secs(5); - while time::now() < deadline { + let deadline = time::Instant::now() + Duration::from_secs(5); + while time::Instant::now() < deadline { socket.work(); } } diff --git a/examples/src/bin/wifi_csi.rs b/examples/src/bin/wifi_csi.rs index 4ee78de277..5e39eebce0 100644 --- a/examples/src/bin/wifi_csi.rs +++ b/examples/src/bin/wifi_csi.rs @@ -64,7 +64,7 @@ fn main() -> ! { }]); socket_set.add(dhcp_socket); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let stack = Stack::new(iface, device, socket_set, now, rng.random()); let client_config = Configuration::Client(ClientConfiguration { diff --git a/examples/src/bin/wifi_dhcp.rs b/examples/src/bin/wifi_dhcp.rs index 8d88c6585f..12f87e6416 100644 --- a/examples/src/bin/wifi_dhcp.rs +++ b/examples/src/bin/wifi_dhcp.rs @@ -77,7 +77,7 @@ fn main() -> ! { }]); socket_set.add(dhcp_socket); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let stack = Stack::new(iface, device, socket_set, now, rng.random()); let client_config = Configuration::Client(ClientConfiguration { @@ -146,13 +146,13 @@ fn main() -> ! { .unwrap(); socket.flush().unwrap(); - let deadline = time::now() + Duration::from_secs(20); + let deadline = time::Instant::now() + Duration::from_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 time::now() > deadline { + if time::Instant::now() > deadline { println!("Timeout"); break; } @@ -161,8 +161,8 @@ fn main() -> ! { socket.disconnect(); - let deadline = time::now() + Duration::from_secs(5); - while time::now() < deadline { + let deadline = time::Instant::now() + Duration::from_secs(5); + while time::Instant::now() < deadline { socket.work(); } } diff --git a/examples/src/bin/wifi_embassy_ble.rs b/examples/src/bin/wifi_embassy_ble.rs index 98c6370775..bce3ec5cac 100644 --- a/examples/src/bin/wifi_embassy_ble.rs +++ b/examples/src/bin/wifi_embassy_ble.rs @@ -91,7 +91,7 @@ async fn main(_spawner: Spawner) -> ! { let connector = BleConnector::new(&init, &mut bluetooth); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let mut ble = Ble::new(connector, now); println!("Connector created"); diff --git a/examples/src/bin/wifi_esp_now.rs b/examples/src/bin/wifi_esp_now.rs index de1d2ee018..fd1aa09f5a 100644 --- a/examples/src/bin/wifi_esp_now.rs +++ b/examples/src/bin/wifi_esp_now.rs @@ -45,7 +45,7 @@ fn main() -> ! { println!("esp-now version {}", esp_now.version().unwrap()); - let mut next_send_time = time::now() + Duration::from_secs(5); + let mut next_send_time = time::Instant::now() + Duration::from_secs(5); loop { let r = esp_now.receive(); if let Some(r) = r { @@ -70,8 +70,8 @@ fn main() -> ! { } } - if time::now() >= next_send_time { - next_send_time = time::now() + Duration::from_secs(5); + if time::Instant::now() >= next_send_time { + next_send_time = time::Instant::now() + Duration::from_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 c126311af2..a904524fd8 100644 --- a/examples/src/bin/wifi_static_ip.rs +++ b/examples/src/bin/wifi_static_ip.rs @@ -64,7 +64,7 @@ fn main() -> ! { let mut socket_set_entries: [SocketStorage; 3] = Default::default(); let socket_set = SocketSet::new(&mut socket_set_entries[..]); - let now = || time::now().duration_since_epoch().as_millis(); + let now = || time::Instant::now().duration_since_epoch().as_millis(); let mut stack = Stack::new(iface, device, socket_set, now, rng.random()); let client_config = Configuration::Client(ClientConfiguration { @@ -143,7 +143,7 @@ fn main() -> ! { println!("Connected"); let mut time_out = false; - let deadline = time::now() + Duration::from_secs(20); + let deadline = time::Instant::now() + Duration::from_secs(20); let mut buffer = [0u8; 1024]; let mut pos = 0; while let Ok(len) = socket.read(&mut buffer[pos..]) { @@ -157,7 +157,7 @@ fn main() -> ! { pos += len; - if time::now() > deadline { + if time::Instant::now() > deadline { println!("Timeout"); time_out = true; break; @@ -185,8 +185,8 @@ fn main() -> ! { println!(); } - let deadline = time::now() + Duration::from_secs(5); - while time::now() < deadline { + let deadline = time::Instant::now() + Duration::from_secs(5); + while time::Instant::now() < deadline { socket.work(); } } diff --git a/hil-test/tests/delay.rs b/hil-test/tests/delay.rs index 25ba454421..8830b52f94 100644 --- a/hil-test/tests/delay.rs +++ b/hil-test/tests/delay.rs @@ -29,9 +29,9 @@ mod tests { #[test] fn delay_ns(mut ctx: Context) { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); ctx.delay.delay_ns(600_000); - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!( @@ -43,9 +43,9 @@ mod tests { #[test] fn delay_70millis(ctx: Context) { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); ctx.delay.delay_millis(70); - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!( @@ -57,9 +57,9 @@ mod tests { #[test] fn delay_1_500us(mut ctx: Context) { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); ctx.delay.delay_us(1_500); - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!( @@ -71,9 +71,9 @@ mod tests { #[test] fn delay_3_00ms(mut ctx: Context) { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); ctx.delay.delay_ms(300); - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!( diff --git a/hil-test/tests/delay_async.rs b/hil-test/tests/delay_async.rs index 3b28d65d15..cb21e1bc8a 100644 --- a/hil-test/tests/delay_async.rs +++ b/hil-test/tests/delay_async.rs @@ -24,9 +24,9 @@ struct Context { async fn test_async_delay_ns(mut timer: impl DelayNs, duration: u32) { for i in 1..5 { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); timer.delay_ns(duration).await; - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!( @@ -41,9 +41,9 @@ async fn test_async_delay_ns(mut timer: impl DelayNs, duration: u32) { async fn test_async_delay_us(mut timer: impl DelayNs, duration: u32) { for _ in 1..5 { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); timer.delay_us(duration).await; - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!( @@ -56,9 +56,9 @@ async fn test_async_delay_us(mut timer: impl DelayNs, duration: u32) { async fn test_async_delay_ms(mut timer: impl DelayNs, duration: u32) { for _ in 1..5 { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); timer.delay_ms(duration).await; - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!( @@ -86,16 +86,16 @@ mod tests { async fn test_systimer_async_delay_ns(ctx: Context) { let alarms = SystemTimer::new(ctx.peripherals.SYSTIMER); - test_async_delay_ns(OneShotTimer::new(alarms.alarm0).into_async(), 10_000).await; + test_async_delay_ns(OneShotTimer::new(alarms.alarm0).into_async(), 10_000_000).await; } #[test] async fn test_timg0_async_delay_ns(ctx: Context) { let timg0 = TimerGroup::new(ctx.peripherals.TIMG0); - test_async_delay_ns(OneShotTimer::new(timg0.timer0).into_async(), 10_000).await; + test_async_delay_ns(OneShotTimer::new(timg0.timer0).into_async(), 10_000_000).await; #[cfg(timg_timer1)] - test_async_delay_ns(OneShotTimer::new(timg0.timer1).into_async(), 10_000).await; + test_async_delay_ns(OneShotTimer::new(timg0.timer1).into_async(), 10_000_000).await; } #[cfg(timg1)] @@ -103,9 +103,9 @@ mod tests { async fn test_timg1_async_delay_ns(ctx: Context) { let timg1 = TimerGroup::new(ctx.peripherals.TIMG1); - test_async_delay_ns(OneShotTimer::new(timg1.timer0).into_async(), 10_000).await; + test_async_delay_ns(OneShotTimer::new(timg1.timer0).into_async(), 10_000_000).await; #[cfg(timg_timer1)] - test_async_delay_ns(OneShotTimer::new(timg1.timer1).into_async(), 10_000).await; + test_async_delay_ns(OneShotTimer::new(timg1.timer1).into_async(), 10_000_000).await; } #[cfg(systimer)] @@ -113,16 +113,16 @@ mod tests { async fn test_systimer_async_delay_us(ctx: Context) { let alarms = SystemTimer::new(ctx.peripherals.SYSTIMER); - test_async_delay_us(OneShotTimer::new(alarms.alarm0).into_async(), 10).await; + test_async_delay_us(OneShotTimer::new(alarms.alarm0).into_async(), 10_000).await; } #[test] async fn test_timg0_async_delay_us(ctx: Context) { let timg0 = TimerGroup::new(ctx.peripherals.TIMG0); - test_async_delay_us(OneShotTimer::new(timg0.timer0).into_async(), 10).await; + test_async_delay_us(OneShotTimer::new(timg0.timer0).into_async(), 10_000).await; #[cfg(timg_timer1)] - test_async_delay_us(OneShotTimer::new(timg0.timer1).into_async(), 10).await; + test_async_delay_us(OneShotTimer::new(timg0.timer1).into_async(), 10_000).await; } #[cfg(timg1)] @@ -130,9 +130,9 @@ mod tests { async fn test_timg1_async_delay_us(ctx: Context) { let timg1 = TimerGroup::new(ctx.peripherals.TIMG1); - test_async_delay_us(OneShotTimer::new(timg1.timer0).into_async(), 10).await; + test_async_delay_us(OneShotTimer::new(timg1.timer0).into_async(), 10_000).await; #[cfg(timg_timer1)] - test_async_delay_us(OneShotTimer::new(timg1.timer1).into_async(), 10).await; + test_async_delay_us(OneShotTimer::new(timg1.timer1).into_async(), 10_000).await; } #[cfg(systimer)] diff --git a/hil-test/tests/embassy_timers_executors.rs b/hil-test/tests/embassy_timers_executors.rs index f95ce8425b..dd45cba6bd 100644 --- a/hil-test/tests/embassy_timers_executors.rs +++ b/hil-test/tests/embassy_timers_executors.rs @@ -49,10 +49,10 @@ mod test_cases { use super::*; pub async fn run_test_one_shot_async() { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); Timer::after_millis(50).await; Timer::after_millis(30).await; - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -65,12 +65,12 @@ mod test_cases { pub fn run_test_periodic_timer(timer: impl Peripheral

) { let mut periodic = PeriodicTimer::new(timer); - let t1 = time::now(); + let t1 = time::Instant::now(); periodic.start(time::Duration::from_millis(100)).unwrap(); periodic.wait(); - let t2 = time::now(); + let t2 = time::Instant::now(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -83,9 +83,9 @@ mod test_cases { pub fn run_test_oneshot_timer(timer: impl Peripheral

) { let mut timer = OneShotTimer::new(timer); - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); timer.delay_millis(50); - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -96,10 +96,10 @@ mod test_cases { } pub async fn run_join_test() { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); embassy_futures::join::join(Timer::after_millis(50), Timer::after_millis(30)).await; Timer::after_millis(50).await; - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -219,11 +219,11 @@ mod test { let outcome = async { let mut ticker = Ticker::every(Duration::from_millis(30)); - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); ticker.next().await; ticker.next().await; ticker.next().await; - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); assert!( @@ -254,13 +254,13 @@ mod test { // We are retrying 5 times because probe-rs polling RTT may introduce some // jitter. for _ in 0..5 { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); let mut ticker = Ticker::every(Duration::from_hz(100_000)); for _ in 0..2000 { ticker.next().await; } - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1, "t2: {:?}, t1: {:?}", t2, t1); let duration = (t2 - t1).as_micros(); diff --git a/hil-test/tests/get_time.rs b/hil-test/tests/get_time.rs index b8ecf2fa13..00e1f76b13 100644 --- a/hil-test/tests/get_time.rs +++ b/hil-test/tests/get_time.rs @@ -1,4 +1,4 @@ -//! time::now Test +//! time::Instant::now Test //% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3 //% FEATURES: unstable @@ -14,9 +14,9 @@ struct Context { } fn time_moves_forward_during(ctx: Context, f: F) { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); f(ctx); - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); } @@ -37,9 +37,9 @@ mod tests { #[test] fn test_current_time(ctx: Context) { - let t1 = esp_hal::time::now(); + let t1 = esp_hal::time::Instant::now(); ctx.delay.delay_millis(500); - let t2 = esp_hal::time::now(); + let t2 = esp_hal::time::Instant::now(); assert!(t2 > t1); assert!((t2 - t1).as_millis() >= 500u64);