Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use raw u64 instead of chrono on RTC API #3200

Merged
merged 13 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (#3200)

### Fixed

- Full-duplex SPI works when mixed with half-duplex SPI (#3176)
Expand Down
1 change: 0 additions & 1 deletion esp-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 12 additions & 29 deletions esp-hal/src/rtc_cntl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
//!
//! 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);
//! rtc.set_current_time(new_time);
//! let new_time = rtc.current_time_us() - 500_000;
//! rtc.set_current_time_us(new_time);
//! }
//! # }
//! ```
Expand Down Expand Up @@ -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);
//! rtc.set_current_time(new_time);
//! let new_time = rtc.current_time_us() - 500_000;
//! rtc.set_current_time_us(new_time);
//! }
//! # }
//! ```

use chrono::{DateTime, NaiveDateTime};

pub use self::rtc::SocResetReason;
#[cfg(not(any(esp32c6, esp32h2)))]
use crate::clock::XtalClock;
Expand Down Expand Up @@ -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();
Expand All @@ -410,31 +408,16 @@ 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
};

DateTime::from_timestamp_micros(current_time_us as i64)
.unwrap()
.naive_utc()
}
}

/// 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_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)

Expand Down
Loading