diff --git a/boards/native/Kconfig b/boards/native/Kconfig index e58330532a..3e5f1d7580 100644 --- a/boards/native/Kconfig +++ b/boards/native/Kconfig @@ -15,6 +15,7 @@ config BOARD_NATIVE # Put defined MCU peripherals here (in alphabetical order) select HAS_PERIPH_RTC + select HAS_PERIPH_RTC_MS select HAS_PERIPH_TIMER select HAS_PERIPH_UART select HAS_PERIPH_GPIO diff --git a/boards/native/Makefile.features b/boards/native/Makefile.features index d0a5c35b1d..c3242a2fc2 100644 --- a/boards/native/Makefile.features +++ b/boards/native/Makefile.features @@ -2,6 +2,7 @@ CPU = native # Put defined MCU peripherals here (in alphabetical order) FEATURES_PROVIDED += periph_rtc +FEATURES_PROVIDED += periph_rtc_ms FEATURES_PROVIDED += periph_timer FEATURES_PROVIDED += periph_uart FEATURES_PROVIDED += periph_gpio diff --git a/cpu/native/periph/rtc.c b/cpu/native/periph/rtc.c index d3d45f17e3..f544c9261d 100644 --- a/cpu/native/periph/rtc.c +++ b/cpu/native/periph/rtc.c @@ -37,6 +37,13 @@ #define ENABLE_DEBUG 0 #include "debug.h" +/** + * @brief Time source of the native RTC + */ +#ifndef NATIVE_RTC_SOURCE +#define NATIVE_RTC_SOURCE CLOCK_REALTIME +#endif + static int _native_rtc_initialized = 0; static int _native_rtc_powered = 0; @@ -141,10 +148,15 @@ int rtc_set_time(struct tm *ttime) warnx("rtc_set_time: out of time_t range"); return -1; } + + struct timespec tv; + _native_syscall_enter(); - _native_rtc_offset = tnew - time(NULL); + clock_gettime(NATIVE_RTC_SOURCE, &tv); _native_syscall_leave(); + _native_rtc_offset = tnew - tv.tv_sec; + if (_native_rtc_alarm_callback) { rtc_set_alarm(&_native_rtc_alarm, _native_rtc_alarm_callback, _native_rtc_timer.arg); @@ -153,9 +165,9 @@ int rtc_set_time(struct tm *ttime) return 0; } -int rtc_get_time(struct tm *ttime) +int rtc_get_time_ms(struct tm *ttime, uint16_t *ms) { - time_t t; + struct timespec tv; if (!_native_rtc_initialized) { warnx("rtc_get_time: not initialized"); @@ -167,9 +179,14 @@ int rtc_get_time(struct tm *ttime) } _native_syscall_enter(); - t = time(NULL) + _native_rtc_offset; + clock_gettime(NATIVE_RTC_SOURCE, &tv); + tv.tv_sec += _native_rtc_offset; - if (localtime_r(&t, ttime) == NULL) { + if (ms) { + *ms = tv.tv_nsec / NS_PER_MS; + } + + if (localtime_r(&tv.tv_sec, ttime) == NULL) { err(EXIT_FAILURE, "rtc_get_time: localtime_r"); } _native_syscall_leave(); @@ -180,6 +197,11 @@ int rtc_get_time(struct tm *ttime) return 0; } +int rtc_get_time(struct tm *ttime) +{ + return rtc_get_time_ms(ttime, NULL); +} + int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg) { if (!_native_rtc_initialized) { diff --git a/pkg/tinydtls/Makefile.dep b/pkg/tinydtls/Makefile.dep index 70e9b16f95..c3087a52c0 100644 --- a/pkg/tinydtls/Makefile.dep +++ b/pkg/tinydtls/Makefile.dep @@ -5,7 +5,7 @@ USEMODULE += hashes USEMODULE += random USEMODULE += tinydtls_aes USEMODULE += tinydtls_ecc -USEMODULE += ztimer64_msec +USEMODULE += ztimer_msec # TinyDTLS only has support for 32-bit architectures ATM FEATURES_REQUIRED += arch_32bit