cancel
Showing results for 
Search instead for 
Did you mean: 

Incomplete RTC initialization?

PKajd.1
Associate

Hey there,

bringing my custom STM32WB board up, I encountered a problem with the RTC initialization. I recently updated CubeMX to 5.5.0 and I don't know, if my problem comes with the update, but with the older versions I didn't have these type of errors. Anyways, I set up the RTC, which at first only worked after a power down reset. Resetting per reset pin brought me trouble with the RTC_EnterInitMode() function. There was only a Timeout error. At first I thought of a hardware problem, but after reconsidering the example brought by the firmware, I saw major differences in the stm32wbxx_hal_msp.c file. The CubeMX generated file lacked the following code

/* USER CODE BEGIN PV */
static uint32_t RtcClockSource
#ifdef RTC_CLOCK_SOURCE_LSE
  = RCC_RTCCLKSOURCE_LSE;
#elif defined (RTC_CLOCK_SOURCE_LSI)
  = RCC_RTCCLKSOURCE_LSI;
#endif
/* USER CODE END PV */
 
.
.
.
 
  /* USER CODE BEGIN RTC_MspInit 0 */
    RCC_OscInitTypeDef        RCC_OscInitStruct = {0};
    RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct = {0};
 
    /* Enables the PWR Clock and Enables access to the backup domain */
    /* To change the source clock of the RTC feature (LSE, LSI), You have to:
       - Enable the power clock using __HAL_RCC_PWR_CLK_ENABLE()
       - Enable write access using HAL_PWR_EnableBkUpAccess() function before to
         configure the RTC clock source (to be done once after reset).
       - Reset the Back up Domain using __HAL_RCC_BACKUPRESET_FORCE() and
         __HAL_RCC_BACKUPRESET_RELEASE().
       - Configure the needed RTC clock source */
    HAL_PWR_EnableBkUpAccess();
    /*##-2- Get RTC clock configuration #########################################*/
    HAL_RCCEx_GetPeriphCLKConfig(&PeriphClkInitStruct);
	
    /*In case of RTC clock already enable, make sur it's the good one */
    if (PeriphClkInitStruct.RTCClockSelection == RtcClockSource)
    {
      /* Do nothing */
    }
    else
    {
      PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
 
      /* If selected source was previously the opposite source clock, first select none*/
      if (PeriphClkInitStruct.RTCClockSelection != RCC_RTCCLKSOURCE_NONE)
      {
        PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_NONE;
        if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
        {
          Error_Handler();
        }
      }
      /* Configure LSE/LSI as RTC clock source */
#ifdef RTC_CLOCK_SOURCE_LSE
      RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1 | RCC_OSCILLATORTYPE_LSE;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
      RCC_OscInitStruct.LSEState = RCC_LSE_ON;
      RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
#elif defined (RTC_CLOCK_SOURCE_LSI)
      RCC_OscInitStruct.OscillatorType =  RCC_OSCILLATORTYPE_LSI1 | RCC_OSCILLATORTYPE_LSE;
      RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
      RCC_OscInitStruct.LSIState = RCC_LSI_ON;
      RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
#else
#error Please select the RTC Clock source inside the main.h file
#endif /*RTC_CLOCK_SOURCE_LSE*/
      if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
      {
        Error_Handler();
      }
 
      PeriphClkInitStruct.RTCClockSelection = RtcClockSource;
      if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
      {
        Error_Handler();
      }
    }
    /* Enable RTC peripheral Clocks */
 
    /* Enable RTC APB clock  */
    __HAL_RCC_RTCAPB_CLK_ENABLE();
  /* USER CODE END RTC_MspInit 0 */

After inserting it in my project, it worked. Did I forgot something some configuration in CubeMX or is it a bug?

Thanks in advance 🙂

0 REPLIES 0