2022-11-07 10:19 PM
Hi. I am using STM32L412KB for my application and internal RTC for maintaining time. I get epoch time over UART and then I call the following function to set the time.
void set_rtc_time(time_t epoch_time)
{
rtc_time = epoch_time;
struct tm *time_sec = localtime(&epoch_time);
RTC_TimeTypeDef time;
time.Hours = (uint8_t)time_sec->tm_hour;
time.Seconds = (uint8_t)time_sec->tm_sec;
time.Minutes = (uint8_t)time_sec->tm_min;
APP_ERROR_CHECK(HAL_RTC_SetTime(&hrtc, &time, RTC_FORMAT_BIN));
RTC_DateTypeDef date;
date.Date = (uint8_t)time_sec->tm_mday;
date.Month = (uint8_t)time_sec->tm_mon + 1;
date.Year = (uint8_t)time_sec->tm_year - 100;
// date.WeekDay = time_sec->tm_wday;
APP_ERROR_CHECK(HAL_RTC_SetDate(&hrtc, &date, RTC_FORMAT_BIN));
}
And after setting the time I respond back with the time set over UART. So, while creating the message to be transmitted over UART, I use following function to read the RTC and convert it to epoch time.
time_t get_rtc_epoch_time(void)
{
RTC_TimeTypeDef rtc_time;
RTC_DateTypeDef rtc_date;
struct tm current_rtc_time;
APP_ERROR_CHECK(HAL_RTC_GetTime(&hrtc, &rtc_time, RTC_FORMAT_BIN));
APP_ERROR_CHECK(HAL_RTC_GetDate(&hrtc, &rtc_date, RTC_FORMAT_BIN));
current_rtc_time.tm_hour = rtc_time.Hours;
current_rtc_time.tm_min = rtc_time.Minutes;
current_rtc_time.tm_sec = rtc_time.Seconds;
current_rtc_time.tm_mday = rtc_date.Date;
current_rtc_time.tm_mon = rtc_date.Month - 1;
current_rtc_time.tm_year = rtc_date.Year + 100;
current_rtc_time.tm_isdst = -1;
return (uint32_t)mktime(¤t_rtc_time);
}
But sometimes I get wrong time. It should be 0x63xxxxxx in hexadecimal if you convert epoch time but I get 0x78xxxxxx.
This is my RTC init function:
void rtc_init(void)
{
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;
APP_ERROR_CHECK (HAL_RTC_Init(&hrtc));
}
This is my MSP Init function:
void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
if(hrtc->Instance==RTC)
{
/** Initializes the peripherals clock
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
APP_ERROR_CHECK (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit));
/* Peripheral clock enable */
__HAL_RCC_RTC_ENABLE();
}
}
I am using LSE as the clock source. What could be the issue here?
NOTE: Here is APP_ERROR_CHECK define:
#define APP_ERROR_CHECK(ERR_CODE) \
do \
{ \
const uint32_t LOCAL_ERR_CODE = (ERR_CODE); \
if (LOCAL_ERR_CODE != HAL_OK) \
{ \
Error_Handler(LOCAL_ERR_CODE, __FILE__, __LINE__); \
} \
} while (0)
2022-11-08 12:19 AM
When setting time/date, make sure all fields of structs used are cleared or explicitly set.
http://www.efton.sk/STM32/gotcha/g113.html
JW