Skip to main content
SSmit.13
Senior
October 17, 2023
Question

Cant access RTC coming out of standby mode

  • October 17, 2023
  • 2 replies
  • 978 views

Hi

  I am programming a STM32L433 and have the processor wakeup from Standby mode. The first thing it does after wakeup is check the cause of the wakeup, and if RTC increase the time, do a few readings and go back into Standby mode. When I am running through the debugger, all works as expected, it comes out of standby and increases the time, and goes back to standby.  If I simply repower the board it comes out of standby but the RTC isnt updated (or read).  I am pulling my hair out and been at this for 2 days, can someone help?

 

I have in my code: 

 

int main(void)

{

......

CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;

DWT->CTRL |= 1;

PWR->CR1 |= PWR_CR1_DBP;

if((RCC->APB1ENR1 & RCC_APB1ENR1_PWREN)==0)

RCC->APB1ENR1 |= RCC_APB1ENR1_PWREN;

if((PWR->CR1 & PWR_CR1_DBP) ==0)

{

PWR->CR1 |= PWR_CR1_DBP;

while((PWR->CR1 & PWR_CR1_DBP)==0);

}

 

if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB)!=RESET)

{

__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);

if(RTC->ISR & RTC_ISR_ALRAF)

{

UpdateRTC=1;

RTC->ISR &= ~(RTC_ISR_ALRAF);

}

}

 

InitRTC();

 

if(!UpdateRTC)

{

... Set time

}

else

ReadTime():

 

//....Add 1 hour and go into standby mode

HAL_PWREx_EnableSRAM2ContentRetention();

HAL_PWR_EnterSTANDBYMode();

}

 

void ReadRTC()

{

while((RTC->ISR & RTC_ISR_RSF)==0);

Tme.Sec=(((RTC->TR & 0x7f) >> 4)*10)+(RTC->TR & 0xf);

 

Tme.Min=((RTC->TR & 0x7f00) >> 8);

Tme.Min=(((Tme.Min & 0x7f)>>4)*10)+(Tme.Min & 0xf);

 

Tme.Hour=((RTC->TR & 0x7f0000) >> 16);

Tme.Hour=(((Tme.Hour & 0x7f)>>4)*10)+(Tme.Hour & 0xf);

 

Dte.Year=((RTC->DR >> 20)*10) + ((RTC->DR >>16) & 0xf);

Dte.Month=((RTC->DR >> 12) & 1)*10 + ((RTC->DR >>8) & 0xf);

Dte.Day=((RTC->DR >> 4) & 3)*10 + (RTC->DR & 0xf);

 

}

 

void InitRTC()

{

if(RTC->BKP0R!=0x1954)

{

RCC->BDCR &= ~(RCC_BDCR_LSEON | RCC_BDCR_LSEBYP);

RCC->BDCR |= RCC_BDCR_BDRST;

RCC->BDCR &= ~RCC_BDCR_BDRST;

while((RCC->BDCR & RCC_BDCR_LSERDY)==0)

RCC->BDCR |= RCC_BDCR_LSEON;

 

RCC->BDCR &= ~RCC_BDCR_RTCSEL;

RCC->BDCR |= RCC_BDCR_RTCSEL_0;

 

RCC->BDCR |= RCC_BDCR_RTCEN;

 

RTC->WPR = 0xCA;

RTC->WPR = 0x53;

 

RTC->ISR |= RTC_ISR_INIT;

if((RTC->ISR & RTC_ISR_INITF) == 0)

RTC->ISR = (uint32_t)0xFFFFFFFFU;

 

while((RTC->ISR & RTC_ISR_INITF) == 0);

while((RTC->ISR & RTC_ISR_ALRAWF)==0);

 

RTC->PRER = 0x007F00FF;

 

RTC->ISR &=~ RTC_ISR_INIT;

RTC->WPR = 0xFF;

 

RCC->BDCR |= RCC_BDCR_RTCEN;

RTC->BKP0R=0x1954;

while((RTC->ISR & RTC_ISR_RSF)==0);

}

}

This topic has been closed for replies.

2 replies

TDK
October 17, 2023

> If I simply repower the board

If the board doesn't have power, it's not in standby and the SB flag won't be set at startup.

"If you feel a post has answered your question, please click ""Accept as Solution""."
SSmit.13
SSmit.13Author
Senior
October 17, 2023

Hi,

 Sorry , I meant after I program it I repower the board and it starts as expected. It then goes into standby and when the RTC interupt wakes it up, i cant seem to update the clock.  But when I run it within the debugger the rtc works as expected after wakening up the processor. When first powered, as the SB isnt set, so the time gets updated, then the alarm gets set before going into standby. Then when wakens , the SB is set but cant read the clock its junk in the variables (unless ran in debugger)

 

Scott.