Skip to main content
Visitor II
July 1, 2024
Solved

STM32G030 Can't setup RTC, HAL_RTC_Init always return HAL_TIMEOUT

  • July 1, 2024
  • 6 replies
  • 2095 views

Hi, i'm trying to set up the RTC timer to be able to use the alarm and HAL_RTC_Init always return HAL_TIMEOUT. It seem like RTC_ICSR_INITF never go to 1.

 

 

uint8_t init_RTC()
{
	 /* USER CODE BEGIN RTC_Init 0 */

	 /* USER CODE END RTC_Init 0 */

	 RTC_TimeTypeDef sTime = {0};
	 RTC_DateTypeDef sDate = {0};

	 /* USER CODE BEGIN RTC_Init 1 */
	 HAL_PWR_EnableBkUpAccess();

	 __HAL_RCC_LSI_ENABLE();
	 __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSI);

	 __HAL_RCC_RTC_ENABLE();
	 while((RCC->CSR & 0x2) == 0);//wait till the clock is ready
	 /* USER CODE END RTC_Init 1 */


	 /** Initialize RTC Only
	 */
	 hrtc.Instance = RTC;
	 hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
	 hrtc.Init.AsynchPrediv = 127;
	 hrtc.Init.SynchPrediv = 255;
	 hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
	 hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
	 hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
	 hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
	 hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;

	 if(HAL_RTC_Init(&hrtc) != HAL_OK)
	 {
	 return 1;
	 }

	 /** Initialize RTC and set the Time and Date
	 */
	 sTime.Hours = 0x0;
	 sTime.Minutes = 0x0;
	 sTime.Seconds = 0x0;
	 sTime.SubSeconds = 0x0;
	 sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
	 sTime.StoreOperation = RTC_STOREOPERATION_RESET;
	 if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
	 {
		 return 1;
	 }
	 sDate.WeekDay = RTC_WEEKDAY_MONDAY;
	 sDate.Month = RTC_MONTH_JANUARY;
	 sDate.Date = 0x1;
	 sDate.Year = 0x0;

	 if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
	 {
		 return 1;
	 }
	 return 0;

}

 

 

    This topic has been closed for replies.
    Best answer by waclawek.jan

    These appear to be OK (i.e. LSI selected as RTC clock and LSI indicated as enabled and running).

    Is RCC_APBENR1.RTCAPBEN set?

    Isn't some tamper source set in RTC?

    Try performing backup-domain reset before setting up RTC.

    JW

    6 replies

    Super User
    July 2, 2024

    Your RTC clock is not running.

    Check in respective RCC registers.

    JW

    fgroleauAuthor
    Visitor II
    July 2, 2024

    Hi,

    Thanks for your reply

     

    It's weird because when i try with to see if the clock was running using this function HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_RTC)

    The function return that the clock is running at 32kHz wich seem to be the LSI frequency wich is the one i want but the HAL_RTC_Init(&hrtc) function was still returning HAL_TIMEOUT

     

     

    Super User
    July 2, 2024

    OK so what's the content of relevant RCC registers (BDCR, CSR, CR)?

    JW

    fgroleauAuthor
    Visitor II
    July 3, 2024

    At line 33 the value are :

    RCC->BDCR = 0x8200

    RCC->CSR = 0x1c000003

    RCC->CR = 0x3000500

    Super User
    July 4, 2024

    These appear to be OK (i.e. LSI selected as RTC clock and LSI indicated as enabled and running).

    Is RCC_APBENR1.RTCAPBEN set?

    Isn't some tamper source set in RTC?

    Try performing backup-domain reset before setting up RTC.

    JW

    fgroleauAuthor
    Visitor II
    July 4, 2024

    Thanks, it seem that setting RCC_APBENR1.RTCAPBEN seem to have fix my issue!