From 7c1b5630d2a77904a450c21a943de2a3a1135cd1 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 4 May 2021 17:27:20 +0200 Subject: [PATCH] cpu/atmega_common: RTC: implement rtc_get_time_ms() --- cpu/atmega_common/Kconfig | 1 + cpu/atmega_common/Makefile.features | 1 + cpu/atmega_common/periph/rtc.c | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/cpu/atmega_common/Kconfig b/cpu/atmega_common/Kconfig index 599b319719..0e1079c5dd 100644 --- a/cpu/atmega_common/Kconfig +++ b/cpu/atmega_common/Kconfig @@ -20,6 +20,7 @@ config CPU_COMMON_ATMEGA select HAS_PERIPH_GPIO select HAS_PERIPH_GPIO_IRQ select HAS_PERIPH_PM + select HAS_PERIPH_RTC_MS select HAS_PERIPH_RTT_SET_COUNTER select HAS_PERIPH_TIMER_PERIODIC select HAS_PERIPH_WDT diff --git a/cpu/atmega_common/Makefile.features b/cpu/atmega_common/Makefile.features index 7b58b012bd..12dc75ff88 100644 --- a/cpu/atmega_common/Makefile.features +++ b/cpu/atmega_common/Makefile.features @@ -10,6 +10,7 @@ FEATURES_PROVIDED += periph_cpuid FEATURES_PROVIDED += periph_eeprom FEATURES_PROVIDED += periph_gpio periph_gpio_irq FEATURES_PROVIDED += periph_pm +FEATURES_PROVIDED += periph_rtc_ms FEATURES_PROVIDED += periph_rtt_set_counter FEATURES_PROVIDED += periph_timer_periodic FEATURES_PROVIDED += periph_wdt diff --git a/cpu/atmega_common/periph/rtc.c b/cpu/atmega_common/periph/rtc.c index 08ad449461..ea80deb851 100644 --- a/cpu/atmega_common/periph/rtc.c +++ b/cpu/atmega_common/periph/rtc.c @@ -102,6 +102,28 @@ int rtc_get_time(struct tm *time) return 0; } +int rtc_get_time_ms(struct tm *time, uint16_t *ms) +{ + uint8_t cnt_before, cnt_after; + + /* loop in case of overflow */ + do { + cnt_before = TCNT2; + + /* prevent compiler from reordering memory access to tm_now, + * including moving it out of the loop + */ + __asm__ volatile ("" : : : "memory"); + *time = tm_now; + + cnt_after = TCNT2; + } while (cnt_before > cnt_after); + + *ms = (cnt_after * 1000UL) >> 8; + + return 0; +} + int rtc_get_alarm(struct tm *time) { *time = tm_alarm;