cancel
Showing results for 
Search instead for 
Did you mean: 

RTC; code code get stuck in systemclockconfig()

sushmagawde95
Associate

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.

12 REPLIES 12
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); 

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


@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.

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

@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.

 

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.