Skip to main content
Explorer
January 16, 2025
Question

STM32F205RG RTC Problem.

  • January 16, 2025
  • 1 reply
  • 597 views

Greetings,

I have connected external crystal of 32.768 KHz to STM32F205RG for RTC time keeping purpose. I have also enabled RTC output on pin PC13. Below is my code for RTC Initialization ...

int main(void)
{
 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_RTC_Init();
			
 if (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x32F2)
 {
 /* Configure RTC Calendar */
 RTC_CalendarConfig();
 }
 else
 {
 /* Check if the Power On Reset flag is set */
 if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
 {
 }
 /* Check if Pin Reset flag is set */
 if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
 {
 }
 /* Clear source Reset Flag */
 __HAL_RCC_CLEAR_RESET_FLAGS();
 }

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
		RTC_CalendarShow(aShowTime, aShowDate);
		if(_set)
		{
			RTC_CalendarConfig();
			_set = 0;
		}
 }
 /* USER CODE END 3 */
}

This is the main code and code for RTC_CalendarShow and RTC_CalendarConfig is as below

static void RTC_CalendarConfig(void)
{ 
 if(HAL_RTC_SetDate(&hrtc,&sDate,RTC_FORMAT_BCD) != HAL_OK)
 {
 /* Initialization Error */
 Error_Handler();
 }
	
 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
 {
 /* Initialization Error */
 Error_Handler();
 }

 /*##-3- Writes a data in a RTC Backup data Register1 #######################*/
 HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2);
}
static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
{
 RTC_DateTypeDef sdatestructureget;
 RTC_TimeTypeDef stimestructureget;
	
 /* Get the RTC current Time */
 HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
 /* Get the RTC current Date */
 HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
 /* Display time Format : hh:mm:ss */
 sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
 /* Display date Format : mm-dd-yy */
 sprintf((char *)showdate, "%2d-%2d-%2d", sdatestructureget.Date, sdatestructureget.Month, 2000 + sdatestructureget.Year);
}

The problem is after like 4 hours of run rtc is ahead approx 2 seconds or more. How to solve this problem ? 

The calibration output on pin PC13 gives me perfect 512 Hz wave as i have verified on oscilloscope. The same code is running prefectly fine on NUCLEO-F207ZG board.

    This topic has been closed for replies.

    1 reply

    Graduate II
    January 16, 2025

    Sorry didn't see any initialization code, need to have different prescalers for LSI vs LSE.

    Need to use 6-7pF crystals on the F2

    Explorer
    January 17, 2025

    Greetings Tesla,

    I have used cube for code initialization. I am attaching the screen shots of the cube for better understanding.Screen 1.pngScreen 2.pngScreen 3.png