2025-10-08 10:24 PM - last edited on 2025-10-09 12:14 AM by Andrew Neil
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:
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.
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.
2025-10-09 1:35 AM
Hello,
Question: why you commented out this line?
//RCC_OscInitStruct.LSEState = RCC_LSE_ON;
2025-10-09 2:13 AM
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?
2025-10-09 6:08 AM
Well, you have LSE turned off and then you try to use it for the RTC. That's not going to work.
2025-10-09 10:43 PM
if i uncomment the line its not working :
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
2025-10-10 1:09 AM - edited 2025-10-10 2:15 AM
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 "
2025-10-10 2:02 AM - edited 2025-10-10 2:16 AM
__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.
When choosing a cap you need to take IO-capacitance and PCB trace stray capacitance into account too.
2025-10-10 4:53 AM
we tried our custom boards of STM32L494ZG .Some PCBs didn't boot, some never booted , others worked sometimes.
2025-10-10 5:03 AM
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 "
2025-10-10 5:03 AM
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 ?