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

Allow setting RTC time #1883

Merged
merged 35 commits into from
Sep 9, 2024
Merged

Allow setting RTC time #1883

merged 35 commits into from
Sep 9, 2024

Conversation

amsam0
Copy link
Contributor

@amsam0 amsam0 commented Jul 30, 2024

Thank you for your contribution!

We appreciate the time and effort you've put into this pull request.
To help us review it efficiently, please ensure you've gone through the following checklist:

Submission Checklist 📝

  • I have updated existing examples or added new ones (if applicable).
  • I have used cargo xtask fmt-packages command to ensure that all changed code is formatted correctly.
  • My changes were added to the CHANGELOG.md in the proper section.
  • My changes are in accordance to the esp-rs API guidelines

Extra:

Pull Request Details 📖

Description

esp-idf has gettimeofday (see here) and settimeofday (see here) functions, allowing the user to get and set the internal RTC time. While esp-hal allows getting the RTC time, it does not allow setting it.

esp-idf sets the RTC time by storing the boot time (see here for getting, here for setting, here for esp32s3 register info). It calculates the current time by adding the boot time and the RTC time. Therefore, you can set the current time by setting the boot time to the new current time minus the RTC time. esp-idf also does some more advanced things like calculating the offset between RTC and systimer and adjusting the boot time, which I haven't taken the time to understand or implement since the current implementation is good enough for my needs.

This PR contains a basic port of esp-idf's boot time usage in the Rtc struct. It adds set_time_us, set_time_ms, get_rtc_time_us, get_rtc_time_us, and renames get_time_raw to get_rtc_time_raw. It should support all esp32 chips.

To avoid overflows when adding the boot time and the rtc time, the boot time also wraps around u64::MAX.

There are also some traits in the rtcc crate that may be worth implementing.

Testing

This PR has been tested on a esp32s3.

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a nice addition, do you plan on finishing this up?

@amsam0
Copy link
Contributor Author

amsam0 commented Aug 7, 2024

Absolutely, but first I'd like to know what the preference is for general API (for instance, should the boot time functions be public?) and for implementing the rtcc traits. There is also the "boot time adjustment" and rtc offset to consider, but I don't really think they're needed.

@amsam0 amsam0 marked this pull request as ready for review August 12, 2024 20:16
@amsam0
Copy link
Contributor Author

amsam0 commented Aug 12, 2024

I've deprecated Rtc::get_time_raw, added the missing get_time functions, added documentation, updated the rtc_time example, updated the changelog, and checked to ensure that RTC set supports all chips. While I only have an esp32s3 to test with, this PR should be ready for merging, unless further changes are wanted.

@Szybet
Copy link
Contributor

Szybet commented Aug 13, 2024

Oh, cool that you have checks for different devices
Yea, it doesn't compile for the c6 (Or any at all?), gonna look for a solution

@Szybet
Copy link
Contributor

Szybet commented Aug 13, 2024

For the c6 I think only this was needed:

        #[cfg(any(esp32h2))]
        let rtc_cntl = unsafe { &*LP_TIMER::ptr() };
        #[cfg(any(esp32c6))]
        let rtc_cntl = unsafe { &*LP_AON::ptr() };

using LP_AON instead of LP_TIMER

Some resources:
https://github.com/espressif/esp-idf/blob/8e4454b285ad39881c5bf3f440d8be617a2f18a8/components/esp_rom/esp32c6/include/esp32c6/rom/rtc.h#L60-L61
https://github.com/espressif/esp-idf/blob/8e4454b285ad39881c5bf3f440d8be617a2f18a8/components/newlib/port/esp_time_impl.c#L110-L121

Full commit here:
dragonnn#1

@amsam0
Copy link
Contributor Author

amsam0 commented Aug 13, 2024

Yes, that's correct. I saw LP_AON in the register headers but assumed it was the same as LP_TIMER, especially since LP_TIMER is used for the other RTC stuff. I'll add a fix when I have some time

esp-hal/src/rtc_cntl/mod.rs Outdated Show resolved Hide resolved
esp-hal/src/rtc_cntl/mod.rs Outdated Show resolved Hide resolved
@Szybet
Copy link
Contributor

Szybet commented Aug 14, 2024

Additionally I tested it on esp32c6 with the latest changes, works good

@amsam0
Copy link
Contributor Author

amsam0 commented Aug 27, 2024

Chrono sounds good and in my own personal usage of this PR, is what I'm converting the current_time to anyways. I'll implement that when I have some time (pun not intended).

Although, it is important to consider that embassy has their own DateTime struct and simply provides a From implementation for chrono, and that some people may be reluctant to add chrono as a dependency.

@amsam0
Copy link
Contributor Author

amsam0 commented Sep 1, 2024

current_time and set_current_time now use chrono's NaiveDateTime. I'm guessing that the preference will be to avoid adding chrono as a dependency and instead use our own DateTime type like embassy, but chrono is definitely a simpler approach.

Currently, set_current_time will panic if the NaiveDateTime is before the Unix epoch. Wrapping should be possible but it's a little more complicated so I didn't implement it (and even if it was implemented, I don't know why anyone would want to set the time to before the Unix epoch).

@amsam0
Copy link
Contributor Author

amsam0 commented Sep 4, 2024

Any chance of further review? It's been more than a month and it would be great to get this merged. I can update the example and fix merge conflicts if the API seems fine.

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM API-wise, I don't see the problem in directly depending on chrono, versus having our own types here.

@amsam0
Copy link
Contributor Author

amsam0 commented Sep 5, 2024

Changelog and examples are updated, so this PR should be ready to be merged

esp-hal/src/rtc_cntl/mod.rs Outdated Show resolved Hide resolved
@bjoernQ
Copy link
Contributor

bjoernQ commented Sep 5, 2024

There is some overlap now between rtc_cntl::Rtc::current_time and time::current_time - not something that should be addressed here - just mentioning

@MabezDev
Copy link
Member

MabezDev commented Sep 5, 2024

Hmm good point, but I would somewhat argue that we should change time::current_time to something else, probably time::now, given that it returns an instant (not wall clock time)?

Imo Rtc::current_time fits quite well for wall clock time.

esp-hal/CHANGELOG.md Outdated Show resolved Hide resolved
examples/src/bin/rtc_time.rs Show resolved Hide resolved
@bjoernQ
Copy link
Contributor

bjoernQ commented Sep 5, 2024

Hmm good point, but I would somewhat argue that we should change time::current_time to something else, probably time::now, given that it returns an instant (not wall clock time)?

Imo Rtc::current_time fits quite well for wall clock time.

👍 Sure - I'd vote for time::uptime() or similar - even the docs say "system uptime"

@bjoernQ
Copy link
Contributor

bjoernQ commented Sep 6, 2024

Thanks - probably you should revert fe84468 since it's already done and merged in #2091

@bjoernQ
Copy link
Contributor

bjoernQ commented Sep 6, 2024

After a rebase this should be fine I guess

@amsam0
Copy link
Contributor Author

amsam0 commented Sep 7, 2024

The PR has been rebased, and some basic info has been added to the migration guide

Copy link
Contributor

@bjoernQ bjoernQ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@MabezDev MabezDev added this pull request to the merge queue Sep 9, 2024
Merged via the queue into esp-rs:main with commit 82a9abf Sep 9, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants