diff --git a/drivers/include/periph/rtc.h b/drivers/include/periph/rtc.h index 9a424fecc7..0071711999 100644 --- a/drivers/include/periph/rtc.h +++ b/drivers/include/periph/rtc.h @@ -90,6 +90,19 @@ int rtc_set_time(struct tm *time); */ int rtc_get_time(struct tm *time); +/** + * @brief Get current RTC time with sub-second component. + * Requires the `periph_rtc_ms` feature. + * + * @param[out] time Pointer to the struct to write the time to. + * @param[out] ms Pointer to a variable to hold the microsecond + * component of the current RTC time. + * + * @return 0 for success + * @return -1 an error occurred + */ +int rtc_get_time_ms(struct tm *time, uint16_t *ms); + /** * @brief Set an alarm for RTC to the specified value. * diff --git a/drivers/rtt_rtc/Makefile.features b/drivers/rtt_rtc/Makefile.features new file mode 100644 index 0000000000..1ed82b05fe --- /dev/null +++ b/drivers/rtt_rtc/Makefile.features @@ -0,0 +1,2 @@ +FEATURES_PROVIDED += periph_rtc +FEATURES_PROVIDED += periph_rtc_ms diff --git a/drivers/rtt_rtc/rtt_rtc.c b/drivers/rtt_rtc/rtt_rtc.c index 878bc6ea94..2e16ac69c6 100644 --- a/drivers/rtt_rtc/rtt_rtc.c +++ b/drivers/rtt_rtc/rtt_rtc.c @@ -28,6 +28,7 @@ #include "periph/rtc.h" #include "periph/rtt.h" +#include "timex.h" #define ENABLE_DEBUG 0 #include "debug.h" @@ -40,6 +41,7 @@ #define TICKS(x) ( (x) * RTT_SECOND) #define SECONDS(x) (_RTT(x) / RTT_SECOND) +#define SUBSECONDS(x) (_RTT(x) % RTT_SECOND) /* Place counter in .noinit section if no backup RAM is available. This means the date is undefined at cold boot, but will likely still @@ -133,6 +135,22 @@ int rtc_set_time(struct tm *time) return 0; } +int rtc_get_time_ms(struct tm *time, uint16_t *ms) +{ + uint32_t prev = rtc_now; + + /* repeat calculation if an alarm triggered in between */ + do { + uint32_t now = rtt_get_counter(); + uint32_t tmp = _rtc_now(now); + + rtc_localtime(tmp, time); + *ms = (SUBSECONDS(now) * MS_PER_SEC) / RTT_SECOND; + } while (prev != rtc_now); + + return 0; +} + int rtc_get_time(struct tm *time) { uint32_t prev = rtc_now;