1 /*
2 * Copyright (c) 2023 Prevas A/S
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT zephyr_fake_rtc
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/device.h>
11 #include <zephyr/drivers/rtc.h>
12 #include <zephyr/drivers/rtc/rtc_fake.h>
13
14 #ifdef CONFIG_ZTEST
15 #include <zephyr/ztest.h>
16 #endif /* CONFIG_ZTEST */
17
18 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_set_time, const struct device *, const struct rtc_time *);
19 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_get_time, const struct device *, struct rtc_time *);
20
21 #ifdef CONFIG_RTC_ALARM
22 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_get_supported_fields, const struct device *, uint16_t,
23 uint16_t);
24 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_time, const struct device *, uint16_t, uint16_t,
25 constr struct rtc_time *);
26 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_get_time, const struct device *, uint16_t, uint16_t,
27 struct rtc_time *);
28 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_is_pending, const struct device *, uint16_t);
29 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_callback, const struct device *uint16_t,
30 rtc_alarm_callback, void *);
31 #endif /* CONFIG_RTC_ALARM */
32
33 #ifdef CONFIG_RTC_UPDATE
34 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_update_set_callback, const struct device *rtc_alarm_callback,
35 void *);
36 #endif /* CONFIG_RTC_UPDATE */
37
38 #ifdef CONFIG_RTC_CALIBRATION
39 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_set_calibration, const struct device *, int32_t);
40 DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_get_calibration, const struct device *, int32_t *);
41 #endif /* CONFIG_RTC_CALIBRATION */
42
43 #ifdef CONFIG_ZTEST
fake_rtc_reset_rule_before(const struct ztest_unit_test * test,void * fixture)44 static void fake_rtc_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
45 {
46 ARG_UNUSED(test);
47 ARG_UNUSED(fixture);
48
49 RESET_FAKE(rtc_fake_set_time);
50 RESET_FAKE(rtc_fake_get_time);
51
52 #ifdef CONFIG_RTC_ALARM
53 RESET_FAKE(rtc_fake_alarm_get_supported_fields);
54 RESET_FAKE(rtc_fake_alarm_set_time);
55 RESET_FAKE(rtc_fake_alarm_get_time);
56 RESET_FAKE(rtc_fake_alarm_is_pending);
57 RESET_FAKE(rtc_fake_alarm_set_callback);
58 #endif /* CONFIG_RTC_ALARM */
59
60 #ifdef CONFIG_RTC_UPDATE
61 RESET_FAKE(rtc_fake_update_set_callback);
62 #endif /* CONFIG_RTC_UPDATE */
63
64 #ifdef CONFIG_RTC_CALIBRATION
65 RESET_FAKE(rtc_fake_set_calibration);
66 RESET_FAKE(rtc_fake_get_calibration);
67 #endif /* CONFIG_RTC_CALIBRATION */
68 }
69
70 ZTEST_RULE(fake_rtc_reset_rule, fake_rtc_reset_rule_before, NULL);
71 #endif /* CONFIG_ZTEST */
72
73 static const struct rtc_driver_api rtc_fake_driver_api = {
74 .set_time = rtc_fake_set_time,
75 .get_time = rtc_fake_get_time,
76 #ifdef CONFIG_RTC_ALARM
77 .alarm_get_supported_fields = rtc_fake_alarm_get_supported_fields,
78 .alarm_set_time = rtc_fake_alarm_set_time,
79 .alarm_get_time = rtc_fake_alarm_get_time,
80 .alarm_is_pending = rtc_fake_alarm_is_pending,
81 .alarm_set_callback = rtc_fake_alarm_set_callback,
82 #endif /* CONFIG_RTC_ALARM */
83 #ifdef CONFIG_RTC_UPDATE
84 .update_set_callback = rtc_fake_update_set_callback,
85 #endif /* CONFIG_RTC_UPDATE */
86 #ifdef CONFIG_RTC_CALIBRATION
87 .set_calibration = rtc_fake_set_calibration,
88 .get_calibration = rtc_fake_get_calibration,
89 #endif /* CONFIG_RTC_CALIBRATION */
90 };
91
92 #define RTC_FAKE_DEVICE_INIT(inst) \
93 DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, NULL, POST_KERNEL, CONFIG_RTC_INIT_PRIORITY, \
94 &rtc_fake_driver_api);
95
96 DT_INST_FOREACH_STATUS_OKAY(RTC_FAKE_DEVICE_INIT);
97