Skip to content

Commit dafb01c

Browse files
committed
Add get/settimeofday retargets
1 parent c27dabe commit dafb01c

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

platform/mbed_rtc_time.cpp

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,22 @@ static void (*_rtc_write)(time_t t) = NULL;
7878
#ifdef __cplusplus
7979
extern "C" {
8080
#endif
81-
#if defined (__ICCARM__)
82-
time_t __time32(time_t *timer)
83-
#else
84-
time_t time(time_t *timer)
85-
#endif
8681

82+
int settimeofday(const struct timeval *tv, MBED_UNUSED const struct timezone *tz)
83+
{
84+
_mutex->lock();
85+
if (_rtc_init != NULL) {
86+
_rtc_init();
87+
}
88+
if (_rtc_write != NULL) {
89+
_rtc_write(tv->tv_sec);
90+
}
91+
_mutex->unlock();
92+
93+
return 0;
94+
}
95+
96+
int gettimeofday(struct timeval *tv, MBED_UNUSED void *tz)
8797
{
8898
_mutex->lock();
8999
if (_rtc_isenabled != NULL) {
@@ -92,28 +102,40 @@ time_t time(time_t *timer)
92102
}
93103
}
94104

95-
time_t t = (time_t) -1;
105+
time_t t = (time_t) - 1;
96106
if (_rtc_read != NULL) {
97107
t = _rtc_read();
98108
}
99109

110+
tv->tv_sec = t;
111+
tv->tv_usec = 0;
112+
113+
_mutex->unlock();
114+
115+
return 0;
116+
}
117+
118+
#if defined (__ICCARM__)
119+
time_t __time32(time_t *timer)
120+
#else
121+
time_t time(time_t *timer)
122+
#endif
123+
{
124+
struct timeval tv;
125+
gettimeofday(&tv, NULL);
126+
100127
if (timer != NULL) {
101-
*timer = t;
128+
*timer = tv.tv_sec;
102129
}
103-
_mutex->unlock();
104-
return t;
130+
131+
return tv.tv_sec;
105132
}
106133

134+
107135
void set_time(time_t t)
108136
{
109-
_mutex->lock();
110-
if (_rtc_init != NULL) {
111-
_rtc_init();
112-
}
113-
if (_rtc_write != NULL) {
114-
_rtc_write(t);
115-
}
116-
_mutex->unlock();
137+
const struct timeval tv = { t, 0 };
138+
settimeofday(&tv, NULL);
117139
}
118140

119141
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void))
@@ -127,7 +149,6 @@ void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init
127149
}
128150

129151

130-
131152
#ifdef __cplusplus
132153
}
133154
#endif

platform/mbed_rtc_time.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
extern "C" {
2929
#endif
3030

31+
/* Timeval definition for non GCC_ARM toolchains */
32+
#if !defined(__GNUC__) || defined(__CC_ARM) || defined(__clang__)
33+
struct timeval {
34+
time_t tv_sec;
35+
int32_t tv_usec;
36+
};
37+
#endif
38+
3139
/** Implementation of the C time.h functions
3240
*
3341
* Provides mechanisms to set and read the current time, based
@@ -90,6 +98,30 @@ void set_time(time_t t);
9098
*/
9199
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
92100

101+
/** Standard lib retarget, get time since Epoch
102+
*
103+
* @param tv Structure containing time_t secondsa and suseconds_t microseconds. Due to
104+
* separate target specific RTC implementations only the seconds component is used.
105+
* @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
106+
* not used.
107+
* @return 0 on success, -1 on a failure.
108+
* @note Synchronization level: Thread safe
109+
*
110+
*/
111+
int gettimeofday(struct timeval *tv, void *tz);
112+
113+
/** Standard lib retarget, set time since Epoch
114+
*
115+
* @param tv Structure containing time_t secondsa and suseconds_t microseconds. Due to
116+
* separate target specific RTC implementations only the seconds component is used.
117+
* @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
118+
* not used.
119+
* @return Time in seconds on success, -1 on a failure.
120+
* @note Synchronization level: Thread safe
121+
*
122+
*/
123+
int settimeofday(const struct timeval *tv, const struct timezone *tz);
124+
93125
#ifdef __cplusplus
94126
}
95127
#endif

0 commit comments

Comments
 (0)