From 21b1592753a181ceac87969fdbb60e8e7f78f293 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 11:51:29 +0100 Subject: [PATCH 01/13] and again --- esp-hal/CHANGELOG.md | 2 ++ esp-hal/Cargo.toml | 1 - esp-hal/src/rtc_cntl/mod.rs | 33 +++++++++------------------------ xtask/Cargo.toml | 2 +- xtask/src/lib.rs | 2 +- 5 files changed, 13 insertions(+), 27 deletions(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 4712e81c6f..f47bfa6fa3 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Replaced `chrono::NaiveDateTime` on the RTC API by raw `u64` timestamps (#3185) + ### Fixed - Full-duplex SPI works when mixed with half-duplex SPI (#3176) diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index f6b744ce7d..1ca3600d38 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -21,7 +21,6 @@ bitflags = "2.8.0" bytemuck = "1.21.0" bitfield = "0.18.1" cfg-if = "1.0.0" -chrono = { version = "0.4.39", default-features = false } critical-section = "1.2.0" defmt = { version = "0.3.10", optional = true } delegate = "0.13.2" diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index b4e84b46a4..819bfd07a3 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -30,11 +30,11 @@ //! //! loop { //! // Print the current RTC time in milliseconds -//! let time_ms = rtc.current_time().and_utc().timestamp_millis(); +//! let time_ms = rtc.current_time_us() / 1000; //! delay.delay_millis(1000); //! //! // Set the time to half a second in the past -//! let new_time = rtc.current_time() - Duration::from_millis(500); +//! let new_time = rtc.current_time_us() - 500_000; //! rtc.set_current_time(new_time); //! } //! # } @@ -102,18 +102,16 @@ //! //! loop { //! // Get the current RTC time in milliseconds -//! let time_ms = rtc.current_time().and_utc().timestamp_millis(); +//! let time_ms = rtc.current_time_us() * 1000; //! delay.delay_millis(1000); //! //! // Set the time to half a second in the past -//! let new_time = rtc.current_time() - Duration::from_millis(500); +//! let new_time = rtc.current_time() - 500_000; //! rtc.set_current_time(new_time); //! } //! # } //! ``` -use chrono::{DateTime, NaiveDateTime}; - pub use self::rtc::SocResetReason; #[cfg(not(any(esp32c6, esp32h2)))] use crate::clock::XtalClock; @@ -400,8 +398,8 @@ impl<'d> Rtc<'d> { h.write(|w| unsafe { w.bits((boot_time_us >> 32) as u32) }); } - /// Get the current time. - pub fn current_time(&self) -> NaiveDateTime { + /// Get the current time in microseconds. + pub fn current_time_us(&self) -> u64 { // Current time is boot time + time since boot let rtc_time_us = self.time_since_boot().as_micros(); @@ -417,24 +415,11 @@ impl<'d> Rtc<'d> { boot_time_us + rtc_time_us }; - DateTime::from_timestamp_micros(current_time_us as i64) - .unwrap() - .naive_utc() + current_time_us } - /// Set the current time. - /// - /// # Panics - /// - /// Panics if `current_time` is before the Unix epoch (meaning the - /// underlying timestamp is negative). - pub fn set_current_time(&self, current_time: NaiveDateTime) { - let current_time_us: u64 = current_time - .and_utc() - .timestamp_micros() - .try_into() - .expect("current_time is negative"); - + /// Set the current time in microseconds. + pub fn set_current_time(&self, current_time_us: u64) { // Current time is boot time + time since boot (rtc time) // So boot time = current time - time since boot (rtc time) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 840a7514ca..8ea7a69a2e 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -7,12 +7,12 @@ publish = false [dependencies] anyhow = "1.0.93" basic-toml = "0.1.9" -chrono = "0.4.38" clap = { version = "4.5.20", features = ["derive", "wrap_help"] } console = "0.15.10" csv = "1.3.1" env_logger = "0.11.5" esp-metadata = { path = "../esp-metadata", features = ["clap"] } +jiff = "0.2" kuchikiki = "0.8.2" log = "0.4.22" minijinja = "2.5.0" diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index f2a4a14c31..5953195bd3 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -359,7 +359,7 @@ pub fn generate_efuse_table( let out_path = out_path.as_ref(); // We will put the date of generation in the file header: - let date = chrono::Utc::now().date_naive(); + let date = jiff::Timestamp::now().to_zoned(jiff::tz::TimeZone::UTC).date(); // Determine the commit (short) hash of the HEAD commit in the // provided ESP-IDF repository: From 5675bf36f700362e669d503a5646f36a46af3c4a Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 14:11:23 +0100 Subject: [PATCH 02/13] examples --- esp-hal/Cargo.toml | 3 +++ esp-hal/src/rtc_cntl/mod.rs | 29 +++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 1ca3600d38..6727c7bff7 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -82,6 +82,9 @@ esp-metadata = { version = "0.6.0", path = "../esp-metadata" } esp-config = { version = "0.3.0", path = "../esp-config", features = ["build"] } serde = { version = "1.0.218", features = ["derive"] } +[dev-dependencies] +jiff = { version = "0.2", default-features = false } + [features] default = [] diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index 819bfd07a3..2127c7655f 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -35,7 +35,7 @@ //! //! // Set the time to half a second in the past //! let new_time = rtc.current_time_us() - 500_000; -//! rtc.set_current_time(new_time); +//! rtc.set_current_time_us(new_time); //! } //! # } //! ``` @@ -106,8 +106,8 @@ //! delay.delay_millis(1000); //! //! // Set the time to half a second in the past -//! let new_time = rtc.current_time() - 500_000; -//! rtc.set_current_time(new_time); +//! let new_time = rtc.current_time_us() - 500_000; +//! rtc.set_current_time_us(new_time); //! } //! # } //! ``` @@ -399,6 +399,16 @@ impl<'d> Rtc<'d> { } /// Get the current time in microseconds. + /// + /// # Example + /// ``` + /// use jiff::Timestamp; + /// + /// let rtc = Rtc::new(peripherals.LPWR); + /// + /// let now = Timestamp::from_microsecond(rtc.current_time_us() as i64); + /// let weekday_in_japan = now.in_tz("Asia/Tokyo").unwrap().weekday(); + /// ``` pub fn current_time_us(&self) -> u64 { // Current time is boot time + time since boot @@ -419,7 +429,18 @@ impl<'d> Rtc<'d> { } /// Set the current time in microseconds. - pub fn set_current_time(&self, current_time_us: u64) { + /// + /// # Example + /// ```no_run + /// use jiff::Timestamp; + /// + /// let rtc = Rtc::new(peripherals.LPWR); + /// + /// # fn ntp() -> Timestamp { Timestamp::UNIX_EPOCH }; + /// let now: Timestamp = ntp(); + /// + /// rtc.set_current_time_us(now.as_microsecond() as u64); + pub fn set_current_time_us(&self, current_time_us: u64) { // Current time is boot time + time since boot (rtc time) // So boot time = current time - time since boot (rtc time) From 1f75025e4c2c18cd415a0dbd6cf814732f8f81c6 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 14:27:26 +0100 Subject: [PATCH 03/13] fix? --- esp-hal/src/rtc_cntl/mod.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index 2127c7655f..ca594f940e 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -399,13 +399,15 @@ impl<'d> Rtc<'d> { } /// Get the current time in microseconds. - /// + /// /// # Example /// ``` + #[doc = crate::before_snippet!()] + /// # use esp_hal::rtc_cntl::Rtc; /// use jiff::Timestamp; - /// + /// /// let rtc = Rtc::new(peripherals.LPWR); - /// + /// /// let now = Timestamp::from_microsecond(rtc.current_time_us() as i64); /// let weekday_in_japan = now.in_tz("Asia/Tokyo").unwrap().weekday(); /// ``` @@ -429,16 +431,18 @@ impl<'d> Rtc<'d> { } /// Set the current time in microseconds. - /// + /// /// # Example - /// ```no_run + /// ``` + #[doc = crate::before_snippet!()] + /// # use esp_hal::rtc_cntl::Rtc; /// use jiff::Timestamp; - /// + /// /// let rtc = Rtc::new(peripherals.LPWR); - /// + /// /// # fn ntp() -> Timestamp { Timestamp::UNIX_EPOCH }; /// let now: Timestamp = ntp(); - /// + /// /// rtc.set_current_time_us(now.as_microsecond() as u64); pub fn set_current_time_us(&self, current_time_us: u64) { // Current time is boot time + time since boot (rtc time) From 413f6dbb020e4558275e13e62270bfa9987e7e7b Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 14:28:30 +0100 Subject: [PATCH 04/13] tz --- esp-hal/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 6727c7bff7..079768ca19 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -83,7 +83,7 @@ esp-config = { version = "0.3.0", path = "../esp-config", features = ["build"] serde = { version = "1.0.218", features = ["derive"] } [dev-dependencies] -jiff = { version = "0.2", default-features = false } +jiff = { version = "0.2" } [features] default = [] From 3a83379cce80187773ef50fafe9f2e38148abc39 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 14:36:25 +0100 Subject: [PATCH 05/13] after snippet --- esp-hal/src/rtc_cntl/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index ca594f940e..792027dcfb 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -410,6 +410,7 @@ impl<'d> Rtc<'d> { /// /// let now = Timestamp::from_microsecond(rtc.current_time_us() as i64); /// let weekday_in_japan = now.in_tz("Asia/Tokyo").unwrap().weekday(); + /// # } /// ``` pub fn current_time_us(&self) -> u64 { // Current time is boot time + time since boot @@ -444,6 +445,7 @@ impl<'d> Rtc<'d> { /// let now: Timestamp = ntp(); /// /// rtc.set_current_time_us(now.as_microsecond() as u64); + /// # } pub fn set_current_time_us(&self, current_time_us: u64) { // Current time is boot time + time since boot (rtc time) // So boot time = current time - time since boot (rtc time) From 055df198cbee2dac31c00ee8ee606af971d749ce Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 14:36:53 +0100 Subject: [PATCH 06/13] fmt --- esp-hal/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 079768ca19..133e6bd19a 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -83,7 +83,7 @@ esp-config = { version = "0.3.0", path = "../esp-config", features = ["build"] serde = { version = "1.0.218", features = ["derive"] } [dev-dependencies] -jiff = { version = "0.2" } +jiff = { version = "0.2" } [features] default = [] From 5a7dde9fa5ef57faf506c31318f4d0d33946d453 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 14:47:01 +0100 Subject: [PATCH 07/13] clippy --- esp-hal/src/rtc_cntl/mod.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index 792027dcfb..1907aa8383 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -421,14 +421,12 @@ impl<'d> Rtc<'d> { // We can detect if we wrapped the boot time by checking if rtc time is greater // than the amount of time we would've wrapped. - let current_time_us = if rtc_time_us > wrapped_boot_time_us { + if rtc_time_us > wrapped_boot_time_us { // We also just checked that this won't overflow rtc_time_us - wrapped_boot_time_us } else { boot_time_us + rtc_time_us - }; - - current_time_us + } } /// Set the current time in microseconds. From 0f8bfce6e724e560d8efa69e2753bd2cab07bcca Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 15:01:26 +0100 Subject: [PATCH 08/13] ugh --- esp-hal/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 133e6bd19a..785fe73430 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -83,7 +83,7 @@ esp-config = { version = "0.3.0", path = "../esp-config", features = ["build"] serde = { version = "1.0.218", features = ["derive"] } [dev-dependencies] -jiff = { version = "0.2" } +jiff = { version = "0.2", default-features = false, features = ["alloc", "tzdb-bundle-always"] } [features] default = [] From 8f9cfdc03fcb878d2fe3754a7ba4c49d027e5909 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 17:18:31 +0100 Subject: [PATCH 09/13] fix --- esp-hal/Cargo.toml | 2 +- esp-hal/src/rtc_cntl/mod.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 785fe73430..104aaf4b85 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -83,7 +83,7 @@ esp-config = { version = "0.3.0", path = "../esp-config", features = ["build"] serde = { version = "1.0.218", features = ["derive"] } [dev-dependencies] -jiff = { version = "0.2", default-features = false, features = ["alloc", "tzdb-bundle-always"] } +jiff = { version = "0.2", default-features = false } [features] default = [] diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index 1907aa8383..d4be0f64a3 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -404,12 +404,12 @@ impl<'d> Rtc<'d> { /// ``` #[doc = crate::before_snippet!()] /// # use esp_hal::rtc_cntl::Rtc; - /// use jiff::Timestamp; + /// use jiff::{Timestamp, tz::TimeZone}; /// /// let rtc = Rtc::new(peripherals.LPWR); /// /// let now = Timestamp::from_microsecond(rtc.current_time_us() as i64); - /// let weekday_in_japan = now.in_tz("Asia/Tokyo").unwrap().weekday(); + /// let weekday_in_london = now.to_zoned(TimeZone::UTC).weekday(); /// # } /// ``` pub fn current_time_us(&self) -> u64 { From 5c0f5d746c146e2bfba5e9e4c78dd8f662d10040 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 17:27:43 +0100 Subject: [PATCH 10/13] ugh --- esp-hal/src/rtc_cntl/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index d4be0f64a3..cae1dcbd22 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -408,8 +408,10 @@ impl<'d> Rtc<'d> { /// /// let rtc = Rtc::new(peripherals.LPWR); /// - /// let now = Timestamp::from_microsecond(rtc.current_time_us() as i64); - /// let weekday_in_london = now.to_zoned(TimeZone::UTC).weekday(); + /// let now = Timestamp::from_microsecond(rtc.current_time_us() as + /// i64).unwrap(); let weekday_in_london = + /// now.to_zoned(TimeZone::UTC).weekday(); # Ok(()) + /// # Ok(()) /// # } /// ``` pub fn current_time_us(&self) -> u64 { @@ -443,6 +445,7 @@ impl<'d> Rtc<'d> { /// let now: Timestamp = ntp(); /// /// rtc.set_current_time_us(now.as_microsecond() as u64); + /// # Ok(()) /// # } pub fn set_current_time_us(&self, current_time_us: u64) { // Current time is boot time + time since boot (rtc time) From 8febc0c7533376e461556f68f6676ba27d1104c9 Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 17:40:37 +0100 Subject: [PATCH 11/13] remove examples --- esp-hal/Cargo.toml | 3 --- esp-hal/src/rtc_cntl/mod.rs | 30 ------------------------------ 2 files changed, 33 deletions(-) diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 104aaf4b85..1ca3600d38 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -82,9 +82,6 @@ esp-metadata = { version = "0.6.0", path = "../esp-metadata" } esp-config = { version = "0.3.0", path = "../esp-config", features = ["build"] } serde = { version = "1.0.218", features = ["derive"] } -[dev-dependencies] -jiff = { version = "0.2", default-features = false } - [features] default = [] diff --git a/esp-hal/src/rtc_cntl/mod.rs b/esp-hal/src/rtc_cntl/mod.rs index cae1dcbd22..3f58172dc6 100644 --- a/esp-hal/src/rtc_cntl/mod.rs +++ b/esp-hal/src/rtc_cntl/mod.rs @@ -399,21 +399,6 @@ impl<'d> Rtc<'d> { } /// Get the current time in microseconds. - /// - /// # Example - /// ``` - #[doc = crate::before_snippet!()] - /// # use esp_hal::rtc_cntl::Rtc; - /// use jiff::{Timestamp, tz::TimeZone}; - /// - /// let rtc = Rtc::new(peripherals.LPWR); - /// - /// let now = Timestamp::from_microsecond(rtc.current_time_us() as - /// i64).unwrap(); let weekday_in_london = - /// now.to_zoned(TimeZone::UTC).weekday(); # Ok(()) - /// # Ok(()) - /// # } - /// ``` pub fn current_time_us(&self) -> u64 { // Current time is boot time + time since boot @@ -432,21 +417,6 @@ impl<'d> Rtc<'d> { } /// Set the current time in microseconds. - /// - /// # Example - /// ``` - #[doc = crate::before_snippet!()] - /// # use esp_hal::rtc_cntl::Rtc; - /// use jiff::Timestamp; - /// - /// let rtc = Rtc::new(peripherals.LPWR); - /// - /// # fn ntp() -> Timestamp { Timestamp::UNIX_EPOCH }; - /// let now: Timestamp = ntp(); - /// - /// rtc.set_current_time_us(now.as_microsecond() as u64); - /// # Ok(()) - /// # } pub fn set_current_time_us(&self, current_time_us: u64) { // Current time is boot time + time since boot (rtc time) // So boot time = current time - time since boot (rtc time) From cc32fb7de550f51ab86e2ba9c77b32631ddf1a9b Mon Sep 17 00:00:00 2001 From: Robert Bastian Date: Tue, 4 Mar 2025 17:43:24 +0100 Subject: [PATCH 12/13] reset xtask --- xtask/Cargo.toml | 2 +- xtask/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 8ea7a69a2e..840a7514ca 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -7,12 +7,12 @@ publish = false [dependencies] anyhow = "1.0.93" basic-toml = "0.1.9" +chrono = "0.4.38" clap = { version = "4.5.20", features = ["derive", "wrap_help"] } console = "0.15.10" csv = "1.3.1" env_logger = "0.11.5" esp-metadata = { path = "../esp-metadata", features = ["clap"] } -jiff = "0.2" kuchikiki = "0.8.2" log = "0.4.22" minijinja = "2.5.0" diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index 5953195bd3..f2a4a14c31 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -359,7 +359,7 @@ pub fn generate_efuse_table( let out_path = out_path.as_ref(); // We will put the date of generation in the file header: - let date = jiff::Timestamp::now().to_zoned(jiff::tz::TimeZone::UTC).date(); + let date = chrono::Utc::now().date_naive(); // Determine the commit (short) hash of the HEAD commit in the // provided ESP-IDF repository: From 3c8a57416c9457360ba58ba1d1602b8611bd6fab Mon Sep 17 00:00:00 2001 From: Robert Bastian <4706271+robertbastian@users.noreply.github.com> Date: Wed, 5 Mar 2025 20:48:42 +0100 Subject: [PATCH 13/13] Update esp-hal/CHANGELOG.md Co-authored-by: Juraj Sadel --- esp-hal/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index f47bfa6fa3..abc5d8f87a 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Replaced `chrono::NaiveDateTime` on the RTC API by raw `u64` timestamps (#3185) +- Replaced `chrono::NaiveDateTime` on the RTC API by raw `u64` timestamps (#3200) ### Fixed