-
Notifications
You must be signed in to change notification settings - Fork 3k
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
STM32 RTC : write RTC time while LPTICKER is enabled #8286
Conversation
struct tm timeinfo; | ||
core_util_critical_section_enter(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was something calling this outside a critical section?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added protection, as rtc_write and rtc_read_lp functions can update the same variables
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Request for few comments to be added
targets/TARGET_STM/rtc_api.c
Outdated
/* Save current SSR */ | ||
uint32_t Read_SubSeconds = (uint32_t)(RTC->SSR); | ||
/* RTC SubSeconds counter is reset to PREDIV_S_VALUE */ | ||
LP_last_RTC_time = (timeStruct.Seconds + timeStruct.Minutes * 60 + timeStruct.Hours * 60 * 60) * PREDIV_S_VALUE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this always fit into a uint32_t ? or would a uint64_t be required ?
Also is LP_last_RTC_time in real seconds ? or µs ?
targets/TARGET_STM/rtc_api.c
Outdated
|
||
/* Save current SSR */ | ||
uint32_t Read_SubSeconds = (uint32_t)(RTC->SSR); | ||
/* RTC SubSeconds counter is reset to PREDIV_S_VALUE */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not clear to me what the comment says and how it relates to the code below: where is Subsecond counter reset to PREDIV_S_VALUE ?
targets/TARGET_STM/rtc_api.c
Outdated
@@ -341,11 +334,16 @@ static void RTC_IRQHandler(void) | |||
|
|||
uint32_t rtc_read_lp(void) | |||
{ | |||
/* LPTICKER time is reading only time RTC register (in second) and add the sub-second part |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is the sub-second part retrieved from and how is it computed ?
targets/TARGET_STM/rtc_api.c
Outdated
/* LPTICKER time is reading only time RTC register (in second) and add the sub-second part | ||
* In order to get a continuous time information (each 24h = 86400s = 0x15180), | ||
* current time is added into an internal counter : LP_continuous_time | ||
* In order to get spent time since last counter update, current RTC time is saved : LP_last_RTC_time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is "last counter update" ? do you mean RTC_write (like application set_time()) ?
Does this comment apply here or in RTC_write function ?
targets/TARGET_STM/rtc_api.c
Outdated
@@ -358,17 +356,18 @@ uint32_t rtc_read_lp(void) | |||
timeinfo.tm_min = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_MNT | RTC_TR_MNU)) >> 8)); | |||
timeinfo.tm_sec = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_ST | RTC_TR_SU)) >> 0)); | |||
|
|||
uint32_t RTC_time_s = timeinfo.tm_sec + timeinfo.tm_min * 60 + timeinfo.tm_hour * 60 * 60; // Max 0x0001-517F => * 8191 + 8191 = 0x2A2E-AE80 | |||
uint32_t RTC_time_s = (timeinfo.tm_sec + timeinfo.tm_min * 60 + timeinfo.tm_hour * 60 * 60) * PREDIV_S_VALUE + PREDIV_S_VALUE - Read_SubSeconds; // Max 0x0001-517F * 8191 + 8191 = 0x2A2E-AE80 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is RTC_time_s still in seconds ? the operation seems to mix it with sub_seconds. If so, need to rename the variable.
targets/TARGET_STM/rtc_api.c
Outdated
@@ -358,17 +356,18 @@ uint32_t rtc_read_lp(void) | |||
timeinfo.tm_min = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_MNT | RTC_TR_MNU)) >> 8)); | |||
timeinfo.tm_sec = RTC_Bcd2ToByte((uint8_t)((Read_time & (RTC_TR_ST | RTC_TR_SU)) >> 0)); | |||
|
|||
uint32_t RTC_time_s = timeinfo.tm_sec + timeinfo.tm_min * 60 + timeinfo.tm_hour * 60 * 60; // Max 0x0001-517F => * 8191 + 8191 = 0x2A2E-AE80 | |||
uint32_t RTC_time_s = (timeinfo.tm_sec + timeinfo.tm_min * 60 + timeinfo.tm_hour * 60 * 60) * PREDIV_S_VALUE + PREDIV_S_VALUE - Read_SubSeconds; // Max 0x0001-517F * 8191 + 8191 = 0x2A2E-AE80 | |||
|
|||
if (LP_last_RTC_time <= RTC_time_s) { | |||
LP_continuous_time += (RTC_time_s - LP_last_RTC_time); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be great to explain in commit message how those 3 variables are used:
LP_continuous_time : is it the time in µsec since start-up ?
RTC_time_s: the current time that was just read from RTC register ?
LP_last_RTC_time: the last time read from RTC register, either in this function, or in RTC_write() ?)
I don't undertstand the "add 24h" case ...
This fix avoid a long waiting loop in rtc_write function, which was not acceptable in TICKLESS context. Implementation comments added. Global variable name has been updated for easier maintenance: - LPTICKER_counter is the U32 continuous tick counter - LPTICKER_RTC_time is the RTC time used to get the time difference between rtc_read_lp() calls
0362e53
to
83caa04
Compare
@LMESTM please check updates |
/morph build |
Build : SUCCESSBuild number : 3274 Triggering tests/morph test |
Exporter Build : FAILUREBuild number : 2899 |
Test : SUCCESSBuild number : 3082 |
/morph export-build |
Exporter Build : SUCCESSBuild number : 2909 |
Description
During RTC register write, only the date and time counters are set,
the SubSecond part is reset.
In the previous implementation, there was a quite long loop (up to 1 second).
@LMESTM @c1728p9 @mattbrown015
Pull request type