1 /*
2 * Copyright (c) 2018, Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/counter.h>
8 #include <zephyr/drivers/rtc/maxim_ds3231.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(test);
13
14 static struct k_sem top_cnt_sem;
15 static struct k_sem alarm_cnt_sem;
16 static struct k_poll_signal sync_sig;
17
18 static void top_handler(const struct device *dev, void *user_data);
19
20 void *exp_user_data = (void *)199;
21
22 #define COUNTER_PERIOD_US USEC_PER_SEC
23
24 struct counter_alarm_cfg alarm_cfg;
25 struct counter_alarm_cfg alarm_cfg2;
26
27 static const struct device *const devices[] = {
28 DEVICE_DT_GET(DT_NODELABEL(ds3231)),
29 };
30 typedef void (*counter_test_func_t)(const struct device *dev);
31
32 typedef bool (*counter_capability_func_t)(const struct device *dev);
33
34
counter_setup_instance(const struct device * dev)35 static void counter_setup_instance(const struct device *dev)
36 {
37 k_sem_reset(&alarm_cnt_sem);
38 }
39
counter_tear_down_instance(const struct device * dev)40 static void counter_tear_down_instance(const struct device *dev)
41 {
42 int err;
43 struct counter_top_cfg top_cfg = {
44 .callback = NULL,
45 .user_data = NULL,
46 .flags = 0
47 };
48
49 top_cfg.ticks = counter_get_max_top_value(dev);
50 err = counter_set_top_value(dev, &top_cfg);
51 if (err == -ENOTSUP) {
52 /* If resetting is not support, attempt without reset. */
53 top_cfg.flags = COUNTER_TOP_CFG_DONT_RESET;
54 err = counter_set_top_value(dev, &top_cfg);
55
56 }
57 zassert_true((err == 0) || (err == -ENOTSUP),
58 "%s: Setting top value to default failed", dev->name);
59
60 err = counter_stop(dev);
61 /* DS3231 counter cannot be stopped */
62 zassert_equal(-ENOTSUP, err, "%s: Counter failed to stop", dev->name);
63
64 }
65
test_all_instances(counter_test_func_t func,counter_capability_func_t capability_check)66 static void test_all_instances(counter_test_func_t func,
67 counter_capability_func_t capability_check)
68 {
69 for (int i = 0; i < ARRAY_SIZE(devices); i++) {
70 counter_setup_instance(devices[i]);
71 if ((capability_check == NULL) ||
72 capability_check(devices[i])) {
73 TC_PRINT("Testing %s\n", devices[i]->name);
74 func(devices[i]);
75 } else {
76 TC_PRINT("Skipped for %s\n", devices[i]->name);
77 }
78 counter_tear_down_instance(devices[i]);
79 /* Allow logs to be printed. */
80 k_sleep(K_MSEC(100));
81 }
82 }
83
set_top_value_capable(const struct device * dev)84 static bool set_top_value_capable(const struct device *dev)
85 {
86 struct counter_top_cfg cfg = {
87 .ticks = counter_get_top_value(dev) - 1
88 };
89 int err;
90
91 err = counter_set_top_value(dev, &cfg);
92 if (err == -ENOTSUP) {
93 return false;
94 }
95
96 cfg.ticks++;
97 err = counter_set_top_value(dev, &cfg);
98 if (err == -ENOTSUP) {
99 return false;
100 }
101
102 return true;
103 }
104
top_handler(const struct device * dev,void * user_data)105 static void top_handler(const struct device *dev, void *user_data)
106 {
107 zassert_true(user_data == exp_user_data,
108 "%s: Unexpected callback", dev->name);
109 k_sem_give(&top_cnt_sem);
110 }
111
test_set_top_value_with_alarm_instance(const struct device * dev)112 void test_set_top_value_with_alarm_instance(const struct device *dev)
113 {
114 int err;
115 uint32_t cnt;
116 uint32_t top_cnt;
117 struct counter_top_cfg top_cfg = {
118 .callback = top_handler,
119 .user_data = exp_user_data,
120 .flags = 0
121 };
122
123 k_sem_reset(&top_cnt_sem);
124
125 top_cfg.ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US);
126 err = counter_start(dev);
127 zassert_equal(0, err, "%s: Counter failed to start", dev->name);
128
129 k_busy_wait(5000);
130
131 err = counter_get_value(dev, &cnt);
132 zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
133 err);
134 if (counter_is_counting_up(dev)) {
135 err = (cnt > 0) ? 0 : 1;
136 } else {
137 top_cnt = counter_get_top_value(dev);
138 err = (cnt < top_cnt) ? 0 : 1;
139 }
140 zassert_true(err == 0, "%s: Counter should progress", dev->name);
141
142 err = counter_set_top_value(dev, &top_cfg);
143 zassert_equal(0, err, "%s: Counter failed to set top value (err: %d)",
144 dev->name, err);
145
146 k_sleep(K_USEC(5.2 * COUNTER_PERIOD_US));
147
148 top_cnt = k_sem_count_get(&top_cnt_sem);
149 zassert_true(top_cnt == 5U,
150 "%s: Unexpected number of turnarounds (%d).",
151 dev->name, top_cnt);
152 }
153
test_set_top_value_with_alarm(void)154 void test_set_top_value_with_alarm(void)
155 {
156 test_all_instances(test_set_top_value_with_alarm_instance,
157 set_top_value_capable);
158 }
159
test_set_top_value_without_alarm_instance(const struct device * dev)160 void test_set_top_value_without_alarm_instance(const struct device *dev)
161 {
162 int err;
163 uint32_t cnt;
164 uint32_t top_cnt;
165 struct counter_top_cfg top_cfg = {
166 .callback = NULL,
167 .user_data = NULL,
168 .flags = 0
169 };
170
171 top_cfg.ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US);
172 err = counter_start(dev);
173 zassert_equal(0, err, "%s: Counter failed to start", dev->name);
174
175 k_sleep(K_USEC(5000));
176
177 err = counter_get_value(dev, &cnt);
178 zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
179 err);
180 if (counter_is_counting_up(dev)) {
181 err = (cnt > 0) ? 0 : 1;
182 } else {
183 top_cnt = counter_get_top_value(dev);
184 err = (cnt < top_cnt) ? 0 : 1;
185 }
186 zassert_true(err == 0, "%s: Counter should progress", dev->name);
187
188 err = counter_set_top_value(dev, &top_cfg);
189 zassert_equal(0, err, "%s: Counter failed to set top value (err: %d)",
190 dev->name, err);
191
192 zassert_true(counter_get_top_value(dev) == top_cfg.ticks,
193 "%s: new top value not in use.",
194 dev->name);
195 }
196
test_set_top_value_without_alarm(void)197 void test_set_top_value_without_alarm(void)
198 {
199 test_all_instances(test_set_top_value_without_alarm_instance,
200 set_top_value_capable);
201 }
202
alarm_handler(const struct device * dev,uint8_t chan_id,uint32_t counter,void * user_data)203 static void alarm_handler(const struct device *dev, uint8_t chan_id,
204 uint32_t counter,
205 void *user_data)
206 {
207 uint32_t now;
208 int err;
209
210 err = counter_get_value(dev, &now);
211 zassert_true(err == 0, "%s: Counter read failed (err: %d)",
212 dev->name, err);
213
214 if (counter_is_counting_up(dev)) {
215 zassert_true(now >= counter,
216 "%s: Alarm (%d) too early now: %d (counting up).",
217 dev->name, counter, now);
218 } else {
219 zassert_true(now <= counter,
220 "%s: Alarm (%d) too early now: %d (counting down).",
221 dev->name, counter, now);
222 }
223
224 if (user_data) {
225 zassert_true(&alarm_cfg == user_data,
226 "%s: Unexpected callback", dev->name);
227 }
228
229 /* DS3231 does not invoke callbacks from interrupt context. */
230 zassert_false(k_is_in_isr(), "%s: Unexpected interrupt context",
231 dev->name);
232 k_sem_give(&alarm_cnt_sem);
233 }
234
test_single_shot_alarm_instance(const struct device * dev,bool set_top)235 void test_single_shot_alarm_instance(const struct device *dev, bool set_top)
236 {
237 int err;
238 uint32_t ticks;
239 uint32_t alarm_cnt;
240 struct counter_top_cfg top_cfg = {
241 .callback = top_handler,
242 .user_data = exp_user_data,
243 .flags = 0
244 };
245
246 ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US);
247 top_cfg.ticks = ticks;
248
249 alarm_cfg.flags = 0;
250 alarm_cfg.callback = alarm_handler;
251 alarm_cfg.user_data = &alarm_cfg;
252
253 k_sem_reset(&alarm_cnt_sem);
254
255 if (counter_get_num_of_channels(dev) < 1U) {
256 /* Counter does not support any alarm */
257 return;
258 }
259
260 err = counter_start(dev);
261 zassert_equal(-EALREADY, err, "%s: Counter failed to start", dev->name);
262
263 if (set_top) {
264 err = counter_set_top_value(dev, &top_cfg);
265
266 zassert_equal(0, err,
267 "%s: Counter failed to set top value", dev->name);
268
269 alarm_cfg.ticks = ticks + 1;
270 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
271 zassert_equal(-EINVAL, err,
272 "%s: Counter should return error because ticks"
273 " exceeded the limit set alarm", dev->name);
274 alarm_cfg.ticks = ticks - 1;
275 }
276
277 alarm_cfg.ticks = ticks;
278 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
279 zassert_equal(0, err, "%s: Counter set alarm failed (err: %d)",
280 dev->name, err);
281
282 err = counter_set_top_value(dev, &top_cfg);
283 k_sleep(K_USEC(2 * (uint32_t)counter_ticks_to_us(dev, ticks)));
284
285 alarm_cnt = k_sem_count_get(&alarm_cnt_sem);
286 zassert_equal(1, alarm_cnt,
287 "%s: Expecting alarm callback", dev->name);
288
289 k_sleep(K_USEC(1.5 * counter_ticks_to_us(dev, ticks)));
290 alarm_cnt = k_sem_count_get(&alarm_cnt_sem);
291 zassert_equal(1, alarm_cnt,
292 "%s: Expecting alarm callback", dev->name);
293
294 err = counter_cancel_channel_alarm(dev, 0);
295 zassert_equal(0, err, "%s: Counter disabling alarm failed %d", dev->name, err);
296
297 top_cfg.ticks = counter_get_max_top_value(dev);
298 top_cfg.callback = NULL;
299 top_cfg.user_data = NULL;
300 err = counter_set_top_value(dev, &top_cfg);
301 if (err == -ENOTSUP) {
302 /* If resetting is not support, attempt without reset. */
303 top_cfg.flags = COUNTER_TOP_CFG_DONT_RESET;
304 err = counter_set_top_value(dev, &top_cfg);
305
306 }
307 zassert_true((err == 0) || (err == -ENOTSUP),
308 "%s: Setting top value to default failed", dev->name);
309
310 err = counter_stop(dev);
311 /* DS3231 counter cannot be stopped */
312 zassert_equal(-ENOTSUP, err, "%s: Counter failed to stop", dev->name);
313 }
314
test_single_shot_alarm_notop_instance(const struct device * dev)315 void test_single_shot_alarm_notop_instance(const struct device *dev)
316 {
317 test_single_shot_alarm_instance(dev, false);
318 }
319
test_single_shot_alarm_top_instance(const struct device * dev)320 void test_single_shot_alarm_top_instance(const struct device *dev)
321 {
322 test_single_shot_alarm_instance(dev, true);
323 }
324
single_channel_alarm_capable(const struct device * dev)325 static bool single_channel_alarm_capable(const struct device *dev)
326 {
327 return (counter_get_num_of_channels(dev) > 0);
328 }
329
single_channel_alarm_and_custom_top_capable(const struct device * dev)330 static bool single_channel_alarm_and_custom_top_capable(const struct device *dev)
331 {
332 return single_channel_alarm_capable(dev) &&
333 set_top_value_capable(dev);
334 }
335
ZTEST(counter_callback,test_single_shot_alarm_notop)336 ZTEST(counter_callback, test_single_shot_alarm_notop)
337 {
338 test_all_instances(test_single_shot_alarm_notop_instance,
339 single_channel_alarm_capable);
340 }
341
ZTEST(counter_callback,test_single_shot_alarm_top)342 ZTEST(counter_callback, test_single_shot_alarm_top)
343 {
344 test_all_instances(test_single_shot_alarm_top_instance,
345 single_channel_alarm_and_custom_top_capable);
346 }
347
348 static void *clbk_data[10];
349
alarm_handler2(const struct device * dev,uint8_t chan_id,uint32_t counter,void * user_data)350 static void alarm_handler2(const struct device *dev, uint8_t chan_id,
351 uint32_t counter,
352 void *user_data)
353 {
354 clbk_data[k_sem_count_get(&alarm_cnt_sem)] = user_data;
355 k_sem_give(&alarm_cnt_sem);
356 }
357
358 /*
359 * Two alarms set. First alarm is absolute, second relative. Because
360 * setting of both alarms is delayed it is expected that second alarm
361 * will expire first (relative to the time called) while first alarm
362 * will expire after next wrap around.
363 */
test_multiple_alarms_instance(const struct device * dev)364 void test_multiple_alarms_instance(const struct device *dev)
365 {
366 int err;
367 uint32_t ticks;
368 uint32_t alarm_cnt;
369 struct counter_top_cfg top_cfg = {
370 .callback = top_handler,
371 .user_data = exp_user_data,
372 .flags = 0
373 };
374
375 ticks = counter_us_to_ticks(dev, COUNTER_PERIOD_US);
376 top_cfg.ticks = ticks;
377
378 alarm_cfg.flags = COUNTER_ALARM_CFG_ABSOLUTE;
379 alarm_cfg.ticks = counter_us_to_ticks(dev, 2000);
380 alarm_cfg.callback = alarm_handler2;
381 alarm_cfg.user_data = &alarm_cfg;
382
383 alarm_cfg2.flags = 0;
384 alarm_cfg2.ticks = counter_us_to_ticks(dev, 2000);
385 alarm_cfg2.callback = alarm_handler2;
386 alarm_cfg2.user_data = &alarm_cfg2;
387
388 k_sem_reset(&alarm_cnt_sem);
389
390 if (counter_get_num_of_channels(dev) < 2U) {
391 /* Counter does not support two alarms */
392 return;
393 }
394
395 err = counter_start(dev);
396 /* DS3231 is always running */
397 zassert_equal(-EALREADY, err, "%s: Counter failed to start", dev->name);
398
399 err = counter_set_top_value(dev, &top_cfg);
400 zassert_equal(-ENOTSUP, err,
401 "%s: Counter failed to set top value: %d", dev->name);
402
403 k_sleep(K_USEC(3 * (uint32_t)counter_ticks_to_us(dev, alarm_cfg.ticks)));
404
405 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
406 zassert_equal(0, err, "%s: Counter set alarm failed", dev->name);
407
408 err = counter_set_channel_alarm(dev, 1, &alarm_cfg2);
409 zassert_equal(0, err, "%s: Counter set alarm failed", dev->name);
410
411 k_sleep(K_USEC(1.2 * counter_ticks_to_us(dev, ticks * 2U)));
412 alarm_cnt = k_sem_count_get(&alarm_cnt_sem);
413 zassert_equal(2, alarm_cnt,
414 "%s: Invalid number of callbacks %d (expected: %d)",
415 dev->name, alarm_cnt, 2);
416
417 zassert_equal(&alarm_cfg2, clbk_data[0],
418 "%s: Expected different order or callbacks",
419 dev->name);
420 zassert_equal(&alarm_cfg, clbk_data[1],
421 "%s: Expected different order or callbacks",
422 dev->name);
423
424 /* tear down */
425 err = counter_cancel_channel_alarm(dev, 0);
426 zassert_equal(0, err, "%s: Counter disabling alarm failed", dev->name);
427
428 err = counter_cancel_channel_alarm(dev, 1);
429 zassert_equal(0, err, "%s: Counter disabling alarm failed", dev->name);
430 }
431
multiple_channel_alarm_capable(const struct device * dev)432 static bool multiple_channel_alarm_capable(const struct device *dev)
433 {
434 return (counter_get_num_of_channels(dev) > 1);
435 }
436
not_capable(const struct device * dev)437 static bool not_capable(const struct device *dev)
438 {
439 return false;
440 }
441
ZTEST(counter_callback,test_multiple_alarms)442 ZTEST(counter_callback, test_multiple_alarms)
443 {
444 /* Test not supported on DS3231 because second alarm resolution is
445 * more coarse than first alarm; code would have to be changed to
446 * align to boundaries and wait over 60 s to verify.
447 *
448 * Basic support for two channels is verified in
449 * test_all_channels_instance().
450 */
451 (void)multiple_channel_alarm_capable;
452 test_all_instances(test_multiple_alarms_instance,
453 not_capable);
454 }
455
test_all_channels_instance(const struct device * dev)456 void test_all_channels_instance(const struct device *dev)
457 {
458 int err;
459 const int n = 10;
460 int nchan = 0;
461 bool limit_reached = false;
462 struct counter_alarm_cfg alarm_cfgs;
463 uint32_t ticks;
464 uint32_t alarm_cnt;
465
466 /* Use a delay large enough to guarantee a minute-counter
467 * rollover so both alarms can be tested.
468 */
469 ticks = counter_us_to_ticks(dev, 61U * COUNTER_PERIOD_US);
470
471 alarm_cfgs.flags = 0;
472 alarm_cfgs.ticks = ticks;
473 alarm_cfgs.callback = alarm_handler2;
474 alarm_cfgs.user_data = NULL;
475
476 err = counter_start(dev);
477 zassert_equal(-EALREADY, err, "%s: Counter failed to start", dev->name);
478
479 for (int i = 0; i < n; i++) {
480 err = counter_set_channel_alarm(dev, i, &alarm_cfgs);
481 if ((err == 0) && !limit_reached) {
482 nchan++;
483 } else if (err == -ENOTSUP) {
484 limit_reached = true;
485 } else {
486 zassert_equal(0, 1,
487 "%s: Unexpected error on setting alarm: %d",
488 dev->name, err);
489 }
490 }
491
492 TC_PRINT("Sleeping %u s to support minute-resolution alarm channel\n",
493 ticks + 1U);
494 k_sleep(K_USEC(counter_ticks_to_us(dev, ticks + 1U)));
495 alarm_cnt = k_sem_count_get(&alarm_cnt_sem);
496 zassert_equal(nchan, alarm_cnt,
497 "%s: Expecting alarm callback", dev->name);
498
499 for (int i = 0; i < nchan; i++) {
500 err = counter_cancel_channel_alarm(dev, i);
501 zassert_equal(0, err,
502 "%s: Unexpected error on disabling alarm", dev->name);
503 }
504
505 for (int i = nchan; i < n; i++) {
506 err = counter_cancel_channel_alarm(dev, i);
507 zassert_equal(-ENOTSUP, err,
508 "%s: Unexpected error on disabling alarm", dev->name);
509 }
510 }
511
ZTEST(counter_z,test_all_channels)512 ZTEST(counter_z, test_all_channels)
513 {
514 test_all_instances(test_all_channels_instance,
515 single_channel_alarm_capable);
516 }
517
518 /**
519 * Test validates if alarm set too late (current tick or current tick + 1)
520 * results in callback being called.
521 */
test_late_alarm_instance(const struct device * dev)522 void test_late_alarm_instance(const struct device *dev)
523 {
524 int err;
525 uint32_t alarm_cnt;
526 uint32_t tick_us = (uint32_t)counter_ticks_to_us(dev, 1);
527 uint32_t guard = counter_us_to_ticks(dev, 200);
528 struct counter_alarm_cfg alarm_cfg = {
529 .callback = alarm_handler,
530 .flags = COUNTER_ALARM_CFG_ABSOLUTE |
531 COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE,
532 .user_data = NULL
533 };
534
535 err = counter_set_guard_period(dev, guard,
536 COUNTER_GUARD_PERIOD_LATE_TO_SET);
537 zassert_equal(0, err, "%s: Unexpected error", dev->name);
538
539 err = counter_start(dev);
540 zassert_equal(0, err, "%s: Unexpected error", dev->name);
541
542 k_sleep(K_USEC(2 * tick_us));
543
544 alarm_cfg.ticks = 0;
545 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
546 zassert_equal(-ETIME, err, "%s: Unexpected error (%d)", dev->name, err);
547
548 /* wait couple of ticks */
549 k_sleep(K_USEC(5 * tick_us));
550
551 alarm_cnt = k_sem_count_get(&alarm_cnt_sem);
552 zassert_equal(1, alarm_cnt,
553 "%s: Expected %d callbacks, got %d\n",
554 dev->name, 1, alarm_cnt);
555
556 err = counter_get_value(dev, &(alarm_cfg.ticks));
557 zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
558 err);
559
560 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
561 zassert_equal(-ETIME, err, "%s: Failed to set an alarm (err: %d)",
562 dev->name, err);
563
564 /* wait to ensure that tick+1 timeout will expire. */
565 k_sleep(K_USEC(3 * tick_us));
566
567 alarm_cnt = k_sem_count_get(&alarm_cnt_sem);
568 zassert_equal(2, alarm_cnt,
569 "%s: Expected %d callbacks, got %d\n",
570 dev->name, 2, alarm_cnt);
571 }
572
test_late_alarm_error_instance(const struct device * dev)573 void test_late_alarm_error_instance(const struct device *dev)
574 {
575 int err;
576 uint32_t tick_us = (uint32_t)counter_ticks_to_us(dev, 1);
577 uint32_t guard = counter_us_to_ticks(dev, 200);
578 struct counter_alarm_cfg alarm_cfg = {
579 .callback = alarm_handler,
580 .flags = COUNTER_ALARM_CFG_ABSOLUTE,
581 .user_data = NULL
582 };
583
584 err = counter_set_guard_period(dev, guard,
585 COUNTER_GUARD_PERIOD_LATE_TO_SET);
586 zassert_equal(0, err, "%s: Unexpected error", dev->name);
587
588 err = counter_start(dev);
589 zassert_equal(0, err, "%s: Unexpected error", dev->name);
590
591 k_sleep(K_USEC(2 * tick_us));
592
593 alarm_cfg.ticks = 0;
594 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
595 zassert_equal(-ETIME, err,
596 "%s: Failed to detect late setting (err: %d)",
597 dev->name, err);
598
599 err = counter_get_value(dev, &(alarm_cfg.ticks));
600 zassert_true(err == 0, "%s: Counter read failed (err: %d)", dev->name,
601 err);
602
603 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
604 zassert_equal(-ETIME, err,
605 "%s: Counter failed to detect late setting (err: %d)",
606 dev->name, err);
607 }
608
late_detection_capable(const struct device * dev)609 static bool late_detection_capable(const struct device *dev)
610 {
611 uint32_t guard = counter_get_guard_period(dev,
612 COUNTER_GUARD_PERIOD_LATE_TO_SET);
613 int err = counter_set_guard_period(dev, guard,
614 COUNTER_GUARD_PERIOD_LATE_TO_SET);
615
616 if (err == -ENOTSUP) {
617 return false;
618 }
619
620 return true;
621 }
622
ZTEST(counter_callback,test_late_alarm)623 ZTEST(counter_callback, test_late_alarm)
624 {
625 test_all_instances(test_late_alarm_instance, late_detection_capable);
626 }
627
ZTEST(counter_callback,test_late_alarm_error)628 ZTEST(counter_callback, test_late_alarm_error)
629 {
630 test_all_instances(test_late_alarm_error_instance,
631 late_detection_capable);
632 }
633
test_short_relative_alarm_instance(const struct device * dev)634 static void test_short_relative_alarm_instance(const struct device *dev)
635 {
636 int err;
637 uint32_t alarm_cnt;
638 uint32_t tick_us = (uint32_t)counter_ticks_to_us(dev, 1);
639 struct counter_alarm_cfg alarm_cfg = {
640 .callback = alarm_handler,
641 .flags = 0,
642 .user_data = NULL
643 };
644
645 err = counter_start(dev);
646 zassert_equal(0, err, "%s: Unexpected error", dev->name);
647
648 alarm_cfg.ticks = 1;
649
650 for (int i = 0; i < 100; ++i) {
651 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
652 zassert_equal(0, err,
653 "%s: Failed to set an alarm (err: %d)",
654 dev->name, err);
655
656 /* wait to ensure that tick+1 timeout will expire. */
657 k_sleep(K_USEC(3 * tick_us));
658
659 alarm_cnt = k_sem_count_get(&alarm_cnt_sem);
660 zassert_equal(i + 1, alarm_cnt,
661 "%s: Expected %d callbacks, got %d\n",
662 dev->name, i + 1, alarm_cnt);
663 }
664 }
665
666 /* Function checks if relative alarm set for 1 tick will expire. If handler is
667 * not called within near future it indicates that driver do not support it and
668 * more extensive testing is skipped.
669 */
short_relative_capable(const struct device * dev)670 static bool short_relative_capable(const struct device *dev)
671 {
672 struct counter_alarm_cfg alarm_cfg = {
673 .callback = alarm_handler,
674 .flags = 0,
675 .user_data = NULL,
676 .ticks = 1
677 };
678 int err;
679 bool ret;
680
681 if (single_channel_alarm_capable(dev) == false) {
682 return false;
683 }
684
685 err = counter_start(dev);
686 if (err != 0) {
687 ret = false;
688 goto end;
689 }
690
691 k_sem_reset(&alarm_cnt_sem);
692 err = counter_set_channel_alarm(dev, 0, &alarm_cfg);
693 if (err != 0) {
694 ret = false;
695 goto end;
696 }
697
698 k_sleep(K_USEC(counter_ticks_to_us(dev, 10)));
699 if (k_sem_count_get(&alarm_cnt_sem) == 1) {
700 ret = true;
701 } else {
702 ret = false;
703 (void)counter_cancel_channel_alarm(dev, 0);
704 }
705
706 end:
707 k_sem_reset(&alarm_cnt_sem);
708 counter_stop(dev);
709 k_sleep(K_USEC(1000));
710
711 return ret;
712 }
713
ZTEST(counter_callback,test_short_relative_alarm)714 ZTEST(counter_callback, test_short_relative_alarm)
715 {
716 test_all_instances(test_short_relative_alarm_instance,
717 short_relative_capable);
718 }
719
test_ds3231_synchronize(void)720 static void test_ds3231_synchronize(void)
721 {
722 const struct device *dev = devices[0];
723 struct sys_notify notify;
724 struct k_poll_event evt = K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
725 K_POLL_MODE_NOTIFY_ONLY,
726 &sync_sig);
727 int rc;
728 int res = 0;
729
730 k_poll_signal_reset(&sync_sig);
731 sys_notify_init_signal(¬ify, &sync_sig);
732 rc = maxim_ds3231_synchronize(dev, ¬ify);
733 zassert_true(rc >= 0,
734 "%s: Failed to initiate synchronize: %d", dev->name, rc);
735
736 rc = k_poll(&evt, 1, K_SECONDS(2));
737 zassert_true(rc == 0,
738 "%s: Sync wait failed: %d\n", dev->name, rc);
739
740 rc = sys_notify_fetch_result(¬ify, &res);
741
742 zassert_true(rc >= 0,
743 "%s: Sync result read failed: %d", dev->name, rc);
744 zassert_true(res >= 0,
745 "%s: Sync operation failed: %d", dev->name, res);
746 }
747
ds3231_get_syncpoint(void)748 static void ds3231_get_syncpoint(void)
749 {
750 const struct device *dev = devices[0];
751 struct maxim_ds3231_syncpoint syncpoint;
752 int rc;
753
754 rc = maxim_ds3231_get_syncpoint(dev, &syncpoint);
755 zassert_true(rc >= 0,
756 "%s: Failed to read syncpoint: %d", dev->name, rc);
757 zassert_equal(syncpoint.rtc.tv_nsec, 0,
758 "%s: Unexpected nanoseconds\n", dev->name);
759
760 TC_PRINT("Time %u at %u local\n", (uint32_t)syncpoint.rtc.tv_sec,
761 syncpoint.syncclock);
762 }
763
test_ds3231_req_syncpoint(void)764 static void test_ds3231_req_syncpoint(void)
765 {
766 const struct device *dev = devices[0];
767 struct k_poll_event evt = K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL,
768 K_POLL_MODE_NOTIFY_ONLY,
769 &sync_sig);
770 int rc;
771
772 k_poll_signal_reset(&sync_sig);
773 rc = maxim_ds3231_req_syncpoint(dev, &sync_sig);
774 zassert_true(rc >= 0,
775 "%s: Failed to request syncpoint: %d", dev->name, rc);
776
777 rc = k_poll(&evt, 1, K_SECONDS(2));
778 zassert_true(rc == 0,
779 "%s: Syncpoint poll failed: %d\n", dev->name, rc);
780 rc = sync_sig.result;
781 zassert_true(rc >= 0,
782 "%s: Syncpoint operation failed: %d\n", dev->name, rc);
783 }
784
ZTEST(counter_supervisor,test_ds3231_get_syncpoint)785 ZTEST(counter_supervisor, test_ds3231_get_syncpoint)
786 {
787 test_ds3231_synchronize();
788 ds3231_get_syncpoint();
789 }
790
ZTEST_USER(counter_user,test_ds3231_get_syncpoint)791 ZTEST_USER(counter_user, test_ds3231_get_syncpoint)
792 {
793 test_ds3231_req_syncpoint();
794 ds3231_get_syncpoint();
795 }
796
797
counter_setup(void)798 static void *counter_setup(void)
799 {
800 int i;
801
802 /* Give required clocks some time to stabilize. In particular, nRF SoCs
803 * need such delay for the Xtal LF clock source to start and for this
804 * test to use the correct timing.
805 */
806 k_busy_wait(USEC_PER_MSEC * 300);
807
808 k_sem_init(&top_cnt_sem, 0, UINT_MAX);
809 k_object_access_grant(&top_cnt_sem, k_current_get());
810
811 k_sem_init(&alarm_cnt_sem, 0, UINT_MAX);
812 k_object_access_grant(&alarm_cnt_sem, k_current_get());
813
814 k_poll_signal_init(&sync_sig);
815 k_object_access_grant(&sync_sig, k_current_get());
816
817 for (i = 0; i < ARRAY_SIZE(devices); i++) {
818 zassert_true(device_is_ready(devices[i]),
819 "Device %s is not ready", devices[i]->name);
820 k_object_access_grant(devices[i], k_current_get());
821 }
822
823 return NULL;
824 }
825
826 /* Uses callbacks, run in supervisor mode */
827 ZTEST_SUITE(counter_callback, NULL, counter_setup, NULL, NULL, NULL);
828
829 /* Supervisor-mode driver-specific API */
830 ZTEST_SUITE(counter_supervisor, NULL, NULL, NULL, NULL, NULL);
831
832 /* User-mode-compatible driver-specific API*/
833 ZTEST_SUITE(counter_user, NULL, NULL, NULL, NULL, NULL);
834
835 /* Supervisor-mode test, takes 63 s so do it last */
836 ZTEST_SUITE(counter_z, NULL, NULL, NULL, NULL, NULL);
837