AnsweredAssumed Answered

STM32L475 - Problem Reading RTC Time and Date after Wakeup

Question asked by Tasche.Christian.001 on Nov 7, 2016
Latest reply on Nov 8, 2016 by FTITI.Walid
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?

Outcomes