diff --git a/cpu/native/Kconfig b/cpu/native/Kconfig index 2b7faae06f05..268185bd0aa1 100644 --- a/cpu/native/Kconfig +++ b/cpu/native/Kconfig @@ -25,7 +25,6 @@ config CPU_ARCH_NATIVE # needed modules select MODULE_PERIPH if TEST_KCONFIG - select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT config CPU_CORE_NATIVE bool diff --git a/cpu/native/Makefile.dep b/cpu/native/Makefile.dep index 2beef965a0a6..d4f8360d6310 100644 --- a/cpu/native/Makefile.dep +++ b/cpu/native/Makefile.dep @@ -40,9 +40,12 @@ ifneq (,$(filter socket_zep,$(USEMODULE))) endif endif -ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) - # requires 64bit for syscalls - USEMODULE += ztimer64_xtimer_compat +ifneq (,$(filter libc_gettimeofday,$(USEMODULE))) + USEMODULE += xtimer + ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) + # requires 64bit timestamps + USEMODULE += ztimer64_xtimer_compat + endif endif USEMODULE += periph diff --git a/cpu/native/syscalls.c b/cpu/native/syscalls.c index bb60137ef662..70b6eeb4d1bf 100644 --- a/cpu/native/syscalls.c +++ b/cpu/native/syscalls.c @@ -42,6 +42,7 @@ #include "xtimer.h" #include "stdio_base.h" +#include "kernel_defines.h" #include "native_internal.h" #define ENABLE_DEBUG 0 @@ -474,10 +475,10 @@ int getpid(void) return -1; } -#ifdef MODULE_XTIMER +#if (IS_USED(MODULE_LIBC_GETTIMEOFDAY)) int _gettimeofday(struct timeval *tp, void *restrict tzp) { - (void) tzp; + (void)tzp; uint64_t now = xtimer_now_usec64(); tp->tv_sec = now / US_PER_SEC; tp->tv_usec = now - tp->tv_sec; diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index fe812ef5c995..cc64d28a9ac6 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -121,6 +121,10 @@ PSEUDOMODULES += log PSEUDOMODULES += log_printfnoformat PSEUDOMODULES += log_color PSEUDOMODULES += lora +## @defgroup pseudomodule_libc_gettimeofday libc_gettimeofday +## @brief Includes implementation of gettimeofday() +## +PSEUDOMODULES += libc_gettimeofday ## @defgroup pseudomodule_mpu_stack_guard mpu_stack_guard ## @brief MPU based stack guard diff --git a/pkg/lua/Makefile.dep b/pkg/lua/Makefile.dep index 74032e30aba2..72a2b594c556 100644 --- a/pkg/lua/Makefile.dep +++ b/pkg/lua/Makefile.dep @@ -17,3 +17,11 @@ FEATURES_BLACKLIST += arch_riscv # - lua/liolib.c:671:38: error: '_IOFBF' undeclared (first use in this function) # - lua/liolib.c:671:46: error: '_IOLBF' undeclared (first use in this function) FEATURES_BLACKLIST += picolibc + +ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) + USEMODULE += libc_gettimeofday +endif + +ifneq (,$(filter native,$(CPU))) + USEMODULE += libc_gettimeofday +endif diff --git a/pkg/tinydtls/Makefile.dep b/pkg/tinydtls/Makefile.dep index 70e9b16f950f..9742974aaf87 100644 --- a/pkg/tinydtls/Makefile.dep +++ b/pkg/tinydtls/Makefile.dep @@ -14,3 +14,11 @@ ifneq (,$(filter sock_dtls,$(USEMODULE))) USEMODULE += tinydtls_sock_dtls USEMODULE += ztimer_usec endif + +ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) + USEMODULE += libc_gettimeofday +endif + +ifneq (,$(filter native,$(CPU))) + USEMODULE += libc_gettimeofday +endif diff --git a/pkg/wolfssl/Makefile.dep b/pkg/wolfssl/Makefile.dep index 17434f252ab3..f4262b024274 100644 --- a/pkg/wolfssl/Makefile.dep +++ b/pkg/wolfssl/Makefile.dep @@ -82,5 +82,13 @@ ifneq (,$(filter wolfcrypt_random,$(USEMODULE))) USEMODULE += random endif +ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) + USEMODULE += libc_gettimeofday +endif + +ifneq (,$(filter native,$(CPU))) + USEMODULE += libc_gettimeofday +endif + # wolfssl is only supported by 32 bit architectures FEATURES_REQUIRED += arch_32bit diff --git a/sys/Kconfig b/sys/Kconfig index ce356f3d6b7f..ef749da2b66d 100644 --- a/sys/Kconfig +++ b/sys/Kconfig @@ -49,9 +49,15 @@ config MODULE_NEWLIB config MODULE_PICOLIBC bool "Picolibc" depends on HAS_PICOLIBC - endchoice +config MODULE_LIBC_GETTIMEOFDAY + bool + select MODULE_XTIMER + select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT + help + Support for gettimeofday() + rsource "Kconfig.newlib" rsource "Kconfig.picolibc" diff --git a/sys/Kconfig.newlib b/sys/Kconfig.newlib index cd1f788a29e3..2f846e80e382 100644 --- a/sys/Kconfig.newlib +++ b/sys/Kconfig.newlib @@ -18,10 +18,17 @@ config MODULE_NEWLIB_SYSCALLS_DEFAULT default y depends on !HAVE_CUSTOM_NEWLIB_SYSCALLS select MODULE_DIV - select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT help Default implementation of newlib system calls. +config MODULE_LIBC_GETTIMEOFDAY + bool + select MODULE_NEWLIB_SYSCALLS_DEFAULT if MODULE_NEWLIB + select MODULE_XTIMER + select MODULE_ZTIMER64_XTIMER_COMPAT if MODULE_ZTIMER_XTIMER_COMPAT + help + Support for gettimeofday() + endif # MODULE_NEWLIB config HAVE_CUSTOM_NEWLIB_SYSCALLS diff --git a/sys/Makefile.dep b/sys/Makefile.dep index beee33facf46..a2343939419f 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -242,9 +242,12 @@ ifneq (,$(filter newlib,$(USEMODULE))) endif ifneq (,$(filter newlib_syscalls_default,$(USEMODULE))) USEMODULE += div - ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) - # requires 64bit timestamps when using xtimer - USEMODULE += ztimer64_xtimer_compat + ifneq (,$(filter libc_gettimeofday,$(USEMODULE))) + USEMODULE += xtimer + ifneq (,$(filter ztimer_xtimer_compat,$(USEMODULE))) + # requires 64bit timestamps + USEMODULE += ztimer64_xtimer_compat + endif endif endif endif diff --git a/sys/newlib_syscalls_default/syscalls.c b/sys/newlib_syscalls_default/syscalls.c index 9ca0379411d3..ab692e831dd7 100644 --- a/sys/newlib_syscalls_default/syscalls.c +++ b/sys/newlib_syscalls_default/syscalls.c @@ -630,24 +630,16 @@ int _kill(pid_t pid, int sig) return -1; } -#ifdef MODULE_XTIMER +#if (IS_USED(MODULE_LIBC_GETTIMEOFDAY)) int _gettimeofday_r(struct _reent *r, struct timeval *restrict tp, void *restrict tzp) { - (void) r; - (void) tzp; + (void)tzp; + (void)r; uint64_t now = xtimer_now_usec64(); tp->tv_sec = div_u64_by_1000000(now); tp->tv_usec = now - (tp->tv_sec * US_PER_SEC); return 0; } -#else -int _gettimeofday_r(struct _reent *r, struct timeval *restrict tp, void *restrict tzp) -{ - (void) tp; - (void) tzp; - r->_errno = ENOSYS; - return -1; -} #endif /** diff --git a/tests/cpp11_mutex/Makefile b/tests/cpp11_mutex/Makefile index 6ec1d9fd3615..6c4a872c216a 100644 --- a/tests/cpp11_mutex/Makefile +++ b/tests/cpp11_mutex/Makefile @@ -2,5 +2,6 @@ include ../Makefile.tests_common USEMODULE += cpp11-compat USEMODULE += xtimer +USEMODULE += libc_gettimeofday include $(RIOTBASE)/Makefile.include