drivers: rtc: rtc_fake: fff rtc driver added

Can be valuable for unit testing modules accessing the RTC

Signed-off-by: Kim Bøndergaard <kim.bondergaard@prevas.dk>
This commit is contained in:
Kim Bøndergaard 2023-06-28 13:14:16 +02:00 committed by Fabio Baltieri
commit 44e18e8d47
6 changed files with 165 additions and 0 deletions

View file

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

View file

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

11
drivers/rtc/Kconfig.fake Normal file
View file

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

96
drivers/rtc/rtc_fake.c Normal file
View file

@ -0,0 +1,96 @@
/*
* Copyright (c) 2023 Prevas A/S
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT zephyr_fake_rtc
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/rtc.h>
#include <zephyr/drivers/rtc/rtc_fake.h>
#ifdef CONFIG_ZTEST_NEW_API
#include <zephyr/ztest.h>
#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);

View file

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

View file

@ -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 <zephyr/drivers/rtc.h>
#include <zephyr/fff.h>
#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_ */