Skip to main content
Mr._Nileshkumar_Solanki
Associate II
January 9, 2024
Solved

STM32L0 LoRaWAN -> Whenever I set date and time to internal RTC, my system will crash.

  • January 9, 2024
  • 2 replies
  • 1415 views

Hello,

I'm currently working with an STM32 microcontroller, and I'm facing a challenge related to the internal Real-Time Clock (RTC). Specifically, whenever I attempt to set the date and time using the internal RTC, my system crashes. I've reviewed the code and checked for potential issues, but I haven't been able to identify the root cause. Has anyone in the STM32 community encountered a similar problem when configuring the internal RTC? If so, could you share any insights, potential reasons for this behavior, or troubleshooting steps that might help me resolve this issue? Your assistance would be greatly appreciated as I work to address this problem in my project. 

    

Here my Set date and time function,

"

/**

* @fn ErrorStatus RTC_SetTimeAndDate(void)

* @brief Function to use Set date and time into Internal RTC

*

* @return SUCCESS / ERROR

*/

ErrorStatus RTC_SetTimeAndDate(void)

{

    RTC_TimeTypeDef sTime;

    RTC_DateTypeDef sDate;

 

  if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) != 0x32F2)

  {

    /** Set the Time and Date **/

    sTime.Hours = AppControl.mRTC.mTime.mHours; // BUILD_HOUR;

    sTime.Minutes = AppControl.mRTC.mTime.mMinutes; // BUILD_MIN;

    sTime.Seconds = AppControl.mRTC.mTime.mSeconds; // BUILD_SEC;

    sTime.SubSeconds = 0; // (AppControl.mRTC.mTime.m100thSeconds * 10); // BUILD_SEC;

    sTime.TimeFormat = RTC_HOURFORMAT_24;

    sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

    sTime.StoreOperation = RTC_STOREOPERATION_RESET;

    if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)

    {

      return ERROR;

    }

 

    sDate.WeekDay = AppControl.mRTC.mDate.mWeekDays; // RTC_GetCurrentDayOfWeek(BUILD_DAY, BUILD_MONTH, BUILD_YEAR);

    sDate.Month = AppControl.mRTC.mDate.mMonths; // BUILD_MONTH;

    sDate.Date = AppControl.mRTC.mDate.mDays; // BUILD_DAY;

    sDate.Year = (AppControl.mRTC.mDate.mYears - 2000); // (BUILD_YEAR - 2000);

    if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)

    {

      return ERROR;

    }

}

    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x32F2); // backup register

 

    return SUCCESS;

}

"

 

Thank you in advance.

 

Best answer by STTwo-32

Using internal RTC for setting date and time is not possible. If you want to create a calendar, you should use a SW solution.

Best Regards.

STTwo-32 

2 replies

STTwo-32
ST Technical Moderator
February 2, 2024

Hello @Mr._Nileshkumar_Solanki 

The internal RTC on the LoRaWAN context is used only for LoRaWAN and it can't be used for date and time.

Best Regards.

STTwo-32

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Mr._Nileshkumar_Solanki
Associate II
February 3, 2024

Thank you @STTwo-32 for your reply,

What if I want to use internal RTC in my application.
Any another way for set date and time.

STTwo-32
STTwo-32Best answer
ST Technical Moderator
February 3, 2024

Using internal RTC for setting date and time is not possible. If you want to create a calendar, you should use a SW solution.

Best Regards.

STTwo-32 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.