From 44e18e8d472db9d4a15a87a2fde3bce84556bf26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20B=C3=B8ndergaard?= Date: Wed, 28 Jun 2023 13:14:16 +0200 Subject: [PATCH] drivers: rtc: rtc_fake: fff rtc driver added MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Can be valuable for unit testing modules accessing the RTC Signed-off-by: Kim Bøndergaard --- drivers/rtc/CMakeLists.txt | 1 + drivers/rtc/Kconfig | 1 + drivers/rtc/Kconfig.fake | 11 +++ drivers/rtc/rtc_fake.c | 96 +++++++++++++++++++++++++++ dts/bindings/rtc/zephyr,fake-rtc.yaml | 10 +++ include/zephyr/drivers/rtc/rtc_fake.h | 46 +++++++++++++ 6 files changed, 165 insertions(+) create mode 100644 drivers/rtc/Kconfig.fake create mode 100644 drivers/rtc/rtc_fake.c create mode 100644 dts/bindings/rtc/zephyr,fake-rtc.yaml create mode 100644 include/zephyr/drivers/rtc/rtc_fake.h diff --git a/drivers/rtc/CMakeLists.txt b/drivers/rtc/CMakeLists.txt index 8e7ba42dc06..a770523f33b 100644 --- a/drivers/rtc/CMakeLists.txt +++ b/drivers/rtc/CMakeLists.txt @@ -12,3 +12,4 @@ zephyr_library_sources_ifdef(CONFIG_RTC_PCF8563 rtc_pcf8563.c) zephyr_library_sources_ifdef(CONFIG_RTC_MOTOROLA_MC146818 rtc_mc146818.c) zephyr_library_sources_ifdef(CONFIG_RTC_STM32 rtc_ll_stm32.c) zephyr_library_sources_ifdef(CONFIG_RTC_SHELL rtc_shell.c) +zephyr_library_sources_ifdef(CONFIG_RTC_FAKE rtc_fake.c) diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig index 5341c8666e5..8df08f38009 100644 --- a/drivers/rtc/Kconfig +++ b/drivers/rtc/Kconfig @@ -42,6 +42,7 @@ config RTC_SHELL RTC Shell commands source "drivers/rtc/Kconfig.emul" +source "drivers/rtc/Kconfig.fake" source "drivers/rtc/Kconfig.pcf8523" source "drivers/rtc/Kconfig.pcf8563" source "drivers/rtc/Kconfig.mc146818" diff --git a/drivers/rtc/Kconfig.fake b/drivers/rtc/Kconfig.fake new file mode 100644 index 00000000000..b969b79aae4 --- /dev/null +++ b/drivers/rtc/Kconfig.fake @@ -0,0 +1,11 @@ +# Fake RTC configuration options +# +# Copyright (c) 2023 Prevas A/S +# SPDX-License-Identifier: Apache-2.0 + +config RTC_FAKE + bool "Fake RTC driver" + default y + depends on DT_HAS_ZEPHYR_FAKE_RTC_ENABLED + help + Enable support for the FFF-based fake RTC driver. diff --git a/drivers/rtc/rtc_fake.c b/drivers/rtc/rtc_fake.c new file mode 100644 index 00000000000..86d66ebd5f8 --- /dev/null +++ b/drivers/rtc/rtc_fake.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2023 Prevas A/S + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT zephyr_fake_rtc + +#include +#include +#include +#include + +#ifdef CONFIG_ZTEST_NEW_API +#include +#endif /* CONFIG_ZTEST_NEW_API */ + +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_set_time, const struct device *, const struct rtc_time *); +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_get_time, const struct device *, struct rtc_time *); + +#ifdef CONFIG_RTC_ALARM +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_get_supported_fields, const struct device *, uint16_t, + uint16_t); +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_time, const struct device *, uint16_t, uint16_t, + constr struct rtc_time *); +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_get_time, const struct device *, uint16_t, uint16_t, + struct rtc_time *); +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_is_pending, const struct device *, uint16_t); +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_callback, const struct device *uint16_t, + rtc_alarm_callback, void *); +#endif /* CONFIG_RTC_ALARM */ + +#ifdef CONFIG_RTC_UPDATE +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_update_set_callback, const struct device *rtc_alarm_callback, + void *); +#endif /* CONFIG_RTC_UPDATE */ + +#ifdef CONFIG_RTC_CALIBRATION +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_set_calibration, const struct device *, int32_t); +DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_get_calibration, const struct device *, int32_t *); +#endif /* CONFIG_RTC_CALIBRATION */ + +#ifdef CONFIG_ZTEST_NEW_API +static void fake_rtc_reset_rule_before(const struct ztest_unit_test *test, void *fixture) +{ + ARG_UNUSED(test); + ARG_UNUSED(fixture); + + RESET_FAKE(rtc_fake_set_time); + RESET_FAKE(rtc_fake_get_time); + +#ifdef CONFIG_RTC_ALARM + RESET_FAKE(rtc_fake_alarm_get_supported_fields); + RESET_FAKE(rtc_fake_alarm_set_time); + RESET_FAKE(rtc_fake_alarm_get_time); + RESET_FAKE(rtc_fake_alarm_is_pending); + RESET_FAKE(rtc_fake_alarm_set_callback); +#endif /* CONFIG_RTC_ALARM */ + +#ifdef CONFIG_RTC_UPDATE + RESET_FAKE(rtc_fake_update_set_callback); +#endif /* CONFIG_RTC_UPDATE */ + +#ifdef CONFIG_RTC_CALIBRATION + RESET_FAKE(rtc_fake_set_calibration); + RESET_FAKE(rtc_fake_get_calibration); +#endif /* CONFIG_RTC_CALIBRATION */ +} + +ZTEST_RULE(fake_rtc_reset_rule, fake_rtc_reset_rule_before, NULL); +#endif /* CONFIG_ZTEST_NEW_API */ + +struct rtc_driver_api rtc_fake_driver_api = { + .set_time = rtc_fake_set_time, + .get_time = rtc_fake_get_time, +#ifdef CONFIG_RTC_ALARM + .alarm_get_supported_fields = rtc_fake_alarm_get_supported_fields, + .alarm_set_time = rtc_fake_alarm_set_time, + .alarm_get_time = rtc_fake_alarm_get_time, + .alarm_is_pending = rtc_fake_alarm_is_pending, + .alarm_set_callback = rtc_fake_alarm_set_callback, +#endif /* CONFIG_RTC_ALARM */ +#ifdef CONFIG_RTC_UPDATE + .update_set_callback = rtc_fake_update_set_callback, +#endif /* CONFIG_RTC_UPDATE */ +#ifdef CONFIG_RTC_CALIBRATION + .set_calibration = rtc_fake_set_calibration, + .get_calibration = rtc_fake_get_calibration, +#endif /* CONFIG_RTC_CALIBRATION */ +}; + +#define RTC_FAKE_DEVICE_INIT(inst) \ + DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, NULL, POST_KERNEL, CONFIG_RTC_INIT_PRIORITY, \ + &rtc_fake_driver_api); + +DT_INST_FOREACH_STATUS_OKAY(RTC_FAKE_DEVICE_INIT); diff --git a/dts/bindings/rtc/zephyr,fake-rtc.yaml b/dts/bindings/rtc/zephyr,fake-rtc.yaml new file mode 100644 index 00000000000..733de7bb07a --- /dev/null +++ b/dts/bindings/rtc/zephyr,fake-rtc.yaml @@ -0,0 +1,10 @@ +# Copyright (c) 2023 Prevas A/S +# SPDX-License-Identifier: Apache-2.0 + +description: | + This binding provides a fake RTC controller for use as either a stub or a mock in Zephyr + testing. + +compatible: "zephyr,fake-rtc" + +include: rtc-device.yaml diff --git a/include/zephyr/drivers/rtc/rtc_fake.h b/include/zephyr/drivers/rtc/rtc_fake.h new file mode 100644 index 00000000000..ad3a2751230 --- /dev/null +++ b/include/zephyr/drivers/rtc/rtc_fake.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Prevas A/S + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_DRIVERS_RTC_RTC_FAKE_H_ +#define ZEPHYR_INCLUDE_DRIVERS_RTC_RTC_FAKE_H_ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_set_time, const struct device *, const struct rtc_time *); +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_get_time, const struct device *, struct rtc_time *); + +#ifdef CONFIG_RTC_ALARM +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_get_supported_fields, const struct device *, uint16_t, + uint16_t); +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_time, const struct device *, uint16_t, uint16_t, + constr struct rtc_time *); +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_get_time, const struct device *, uint16_t, uint16_t, + struct rtc_time *); +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_is_pending, const struct device *, uint16_t); +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_callback, const struct device *uint16_t, + rtc_alarm_callback, void *); +#endif /* CONFIG_RTC_ALARM */ + +#ifdef CONFIG_RTC_UPDATE +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_update_set_callback, const struct device *rtc_alarm_callback, + void *); +#endif /* CONFIG_RTC_UPDATE */ + +#ifdef CONFIG_RTC_CALIBRATION +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_set_calibration, const struct device *, int32_t); +DECLARE_FAKE_VALUE_FUNC(int, rtc_fake_get_calibration, const struct device *, int32_t *); +#endif /* CONFIG_RTC_CALIBRATION */ + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_DRIVERS_RTC_RTC_FAKE_H_ */