Skip to content

Add get/settimeofday retargets #8497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions platform/mbed_rtc_time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,22 @@ static void (*_rtc_write)(time_t t) = NULL;
#ifdef __cplusplus
extern "C" {
#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();
if (_rtc_isenabled != NULL) {
Expand All @@ -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) {
t = _rtc_read();
}

tv->tv_sec = t;
tv->tv_usec = 0;

_mutex->unlock();

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 = t;
*timer = tv.tv_sec;
}
_mutex->unlock();
return t;

return tv.tv_sec;
}


void set_time(time_t t)
{
_mutex->lock();
if (_rtc_init != NULL) {
_rtc_init();
}
if (_rtc_write != NULL) {
_rtc_write(t);
}
_mutex->unlock();
const struct timeval tv = { t, 0 };
settimeofday(&tv, NULL);
}

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



#ifdef __cplusplus
}
#endif
32 changes: 32 additions & 0 deletions platform/mbed_rtc_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
extern "C" {
#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
*
* Provides mechanisms to set and read the current time, based
Expand Down Expand Up @@ -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));

/** 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
}
#endif
Expand Down