2024-07-01 01:56 PM - edited 2024-07-01 01:56 PM
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;
}
Solved! Go to Solution.
2024-07-04 03:19 AM
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
2024-07-01 11:17 PM - edited 2024-07-01 11:19 PM
Your RTC clock is not running.
Check in respective RCC registers.
JW
2024-07-02 08:05 AM
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
2024-07-02 10:14 AM
OK so what's the content of relevant RCC registers (BDCR, CSR, CR)?
JW
2024-07-02 07:08 PM
At line 33 the value are :
RCC->BDCR = 0x8200
RCC->CSR = 0x1c000003
RCC->CR = 0x3000500
2024-07-04 03:19 AM
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
2024-07-04 08:48 AM
Thanks, it seem that setting RCC_APBENR1.RTCAPBEN seem to have fix my issue!