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.
Solved! Go to Solution.
2025-10-10 5:04 AM
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
we are using above function for configuration of LSE crystal drive.
2025-10-10 5:29 AM
@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.
2025-10-10 5:42 AM - edited 2025-10-10 5:43 AM
@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:
2025-11-28 4:38 AM
If the external 32.768 kHz LSE crystal is not ‘not shorted/terminated’ to each other , can that cause the code to get stuck in system clock configuration or RTC initialization — i.e. the MCU never completes startup? What is the recommended behavior of the hardware in that case?”
2025-11-28 6:09 AM
If the crystal isn't installed correctly, the code will wait forever for the LSE to start.
2025-11-28 6:51 AM
@sushmagawde95 wrote:If the external 32.768 kHz LSE crystal is not ‘not shorted/terminated’ to each other , can that cause the code to get stuck in system clock configuration or RTC initialization — i.e. the MCU never completes startup? What is the recommended behavior of the hardware in that case?”
Yes. It never finishes. Or worse. It sometimes works. We've have some boards never finish initialization, others sometimes working or sometimes taking a longer time, and others having no issue (so far, under lab conditions). We ended up selecting a proper crystal and correct driver strength and there no longer was an issue.
Recommendation? Always use a watchdog timer. Then at least the board can retry. Perhaps you can use a fallback option and give an error indication to the user.