Skip to content

Commit

Permalink
Merge #19340 #19352
Browse files Browse the repository at this point in the history
19340: cpu/native: implement periph_rtc_ms r=benpicco a=benpicco



19352: pkg/tinydtls: don't require ztimer64 r=benpicco a=benpicco



Co-authored-by: Benjamin Valentin <benpicco@beuth-hochschule.de>
  • Loading branch information
bors[bot] and benpicco authored Mar 6, 2023
3 parents bc517b5 + ce36460 + 8af8230 commit 777857a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions boards/native/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions boards/native/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions cpu/native/periph/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand All @@ -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();
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/tinydtls/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 777857a

Please sign in to comment.