Skip to content

Commit

Permalink
Merge branch 'fix/newlib_timefunc_weak' into 'master'
Browse files Browse the repository at this point in the history
fix(newlib): Allow for timefunc customization if not implemented

See merge request espressif/esp-idf!30059
  • Loading branch information
david-cermak committed Apr 9, 2024
2 parents 96c81c8 + b1ea47a commit f7fceb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
11 changes: 8 additions & 3 deletions components/newlib/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ menu "Newlib"

LF: no modification is applied, stdout is sent as is

CR: each occurence of LF is replaced with CR
CR: each occurrence of LF is replaced with CR

This option doesn't affect behavior of the UART driver (drivers/uart.h).

Expand All @@ -36,7 +36,7 @@ menu "Newlib"

LF: no modification is applied, input is sent to stdin as is

CR: each occurence of CR is replaced with LF
CR: each occurrence of CR is replaced with LF

This option doesn't affect behavior of the UART driver (drivers/uart.h).

Expand Down Expand Up @@ -93,7 +93,12 @@ menu "Newlib"
resolution. Also the gettimeofday function itself may take
longer to run.
- If no timers are used, gettimeofday and time functions
return -1 and set errno to ENOSYS.
return -1 and set errno to ENOSYS; they are defined as weak,
so they could be overridden.
If you want to customize gettimeofday() and other time functions,
please choose this option and refer to the 'time.c' source file
for the exact prototypes of these functions.

- When RTC is used for timekeeping, two RTC_STORE registers are
used to keep time in deep sleep mode.

Expand Down
19 changes: 13 additions & 6 deletions components/newlib/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#endif

#if IMPL_NEWLIB_TIME_FUNCS
// time functions are implemented -- they should not be weak
#define WEAK_UNLESS_TIMEFUNC_IMPL

// stores the start time of the slew
static uint64_t s_adjtime_start_us;
// is how many microseconds total to slew
Expand Down Expand Up @@ -102,9 +105,13 @@ static void adjtime_corr_stop(void)
}
_lock_release(&s_time_lock);
}
#else

// no time functions are actually implemented -- allow users to override them
#define WEAK_UNLESS_TIMEFUNC_IMPL __attribute__((weak))
#endif

int adjtime(const struct timeval *delta, struct timeval *outdelta)
WEAK_UNLESS_TIMEFUNC_IMPL int adjtime(const struct timeval *delta, struct timeval *outdelta)
{
#if IMPL_NEWLIB_TIME_FUNCS
if (outdelta != NULL) {
Expand Down Expand Up @@ -157,7 +164,7 @@ clock_t IRAM_ATTR _times_r(struct _reent *r, struct tms *ptms)
return (clock_t) tv.tv_sec;
}

int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
WEAK_UNLESS_TIMEFUNC_IMPL int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
{
(void) tz;

Expand All @@ -174,7 +181,7 @@ int IRAM_ATTR _gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
#endif
}

int settimeofday(const struct timeval *tv, const struct timezone *tz)
WEAK_UNLESS_TIMEFUNC_IMPL int settimeofday(const struct timeval *tv, const struct timezone *tz)
{
(void) tz;
#if IMPL_NEWLIB_TIME_FUNCS
Expand Down Expand Up @@ -211,7 +218,7 @@ unsigned int sleep(unsigned int seconds)
return 0;
}

int clock_settime(clockid_t clock_id, const struct timespec *tp)
WEAK_UNLESS_TIMEFUNC_IMPL int clock_settime(clockid_t clock_id, const struct timespec *tp)
{
#if IMPL_NEWLIB_TIME_FUNCS
if (tp == NULL) {
Expand All @@ -236,7 +243,7 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp)
#endif
}

int clock_gettime(clockid_t clock_id, struct timespec *tp)
WEAK_UNLESS_TIMEFUNC_IMPL int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
#if IMPL_NEWLIB_TIME_FUNCS
if (tp == NULL) {
Expand Down Expand Up @@ -267,7 +274,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp)
#endif
}

int clock_getres(clockid_t clock_id, struct timespec *res)
WEAK_UNLESS_TIMEFUNC_IMPL int clock_getres(clockid_t clock_id, struct timespec *res)
{
#if IMPL_NEWLIB_TIME_FUNCS
if (res == NULL) {
Expand Down

0 comments on commit f7fceb5

Please sign in to comment.