From 79df157727b9aaa55ed0c9c58a8f6e2e89682f33 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Sun, 19 Dec 2021 16:24:48 +0100 Subject: [PATCH] sys: move rtc utility functions to their own module --- drivers/include/periph/rtc.h | 66 +----------- drivers/periph_common/Kconfig.rtc | 1 + makefiles/features_modules.inc.mk | 5 + sys/Kconfig | 1 + sys/include/rtc_utils.h | 100 ++++++++++++++++++ sys/rtc_utils/Kconfig | 10 ++ sys/rtc_utils/Makefile | 1 + .../rtc.c => sys/rtc_utils/rtc_utils.c | 4 +- tests/unittests/tests-rtc/Makefile.include | 2 +- 9 files changed, 122 insertions(+), 68 deletions(-) create mode 100644 sys/include/rtc_utils.h create mode 100644 sys/rtc_utils/Kconfig create mode 100644 sys/rtc_utils/Makefile rename drivers/periph_common/rtc.c => sys/rtc_utils/rtc_utils.c (98%) diff --git a/drivers/include/periph/rtc.h b/drivers/include/periph/rtc.h index 0071711999..aae0b47856 100644 --- a/drivers/include/periph/rtc.h +++ b/drivers/include/periph/rtc.h @@ -40,6 +40,7 @@ #include #include #include +#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 diff --git a/drivers/periph_common/Kconfig.rtc b/drivers/periph_common/Kconfig.rtc index 4999efebd6..5aa46f0d74 100644 --- a/drivers/periph_common/Kconfig.rtc +++ b/drivers/periph_common/Kconfig.rtc @@ -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 diff --git a/makefiles/features_modules.inc.mk b/makefiles/features_modules.inc.mk index 127fd82732..9c3785c3f3 100644 --- a/makefiles/features_modules.inc.mk +++ b/makefiles/features_modules.inc.mk @@ -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)) diff --git a/sys/Kconfig b/sys/Kconfig index 24e208eabe..93570367eb 100644 --- a/sys/Kconfig +++ b/sys/Kconfig @@ -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" diff --git a/sys/include/rtc_utils.h b/sys/include/rtc_utils.h new file mode 100644 index 0000000000..e48a5d10a8 --- /dev/null +++ b/sys/include/rtc_utils.h @@ -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 + */ + +#ifndef RTC_UTILS_H +#define RTC_UTILS_H + +#include +#include +#include + +#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 */ +/** @} */ diff --git a/sys/rtc_utils/Kconfig b/sys/rtc_utils/Kconfig new file mode 100644 index 0000000000..0a3c88108a --- /dev/null +++ b/sys/rtc_utils/Kconfig @@ -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 diff --git a/sys/rtc_utils/Makefile b/sys/rtc_utils/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/sys/rtc_utils/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/drivers/periph_common/rtc.c b/sys/rtc_utils/rtc_utils.c similarity index 98% rename from drivers/periph_common/rtc.c rename to sys/rtc_utils/rtc_utils.c index e12dce5d94..6c1c792179 100644 --- a/drivers/periph_common/rtc.c +++ b/sys/rtc_utils/rtc_utils.c @@ -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 * diff --git a/tests/unittests/tests-rtc/Makefile.include b/tests/unittests/tests-rtc/Makefile.include index 820d6f659b..6305881969 100644 --- a/tests/unittests/tests-rtc/Makefile.include +++ b/tests/unittests/tests-rtc/Makefile.include @@ -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