Skip to content

Commit 5343ae6

Browse files
bjarki-andreasennashif
authored andcommitted
tests: drivers: rtc: rtc_api: add config for alarm time mask
RTCs support a variety of combinations of alarm time fields, set by the alarm time mask. Until now, alarm tests have selected only the minute and hour fields, as these are always supported, but some RTCs require setting every supported field when setting the alarm time, while other RTCs don't support setting every field. To support all RTCs in the test suite, a configuration has been added which makes the alarm time mask configurable. Boards can now define the specific alarm time mask they want to test within their boards .conf files. Additionally, the alarm tests have been refactored to not depend on the time.h library to determine the struct rtc_time times to set as these are constant, so they are now provided as const structs instead. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent 90ab94f commit 5343ae6

File tree

3 files changed

+148
-111
lines changed

3 files changed

+148
-111
lines changed

tests/drivers/rtc/rtc_api/Kconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) 2024 Bjarki Arge Andreasen
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
source "Kconfig.zephyr"
5+
6+
if RTC_ALARM
7+
8+
config TEST_RTC_ALARM_TIME_MASK
9+
int "Alarm fields to set"
10+
default 6
11+
range 1 511
12+
help
13+
Set the RTC_ALARM_TIME_MASK_ bitmask to use when setting
14+
RTC alarms during testing.
15+
16+
endif # RTC_ALARM

tests/drivers/rtc/rtc_api/src/test_alarm.c

Lines changed: 99 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,67 @@
99
#include <zephyr/device.h>
1010
#include <zephyr/drivers/rtc.h>
1111
#include <zephyr/sys/atomic.h>
12-
#include <zephyr/sys/timeutil.h>
1312
#include <zephyr/sys/util.h>
1413

15-
#include <time.h>
16-
17-
/* Fri Jan 01 2021 13:29:50 GMT+0000 */
18-
#define RTC_TEST_ALARM_SET_TIME (1609507790)
1914
#define RTC_TEST_ALARM_TEST_NOT_PENDING_DELAY (3)
2015
#define RTC_TEST_ALARM_TEST_PENDING_DELAY (10)
21-
#define RTC_TEST_ALARM_TIME_MINUTE (30)
22-
#define RTC_TEST_ALARM_TIME_HOUR (13)
2316

2417
static const struct device *rtc = DEVICE_DT_GET(DT_ALIAS(rtc));
2518
static const uint16_t alarms_count = DT_PROP(DT_ALIAS(rtc), alarms_count);
19+
static const uint16_t test_alarm_time_mask_set = CONFIG_TEST_RTC_ALARM_TIME_MASK;
20+
21+
/* Fri Jan 01 2021 13:29:50 GMT+0000 */
22+
static const struct rtc_time test_rtc_time_set = {
23+
.tm_sec = 50,
24+
.tm_min = 29,
25+
.tm_hour = 13,
26+
.tm_mday = 1,
27+
.tm_mon = 0,
28+
.tm_year = 121,
29+
.tm_wday = 5,
30+
.tm_yday = 1,
31+
.tm_isdst = -1,
32+
.tm_nsec = 0,
33+
};
34+
35+
/* Fri Jan 01 2021 13:30:00 GMT+0000 */
36+
static const struct rtc_time test_alarm_time_set = {
37+
.tm_sec = 0,
38+
.tm_min = 30,
39+
.tm_hour = 13,
40+
.tm_mday = 1,
41+
.tm_mon = 0,
42+
.tm_year = 121,
43+
.tm_wday = 5,
44+
.tm_yday = 1,
45+
.tm_isdst = -1,
46+
.tm_nsec = 0,
47+
};
48+
49+
static const struct rtc_time test_alarm_time_invalid = {
50+
.tm_sec = 70,
51+
.tm_min = 70,
52+
.tm_hour = 25,
53+
.tm_mday = 35,
54+
.tm_mon = 15,
55+
.tm_year = 8000,
56+
.tm_wday = 8,
57+
.tm_yday = 370,
58+
.tm_nsec = INT32_MAX,
59+
};
60+
61+
static const uint16_t test_alarm_time_masks[] = {
62+
RTC_ALARM_TIME_MASK_SECOND, RTC_ALARM_TIME_MASK_MINUTE,
63+
RTC_ALARM_TIME_MASK_HOUR, RTC_ALARM_TIME_MASK_MONTHDAY,
64+
RTC_ALARM_TIME_MASK_MONTH, RTC_ALARM_TIME_MASK_YEAR,
65+
RTC_ALARM_TIME_MASK_WEEKDAY, RTC_ALARM_TIME_MASK_YEARDAY,
66+
RTC_ALARM_TIME_MASK_NSEC
67+
};
2668

2769
ZTEST(rtc_api, test_alarm)
2870
{
2971
int ret;
30-
time_t timer_set;
31-
struct rtc_time time_set;
32-
struct rtc_time alarm_time_set;
3372
uint16_t alarm_time_mask_supported;
34-
uint16_t alarm_time_mask_set;
3573
struct rtc_time alarm_time_get;
3674
uint16_t alarm_time_mask_get;
3775

@@ -53,29 +91,13 @@ ZTEST(rtc_api, test_alarm)
5391
/* Every supported alarm field should reject invalid values. */
5492
for (uint16_t i = 0; i < alarms_count; i++) {
5593
ret = rtc_alarm_get_supported_fields(rtc, i, &alarm_time_mask_supported);
56-
5794
zassert_ok(ret, "Failed to get supported alarm %d fields", i);
5895

59-
alarm_time_set = (struct rtc_time) {
60-
.tm_sec = 70,
61-
.tm_min = 70,
62-
.tm_hour = 25,
63-
.tm_mday = 35,
64-
.tm_mon = 15,
65-
.tm_year = 8000,
66-
.tm_wday = 8,
67-
.tm_yday = 370,
68-
.tm_nsec = INT32_MAX,
69-
};
70-
uint16_t masks[] = {RTC_ALARM_TIME_MASK_SECOND, RTC_ALARM_TIME_MASK_MINUTE,
71-
RTC_ALARM_TIME_MASK_HOUR, RTC_ALARM_TIME_MASK_MONTHDAY,
72-
RTC_ALARM_TIME_MASK_MONTH, RTC_ALARM_TIME_MASK_YEAR,
73-
RTC_ALARM_TIME_MASK_WEEKDAY, RTC_ALARM_TIME_MASK_YEARDAY,
74-
RTC_ALARM_TIME_MASK_NSEC};
75-
ARRAY_FOR_EACH(masks, j)
96+
ARRAY_FOR_EACH(test_alarm_time_masks, j)
7697
{
77-
if (masks[j] & alarm_time_mask_supported) {
78-
ret = rtc_alarm_set_time(rtc, i, masks[j], &alarm_time_set);
98+
if (test_alarm_time_masks[j] & alarm_time_mask_supported) {
99+
ret = rtc_alarm_set_time(rtc, i, test_alarm_time_masks[j],
100+
&test_alarm_time_invalid);
79101
zassert_equal(
80102
-EINVAL, ret,
81103
"%s: RTC should reject invalid alarm %d time in field %zu.",
@@ -87,61 +109,79 @@ ZTEST(rtc_api, test_alarm)
87109
/* Validate alarms supported fields */
88110
for (uint16_t i = 0; i < alarms_count; i++) {
89111
ret = rtc_alarm_get_supported_fields(rtc, i, &alarm_time_mask_supported);
90-
91112
zassert_ok(ret, "Failed to get supported alarm %d fields", i);
92113

93-
/* Skip test if alarm does not support the minute and hour fields */
94-
if (((RTC_ALARM_TIME_MASK_MINUTE & alarm_time_mask_supported) == 0) ||
95-
((RTC_TEST_ALARM_TIME_HOUR & alarm_time_mask_supported) == 0)) {
96-
ztest_test_skip();
97-
}
114+
ret = (test_alarm_time_mask_set & (~alarm_time_mask_supported)) ? -EINVAL : 0;
115+
zassert_ok(ret, "Configured alarm time fields to set are not supported");
98116
}
99117

100-
/* Set alarm time */
101-
alarm_time_set.tm_min = RTC_TEST_ALARM_TIME_MINUTE;
102-
alarm_time_set.tm_hour = RTC_TEST_ALARM_TIME_HOUR;
103-
alarm_time_mask_set = (RTC_ALARM_TIME_MASK_MINUTE | RTC_ALARM_TIME_MASK_HOUR);
104-
105118
for (uint16_t i = 0; i < alarms_count; i++) {
106-
ret = rtc_alarm_set_time(rtc, i, alarm_time_mask_set, &alarm_time_set);
107-
119+
ret = rtc_alarm_set_time(rtc, i, test_alarm_time_mask_set, &test_alarm_time_set);
108120
zassert_ok(ret, "Failed to set alarm %d time", i);
109121
}
110122

111123
/* Validate alarm time */
112124
for (uint16_t i = 0; i < alarms_count; i++) {
113125
ret = rtc_alarm_get_time(rtc, i, &alarm_time_mask_get, &alarm_time_get);
114-
115126
zassert_ok(ret, "Failed to set alarm %d time", i);
116127

117-
zassert_equal(alarm_time_mask_get, alarm_time_mask_set,
128+
zassert_equal(alarm_time_mask_get, test_alarm_time_mask_set,
118129
"Incorrect alarm %d time mask", i);
119130

120-
zassert_equal(alarm_time_get.tm_min, alarm_time_set.tm_min,
121-
"Incorrect alarm %d time minute field", i);
131+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_SECOND) {
132+
zassert_equal(alarm_time_get.tm_sec, test_alarm_time_set.tm_sec,
133+
"Incorrect alarm %d tm_sec field", i);
134+
}
122135

123-
zassert_equal(alarm_time_get.tm_hour, alarm_time_set.tm_hour,
124-
"Incorrect alarm %d time hour field", i);
125-
}
136+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_MINUTE) {
137+
zassert_equal(alarm_time_get.tm_min, test_alarm_time_set.tm_min,
138+
"Incorrect alarm %d tm_min field", i);
139+
}
126140

127-
/* Initialize RTC time to set */
128-
timer_set = RTC_TEST_ALARM_SET_TIME;
141+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_HOUR) {
142+
zassert_equal(alarm_time_get.tm_hour, test_alarm_time_set.tm_hour,
143+
"Incorrect alarm %d tm_hour field", i);
144+
}
129145

130-
gmtime_r(&timer_set, (struct tm *)(&time_set));
146+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_MONTHDAY) {
147+
zassert_equal(alarm_time_get.tm_mday, test_alarm_time_set.tm_mday,
148+
"Incorrect alarm %d tm_mday field", i);
149+
}
131150

132-
time_set.tm_isdst = -1;
133-
time_set.tm_nsec = 0;
151+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_MONTH) {
152+
zassert_equal(alarm_time_get.tm_mon, test_alarm_time_set.tm_mon,
153+
"Incorrect alarm %d tm_mon field", i);
154+
}
155+
156+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_YEAR) {
157+
zassert_equal(alarm_time_get.tm_year, test_alarm_time_set.tm_year,
158+
"Incorrect alarm %d tm_year field", i);
159+
}
160+
161+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_WEEKDAY) {
162+
zassert_equal(alarm_time_get.tm_wday, test_alarm_time_set.tm_wday,
163+
"Incorrect alarm %d tm_wday field", i);
164+
}
165+
166+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_YEARDAY) {
167+
zassert_equal(alarm_time_get.tm_yday, test_alarm_time_set.tm_yday,
168+
"Incorrect alarm %d tm_yday field", i);
169+
}
170+
171+
if (test_alarm_time_mask_set & RTC_ALARM_TIME_MASK_NSEC) {
172+
zassert_equal(alarm_time_get.tm_nsec, test_alarm_time_set.tm_nsec,
173+
"Incorrect alarm %d tm_nsec field", i);
174+
}
175+
}
134176

135177
for (uint8_t k = 0; k < 2; k++) {
136178
/* Set RTC time */
137-
ret = rtc_set_time(rtc, &time_set);
138-
179+
ret = rtc_set_time(rtc, &test_rtc_time_set);
139180
zassert_ok(ret, "Failed to set time");
140181

141182
/* Clear alarm pending status */
142183
for (uint16_t i = 0; i < alarms_count; i++) {
143184
ret = rtc_alarm_is_pending(rtc, i);
144-
145185
zassert_true(ret > -1, "Failed to clear alarm %d pending status", i);
146186
}
147187

@@ -151,7 +191,6 @@ ZTEST(rtc_api, test_alarm)
151191
/* Validate alarm are not pending */
152192
for (uint16_t i = 0; i < alarms_count; i++) {
153193
ret = rtc_alarm_is_pending(rtc, i);
154-
155194
zassert_ok(ret, "Alarm %d should not be pending", i);
156195
}
157196

@@ -161,19 +200,16 @@ ZTEST(rtc_api, test_alarm)
161200
/* Validate alarm is pending */
162201
for (uint16_t i = 0; i < alarms_count; i++) {
163202
ret = rtc_alarm_is_pending(rtc, i);
164-
165203
zassert_equal(ret, 1, "Alarm %d should be pending", i);
166204
}
167205
}
168206

169207
/* Disable and clear alarms */
170208
for (uint16_t i = 0; i < alarms_count; i++) {
171209
ret = rtc_alarm_set_time(rtc, i, 0, NULL);
172-
173210
zassert_ok(ret, "Failed to disable alarm %d", i);
174211

175212
ret = rtc_alarm_is_pending(rtc, i);
176-
177213
zassert_true(ret > -1, "Failed to clear alarm %d pending state", i);
178214
}
179215
}

0 commit comments

Comments
 (0)