STM32F412 STOP mode HAL_RTC_GetTime return time before sleep

345 Views Asked by At

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.

0

There are 0 best solutions below