Skip to main content
Visitor II
October 9, 2025
Solved

RTC; code code get stuck in systemclockconfig()

  • October 9, 2025
  • 5 replies
  • 1087 views

we are observing with the RTC (Real Time Clock) on the STM32L496ZG in our project. kindly help us to resolve this . The code gets stuck on power-on . Below are the details and what we have tried so far:

1. Summary of the Issue

  • After a full  powering on, while debugging the code code get stuck in systemclockconfig()  function. 

  • The issue occurs only on power-on with some hardware only and with same version of hardware its working correctly. 

2. Hardware / System Details

  • MCU: STM32L496ZG

  • RTC clock source: (external crystal of 32.768KHz oscillator with 10pF capacitors used)

  • On VBAT pin its 3.2v  battery cell is used

we uses below function for initialization :

 
 
void SystemClock_Config(void)
{
#if 1 //og
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}

/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_MSI|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
//RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

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

void MX_RTC_Init(void)
{
/* USER CODE BEGIN RTC_Init 0 */
/* USER CODE END RTC_Init 0 */

/* USER CODE BEGIN RTC_Init 1 */
__HAL_RCC_RTC_ENABLE();
/* USER CODE END RTC_Init 1 */

HAL_PWR_EnableBkUpAccess(); // Unlock backup domain

__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); // Set high drive strength

__HAL_RCC_LSE_CONFIG(RCC_LSE_ON); // Enable the LSE oscillator

while (__HAL_RCC_GET_FLAG(RCC_FLAG_LSERDY) == RESET);// { } // Wait for readiness

/** Initialize RTC Only
*/
RtcHandle.Instance = RTC;
RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24;
RtcHandle.Init.AsynchPrediv = 127;
RtcHandle.Init.SynchPrediv = 255;
RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
RtcHandle.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&RtcHandle) != HAL_OK)
{
#if DEBUG_ERROR
uart_length = sprintf(uart_tx_buffer,"\r\nE4");
HAL_UART_Transmit(&huart4,uart_tx_buffer,(uint16_t)uart_length,1000);
#endif
Error_Handler();
}

/* USER CODE BEGIN RTC_Init 2 */
/* USER CODE END RTC_Init 2 */
}

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
// if(hrtc->Instance==RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */

/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;//RCC_RTCCLKSOURCE_LSI;//
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}

/* Peripheral clock enable */
__HAL_RCC_RTC_ENABLE();

/* USER CODE BEGIN RTC_MspInit 1 */
/* USER CODE END RTC_MspInit 1 */
}
}

 


Edited to apply source code formatting - please see How to insert source code for future reference.

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

    @sushmagawde95 wrote:

    32Khz Crystal oscillator is compatible with 10pf capacitor ?


    We already gave you all the resources to find this out.
    Let me help you a bit more:

     

    1. read: https://community.st.com/t5/stm32-mcus/how-to-select-a-compatible-crystal-and-load-capacitors-for-stm32/ta-p/780236
    2. read: AN2867
    3. look up your crystal datasheet
    4. a 32k crystal is connected to LSE
    5. STM32L494ZG is L4 family
    6. Check Table 7 for recommended LSE crystals for L4 family.
    7. check if your crystal is recommended or at least compatible
    8. calculate capacitor values
    9. Check Table 5 for max GM for each drive strength setting for L4 family and select the right drive strength for LSE. Too low drive strength means it won't work reliably, too high drive strength uses more current.

     

    5 replies

    Technical Moderator
    October 9, 2025

    Hello,

    Question: why you commented out this line?

    //RCC_OscInitStruct.LSEState = RCC_LSE_ON;
    Technical Moderator
    October 9, 2025

    Hello @sushmagawde95 


    @sushmagawde95 wrote:
    • The issue occurs only on power-on with some hardware only and with same version of hardware its


    Could you give a more detailed explanation, please?

    Visitor II
    October 10, 2025

    we tried our custom boards of STM32L494ZG .Some PCBs didn't boot, some never booted , others worked sometimes.

    Technical Moderator
    October 10, 2025

    Hello,


    @sushmagawde95 wrote:

    we tried our custom boards of STM32L494ZG .Some PCBs didn't boot, some never booted , others worked sometimes.


    This is not a software issue but more related to your hardware and more specifically to your LSE crystal part.

    So as @unsigned_char_array and I mentioned above, you need to check the compatibility of your crystal by computing the gain margin based on the drive level you selected, the crystal proprieties and the load capacitor values.

    Mostly, the CL value you are using exceeds the values needed.

    So again: need to read this article: How to select a compatible crystal and load capacitors for STM32 with layout guidelines

    For more details: AN2867 "Guidelines for oscillator design on STM8AF/AL/S and STM32 MCUs/MPUs "

    Super User
    October 9, 2025

    Well, you have LSE turned off and then you try to use it for the RTC. That's not going to work.

    Visitor II
    October 10, 2025

    if i uncomment the line its not working :

    RCC_OscInitStruct.LSEState = RCC_LSE_ON;

     

    Technical Moderator
    October 10, 2025

    What board are you using? NUCLEO-L496? if yes, are R39 and R40 soldred?

    If it's a custom board, did you check that the crystal you have used for LSE is compatible with the MCU versus the drive level selected? 

    Read: How to select a compatible crystal and load capacitors for STM32 with layout guidelines

    For more details: AN2867 "Guidelines for oscillator design on STM8AF/AL/S and STM32 MCUs/MPUs "

    Visitor II
    October 10, 2025

    we tried our custom boards of STM32L494ZG .Some PCBs didn't boot, some never booted , others worked sometimes. 32Khz Crystal oscillator is compatible with 10pf capacitor ?

    Graduate II
    October 10, 2025
    __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); // Set high drive strength

     

     

    Is this high or low drive strength?
    It's important to select the correct drive strength.

    "STM32 devices have historically been very intolerant of 9 and 12pF crystals (slow to start, don't start)You will get far fewer headaches using 6-7pF crystals."
    https://community.st.com/t5/stm32-mcus-products/lse-oscillator-on-stm32f769/m-p/470259/highlight/true#M159494

    I've had the same issue. Some PCBs didn't boot, some never booted or took longer than 5 seconds (causing a timeout), others worked sometimes.
    Issue turned out to be the load of the crystal. A lower capacitance crystal was needed and the appropriate drive strength needed to be selected. If the crystal never oscillates the PLL will never lock and the MCU will never complete clock initialization routine.

    You can look up the supported capacitance for each STM MCU family in datasheets/application notes.

    https://www.st.com/content/ccc/resource/technical/document/application_note/c6/eb/5e/11/e3/69/43/eb/CD00221665.pdf/files/CD00221665.pdf/jcr:content/translations/en.CD00221665.pdf

    https://community.st.com/t5/stm32-mcus/how-to-select-a-compatible-crystal-and-load-capacitors-for-stm32/ta-p/780236

    When choosing a cap you need to take IO-capacitance and PCB trace stray capacitance into account too.

    Visitor II
    October 10, 2025
    __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); 

    we are using above function for configuration of LSE crystal drive.

    Graduate II
    October 10, 2025

    @sushmagawde95 wrote:
    __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); 

    we are using above function for configuration of LSE crystal drive.


    So the comment "Set high drive strength" is false.