9
9
#include <zephyr/device.h>
10
10
#include <zephyr/drivers/rtc.h>
11
11
#include <zephyr/sys/atomic.h>
12
- #include <zephyr/sys/timeutil.h>
13
12
#include <zephyr/sys/util.h>
14
13
15
- #include <time.h>
16
-
17
- /* Fri Jan 01 2021 13:29:50 GMT+0000 */
18
- #define RTC_TEST_ALARM_SET_TIME (1609507790)
19
14
#define RTC_TEST_ALARM_TEST_NOT_PENDING_DELAY (3)
20
15
#define RTC_TEST_ALARM_TEST_PENDING_DELAY (10)
21
- #define RTC_TEST_ALARM_TIME_MINUTE (30)
22
- #define RTC_TEST_ALARM_TIME_HOUR (13)
23
16
24
17
static const struct device * rtc = DEVICE_DT_GET (DT_ALIAS (rtc ));
25
18
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
+ };
26
68
27
69
ZTEST (rtc_api , test_alarm )
28
70
{
29
71
int ret ;
30
- time_t timer_set ;
31
- struct rtc_time time_set ;
32
- struct rtc_time alarm_time_set ;
33
72
uint16_t alarm_time_mask_supported ;
34
- uint16_t alarm_time_mask_set ;
35
73
struct rtc_time alarm_time_get ;
36
74
uint16_t alarm_time_mask_get ;
37
75
@@ -53,29 +91,13 @@ ZTEST(rtc_api, test_alarm)
53
91
/* Every supported alarm field should reject invalid values. */
54
92
for (uint16_t i = 0 ; i < alarms_count ; i ++ ) {
55
93
ret = rtc_alarm_get_supported_fields (rtc , i , & alarm_time_mask_supported );
56
-
57
94
zassert_ok (ret , "Failed to get supported alarm %d fields" , i );
58
95
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 )
76
97
{
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 );
79
101
zassert_equal (
80
102
- EINVAL , ret ,
81
103
"%s: RTC should reject invalid alarm %d time in field %zu." ,
@@ -87,61 +109,79 @@ ZTEST(rtc_api, test_alarm)
87
109
/* Validate alarms supported fields */
88
110
for (uint16_t i = 0 ; i < alarms_count ; i ++ ) {
89
111
ret = rtc_alarm_get_supported_fields (rtc , i , & alarm_time_mask_supported );
90
-
91
112
zassert_ok (ret , "Failed to get supported alarm %d fields" , i );
92
113
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" );
98
116
}
99
117
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
-
105
118
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 );
108
120
zassert_ok (ret , "Failed to set alarm %d time" , i );
109
121
}
110
122
111
123
/* Validate alarm time */
112
124
for (uint16_t i = 0 ; i < alarms_count ; i ++ ) {
113
125
ret = rtc_alarm_get_time (rtc , i , & alarm_time_mask_get , & alarm_time_get );
114
-
115
126
zassert_ok (ret , "Failed to set alarm %d time" , i );
116
127
117
- zassert_equal (alarm_time_mask_get , alarm_time_mask_set ,
128
+ zassert_equal (alarm_time_mask_get , test_alarm_time_mask_set ,
118
129
"Incorrect alarm %d time mask" , i );
119
130
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
+ }
122
135
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
+ }
126
140
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
+ }
129
145
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
+ }
131
150
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
+ }
134
176
135
177
for (uint8_t k = 0 ; k < 2 ; k ++ ) {
136
178
/* Set RTC time */
137
- ret = rtc_set_time (rtc , & time_set );
138
-
179
+ ret = rtc_set_time (rtc , & test_rtc_time_set );
139
180
zassert_ok (ret , "Failed to set time" );
140
181
141
182
/* Clear alarm pending status */
142
183
for (uint16_t i = 0 ; i < alarms_count ; i ++ ) {
143
184
ret = rtc_alarm_is_pending (rtc , i );
144
-
145
185
zassert_true (ret > -1 , "Failed to clear alarm %d pending status" , i );
146
186
}
147
187
@@ -151,7 +191,6 @@ ZTEST(rtc_api, test_alarm)
151
191
/* Validate alarm are not pending */
152
192
for (uint16_t i = 0 ; i < alarms_count ; i ++ ) {
153
193
ret = rtc_alarm_is_pending (rtc , i );
154
-
155
194
zassert_ok (ret , "Alarm %d should not be pending" , i );
156
195
}
157
196
@@ -161,19 +200,16 @@ ZTEST(rtc_api, test_alarm)
161
200
/* Validate alarm is pending */
162
201
for (uint16_t i = 0 ; i < alarms_count ; i ++ ) {
163
202
ret = rtc_alarm_is_pending (rtc , i );
164
-
165
203
zassert_equal (ret , 1 , "Alarm %d should be pending" , i );
166
204
}
167
205
}
168
206
169
207
/* Disable and clear alarms */
170
208
for (uint16_t i = 0 ; i < alarms_count ; i ++ ) {
171
209
ret = rtc_alarm_set_time (rtc , i , 0 , NULL );
172
-
173
210
zassert_ok (ret , "Failed to disable alarm %d" , i );
174
211
175
212
ret = rtc_alarm_is_pending (rtc , i );
176
-
177
213
zassert_true (ret > -1 , "Failed to clear alarm %d pending state" , i );
178
214
}
179
215
}
0 commit comments