Skip to content

Commit 7568ec9

Browse files
RCSNRbb666
authored andcommitted
bsp: stm32: drv_rtc: add local time conversion for get timeval and set stamp
- include the year, month, and day for rtc_alarm_time_set API - add local time conversion for get timeval and set stamp Signed-off-by: Runcheng Lu <[email protected]>
1 parent 72c4504 commit 7568ec9

File tree

1 file changed

+14
-2
lines changed
  • bsp/stm32/libraries/HAL_Drivers/drivers

1 file changed

+14
-2
lines changed

bsp/stm32/libraries/HAL_Drivers/drivers/drv_rtc.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
1010
* 2021-02-05 Meco Man fix the problem of mixing local time and UTC time
1111
* 2021-07-05 iysheng implement RTC framework V2.0
12+
* 2025-06-05 RCSN add local time conversion for get timeval and set stamp
1213
*/
1314

1415
#include "board.h"
@@ -71,8 +72,11 @@ static rt_err_t stm32_rtc_get_timeval(struct timeval *tv)
7172
tm_new.tm_mon = RTC_DateStruct.Month - 1;
7273
tm_new.tm_year = RTC_DateStruct.Year + 100;
7374

75+
#ifdef RT_ALARM_USING_LOCAL_TIME
76+
tv->tv_sec = mktime(&tm_new);
77+
#else
7478
tv->tv_sec = timegm(&tm_new);
75-
79+
#endif
7680
#if defined(SOC_SERIES_STM32H7)
7781
tv->tv_usec = (255.0 - RTC_TimeStruct.SubSeconds * 1.0) / 256.0 * 1000.0 * 1000.0;
7882
#endif
@@ -85,8 +89,11 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
8589
RTC_TimeTypeDef RTC_TimeStruct = {0};
8690
RTC_DateTypeDef RTC_DateStruct = {0};
8791
struct tm tm = {0};
88-
92+
#ifdef RT_ALARM_USING_LOCAL_TIME
93+
localtime_r(&time_stamp,&tm);
94+
#else
8995
gmtime_r(&time_stamp, &tm);
96+
#endif
9097
if (tm.tm_year < 100)
9198
{
9299
return -RT_ERROR;
@@ -318,6 +325,11 @@ static rt_err_t stm32_rtc_set_alarm(struct rt_rtc_wkalarm *alarm)
318325
rtc_device.wkalarm.tm_hour = alarm->tm_hour;
319326
rtc_device.wkalarm.tm_min = alarm->tm_min;
320327
rtc_device.wkalarm.tm_sec = alarm->tm_sec;
328+
/* must include the year, month, and day */
329+
/* as the alarm in RT_ALARM_ONESHOT mode compares the current timestamp with the alarm timestamp */
330+
rtc_device.wkalarm.tm_year = alarm->tm_year;
331+
rtc_device.wkalarm.tm_mon = alarm->tm_mon;
332+
rtc_device.wkalarm.tm_mday = alarm->tm_mday;
321333
rtc_alarm_time_set(&rtc_device);
322334
}
323335
else

0 commit comments

Comments
 (0)