Skip to content

Commit 52733fe

Browse files
committed
[M487] Replace mktime/localtime with interrupt-safe version in rtc
The use of mktime was causing a fault when called in interrupt handler because on GCC it lock the mutex protecting the environment, To overcome this issue, this patch add dedicated routine to convert a time_t into a tm and vice versa. In the process mktime has been optimized and is now an order of magnitude faster than the routines present in the C library.
1 parent b363d00 commit 52733fe

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

targets/TARGET_NUVOTON/TARGET_M480/rtc_api.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "mbed_error.h"
2323
#include "nu_modutil.h"
2424
#include "nu_miscutil.h"
25+
#include "mbed_mktime.h"
2526

2627
#define YEAR0 1900
2728
//#define EPOCH_YR 1970
@@ -92,7 +93,7 @@ time_t rtc_read(void)
9293
timeinfo.tm_sec = rtc_datetime.u32Second;
9394

9495
// Convert to timestamp
95-
time_t t = mktime(&timeinfo);
96+
time_t t = _rtc_mktime(&timeinfo);
9697

9798
return t;
9899
}
@@ -104,18 +105,21 @@ void rtc_write(time_t t)
104105
}
105106

106107
// Convert timestamp to struct tm
107-
struct tm *timeinfo = localtime(&t);
108+
struct tm timeinfo;
109+
if (_rtc_localtime(t, &timeinfo) == false) {
110+
return;
111+
}
108112

109113
S_RTC_TIME_DATA_T rtc_datetime;
110114

111115
// Convert S_RTC_TIME_DATA_T to struct tm
112-
rtc_datetime.u32Year = timeinfo->tm_year + YEAR0;
113-
rtc_datetime.u32Month = timeinfo->tm_mon + 1;
114-
rtc_datetime.u32Day = timeinfo->tm_mday;
115-
rtc_datetime.u32DayOfWeek = timeinfo->tm_wday;
116-
rtc_datetime.u32Hour = timeinfo->tm_hour;
117-
rtc_datetime.u32Minute = timeinfo->tm_min;
118-
rtc_datetime.u32Second = timeinfo->tm_sec;
116+
rtc_datetime.u32Year = timeinfo.tm_year + YEAR0;
117+
rtc_datetime.u32Month = timeinfo.tm_mon + 1;
118+
rtc_datetime.u32Day = timeinfo.tm_mday;
119+
rtc_datetime.u32DayOfWeek = timeinfo.tm_wday;
120+
rtc_datetime.u32Hour = timeinfo.tm_hour;
121+
rtc_datetime.u32Minute = timeinfo.tm_min;
122+
rtc_datetime.u32Second = timeinfo.tm_sec;
119123
rtc_datetime.u32TimeScale = RTC_CLOCK_24;
120124

121125
// NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational.

0 commit comments

Comments
 (0)