cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L475 - Problem Reading RTC Time and Date after Wakeup

christiantasche9
Associate II
Posted on November 07, 2016 at 08:54

I'm using the RTC to Wakeup my STM32L475 µC from Standby Mode. That is working fine.

And i use the The HAL Library 1.5.2 for STM32L4 The main problem is i have to read the Time and Date after Wakeup. I use the functions: HAL_RTC_GetTime(&hrtc, &strTimeouts.strLastCheckTime, RTC_FORMAT_BIN); HAL_RTC_GetDate(&hrtc, &strTimeouts.strLastCheckDate, RTC_FORMAT_BIN); I know that i have to read the time first, than the date. And i know that i have to reset the RSF (Register Syncronisation Flag) after Wakeup. Since i don't want to reinit my completet RTC with Date and Time i have made some own Init functions, wich where called after wakeup:

/**
* @brief Initialize the RTC according to the specified parameters 
* in the RTC_InitTypeDef structure and initialize the associated handle.
* @param hrtc: RTC handle
* @retval HAL status
*/
HAL_StatusTypeDef HAL_RTC_Wakeup_Init(RTC_HandleTypeDef *hrtc)
{
//uint32_t tmpreg = 0;
/* Check the RTC peripheral state */
if(hrtc == NULL)
{
return HAL_ERROR;
}
/* Check the parameters */
assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
if(hrtc->State == HAL_RTC_STATE_RESET)
{
/* Allocate lock resource and initialize it */
hrtc->Lock = HAL_UNLOCKED;
/* Initialize RTC MSP */
//HAL_RTC_MspInit(hrtc);
// Abfragen ob RTC Clock bereits aktiviert wurde
if((RCC->BDCR & RCC_BDCR_RTCEN) == (uint32_t)RESET)
{
__HAL_RCC_RTC_ENABLE();
}
HAL_NVIC_SetPriority(RTC_WKUP_IRQn, 6, 0);
HAL_NVIC_EnableIRQ(RTC_WKUP_IRQn); 
}
/* Set RTC state */
hrtc->State = HAL_RTC_STATE_BUSY;
/* Disable the write protection for RTC registers */
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
/* Set Initialization mode */
if(RTC_EnterInitMode(hrtc) != HAL_OK)
{
/* Enable the write protection for RTC registers */
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
/* Set RTC state */
hrtc->State = HAL_RTC_STATE_ERROR;
return HAL_ERROR;
}
else
{
/* Clear RTC_CR FMT, OSEL and POL Bits */
hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
/* Set RTC_CR register */
hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
/* Configure the RTC PRER */
hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 
16
);
// Clear RSF Flag after Wakeup
hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_RSF);
/* Exit Initialization mode */
hrtc->Instance->ISR &= ((uint32_t)~RTC_ISR_INIT);
hrtc->Instance->OR &= (uint32_t)~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
hrtc->Instance->OR |= (uint32_t)(hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
/* Enable the write protection for RTC registers */
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
/* Set RTC state */
hrtc->State = HAL_RTC_STATE_READY;
return HAL_OK;
}
}
void MX_RTC_Wakeup_Init(void)
{
//Initialize RTC 
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Wakeup_Init(&hrtc) != HAL_OK)
{
Error_Handler();
} 
}

When i debug the code everything is woking fine. The registerer were set and reset as i expected and i could read the time and date. But when i exit the debugger, the time and date could not be read proberly. Could this be timing problem? #rtc-wakeup
3 REPLIES 3
Walid FTITI_O
Senior II
Posted on November 07, 2016 at 19:21

Hi Erics, 

After resuming from standby (check the SB flag), clear the SB flag . if it is resuming from standby you should neot reconfigure you RTC calendar. to not have after wrong values.

You may check the ''RTC_calender'' and ''Standby_RTC'' examples for more help in coding in STM32CubeL4 package.

-Hannibal-

christiantasche9
Associate II
Posted on November 08, 2016 at 08:40

Thanks Hannibal for your reply.

After Wakeup i check the Wakeup Flag from the RTC and dissable the Wakeup Timer:

if((READ_REG(RTC->ISR) & 0x00000400) == 0x00000400)
{
HAL_PWR_EnableBkUpAccess();
// Write Protection RTC disable 
WRITE_REG( RTC->WPR, 0xCA ); 
WRITE_REG( RTC->WPR, 0x53 ); 
// Deactivate WakeUpTimer RTC
tmpreg = (uint32_t)(RTC->CR & (uint32_t)~(RTC_CR_WUTE));
RTC->CR = (uint32_t)tmpreg; // Wakeup Timer disabled 
// Clear WUTF Flag
tmpreg = (uint32_t)(RTC->ISR & (uint32_t)~(RTC_ISR_WUTF));
RTC->ISR = (uint32_t)tmpreg; // Clear Flags 
//Write Protection enabled
WRITE_REG( RTC->WPR, 0xFF ); 
}

That works for me. I only make the posted Init because i cannont use the RTC HAL functions without it. I use the ''HAL_RTCEx_SetWakeUpTimer_IT()'' function before i go in the standby mode. After Wakeup i use the get date and time functions to compare times for timeouts. When i set a breakpoint in IAR to see if i get the right times, everything is fine and my timeouts where set an reset right. When i do not set any breakpoints, my timeouts where never reached. It seamed that the get date and time functions do not get the right time. It's like you only try to read the the time, without reading the date too. Every Example that i saw do not realy work with get time and date after a wakeup. The normale Wakup feature from the RTC is working. But i need a sequence that i can call, after wakup from standby that i can get a right time and date. Could anyone give me an example for that?
Walid FTITI_O
Senior II
Posted on November 08, 2016 at 12:19

Hi Erics,

Try to do like the following sequence ( same applied in AN4538 firmware) :

/* Reset of all peripherals, Initializes the Flash interface and the Systick */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize the RTC instance to work fo both cases*/
RTCHandle.Instance = RTC; 
/* Check and Clear the Wakeup flag */
if(__HAL_PWR_GET_FLAG(PWR_FLAG_WU) != RESET)
{
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
}
if (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) == RESET)
{
/* Clear standby flag*/
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB); 
/***** Entering in standby mode *****/
#ifdef DEBUG
/* Enable the Debug Module during STANDBY mode */
HAL_EnableDBGStandbyMode();
#endif 
/* Configure the RTC module */
RTC_Configuration();
/* Initialize the time and date */
RTC_SetCalendar();
/* Enter standby mode */
StandbyMode();
}
else
{
/* Read and save the current time */
......
}

-Hannibal-