Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 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 | |
Anas Nashif | 85c4703 | 2023-10-09 23:39:26 +0000 | [diff] [blame] | 14 | #ifdef CONFIG_ZTEST |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 15 | #include <zephyr/ztest.h> |
Anas Nashif | 85c4703 | 2023-10-09 23:39:26 +0000 | [diff] [blame] | 16 | #endif /* CONFIG_ZTEST */ |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 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, |
Bjarki Arge Andreasen | 3f68180 | 2024-08-07 10:05:41 +0200 | [diff] [blame] | 23 | uint16_t *); |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 24 | DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_time, const struct device *, uint16_t, uint16_t, |
Bjarki Arge Andreasen | 3f68180 | 2024-08-07 10:05:41 +0200 | [diff] [blame] | 25 | const struct rtc_time *); |
| 26 | DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_get_time, const struct device *, uint16_t, uint16_t *, |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 27 | struct rtc_time *); |
| 28 | DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_is_pending, const struct device *, uint16_t); |
Bjarki Arge Andreasen | 3f68180 | 2024-08-07 10:05:41 +0200 | [diff] [blame] | 29 | DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_alarm_set_callback, const struct device *, uint16_t, |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 30 | rtc_alarm_callback, void *); |
| 31 | #endif /* CONFIG_RTC_ALARM */ |
| 32 | |
| 33 | #ifdef CONFIG_RTC_UPDATE |
Bjarki Arge Andreasen | 3f68180 | 2024-08-07 10:05:41 +0200 | [diff] [blame] | 34 | DEFINE_FAKE_VALUE_FUNC(int, rtc_fake_update_set_callback, const struct device *, |
| 35 | rtc_update_callback, void *); |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 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 | |
Anas Nashif | 85c4703 | 2023-10-09 23:39:26 +0000 | [diff] [blame] | 43 | #ifdef CONFIG_ZTEST |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 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); |
Anas Nashif | 85c4703 | 2023-10-09 23:39:26 +0000 | [diff] [blame] | 71 | #endif /* CONFIG_ZTEST */ |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 72 | |
Pisit Sawangvonganan | 250d50e | 2023-12-25 00:10:03 +0700 | [diff] [blame] | 73 | static const struct rtc_driver_api rtc_fake_driver_api = { |
Kim Bøndergaard | 44e18e8 | 2023-06-28 13:14:16 +0200 | [diff] [blame] | 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); |