8000 Adds date retention in the backup memory for the stm32f1xx by FRASTM · Pull Request #58 · stm32duino/STM32RTC · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Adds date retention in the backup memory for the stm32f1xx #58

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

Merged
merged 3 commits into from
Dec 16, 2021

Conversation

FRASTM
Copy link
Contributor
@FRASTM FRASTM commented Dec 3, 2021

This PR adds a RTC date retention for the stm32F1 MCUs.
This new feature is saving/restoring the date (year, month, day, weekday) in the backup memory of he MCUs.
On the stm32F1, the Date is not present in the RTC peripheral but computed by the RTC driver.
The BackUp memory of the stm32F1 is powered by the VBat when VDD is switched off.
It is not altered by the system Reset.

Each one-byte element of the RTC Date (DateToUpdate) is well placed in two contiguous 16bit backup registers (RTC_BKP_DATE, RTC_BKP_DATE + 1). The registers are arbitrary selected to store the date and can be easily changed in the rtc.h to choose other memory positions.

Moreover, for the stm32F1, the 'reset' flag is now available in the RTC::begin to reset the content of the backUp register.
Note that the reset value of the RTC date in the stm32F1 is '01/01/2000'

A simple RTC examples/backupRTC is given to demonstrate this feature (--> test with different the reset_bkup bool values).

This PR is inspired by the #41

Fixes #32

Signed-off-by: Francois Ramu francois.ramu@st.com

@FRASTM FRASTM force-pushed the f1_bkup_date branch 3 times, most recently from 1fc2397 to 9cee107 Compare December 13, 2021 09:00
@fpistm fpistm added the enhancement New feature or request label Dec 13, 2021
@fpistm fpistm added this to the 1.2.0 milestone Dec 13, 2021
8000
@FRASTM
Copy link
Contributor Author
FRASTM commented Dec 14, 2021

update after review:
two 16 bit registers LL_RTC_BKP_DR6 & LL_RTC_BKP_DR7 are reserved in the stm32F1 backUp memory to keep the date (RTC_BKP_DATE). Function getBackupRegister/setBackupRegister are used to access ths bakup mem.

Copy link
Member
@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

Please update the README.md.
One suggestion in the example to use \n instead of \r mainly for windows.
Else LGTM.

@FRASTM FRASTM force-pushed the < 8000 span > f1_bkup_date branch from 3379426 to 4f53644 Compare December 14, 2021 15:29
@FRASTM
Copy link
Contributor Author
FRASTM commented Dec 14, 2021

updates the README.md

@FRASTM FRASTM force-pushed the f1_bkup_date branch 4 times, most recently from 011ba02 to a802a9b Compare December 15, 2021 13:47
@FRASTM
Copy link
Contributor Author
FRASTM commented Dec 15, 2021

rename sketch to F1RTCDateRetention.ino
change print text with "\n"

@fpistm fpistm linked an issue Dec 15, 2021 that may be closed by this pull request
Copy link
Member
@fpistm fpistm left a comment

Choose a reason for hiding this comment

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

Hi @FRASTM
as #57 has been merged could you please rebase this one on top to resolve conflict.

The stm32F1 RTC date value are read/write from/to
a BackUp register : RTC_BKP_DATE
The BackUp register DR6 is choosen and can be changed easily,
with a not-already-used one.
The stm32F1 has 16bit backup reg. : 2 consecutive addresses are reserved.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
Signed-off-by: Francois Ramu <francois.ramu@st.com>
On the stm32F1, this example saves the date of the current RTC.
With flag true, the BackUp Registers are cleared. Then on the next
reset the RTC starts the from initial date (1/1/2000).
With Flag false, on the next (warm) reset, the calendar starts
from the (latest) saved value.

Signed-off-by: Francois Ramu <francois.ramu@st.com>
@FRASTM
Copy link
Contributor Author
FRASTM commented Dec 16, 2021

rebase after merge of PR #57

@bxparks
Copy link
bxparks commented Feb 5, 2022

Just want to thank you for this PR. I reviewed the code out of curiosity and it looks good.

For future reference for other people who may come here and wonder, I verified that the code actually handles the 2 edge cases that I was worried about:

  1. The microcontroller is powered-down during a transition from 23:59:59 to 24:00:00 the next day, and
  2. The microcontroller is powered-down for more than 24h.

Both cases are handled because the RTC code in the HAL (stm32f1xx_hal_rtc.c) performs a renormalization of the 32-bit RTC counter every time HAL_RTC_GetTime() is called. The RTC counter counter_time is normalized to be an offset from 00:00:00 of the current date. If more than 24h has passed from the last time HAL_RTC_GetTime() was called, then the the date components are adjusted forward by the number of days that have passed. So if the microcontroller is powered down for more than 24hours, then upon reboot, the date components are restored from the backup registers, fed into the HAL, then HAL_RTC_GetTime() will adjusted the date forward by the whole number of days indicated by the RTC counter. Leap years are accounted for correctly, including those divisible by 400.

If I understand the HAL code correctly, I think there might be a small data-race condition inside HAL_RTC_GetTime(), but it's probably not worth worrying about. In the few microseconds between the call to RTC_ReadTimeCounter() and the call to RTC_WriteTimeCounter() that writes the renormalized counter_time, the hardware RTC could have incremented the RTC_CNT registers by 1. I'm not sure that there's anything that can be done about this under the current architecture of HAL for the STM32F1.

@bxparks
Copy link
bxparks commented Feb 5, 2022

It occurred to me last night that RTC_WriteTim 6D40 eCounter() is called only if counter_time is greater than 24h. So the race condition occurs only during that transition. I wrote a test program to try to trigger this race condition by calling HAL_RTC_GetTime() as fast as possible (around 150,000 times/second in my tests) during the 23:59:59 to 24:00:00 transition, but I could not trigger it. The probability of triggering this race condition is so small, and the impact (the loss of one second) is minor, so it is not worth worrying about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

times reset after power cycle
3 participants
0