cancel
Showing results for 
Search instead for 
Did you mean: 

RTC; code code get stuck in systemclockconfig()

sushmagawde95
Visitor

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.

3 REPLIES 3
mƎALLEm
ST Employee

Hello,

Question: why you commented out this line?

//RCC_OscInitStruct.LSEState = RCC_LSE_ON;
To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om
ST Employee

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?

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Saket_Om
TDK
Super User

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

If you feel a post has answered your question, please click "Accept as Solution".