periph_common/rtc: add rtc_mktime() & rtc_localtime() helper functions for RTC implementations#13284
Conversation
|
Hmm, 2156 seems to be reasonablly far in the future; at least our generation can be unconcerned that we will ever face issues :-D. (Same line of thoughts our politicians seem to have when it comes to climate change...) However, is there a use case where it would make sense to set the RTC to a point in time in the past? I would say no - but if there is, this use case will be impossible now. |
|
You could still re-define (Force-pushed because I accidentally a word) |
|
@benpicco This is pretty cool. I'm wondering whether it would be better to split into a PR which extends the RTC API and a PR with the changes for |
|
@gschorcht I can just drop the stm32f1 commit from this PR, I thought it might be good to have an example of how those can be useful. |
Add a function to convert a time struct to an unsigned timestamp (non-UNIX).
Not neccessary, I read the test instruction again 😎 It makes sense to have a platform as POC. With the changed title the PR makes sense as it is 😉 |
Alright! But now I converted esp32 instead. |
|
OK, I tested it with your initial commit for |
| else { | ||
| return -1; | ||
| } | ||
| return 0; |
There was a problem hiding this comment.
Wow, it's much easier to use 👍 I like it.
There was a problem hiding this comment.
It should also save a good chunk of memory:
master
esp32-wroom-32:
text data bss dec hex filename
69261 4624 6432 80317 139bd /home/benpicco/dev/RIOT/tests/periph_rtc/bin/esp32-wroom-32/tests_periph_rtc.el
blackpill-128kib:
text data bss dec hex filename
23416 232 2736 26384 6710 /home/benpicco/dev/RIOT/tests/periph_rtc/bin/blackpill-128kib/tests_periph_rtc.elf
this branch
esp32-wroom-32:
text data bss dec hex filename
66945 4368 6424 77737 12fa9 /home/benpicco/dev/RIOT/tests/periph_rtc/bin/esp32-wroom-32/tests_periph_rtc.elf
blackpill-128kib:
text data bss dec hex filename
14272 132 2696 17100 42cc /home/benpicco/dev/RIOT/tests/periph_rtc/bin/blackpill-128kib/tests_periph_rtc.elf
|
Yes if it can go in with this PR there is no need for a separate PR. |
Add function to convert a RTC timestamp (non-UNIX) to a time struct.
By using a custom EPOCH for the RTC implementation, we can extend the range of the 32 bit counter based RTC by 118 years. It also reduces the code size compared to the stdlib based POSIX functions.
|
Alright, squashed & dropped the stm32f1 commit. |
|
Thank you! |
Contribution description
Some RTC implementations are based on a 1 Hz RTT and use a Unix timestamp and
mktime()/localtime()to convert thestruct tmto a timestamp and back.While convenient, it is not necessary to start there, but worse it will also stop working on 2038-01-19.
This introduces two new functions:
rtc_mktime()rtc_localtime()They function similar to
mktime()andlocaltime_r()but forego POSIX compatibility to be used by RTC implementations where this is not needed.Key differences are:
Since RTC is unlikely to be set to a time in the past, we can use this to easily push the Y2038 problem to the year 2156 (2156-02-07 06:28:15).
Since RTC does not need to handle dailight savings time, timezone and such, the
rtc_implementation saves ~9k compared to using the newlib functions.Testing procedure
I only converted the stm32f1 RTC as an example.
Run
tests/periph_rtcon e.g. abluepillboard.Issues/PRs references
possible solution for #13277