diff --git a/sys/ztimer/Kconfig b/sys/ztimer/Kconfig index 9d7a29fc62..124e9c261b 100644 --- a/sys/ztimer/Kconfig +++ b/sys/ztimer/Kconfig @@ -243,6 +243,13 @@ config MODULE_ZTIMER_ONDEMAND_TIMER Turn off the underlying Timer peripheral if related ztimer clocks have no users. +config MODULE_ZTIMER_ONDEMAND_RTC + bool "Run RTC only on demand" + depends on MODULE_ZTIMER_PERIPH_RTC + help + Turn off the underlying RTC peripheral if related ztimer clocks + have no users. + endif # MODULE_ZTIMER_ONDEMAND config MODULE_ZTIMER_INIT diff --git a/sys/ztimer/periph_rtc.c b/sys/ztimer/periph_rtc.c index 7144706d1a..b194053028 100644 --- a/sys/ztimer/periph_rtc.c +++ b/sys/ztimer/periph_rtc.c @@ -138,10 +138,28 @@ static void _ztimer_periph_rtc_cancel(ztimer_clock_t *clock) rtc_clear_alarm(); } +#if MODULE_ZTIMER_ONDEMAND_RTC +static void _ztimer_periph_rtc_start(ztimer_clock_t *clock) +{ + (void)clock; + rtc_poweron(); +} + +static void _ztimer_periph_rtc_stop(ztimer_clock_t *clock) +{ + (void)clock; + rtc_poweroff(); +} +#endif /* MODULE_ZTIMER_ONDEMAND_RTC */ + static const ztimer_ops_t _ztimer_periph_rtc_ops = { .set = _ztimer_periph_rtc_set, .now = _ztimer_periph_rtc_now, .cancel = _ztimer_periph_rtc_cancel, +#if MODULE_ZTIMER_ONDEMAND_RTC + .start = _ztimer_periph_rtc_start, + .stop = _ztimer_periph_rtc_stop, +#endif }; void ztimer_periph_rtc_init(ztimer_periph_rtc_t *clock) @@ -149,5 +167,10 @@ void ztimer_periph_rtc_init(ztimer_periph_rtc_t *clock) clock->ops = &_ztimer_periph_rtc_ops; clock->max_value = UINT32_MAX; rtc_init(); + +#if MODULE_ZTIMER_ONDEMAND_RTC + rtc_poweroff(); +#else rtc_poweron(); +#endif }