cancel
Showing results for 
Search instead for 
Did you mean: 

STM32WL_RTC calendar won't update the time

Nikosant03
Associate II

Hi guys,

I am working with the LoRaWAN_End_Node example provided in STM32WL SDK. I want to set up the calendar and get the time and date. The example itself initializes the RTC in rtc.c. Then I set the time and date and I call getTime and getDate from the main.c

My problem is that the time does not update and the seconds are always 0 as shown in the image below.

However, when I create a fresh new project the calendar works fine. But when I work with the End_Node example it doesn't. I have also read AN5406 but I didn't find any related information.

Any help?

rtc.c file

/* USER CODE BEGIN 1 */
void setTime(void) {
	sTime.Hours = 16;
	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();
	}
}
 
void setDate(void) {
	sDate.WeekDay = RTC_WEEKDAY_SATURDAY;
	sDate.Month = RTC_MONTH_AUGUST;
	sDate.Date = 13;
	sDate.Year = 0;
	if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK) {
		Error_Handler();
	}
 
}
 
void getTime(void) {
	HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
 
}
 
void getDate(void) {
	HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
}
 
/* USER CODE END 1 */

main.c

while (1) {
		getTime();
		getDate();
		HAL_Delay(1000);
	}

 debugging

0693W00000QO8OIQA1.png

10 REPLIES 10
Nikosant03
Associate II

Any update on that?

Nikosant03
Associate II

An update here

I set a break point in HAL_Delay and I just realized that the RTC_TR register does not update at all after each tripping. Any advice?

0693W00000StkalQAB.png0693W00000StkavQAB.png 

Also the LoRaWAN_End_Node example enables the bypass shadow register. Should I reset the RSF bit of RTC_ICSR register?

0693W00000StkcNQAR.png

UTIL_TIMER_Status_t TIMER_IF_Init(void) // enters once
{
  UTIL_TIMER_Status_t ret = UTIL_TIMER_OK;
  /* USER CODE BEGIN TIMER_IF_Init_1 */
 
  /* USER CODE END TIMER_IF_Init_1 */
  if (RTC_Initialized == false)
  {
    hrtc.IsEnabled.RtcFeatures = UINT32_MAX;
    /*Init RTC*/
    MX_RTC_Init();
    /*Stop Timer */
    TIMER_IF_StopTimer();
    /** DeActivate the Alarm A enabled by MX during MX_RTC_Init() */
    HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A);
    /*overload RTC feature enable*/
    hrtc.IsEnabled.RtcFeatures = UINT32_MAX;
 
    /*Enable Direct Read of the calendar registers (not through Shadow) */
    HAL_RTCEx_EnableBypassShadow(&hrtc); // see the reference manual section 30.6.7 pg 915
    /*Initialise MSB ticks*/
    TIMER_IF_BkUp_Write_MSBticks(0);
 
    TIMER_IF_SetTimerContext();
 
    RTC_Initialized = true;
  }

Louis AUDOLY
ST Employee

Hello @Nikosant03​ ,

To read the calendar through the shadow registers after calendar initialization, calendar update or after wakeup from low power modes the software must first clear the RSF flag.

The software must then wait until it is set again before reading the calendar, which means that the calendar registers have been correctly copied into the RTC_TR and RTC_DR shadow registers.

The "HAL_RTC_WaitForSynchro()" function implements the above software sequence (RSF clear and RSF check)

I hope this help, let me know if this solve your issue.

Best regards

Louis

Nikosant03
Associate II

Thank you for your responce Louis,

I am a bit confused because the LoRaWAN_End_Node example bypasses the shadow register by calling the HAL_RTCEx_EnableBypassShadow API (line 202).

The description of that function says "When the Bypass Shadow is enabled the calendar value is taken directly from the Calendar counter"

That means that I won't read the calendar through the shadow registers right?

So, should I call the HAL_RTC_WaitForSynchro() after initialization?

Louis AUDOLY
ST Employee

You are right sorry,

You don't need to use this function, it's only used when shadow is enable.

The difference came from the End_Node project is not based on Systick but on TimeServer , unlike the RTC_Calendar example.

Anyway you can use the functions located in Utilities/stm32_timer.c which will basically do the same job as the functions from HAL to handle time and calendar.

Best Regards

Louis

Nikosant03
Associate II

Thank you Louis,

I do not get it how can I handle the time and calendar with the functions in Utilities/stm32_timer.c

I mean, these functions are just for setting timers and there is no API for timekeeping (hh:mm:ss) and calendar (dd/mm/yyyy)

Could you please elaborate more on that?

Thanks for your help

Nikosant03
Associate II

Hi Louis,

Any update on this?

We want to synchronize the real-time clock of the end device periodically with the network's clock through clock synchronization requests to the network server

GLee.12
Associate II

also curious about this as I am trying to add datalogging functionality if no gateway is in the vicinity. Ran into the same issue where the RTC_DateTypeDef would keep if I set a specific date but the RTC_TimeTypeDef refuses to save the values I set nor would it count.

A timestamp/date is needed to reference each reading/measurement that's taken when unable to connect to a gateway.

take a look at stm32_systime.c, I think Louis referred to the wrong file. Time is kept properly in the "tm" struct, SysTimeLocalTime returns the "tm" struct and changes correctly without the need to read back date as well.