0

I work with STM32F412/STM32F413 processor. I have also external LSE.

I have the next problem related to STOP mode and RTC.

There are some scenarios we want the processor to go to sleep for x hours.

The processor goes to sleep for 5 seconds, doing some job and return to sleep for 5 seconds and so on in infinite loop till it is broke by interrupt or if time elapsed( the x hours ).

The problem is that sometimes during the sleep phase - The RTC read after HAL_PWR_EnterSTOPMode is the same as before(sleepTime =0) even if in fact the processor sleeps for 5 seconds(We checked it by led and by debug code). This causes the total sleep time to increase and awakening delayed after several hours!!!!

I thought that maybe because the RTC is not ready between HAL_PWR_EnterSTOPMode and the RTC read, so I added a delay after HAL_PWR_EnterSTOPMode line and also add after the delay a piece of code that if sleepTime is 0 -> reading again the RTC. It seems that it solved the problem, but this is not solution I like and it looks bad. I prefer to a solution to read a register or a variable and by that to know that the RTC is ready to be read.

The next is a sample of the relevant code(without my "solution"):

HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 5, RTC_WAKEUPCLOCK_CK_SPRE_16BITS) ;
RTC_TimeTypeDef sTimeBefore, sTimeAfter ;
RTC_DateTypeDef sDateBefore, sDateAfter ;
int sleepTime = 0 ;
int totalSleepTime = 0 ;

while(sleep) {
  ReadRTC(&sTimeBefore, &sDateBefore);
  // This function calls the HAL_RTC_GetTime function and after that the HAL_RTC_GetDate function
  HAL_PWR_EnterSTOPMode( PWR_LOWPOWERREGULATOR_ON , PWR_STOPENTRY_WFE ) ;
ReadRTC(&sTimeAfter, &sDateAfter);
sleepTime = CalculateSleepTime(sTimeBefore, sDateBefore, sTimeAfter, sDateAfter);
totalSleepTime += sleepTime;
if (totalSleepTime > x) sleep = 0 ;
}

Please any assistance.

Shai

I thought that maybe because the RTC is not ready between HAL_PWR_EnterSTOPMode and the RTC read, so I added a delay after HAL_PWR_EnterSTOPMode line and also after the delay a piece of code that if sleepTime is 0 -> reading again the RTC. It seems that it solved the problem, but this is not a solution I like and it looks bad. I would prefer to read a register or a variable and by that know that the RTC is ready to be read.

dda
  • 6,030
  • 2
  • 25
  • 34
  • I see this in section 6.2.8 of the [reference manual](https://www.st.com/resource/en/reference_manual/rm0402-stm32f412-advanced-armbased-32bit-mcus-stmicroelectronics.pdf): "To read the RTC calendar register when the APB1 clock frequency is less than seven times the RTC clock frequency (fAPB1 < 7xfRTCLCK), the software must read the calendar time and date registers twice. The data are correct if the second read access to RTC_TR gives the same result than the first one. Otherwise a third read access must be performed." I don't know if this applies to you. – pmacfarlane May 02 '23 at 14:52
  • Hi pmacfarlane, Thank you for your reply. I don't understand how can I read access to RTC_TR. The HAL functions read time and date return always HAL_OK: HAL_RTC_GetTime() and HAL_RTC_GetDate() return always HAL_OK. Best Regards, Shai – Shai Amar May 16 '23 at 15:19

0 Answers0