cancel
Showing results for 
Search instead for 
Did you mean: 

I am using STM32L0 and I want to run RTC in my custom board. I using Internal LSI clock for RTC. When I set Alarm for 60 sec it is generated in around 54 sec so i think RTC is running faster . So how can i calibrate it?

SPraj.1
Associate II

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 /** Configure the main internal regulator output voltage

 */

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 /** Initializes the RCC Oscillators according to the specified parameters

 * in the RCC_OscInitTypeDef structure.

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI

               |RCC_OSCILLATORTYPE_HSE;

 RCC_OscInitStruct.HSEState = RCC_HSE_ON;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

 RCC_OscInitStruct.LSIState = RCC_LSI_ON;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_3;

 RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_3;

 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_HSE;

 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_1) != HAL_OK)

 {

  Error_Handler();

 }

 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_I2C1

               |RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USB;

 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;

 PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;

 PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;

 PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL;

 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

 {

  Error_Handler();

 }

}

Ams this ias code of RTC.c

0693W00000BckU2QAJ.png

3 REPLIES 3

LSI is a very imprecise clock source, see datasheet. You can "calibrate" it by changing the asynchronous and synchronous pedalers, but it will never be stable and precise.

JW

SPraj.1
Associate II

so what is the best option to go with?

Best option for what?

To maintain real time, you need to use LSE and a 32.768kHz crystal.

If you have a crystal on HSE and you don't want to use some of the low-power modes (in some of which LSI doesn't work either, btw.), you can derive the RTC clock from HSE too.

JW