1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

cpu/native: implement periph_rtc_ms

This commit is contained in:
Benjamin Valentin 2023-03-02 16:45:01 +01:00 committed by Benjamin Valentin
parent a9310ed46c
commit ce36460b16
3 changed files with 29 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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) {