Skip to content

periph_common/rtc: add rtc_mktime() & rtc_localtime() helper functions for RTC implementations#13284

Merged
gschorcht merged 5 commits intoRIOT-OS:masterfrom
benpicco:rtc_mktime
Mar 3, 2020
Merged

periph_common/rtc: add rtc_mktime() & rtc_localtime() helper functions for RTC implementations#13284
gschorcht merged 5 commits intoRIOT-OS:masterfrom
benpicco:rtc_mktime

Conversation

@benpicco
Copy link
Copy Markdown
Contributor

@benpicco benpicco commented Feb 4, 2020

Contribution description

Some RTC implementations are based on a 1 Hz RTT and use a Unix timestamp and mktime()/localtime() to convert the struct tm to 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() and localtime_r() but forego POSIX compatibility to be used by RTC implementations where this is not needed.

Key differences are:

  • timestamp is unsigned
  • epoch starts in 2020

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_rtc on e.g. a bluepill board.

Issues/PRs references

possible solution for #13277

@benpicco benpicco added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: drivers Area: Device drivers Area: cpu Area: CPU/MCU ports labels Feb 4, 2020
@benpicco benpicco requested review from kaspar030 and maribu February 4, 2020 15:39
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 4, 2020
@maribu
Copy link
Copy Markdown
Member

maribu commented Feb 10, 2020

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.

@benpicco benpicco removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 10, 2020
@benpicco
Copy link
Copy Markdown
Contributor Author

You could still re-define RIOT_EPOCH if you want to dwell in the past.
Since this is all internal to the RTC implementation, the absolute values of the RTT timer don't need to be portable, the user will only get a struct tm.

(Force-pushed because I accidentally a word)

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 25, 2020
@benpicco benpicco changed the title cpu/stm32f1: make RTC Y2038 safe cpu/stm32f1: make RTC Y2038 safe, add rtc_mktime() & rtc_localtime() Feb 29, 2020
@gschorcht
Copy link
Copy Markdown
Contributor

@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 cpu/stm32f1

@benpicco
Copy link
Copy Markdown
Contributor Author

benpicco commented Feb 29, 2020

@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).
@benpicco benpicco changed the title cpu/stm32f1: make RTC Y2038 safe, add rtc_mktime() & rtc_localtime() periph_common/rtc: add rtc_mktime() & rtc_localtime() helper functions for RTC implementations Feb 29, 2020
@gschorcht
Copy link
Copy Markdown
Contributor

@gschorcht I can just drop the stm32f1 commit from this PR

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 😉

@benpicco
Copy link
Copy Markdown
Contributor Author

benpicco commented Feb 29, 2020

I read the test instruction again It makes sense to have a platform as POC.

Alright! But now I converted esp32 instead.

@gschorcht
Copy link
Copy Markdown
Contributor

OK, I tested it with your initial commit for stm32f1. It works as expected.

else {
return -1;
}
return 0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wow, it's much easier to use 👍 I like it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

@gschorcht gschorcht added the Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines label Feb 29, 2020
@gschorcht gschorcht added Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines labels Feb 29, 2020
@benpicco benpicco removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Feb 29, 2020
@gschorcht
Copy link
Copy Markdown
Contributor

Does commit e1c14c6 make PR #13520 obsolete?

@benpicco
Copy link
Copy Markdown
Contributor Author

benpicco commented Mar 1, 2020

Yes if it can go in with this PR there is no need for a separate PR.

@gschorcht
Copy link
Copy Markdown
Contributor

@benpicco Since you already have PR #13520 opened it is might be better to have one platform (ESP32) as POC and for testing as part of this PR and to add the changes for the other platforms as separate changes.

Please squash, feel free to drop or leave e1c14c6 part of this PR or not.

benpicco and others added 3 commits March 3, 2020 11:01
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.
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Mar 3, 2020
@benpicco
Copy link
Copy Markdown
Contributor Author

benpicco commented Mar 3, 2020

Alright, squashed & dropped the stm32f1 commit.

Copy link
Copy Markdown
Contributor

@gschorcht gschorcht left a comment

Choose a reason for hiding this comment

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

ACK

@gschorcht gschorcht merged commit 9e37210 into RIOT-OS:master Mar 3, 2020
@benpicco benpicco deleted the rtc_mktime branch March 3, 2020 13:19
@benpicco
Copy link
Copy Markdown
Contributor Author

benpicco commented Mar 3, 2020

Thank you!

@leandrolanzieri leandrolanzieri added this to the Release 2020.04 milestone Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: cpu Area: CPU/MCU ports Area: drivers Area: Device drivers CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Reviewed: 1-fundamentals The fundamentals of the PR were reviewed according to the maintainer guidelines Reviewed: 2-code-design The code design of the PR was reviewed according to the maintainer guidelines Reviewed: 3-testing The PR was tested according to the maintainer guidelines Reviewed: 4-code-style The adherence to coding conventions by the PR were reviewed according to the maintainer guidelines Reviewed: 5-documentation The documentation details of the PR were reviewed according to the maintainer guidelines Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants