1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-28 22:49:47 +01:00

sys: move rtc utility functions to their own module

This commit is contained in:
Alexandre Abadie 2021-12-19 16:24:48 +01:00
parent 749efa507d
commit 79df157727
No known key found for this signature in database
GPG Key ID: 1C919A403CAE1405
9 changed files with 122 additions and 68 deletions

View File

@ -40,6 +40,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include "rtc_utils.h"
#include "periph_conf.h"
#ifdef __cplusplus
@ -143,71 +144,6 @@ void rtc_poweron(void);
*/
void rtc_poweroff(void);
/**
* @brief Normalize the time struct
*
* @note The function modifies the fields of the tm structure as follows:
* If structure members are outside their valid interval,
* they will be normalized.
* So that, for example, 40 October is changed into 9 November.
*
* If RTC_NORMALIZE_COMPAT is 1 `tm_wday` and `tm_yday` are set
* to values determined from the contents of the other fields.
*
* @param time Pointer to the struct to normalize.
*/
void rtc_tm_normalize(struct tm *time);
/**
* @brief Compare two time structs.
*
* @pre The time structs @p a and @p b are assumed to be normalized.
* Use @ref rtc_tm_normalize to normalize a struct tm that has been
* manually edited.
*
* @param[in] a The first time struct.
* @param[in] b The second time struct.
*
* @return an integer < 0 if a is earlier than b
* @return an integer > 0 if a is later than b
* @return 0 if a and b are equal
*/
int rtc_tm_compare(const struct tm *a, const struct tm *b);
/**
* @brief Convert time struct into timestamp.
*
* @pre The time structs @p a and @p b are assumed to be normalized.
* Use @ref rtc_tm_normalize to normalize a struct tm that has been
* manually edited.
*
* @param[in] t The time struct to convert
*
* @return elapsed seconds since `RIOT_EPOCH`
*/
uint32_t rtc_mktime(struct tm *t);
/**
* @brief Converts an RTC timestamp into a time struct.
*
* @param[in] time elapsed seconds since `RIOT_EPOCH`
* @param[out] t the corresponding timestamp
*/
void rtc_localtime(uint32_t time, struct tm *t);
/**
* @brief Verify that a time struct @p t contains valid data.
*
* @note This function checks whether the fields of the
* struct @p t are positive and within the bounds set
* by @ref rtc_tm_normalize.
*
* @param[in] t The struct to be checked.
*
* @return true when valid, false if not
*/
bool rtc_tm_valid(const struct tm *t);
#ifdef __cplusplus
}
#endif

View File

@ -9,6 +9,7 @@ menuconfig MODULE_PERIPH_RTC
bool "RTC peripheral driver"
depends on HAS_PERIPH_RTC
select MODULE_PERIPH_COMMON
select MODULE_RTC_UTILS
if MODULE_PERIPH_RTC

View File

@ -51,6 +51,11 @@ ifneq (,$(filter periph_%, $(USEMODULE)))
USEMODULE += periph_common
endif
# include rtc_utils if periph_rtc is used
ifneq (,$(filter periph_rtc, $(USEMODULE)))
USEMODULE += rtc_utils
endif
# select cortexm_svc pseudomodule if the corresponding feature is used
USEMODULE += $(filter cortexm_svc, $(FEATURES_USED))

View File

@ -70,6 +70,7 @@ rsource "pm_layered/Kconfig"
rsource "progress_bar/Kconfig"
rsource "ps/Kconfig"
rsource "random/Kconfig"
rsource "rtc_utils/Kconfig"
rsource "saul_reg/Kconfig"
rsource "schedstatistics/Kconfig"
rsource "sema/Kconfig"

100
sys/include/rtc_utils.h Normal file
View File

@ -0,0 +1,100 @@
/*
* Copyright (C) 2019 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @defgroup sys_rtc_utils RTC helpers
* @ingroup sys
* @brief Common RTC helper functions
* @{
* @file
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef RTC_UTILS_H
#define RTC_UTILS_H
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Normalize the time struct
*
* @note The function modifies the fields of the tm structure as follows:
* If structure members are outside their valid interval,
* they will be normalized.
* So that, for example, 40 October is changed into 9 November.
*
* If RTC_NORMALIZE_COMPAT is 1 `tm_wday` and `tm_yday` are set
* to values determined from the contents of the other fields.
*
* @param time Pointer to the struct to normalize.
*/
void rtc_tm_normalize(struct tm *time);
/**
* @brief Compare two time structs.
*
* @pre The time structs @p a and @p b are assumed to be normalized.
* Use @ref rtc_tm_normalize to normalize a struct tm that has been
* manually edited.
*
* @param[in] a The first time struct.
* @param[in] b The second time struct.
*
* @return an integer < 0 if a is earlier than b
* @return an integer > 0 if a is later than b
* @return 0 if a and b are equal
*/
int rtc_tm_compare(const struct tm *a, const struct tm *b);
/**
* @brief Convert time struct into timestamp.
*
* @pre The time structs @p a and @p b are assumed to be normalized.
* Use @ref rtc_tm_normalize to normalize a struct tm that has been
* manually edited.
*
* @param[in] t The time struct to convert
*
* @return elapsed seconds since `RIOT_EPOCH`
*/
uint32_t rtc_mktime(struct tm *t);
/**
* @brief Converts an RTC timestamp into a time struct.
*
* @param[in] time elapsed seconds since `RIOT_EPOCH`
* @param[out] t the corresponding timestamp
*/
void rtc_localtime(uint32_t time, struct tm *t);
/**
* @brief Verify that a time struct @p t contains valid data.
*
* @note This function checks whether the fields of the
* struct @p t are positive and within the bounds set
* by @ref rtc_tm_normalize.
*
* @param[in] t The struct to be checked.
*
* @return true when valid, false if not
*/
bool rtc_tm_valid(const struct tm *t);
#ifdef __cplusplus
}
#endif
#endif /* RTC_UTILS_H */
/** @} */

10
sys/rtc_utils/Kconfig Normal file
View File

@ -0,0 +1,10 @@
# Copyright (c) 2021 Inria
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#
config MODULE_RTC_UTILS
bool "RTC helper functions"
depends on TEST_KCONFIG

1
sys/rtc_utils/Makefile Normal file
View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -7,11 +7,11 @@
*/
/**
* @ingroup drivers_periph_rtc
* @ingroup sys_rtc_utils
* @{
*
* @file
* @brief common RTC function fallback implementations
* @brief common RTC helper functions
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*

View File

@ -1,6 +1,6 @@
CFLAGS += -DRTC_NORMALIZE_COMPAT=1
USEMODULE += periph_rtc_common
USEMODULE += rtc_utils
# AVR/ATmega uses `int8_t` for `struct tm` which leads to integer overflows
# in these tests