Cant access RTC coming out of standby mode
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);
}
}