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

sys/ztimer: add ZTIMER_SEC, improve auto_init #16172

Merged
merged 11 commits into from
Apr 2, 2021
32 changes: 30 additions & 2 deletions cpu/native/periph/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ static void _native_rtc_cb(void *arg) {
_native_rtc_alarm_callback = NULL;
}

/* RIOT does not expect DST or TZ information */
static void _remove_struct_tm_extra( struct tm * t ){
struct tm tmp = {.tm_year = t->tm_year,
.tm_mon = t->tm_mon,
.tm_mday = t->tm_mday,
.tm_hour = t->tm_hour,
.tm_min = t->tm_min,
.tm_sec = t->tm_sec,
.tm_wday = t->tm_wday
};
*t = tmp;
}

void rtc_init(void)
{
DEBUG("rtc_init\n");
Expand Down Expand Up @@ -115,8 +128,14 @@ int rtc_set_time(struct tm *ttime)
warnx("rtc_set_time: not powered on");
return -1;
}
/* ensure there is no accidental extra information */
struct tm itime = *ttime;
_remove_struct_tm_extra(&itime);

/* mktime() and localtime are only inverse functions if tm_isdst == -1 */
itime.tm_isdst = -1;
time_t tnew = mktime(&itime);

time_t tnew = mktime(ttime);
if (tnew == -1) {
warnx("rtc_set_time: out of time_t range");
return -1;
Expand Down Expand Up @@ -154,6 +173,9 @@ int rtc_get_time(struct tm *ttime)
}
_native_syscall_leave();

/* RIOT does not handle DST or TZ information */
_remove_struct_tm_extra(ttime);

return 0;
}

Expand All @@ -171,7 +193,13 @@ int rtc_set_alarm(struct tm *time, rtc_alarm_cb_t cb, void *arg)
struct tm now;
rtc_get_time(&now);

time_t tdiff_secs = mktime(time) - mktime(&now);
/* ensure there is no accidental extra information */
struct tm intime = *time;
_remove_struct_tm_extra(&intime);

/* tm_idst are ignored for these mktime calls since
* both times carry the same (00) timezone information */
time_t tdiff_secs = mktime(&intime) - mktime(&now);

if (_native_rtc_alarm_callback) {
xtimer_remove(&_native_rtc_timer);
Expand Down
21 changes: 17 additions & 4 deletions sys/include/ztimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,20 @@
*
* For now, there are:
*
* ZTIMER_USEC: clock providing microsecond ticks
* ZTIMER_USEC: clock providing microsecond ticks, always uses a basic timer
* (ztimer_periph_timer)
*
* ZTIMER_MSEC: clock providing millisecond ticks, using a low power timer if
* available on the platform
* ZTIMER_MSEC: clock providing millisecond ticks, using a low power timer
* (ztimer_periph_rtt) if it is available on the platform
* and it running at 1kHz or above else it uses the same
* basic timer as ZTIMER_USEC does.
*
* ZTIMER_SEC: clock providing second time, possibly using epoch semantics
* ZTIMER_SEC: clock providing second time, possibly using epoch semantics,
* it will use a low power timer (ztimer_periph_rtt)
* if it is available on the platform alternately it uses
* ztimer_periph_rtc if it is available and configured
* if if these are missing it will use same basic timer
* as ZTIMER_USEC does.
*
* These pointers are defined in `ztimer.h` and can be used like this:
*
Expand Down Expand Up @@ -595,6 +603,11 @@ extern ztimer_clock_t *const ZTIMER_USEC;
*/
extern ztimer_clock_t *const ZTIMER_MSEC;

/**
* @brief Default ztimer second clock
*/
extern ztimer_clock_t *const ZTIMER_SEC;

/**
* @brief Base ztimer for the microsecond clock (ZTIMER_USEC)
*
Expand Down
31 changes: 25 additions & 6 deletions sys/include/ztimer/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,36 @@ extern "C" {
#endif /* MODULE_ZTIMER_PERIPH_RTT */

/**
* @brief The minimum pm mode required for ZTIMER_USEC to run.
* @brief The minimum pm mode required for ZTIMER_TIMER to run.
*/
#ifndef CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE
#define CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE ZTIMER_CLOCK_NO_REQUIRED_PM_MODE
#ifndef CONFIG_ZTIMER_TIMER_BLOCK_PM_MODE
# ifdef CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE
# define CONFIG_ZTIMER_TIMER_BLOCK_PM_MODE CONFIG_ZTIMER_USEC_REQUIRED_PM_MODE
# else
# define CONFIG_ZTIMER_TIMER_BLOCK_PM_MODE ZTIMER_CLOCK_NO_REQUIRED_PM_MODE
# endif
#endif

/**
* @brief The minimum pm mode required for ZTIMER_RTT to run
*/
#ifndef CONFIG_ZTIMER_RTT_BLOCK_PM_MODE
# ifdef CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE
# define CONFIG_ZTIMER_RTT_BLOCK_PM_MODE CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE
# else
# define CONFIG_ZTIMER_RTT_BLOCK_PM_MODE ZTIMER_CLOCK_NO_REQUIRED_PM_MODE
# endif
#endif

/**
* @brief The minimum pm mode required for ZTIMER_MSEC to run
* @brief The minimum pm mode required for ZTIMER_RTC to run
*/
#ifndef CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE
#define CONFIG_ZTIMER_MSEC_REQUIRED_PM_MODE ZTIMER_CLOCK_NO_REQUIRED_PM_MODE
#ifndef CONFIG_ZTIMER_RTC_BLOCK_PM_MODE
# ifdef CONFIG_ZTIMER_SEC_REQUIRED_PM_MODE
# define CONFIG_ZTIMER_RTC_BLOCK_PM_MODE CONFIG_ZTIMER_SEC_REQUIRED_PM_MODE
# else
# define CONFIG_ZTIMER_RTC_BLOCK_PM_MODE ZTIMER_CLOCK_NO_REQUIRED_PM_MODE
# endif
#endif

#ifdef __cplusplus
Expand Down
4 changes: 4 additions & 0 deletions sys/ztimer/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ config MODULE_ZTIMER_MSEC
bool "Milliseconds"
depends on MODULE_ZTIMER_PERIPH_TIMER || MODULE_ZTIMER_PERIPH_RTT

config MODULE_ZTIMER_SEC
bool "Seconds"
depends on MODULE_ZTIMER_PERIPH_TIMER || MODULE_ZTIMER_PERIPH_RTT || MODULE_ZTIMER_PERIPH_RTC

endmenu # Clocks


Expand Down
4 changes: 4 additions & 0 deletions sys/ztimer/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ ifneq (,$(filter ztimer_periph_timer,$(USEMODULE)))
FEATURES_REQUIRED += periph_timer
endif

ifneq (,$(filter ztimer_periph_rtc,$(USEMODULE)))
FEATURES_REQUIRED += periph_rtc
endif

ifneq (,$(filter ztimer_periph_rtt,$(USEMODULE)))
FEATURES_REQUIRED += periph_rtt
endif
Expand Down
Loading