Skip to main content
Visitor II
September 11, 2025
Solved

RTC: RTC_EnterInitMode returns HAL_TIMEOUT

  • September 11, 2025
  • 3 replies
  • 673 views

Hi,

I'm using a SMT32H562ZGT6 uC with RTC pheripheral enabled and clock source from LSE. But HAL_RTC_Init() function returns HAL_TIMEOUT. Investigating I found the method RTC_EnterInitMode returns HAL_TIMEOUT.

Clock configuration is performed before calling the RTC initiazlization using The SystemClock_Config generated by cubeMX:

void BSP::SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage
 */
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);

 while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {
 }

 /** Configure LSE Drive Capability
 * Warning : Only applied when the LSE is disabled.
 */
 HAL_PWR_EnableBkUpAccess();
 __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType =
 RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_LSI |
 RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
 RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLL1_SOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLM = 2;
 RCC_OscInitStruct.PLL.PLLN = 40;
 RCC_OscInitStruct.PLL.PLLP = 2;
 RCC_OscInitStruct.PLL.PLLQ = 2;
 RCC_OscInitStruct.PLL.PLLR = 2;
 RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1_VCIRANGE_3;
 RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1_VCORANGE_WIDE;
 RCC_OscInitStruct.PLL.PLLFRACN = 0;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
 RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 |
 RCC_CLOCKTYPE_PCLK3;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) {
 Error_Handler();
 }

 /** Configure the programming delay
 */
 __HAL_FLASH_SET_PROGRAM_DELAY(FLASH_PROGRAMMING_DELAY_2);
}

After that I'm initializing the RTC as follows:

bool RTC_C::initialize()
{
 _RTCHandler.Instance = RTC;
 _RTCHandler.Init.HourFormat = RTC_HOURFORMAT_24;
 _RTCHandler.Init.AsynchPrediv = 127;
 _RTCHandler.Init.SynchPrediv = 255;
 _RTCHandler.Init.OutPut = RTC_OUTPUT_DISABLE;
 _RTCHandler.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
 _RTCHandler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
 _RTCHandler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 _RTCHandler.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
 _RTCHandler.Init.BinMode = RTC_BINARY_NONE;
 if (HAL_RTC_Init(&_RTCHandler) != HAL_OK) {
 return false;
 }

 RTC_PrivilegeStateTypeDef privilegeState = {0};
 privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO;
 privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE;
 privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0;
 privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0;
 if (HAL_RTCEx_PrivilegeModeSet(&_RTCHandler, &privilegeState) != HAL_OK) {
 return false;
 }

 if (HAL_OK != HAL_RTC_WaitForSynchro(&_RTCHandler)) {
 return false;
 }

 return true;
}

But initialization fails.

Can someone give me some hints? or have alredy experienced a similar issue?
Thank you in advance for the support.

 

    This topic has been closed for replies.
    Best answer by MattiaB

    This is the value of RCC_BDCR:
    MattiaB_0-1757582577352.png
    I've not used ST examples yet, but seems I've found something...
    When HAL_RTC_Init() is called the following check is performed:

     

    if (hrtc->State == HAL_RTC_STATE_RESET)
    {
     /* Allocate lock resource and initialize it */
     hrtc->Lock = HAL_UNLOCKED;
    
     /* Initialize RTC MSP */
     HAL_RTC_MspInit(hrtc);
    }

     

    But initially the state is not  HAL_RTC_STATE_RESET and HAL_RTC_MspInit() is not performed.
    Calling HAL_RTC_MspInit() before the HAL_RTC_Init() the RTC_EnterInitMode() function doesn't return HAL_TIMEOUT anymore:

    bool RTC_C::initialize()
    {
     _RTCHandler.Instance = RTC;
     _RTCHandler.Init.HourFormat = RTC_HOURFORMAT_24;
     _RTCHandler.Init.AsynchPrediv = 127;
     _RTCHandler.Init.SynchPrediv = 255;
     _RTCHandler.Init.OutPut = RTC_OUTPUT_DISABLE;
     _RTCHandler.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
     _RTCHandler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
     _RTCHandler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
     _RTCHandler.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
     _RTCHandler.Init.BinMode = RTC_BINARY_NONE;
     
     HAL_RTC_MspInit(&_RTCHandler);
     
     if (HAL_RTC_Init(&_RTCHandler) != HAL_OK) {
     return false;
     }
    
     RTC_PrivilegeStateTypeDef privilegeState = {0};
     privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO;
     privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE;
     privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0;
     privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0;
     if (HAL_RTCEx_PrivilegeModeSet(&_RTCHandler, &privilegeState) != HAL_OK) {
     return false;
     }
    
     if (HAL_OK != HAL_RTC_WaitForSynchro(&_RTCHandler)) {
     return false;
     }
    
     return true;
    }

    I don't know if this can be considered a fix or a workaround.

     

    3 replies

    ST Employee
    September 11, 2025

    Hello,

    Can you check the status of the RCC_BDCR register when this issue happens ?

    Also, have you tried any of the STM32CubeMX RTC examples ? it could be useful to test if these basic examples work to eliminate the possibility that you have a hardware problem.

    MattiaBAuthorAnswer
    Visitor II
    September 11, 2025

    This is the value of RCC_BDCR:
    MattiaB_0-1757582577352.png
    I've not used ST examples yet, but seems I've found something...
    When HAL_RTC_Init() is called the following check is performed:

     

    if (hrtc->State == HAL_RTC_STATE_RESET)
    {
     /* Allocate lock resource and initialize it */
     hrtc->Lock = HAL_UNLOCKED;
    
     /* Initialize RTC MSP */
     HAL_RTC_MspInit(hrtc);
    }

     

    But initially the state is not  HAL_RTC_STATE_RESET and HAL_RTC_MspInit() is not performed.
    Calling HAL_RTC_MspInit() before the HAL_RTC_Init() the RTC_EnterInitMode() function doesn't return HAL_TIMEOUT anymore:

    bool RTC_C::initialize()
    {
     _RTCHandler.Instance = RTC;
     _RTCHandler.Init.HourFormat = RTC_HOURFORMAT_24;
     _RTCHandler.Init.AsynchPrediv = 127;
     _RTCHandler.Init.SynchPrediv = 255;
     _RTCHandler.Init.OutPut = RTC_OUTPUT_DISABLE;
     _RTCHandler.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
     _RTCHandler.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
     _RTCHandler.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
     _RTCHandler.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;
     _RTCHandler.Init.BinMode = RTC_BINARY_NONE;
     
     HAL_RTC_MspInit(&_RTCHandler);
     
     if (HAL_RTC_Init(&_RTCHandler) != HAL_OK) {
     return false;
     }
    
     RTC_PrivilegeStateTypeDef privilegeState = {0};
     privilegeState.rtcPrivilegeFull = RTC_PRIVILEGE_FULL_NO;
     privilegeState.backupRegisterPrivZone = RTC_PRIVILEGE_BKUP_ZONE_NONE;
     privilegeState.backupRegisterStartZone2 = RTC_BKP_DR0;
     privilegeState.backupRegisterStartZone3 = RTC_BKP_DR0;
     if (HAL_RTCEx_PrivilegeModeSet(&_RTCHandler, &privilegeState) != HAL_OK) {
     return false;
     }
    
     if (HAL_OK != HAL_RTC_WaitForSynchro(&_RTCHandler)) {
     return false;
     }
    
     return true;
    }

    I don't know if this can be considered a fix or a workaround.

     

    Technical Moderator
    September 11, 2025

    Hello @MattiaB 

    What is the handle state before executing HAL_RTC_Init(). Is the first init of the RTC peripheral?  

    ST Employee
    September 11, 2025

    Hello @MattiaB,

    Could you please test the RTC examples provided in the STM32CubeH5: Link(here)

    Additionally, I recommend checking out this article: Getting started with RTC

    I hope my answer has helped you. When your question is answered, please select this topic as the solution that answered you, as it will help others find that answer faster.

    Thanks for your contribution.

    Dor_RH

    MattiaBAuthor
    Visitor II
    September 11, 2025

    .