Skip to content
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

cpu/native: implement periph_rtc_ms #19340

Merged
merged 1 commit into from
Mar 6, 2023
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
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