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

drivers/rtt_rtc: implement rtc_get_time_ms()

RTC on RTT usually runs at a frequency greater than 1 Hz, so sub-second
precision is available.
Add a function to get the current RTC timestamp together with it's sub-second
component.
This commit is contained in:
Benjamin Valentin 2021-04-19 13:56:57 +02:00 committed by Benjamin Valentin
parent 0397cab91c
commit dcebc7d480
3 changed files with 33 additions and 0 deletions

View File

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

View File

@ -0,0 +1,2 @@
FEATURES_PROVIDED += periph_rtc
FEATURES_PROVIDED += periph_rtc_ms

View File

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