cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L496 HAL_RTC_GetTime function - delayed value

AFahr.1
Associate III

Hi all,

I have a function that prints out on a prompt the current RTC Date and Time values from the RTC module... basicaly when it's called it goes like this:

RTC_TimeTypeDef rmi_time = {0};
RTC_DateTypeDef rmi_date = {0};
 
HAL_RTC_GetTime(&hrtc, &rmi_time, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &rmi_date, RTC_FORMAT_BIN);
service_msg_out.config.rtclock.year = rmi_date.Year;
service_msg_out.config.rtclock.month = rmi_date.Month;
service_msg_out.config.rtclock.day = rmi_date.Date;
service_msg_out.config.rtclock.weekday = rmi_date.WeekDay;
service_msg_out.config.rtclock.hour = rmi_time.Hours;
service_msg_out.config.rtclock.minute = rmi_time.Minutes;
service_msg_out.config.rtclock.second = rmi_time.Seconds;
service_msg_out.config.rtclock.has_subsecond = true;
service_msg_out.config.rtclock.subsecond = rmi_time.SubSeconds;

The RTC is using a 32.768 KHz oscillator, and that works from a HW perspective. The RTC init is almost the default from CubeMX (with a slight change):

void MX_RTC_Init(void)
{
  RTC_TimeTypeDef sTime = {0};
  RTC_TimeTypeDef alarm_Time = {0};
  RTC_DateTypeDef sDate = {0};
  RTC_AlarmTypeDef alrm_a = {0};
 
  /** 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;
 
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }
 
  /* USER CODE BEGIN Check_RTC_BKUP */
 
  if(0x0D15EA5E != RTC->BKP0R)
  {
	  init_rtc = true;
	  RTC->BKP0R = 0x0D15EA5E;
  }
 
  /* USER CODE BEGIN Set alarm event */
  alarm_Time.Hours = 22;
  alarm_Time.Minutes = 0;
  alarm_Time.Seconds = 0;
  alarm_Time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
 
  alrm_a.Alarm = RTC_ALARM_A;
  alrm_a.AlarmMask = RTC_ALRMAR_MSK4;
  alrm_a.AlarmTime = alarm_Time;
  alrm_a.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
 
  if (HAL_RTC_SetAlarm(&hrtc, &alrm_a, RTC_FORMAT_BIN) != HAL_OK)
  {
    Error_Handler();
  }
 
  __HAL_RTC_ALARMA_ENABLE(&hrtc);
  __HAL_RTC_ALARM_EXTI_ENABLE_EVENT();
 
 
  /** Initialize RTC and set the Time and Date
  */
  if(init_rtc)
  {
	  sTime.Hours = 0;
	  sTime.Minutes = 0;
	  sTime.Seconds = 0;
	  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
	  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
	  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
	  {
	    Error_Handler();
	  }
	  sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
	  sDate.Month = RTC_MONTH_JANUARY;
	  sDate.Date = 1;
	  sDate.Year = 0;
 
	  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
	  {
	    Error_Handler();
	  }
	  init_rtc = false;
  }
}

But my main question is the following:

  • When I read out the RTC register values, I'm pretty sure there is a small delay, basically, the value read out the first time is close to 00:00:00 and the second value is the correct initialised value. Eg: I read out time at 12:45:45, I get 12:45:30, and then the second time I get the actual 12:45:45.

Is there any explanation for that, am I reading or configuring the RTC wrong?

Cheers,

Alberto

2 REPLIES 2

Although the code you've presented does this, do you always read out date after time?

JW

AFahr.1
Associate III

Hi JW, yes, I always do that in all the places, however, thanks very much for that interesting link, I'm trying the double/triple readout and it seems to be working reliably now... but early days...

		equal = false;
		while(false == equal)
		{
			HAL_RTC_GetTime(&hrtc, &current_time, RTC_FORMAT_BIN);
			HAL_RTC_GetDate(&hrtc, &current_date, RTC_FORMAT_BIN);
 
			HAL_RTC_GetTime(&hrtc, &aux_time, RTC_FORMAT_BIN);
			HAL_RTC_GetDate(&hrtc, &aux_date, RTC_FORMAT_BIN);
 
			if(current_time.SubSeconds == aux_time.SubSeconds)
			{
				equal = true;
			}
		}

Ideally it's something I'd avoid.... but fortunately the time readout happens seldom only.

Cheers,

Alberto