Add get/settimeofday retargets

pull/8497/head
kegilbert 2018-10-22 16:21:25 -05:00
parent c27dabe765
commit dafb01c755
2 changed files with 72 additions and 19 deletions

View File

@ -78,12 +78,22 @@ static void (*_rtc_write)(time_t t) = NULL;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#if defined (__ICCARM__)
time_t __time32(time_t *timer)
#else
time_t time(time_t *timer)
#endif
int settimeofday(const struct timeval *tv, MBED_UNUSED const struct timezone *tz)
{
_mutex->lock();
if (_rtc_init != NULL) {
_rtc_init();
}
if (_rtc_write != NULL) {
_rtc_write(tv->tv_sec);
}
_mutex->unlock();
return 0;
}
int gettimeofday(struct timeval *tv, MBED_UNUSED void *tz)
{ {
_mutex->lock(); _mutex->lock();
if (_rtc_isenabled != NULL) { if (_rtc_isenabled != NULL) {
@ -92,28 +102,40 @@ time_t time(time_t *timer)
} }
} }
time_t t = (time_t) -1; time_t t = (time_t) - 1;
if (_rtc_read != NULL) { if (_rtc_read != NULL) {
t = _rtc_read(); t = _rtc_read();
} }
if (timer != NULL) { tv->tv_sec = t;
*timer = t; tv->tv_usec = 0;
}
_mutex->unlock(); _mutex->unlock();
return t;
return 0;
} }
#if defined (__ICCARM__)
time_t __time32(time_t *timer)
#else
time_t time(time_t *timer)
#endif
{
struct timeval tv;
gettimeofday(&tv, NULL);
if (timer != NULL) {
*timer = tv.tv_sec;
}
return tv.tv_sec;
}
void set_time(time_t t) void set_time(time_t t)
{ {
_mutex->lock(); const struct timeval tv = { t, 0 };
if (_rtc_init != NULL) { settimeofday(&tv, NULL);
_rtc_init();
}
if (_rtc_write != NULL) {
_rtc_write(t);
}
_mutex->unlock();
} }
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) 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
} }
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -28,6 +28,14 @@
extern "C" { extern "C" {
#endif #endif
/* Timeval definition for non GCC_ARM toolchains */
#if !defined(__GNUC__) || defined(__CC_ARM) || defined(__clang__)
struct timeval {
time_t tv_sec;
int32_t tv_usec;
};
#endif
/** Implementation of the C time.h functions /** Implementation of the C time.h functions
* *
* Provides mechanisms to set and read the current time, based * Provides mechanisms to set and read the current time, based
@ -90,6 +98,30 @@ void set_time(time_t t);
*/ */
void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)); void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void));
/** Standard lib retarget, get time since Epoch
*
* @param tv Structure containing time_t secondsa and suseconds_t microseconds. Due to
* separate target specific RTC implementations only the seconds component is used.
* @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
* not used.
* @return 0 on success, -1 on a failure.
* @note Synchronization level: Thread safe
*
*/
int gettimeofday(struct timeval *tv, void *tz);
/** Standard lib retarget, set time since Epoch
*
* @param tv Structure containing time_t secondsa and suseconds_t microseconds. Due to
* separate target specific RTC implementations only the seconds component is used.
* @param tz DEPRECATED IN THE STANDARD: This parameter is left in for legacy code. It is
* not used.
* @return Time in seconds on success, -1 on a failure.
* @note Synchronization level: Thread safe
*
*/
int settimeofday(const struct timeval *tv, const struct timezone *tz);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif