2 * QTest testcase for the ARM MPTimer
4 * Copyright (c) 2016 Dmitry Osipenko <digetx@gmail.com>
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qemu/timer.h"
14 #define TIMER_BLOCK_SCALE(s) ((((s) & 0xff) + 1) * 10)
16 #define TIMER_BLOCK_STEP(scaler, steps_nb) \
17 clock_step(TIMER_BLOCK_SCALE(scaler) * (int64_t)(steps_nb) + 1)
19 #define TIMER_BASE_PHYS 0x1e000600
21 #define TIMER_LOAD 0x00
22 #define TIMER_COUNTER 0x04
23 #define TIMER_CONTROL 0x08
24 #define TIMER_INTSTAT 0x0C
26 #define TIMER_CONTROL_ENABLE (1 << 0)
27 #define TIMER_CONTROL_PERIODIC (1 << 1)
28 #define TIMER_CONTROL_IT_ENABLE (1 << 2)
29 #define TIMER_CONTROL_PRESCALER(p) (((p) & 0xff) << 8)
35 static int nonscaled = NOSCALE;
36 static int scaled = 122;
38 static void timer_load(uint32_t load)
40 writel(TIMER_BASE_PHYS + TIMER_LOAD, load);
43 static void timer_start(int periodic, uint32_t scale)
45 uint32_t ctl = TIMER_CONTROL_ENABLE | TIMER_CONTROL_PRESCALER(scale);
48 ctl |= TIMER_CONTROL_PERIODIC;
51 writel(TIMER_BASE_PHYS + TIMER_CONTROL, ctl);
54 static void timer_stop(void)
56 writel(TIMER_BASE_PHYS + TIMER_CONTROL, 0);
59 static void timer_int_clr(void)
61 writel(TIMER_BASE_PHYS + TIMER_INTSTAT, 1);
64 static void timer_reset(void)
71 static uint32_t timer_get_and_clr_int_sts(void)
73 uint32_t int_sts = readl(TIMER_BASE_PHYS + TIMER_INTSTAT);
82 static uint32_t timer_counter(void)
84 return readl(TIMER_BASE_PHYS + TIMER_COUNTER);
87 static void timer_set_counter(uint32_t value)
89 writel(TIMER_BASE_PHYS + TIMER_COUNTER, value);
92 static void test_timer_oneshot(gconstpointer arg)
94 int scaler = *((int *) arg);
98 timer_start(ONESHOT, scaler);
100 TIMER_BLOCK_STEP(scaler, 9999);
102 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
103 g_assert_cmpuint(timer_counter(), ==, 9990000);
105 TIMER_BLOCK_STEP(scaler, 9990000);
107 g_assert_cmpuint(timer_counter(), ==, 0);
108 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
110 TIMER_BLOCK_STEP(scaler, 9990000);
112 g_assert_cmpuint(timer_counter(), ==, 0);
113 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
116 static void test_timer_pause(gconstpointer arg)
118 int scaler = *((int *) arg);
121 timer_load(999999999);
122 timer_start(ONESHOT, scaler);
124 TIMER_BLOCK_STEP(scaler, 999);
126 g_assert_cmpuint(timer_counter(), ==, 999999000);
127 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
129 TIMER_BLOCK_STEP(scaler, 9000);
131 g_assert_cmpuint(timer_counter(), ==, 999990000);
132 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
136 g_assert_cmpuint(timer_counter(), ==, 999990000);
137 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
139 TIMER_BLOCK_STEP(scaler, 90000);
141 g_assert_cmpuint(timer_counter(), ==, 999990000);
142 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
144 timer_start(ONESHOT, scaler);
146 TIMER_BLOCK_STEP(scaler, 999990000);
148 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
149 g_assert_cmpuint(timer_counter(), ==, 0);
151 TIMER_BLOCK_STEP(scaler, 999990000);
153 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
154 g_assert_cmpuint(timer_counter(), ==, 0);
157 static void test_timer_reload(gconstpointer arg)
159 int scaler = *((int *) arg);
162 timer_load(UINT32_MAX);
163 timer_start(ONESHOT, scaler);
165 TIMER_BLOCK_STEP(scaler, 90000);
167 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 90000);
168 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
170 timer_load(UINT32_MAX);
172 TIMER_BLOCK_STEP(scaler, 90000);
174 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 90000);
175 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
178 static void test_timer_periodic(gconstpointer arg)
180 int scaler = *((int *) arg);
185 timer_start(PERIODIC, scaler);
188 clock_step(TIMER_BLOCK_SCALE(scaler) * (101 + repeat) + 1);
190 g_assert_cmpuint(timer_counter(), ==, 100 - repeat);
191 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
193 clock_step(TIMER_BLOCK_SCALE(scaler) * (101 - repeat) - 1);
197 static void test_timer_oneshot_to_periodic(gconstpointer arg)
199 int scaler = *((int *) arg);
203 timer_start(ONESHOT, scaler);
205 TIMER_BLOCK_STEP(scaler, 1000);
207 g_assert_cmpuint(timer_counter(), ==, 9000);
208 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
210 timer_start(PERIODIC, scaler);
212 TIMER_BLOCK_STEP(scaler, 14001);
214 g_assert_cmpuint(timer_counter(), ==, 5000);
215 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
218 static void test_timer_periodic_to_oneshot(gconstpointer arg)
220 int scaler = *((int *) arg);
223 timer_load(99999999);
224 timer_start(PERIODIC, scaler);
226 TIMER_BLOCK_STEP(scaler, 999);
228 g_assert_cmpuint(timer_counter(), ==, 99999000);
229 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
231 timer_start(ONESHOT, scaler);
233 TIMER_BLOCK_STEP(scaler, 99999009);
235 g_assert_cmpuint(timer_counter(), ==, 0);
236 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
239 static void test_timer_prescaler(void)
243 timer_start(ONESHOT, NOSCALE);
245 TIMER_BLOCK_STEP(NOSCALE, 9999998);
247 g_assert_cmpuint(timer_counter(), ==, 1);
248 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
250 TIMER_BLOCK_STEP(NOSCALE, 1);
252 g_assert_cmpuint(timer_counter(), ==, 0);
253 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
257 timer_start(ONESHOT, 0xAB);
259 TIMER_BLOCK_STEP(0xAB, 9999998);
261 g_assert_cmpuint(timer_counter(), ==, 1);
262 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
264 TIMER_BLOCK_STEP(0xAB, 1);
266 g_assert_cmpuint(timer_counter(), ==, 0);
267 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
270 static void test_timer_prescaler_on_the_fly(void)
274 timer_start(ONESHOT, NOSCALE);
276 TIMER_BLOCK_STEP(NOSCALE, 999);
278 g_assert_cmpuint(timer_counter(), ==, 9999000);
279 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
281 timer_start(ONESHOT, 0xAB);
283 TIMER_BLOCK_STEP(0xAB, 9000);
285 g_assert_cmpuint(timer_counter(), ==, 9990000);
286 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
289 static void test_timer_set_oneshot_counter_to_0(gconstpointer arg)
291 int scaler = *((int *) arg);
294 timer_load(UINT32_MAX);
295 timer_start(ONESHOT, scaler);
297 TIMER_BLOCK_STEP(scaler, 1);
299 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 1);
300 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
302 timer_set_counter(0);
304 TIMER_BLOCK_STEP(scaler, 10);
306 g_assert_cmpuint(timer_counter(), ==, 0);
307 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
310 static void test_timer_set_periodic_counter_to_0(gconstpointer arg)
312 int scaler = *((int *) arg);
315 timer_load(UINT32_MAX);
316 timer_start(PERIODIC, scaler);
318 TIMER_BLOCK_STEP(scaler, 1);
320 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 1);
321 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
323 timer_set_counter(0);
325 TIMER_BLOCK_STEP(scaler, 1);
327 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - (scaler ? 0 : 1));
328 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
331 timer_set_counter(UINT32_MAX);
332 timer_start(PERIODIC, scaler);
334 TIMER_BLOCK_STEP(scaler, 1);
336 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 1);
337 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
339 timer_set_counter(0);
341 TIMER_BLOCK_STEP(scaler, 1);
343 g_assert_cmpuint(timer_counter(), ==, 0);
344 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
347 static void test_timer_noload_oneshot(gconstpointer arg)
349 int scaler = *((int *) arg);
352 timer_start(ONESHOT, scaler);
354 TIMER_BLOCK_STEP(scaler, 1);
356 g_assert_cmpuint(timer_counter(), ==, 0);
357 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
359 TIMER_BLOCK_STEP(scaler, 1);
361 g_assert_cmpuint(timer_counter(), ==, 0);
362 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
365 static void test_timer_noload_periodic(gconstpointer arg)
367 int scaler = *((int *) arg);
370 timer_start(PERIODIC, scaler);
372 TIMER_BLOCK_STEP(scaler, 1);
374 g_assert_cmpuint(timer_counter(), ==, 0);
375 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
377 TIMER_BLOCK_STEP(scaler, 1);
379 g_assert_cmpuint(timer_counter(), ==, 0);
380 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
383 static void test_timer_zero_load_oneshot(gconstpointer arg)
385 int scaler = *((int *) arg);
388 timer_start(ONESHOT, scaler);
390 TIMER_BLOCK_STEP(scaler, 1);
392 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
393 g_assert_cmpuint(timer_counter(), ==, 0);
397 TIMER_BLOCK_STEP(scaler, 1);
399 g_assert_cmpuint(timer_counter(), ==, 0);
400 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
402 TIMER_BLOCK_STEP(scaler, 1);
404 g_assert_cmpuint(timer_counter(), ==, 0);
405 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
408 static void test_timer_zero_load_periodic(gconstpointer arg)
410 int scaler = *((int *) arg);
413 timer_start(PERIODIC, scaler);
415 TIMER_BLOCK_STEP(scaler, 1);
417 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
418 g_assert_cmpuint(timer_counter(), ==, 0);
422 TIMER_BLOCK_STEP(scaler, 1);
424 g_assert_cmpuint(timer_counter(), ==, 0);
425 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
427 TIMER_BLOCK_STEP(scaler, 1);
429 g_assert_cmpuint(timer_counter(), ==, 0);
430 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
433 static void test_timer_zero_load_oneshot_to_nonzero(gconstpointer arg)
435 int scaler = *((int *) arg);
438 timer_start(ONESHOT, scaler);
440 TIMER_BLOCK_STEP(scaler, 1);
442 g_assert_cmpuint(timer_counter(), ==, 0);
443 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
447 TIMER_BLOCK_STEP(scaler, 1);
449 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
450 g_assert_cmpuint(timer_counter(), ==, 0);
454 TIMER_BLOCK_STEP(scaler, 1001);
456 g_assert_cmpuint(timer_counter(), ==, 0);
457 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
460 static void test_timer_zero_load_periodic_to_nonzero(gconstpointer arg)
462 int scaler = *((int *) arg);
466 timer_start(PERIODIC, scaler);
468 TIMER_BLOCK_STEP(scaler, 1);
470 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
471 g_assert_cmpuint(timer_counter(), ==, 0);
475 TIMER_BLOCK_STEP(scaler, 1);
477 g_assert_cmpuint(timer_counter(), ==, 0);
478 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
482 for (i = 1; i < 10; i++) {
483 TIMER_BLOCK_STEP(scaler, 2000001);
485 g_assert_cmpuint(timer_counter(), ==, 1999999 - i);
486 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
487 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
491 static void test_timer_nonzero_load_oneshot_to_zero(gconstpointer arg)
493 int scaler = *((int *) arg);
496 timer_start(ONESHOT, scaler);
498 TIMER_BLOCK_STEP(scaler, 1);
500 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
501 g_assert_cmpuint(timer_counter(), ==, 0);
503 timer_load(UINT32_MAX);
506 TIMER_BLOCK_STEP(scaler, 100);
508 g_assert_cmpuint(timer_counter(), ==, 0);
509 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
512 static void test_timer_nonzero_load_periodic_to_zero(gconstpointer arg)
514 int scaler = *((int *) arg);
517 timer_start(PERIODIC, scaler);
519 TIMER_BLOCK_STEP(scaler, 1);
521 g_assert_cmpuint(timer_counter(), ==, 0);
522 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
524 timer_load(UINT32_MAX);
527 TIMER_BLOCK_STEP(scaler, 100);
529 g_assert_cmpuint(timer_counter(), ==, 0);
530 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
533 static void test_timer_set_periodic_counter_on_the_fly(gconstpointer arg)
535 int scaler = *((int *) arg);
538 timer_load(UINT32_MAX / 2);
539 timer_start(PERIODIC, scaler);
541 TIMER_BLOCK_STEP(scaler, 100);
543 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX / 2 - 100);
544 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
546 timer_set_counter(UINT32_MAX);
548 TIMER_BLOCK_STEP(scaler, 100);
550 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
551 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
554 static void test_timer_enable_and_set_counter(gconstpointer arg)
556 int scaler = *((int *) arg);
559 timer_start(ONESHOT, scaler);
561 TIMER_BLOCK_STEP(scaler, 1);
563 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
565 timer_set_counter(UINT32_MAX);
567 TIMER_BLOCK_STEP(scaler, 100);
569 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
570 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
573 static void test_timer_set_counter_and_enable(gconstpointer arg)
575 int scaler = *((int *) arg);
578 timer_set_counter(UINT32_MAX);
579 timer_start(ONESHOT, scaler);
581 TIMER_BLOCK_STEP(scaler, 100);
583 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
584 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
587 static void test_timer_set_counter_disabled(void)
590 timer_set_counter(999999999);
592 TIMER_BLOCK_STEP(NOSCALE, 100);
594 g_assert_cmpuint(timer_counter(), ==, 999999999);
595 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
598 static void test_timer_load_disabled(void)
601 timer_load(999999999);
603 TIMER_BLOCK_STEP(NOSCALE, 100);
605 g_assert_cmpuint(timer_counter(), ==, 999999999);
606 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
609 static void test_timer_oneshot_with_counter_0_on_start(gconstpointer arg)
611 int scaler = *((int *) arg);
615 timer_set_counter(0);
616 timer_start(ONESHOT, scaler);
618 TIMER_BLOCK_STEP(scaler, 100);
620 g_assert_cmpuint(timer_counter(), ==, 0);
621 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
623 TIMER_BLOCK_STEP(scaler, 100);
625 g_assert_cmpuint(timer_counter(), ==, 0);
626 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
629 static void test_timer_periodic_with_counter_0_on_start(gconstpointer arg)
631 int scaler = *((int *) arg);
635 timer_load(UINT32_MAX);
636 timer_set_counter(0);
638 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
639 g_assert_cmpuint(timer_counter(), ==, 0);
641 timer_start(PERIODIC, scaler);
643 TIMER_BLOCK_STEP(scaler, 100);
645 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
646 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX + (scaler ? 1 : 0) - 100);
648 TIMER_BLOCK_STEP(scaler, 100);
650 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX + (scaler ? 1 : 0) - 200);
651 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
655 timer_set_counter(0);
657 g_assert_cmpuint(timer_counter(), ==, 0);
658 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
660 TIMER_BLOCK_STEP(scaler, 1);
662 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
664 timer_start(PERIODIC, scaler);
666 TIMER_BLOCK_STEP(scaler, 1);
668 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
670 for (i = 2 - (!!scaler ? 1 : 0); i < 10; i++) {
671 TIMER_BLOCK_STEP(scaler, 2000001);
673 g_assert_cmpuint(timer_counter(), ==, 1999999 - i);
674 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
675 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
679 static void test_periodic_counter(gconstpointer arg)
681 const int test_load = 10;
682 int scaler = *((int *) arg);
686 timer_load(test_load);
687 timer_start(PERIODIC, scaler);
691 for (test_val = 0; test_val <= test_load; test_val++) {
692 clock_step(TIMER_BLOCK_SCALE(scaler) * test_load);
693 g_assert_cmpint(timer_counter(), ==, test_val);
697 static void test_timer_set_counter_periodic_with_zero_load(gconstpointer arg)
699 int scaler = *((int *) arg);
702 timer_start(PERIODIC, scaler);
705 TIMER_BLOCK_STEP(scaler, 1);
707 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
709 TIMER_BLOCK_STEP(scaler, 1);
711 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
713 timer_set_counter(999);
715 TIMER_BLOCK_STEP(scaler, 999);
717 g_assert_cmpuint(timer_counter(), ==, 0);
718 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
720 TIMER_BLOCK_STEP(scaler, 1);
722 g_assert_cmpuint(timer_counter(), ==, 0);
723 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
726 static void test_timer_set_oneshot_load_to_0(gconstpointer arg)
728 int scaler = *((int *) arg);
731 timer_load(UINT32_MAX);
732 timer_start(ONESHOT, scaler);
734 TIMER_BLOCK_STEP(scaler, 100);
736 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
737 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
741 TIMER_BLOCK_STEP(scaler, 100);
743 g_assert_cmpuint(timer_counter(), ==, 0);
744 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
746 TIMER_BLOCK_STEP(scaler, 100);
748 g_assert_cmpuint(timer_counter(), ==, 0);
749 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
752 static void test_timer_set_periodic_load_to_0(gconstpointer arg)
754 int scaler = *((int *) arg);
757 timer_load(UINT32_MAX);
758 timer_start(PERIODIC, scaler);
760 TIMER_BLOCK_STEP(scaler, 100);
762 g_assert_cmpuint(timer_counter(), ==, UINT32_MAX - 100);
763 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
767 TIMER_BLOCK_STEP(scaler, 100);
769 g_assert_cmpuint(timer_counter(), ==, 0);
770 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
772 TIMER_BLOCK_STEP(scaler, 100);
774 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
775 g_assert_cmpuint(timer_counter(), ==, 0);
778 static void test_deferred_trigger(void)
784 timer_start(mode, 255);
788 g_assert_cmpuint(timer_counter(), ==, 0);
790 TIMER_BLOCK_STEP(255, 1);
792 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
796 timer_start(mode, 255);
800 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
802 TIMER_BLOCK_STEP(255, 1);
804 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
806 TIMER_BLOCK_STEP(255, 1);
808 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
811 timer_load(UINT32_MAX);
812 timer_start(mode, 255);
816 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
818 timer_set_counter(0);
822 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
824 TIMER_BLOCK_STEP(255, 1);
826 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
829 timer_load(UINT32_MAX);
830 timer_start(mode, 255);
834 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
840 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
842 TIMER_BLOCK_STEP(255, 1);
844 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
846 if (mode == ONESHOT) {
852 static void test_timer_zero_load_mode_switch(gconstpointer arg)
854 int scaler = *((int *) arg);
858 timer_start(PERIODIC, scaler);
860 TIMER_BLOCK_STEP(scaler, 1);
862 g_assert_cmpuint(timer_counter(), ==, 0);
863 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
865 TIMER_BLOCK_STEP(scaler, 1);
867 timer_start(ONESHOT, scaler);
869 TIMER_BLOCK_STEP(scaler, 1);
871 g_assert_cmpuint(timer_counter(), ==, 0);
872 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
874 TIMER_BLOCK_STEP(scaler, 1);
876 g_assert_cmpuint(timer_counter(), ==, 0);
877 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
879 TIMER_BLOCK_STEP(scaler, 1);
881 timer_start(PERIODIC, scaler);
883 TIMER_BLOCK_STEP(scaler, 1);
885 g_assert_cmpuint(timer_counter(), ==, 0);
886 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, !!scaler);
889 static void test_timer_zero_load_prescaled_periodic_to_nonscaled_oneshot(void)
893 timer_start(PERIODIC, 255);
895 TIMER_BLOCK_STEP(255, 1);
897 g_assert_cmpuint(timer_counter(), ==, 0);
898 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
899 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
901 TIMER_BLOCK_STEP(255, 1);
903 g_assert_cmpuint(timer_counter(), ==, 0);
904 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
905 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
907 TIMER_BLOCK_STEP(255, 1);
909 timer_start(ONESHOT, NOSCALE);
911 TIMER_BLOCK_STEP(NOSCALE, 1);
913 g_assert_cmpuint(timer_counter(), ==, 0);
914 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
915 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
917 TIMER_BLOCK_STEP(NOSCALE, 1);
919 g_assert_cmpuint(timer_counter(), ==, 0);
920 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
923 static void test_timer_zero_load_prescaled_oneshot_to_nonscaled_periodic(void)
927 timer_start(ONESHOT, 255);
929 TIMER_BLOCK_STEP(255, 1);
931 g_assert_cmpuint(timer_counter(), ==, 0);
932 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
933 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
935 timer_start(PERIODIC, NOSCALE);
937 TIMER_BLOCK_STEP(NOSCALE, 1);
939 g_assert_cmpuint(timer_counter(), ==, 0);
940 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
943 static void test_timer_zero_load_nonscaled_oneshot_to_prescaled_periodic(void)
947 timer_start(ONESHOT, NOSCALE);
949 TIMER_BLOCK_STEP(NOSCALE, 1);
951 g_assert_cmpuint(timer_counter(), ==, 0);
952 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
954 timer_start(PERIODIC, 255);
956 TIMER_BLOCK_STEP(255, 1);
958 g_assert_cmpuint(timer_counter(), ==, 0);
959 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
960 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
962 TIMER_BLOCK_STEP(255, 1);
964 g_assert_cmpuint(timer_counter(), ==, 0);
965 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
966 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
969 static void test_timer_zero_load_nonscaled_periodic_to_prescaled_oneshot(void)
973 timer_start(PERIODIC, NOSCALE);
975 TIMER_BLOCK_STEP(NOSCALE, 1);
977 g_assert_cmpuint(timer_counter(), ==, 0);
978 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
980 timer_start(ONESHOT, 255);
982 TIMER_BLOCK_STEP(255, 1);
984 g_assert_cmpuint(timer_counter(), ==, 0);
985 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 1);
986 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
988 TIMER_BLOCK_STEP(255, 1);
990 g_assert_cmpuint(timer_counter(), ==, 0);
991 g_assert_cmpuint(timer_get_and_clr_int_sts(), ==, 0);
994 int main(int argc, char **argv)
996 int *scaler = &nonscaled;
999 g_test_init(&argc, &argv, NULL);
1001 qtest_add_func("mptimer/deferred_trigger", test_deferred_trigger);
1002 qtest_add_func("mptimer/load_disabled", test_timer_load_disabled);
1003 qtest_add_func("mptimer/set_counter_disabled", test_timer_set_counter_disabled);
1004 qtest_add_func("mptimer/zero_load_prescaled_periodic_to_nonscaled_oneshot",
1005 test_timer_zero_load_prescaled_periodic_to_nonscaled_oneshot);
1006 qtest_add_func("mptimer/zero_load_prescaled_oneshot_to_nonscaled_periodic",
1007 test_timer_zero_load_prescaled_oneshot_to_nonscaled_periodic);
1008 qtest_add_func("mptimer/zero_load_nonscaled_oneshot_to_prescaled_periodic",
1009 test_timer_zero_load_nonscaled_oneshot_to_prescaled_periodic);
1010 qtest_add_func("mptimer/zero_load_nonscaled_periodic_to_prescaled_oneshot",
1011 test_timer_zero_load_nonscaled_periodic_to_prescaled_oneshot);
1012 qtest_add_func("mptimer/prescaler", test_timer_prescaler);
1013 qtest_add_func("mptimer/prescaler_on_the_fly", test_timer_prescaler_on_the_fly);
1015 tests_with_prescaler_arg:
1016 qtest_add_data_func(
1017 g_strdup_printf("mptimer/oneshot scaler=%d", *scaler),
1018 scaler, test_timer_oneshot);
1019 qtest_add_data_func(
1020 g_strdup_printf("mptimer/pause scaler=%d", *scaler),
1021 scaler, test_timer_pause);
1022 qtest_add_data_func(
1023 g_strdup_printf("mptimer/reload scaler=%d", *scaler),
1024 scaler, test_timer_reload);
1025 qtest_add_data_func(
1026 g_strdup_printf("mptimer/periodic scaler=%d", *scaler),
1027 scaler, test_timer_periodic);
1028 qtest_add_data_func(
1029 g_strdup_printf("mptimer/oneshot_to_periodic scaler=%d", *scaler),
1030 scaler, test_timer_oneshot_to_periodic);
1031 qtest_add_data_func(
1032 g_strdup_printf("mptimer/periodic_to_oneshot scaler=%d", *scaler),
1033 scaler, test_timer_periodic_to_oneshot);
1034 qtest_add_data_func(
1035 g_strdup_printf("mptimer/set_oneshot_counter_to_0 scaler=%d", *scaler),
1036 scaler, test_timer_set_oneshot_counter_to_0);
1037 qtest_add_data_func(
1038 g_strdup_printf("mptimer/set_periodic_counter_to_0 scaler=%d", *scaler),
1039 scaler, test_timer_set_periodic_counter_to_0);
1040 qtest_add_data_func(
1041 g_strdup_printf("mptimer/noload_oneshot scaler=%d", *scaler),
1042 scaler, test_timer_noload_oneshot);
1043 qtest_add_data_func(
1044 g_strdup_printf("mptimer/noload_periodic scaler=%d", *scaler),
1045 scaler, test_timer_noload_periodic);
1046 qtest_add_data_func(
1047 g_strdup_printf("mptimer/zero_load_oneshot scaler=%d", *scaler),
1048 scaler, test_timer_zero_load_oneshot);
1049 qtest_add_data_func(
1050 g_strdup_printf("mptimer/zero_load_periodic scaler=%d", *scaler),
1051 scaler, test_timer_zero_load_periodic);
1052 qtest_add_data_func(
1053 g_strdup_printf("mptimer/zero_load_oneshot_to_nonzero scaler=%d", *scaler),
1054 scaler, test_timer_zero_load_oneshot_to_nonzero);
1055 qtest_add_data_func(
1056 g_strdup_printf("mptimer/zero_load_periodic_to_nonzero scaler=%d", *scaler),
1057 scaler, test_timer_zero_load_periodic_to_nonzero);
1058 qtest_add_data_func(
1059 g_strdup_printf("mptimer/nonzero_load_oneshot_to_zero scaler=%d", *scaler),
1060 scaler, test_timer_nonzero_load_oneshot_to_zero);
1061 qtest_add_data_func(
1062 g_strdup_printf("mptimer/nonzero_load_periodic_to_zero scaler=%d", *scaler),
1063 scaler, test_timer_nonzero_load_periodic_to_zero);
1064 qtest_add_data_func(
1065 g_strdup_printf("mptimer/set_periodic_counter_on_the_fly scaler=%d", *scaler),
1066 scaler, test_timer_set_periodic_counter_on_the_fly);
1067 qtest_add_data_func(
1068 g_strdup_printf("mptimer/enable_and_set_counter scaler=%d", *scaler),
1069 scaler, test_timer_enable_and_set_counter);
1070 qtest_add_data_func(
1071 g_strdup_printf("mptimer/set_counter_and_enable scaler=%d", *scaler),
1072 scaler, test_timer_set_counter_and_enable);
1073 qtest_add_data_func(
1074 g_strdup_printf("mptimer/oneshot_with_counter_0_on_start scaler=%d", *scaler),
1075 scaler, test_timer_oneshot_with_counter_0_on_start);
1076 qtest_add_data_func(
1077 g_strdup_printf("mptimer/periodic_with_counter_0_on_start scaler=%d", *scaler),
1078 scaler, test_timer_periodic_with_counter_0_on_start);
1079 qtest_add_data_func(
1080 g_strdup_printf("mptimer/periodic_counter scaler=%d", *scaler),
1081 scaler, test_periodic_counter);
1082 qtest_add_data_func(
1083 g_strdup_printf("mptimer/set_counter_periodic_with_zero_load scaler=%d", *scaler),
1084 scaler, test_timer_set_counter_periodic_with_zero_load);
1085 qtest_add_data_func(
1086 g_strdup_printf("mptimer/set_oneshot_load_to_0 scaler=%d", *scaler),
1087 scaler, test_timer_set_oneshot_load_to_0);
1088 qtest_add_data_func(
1089 g_strdup_printf("mptimer/set_periodic_load_to_0 scaler=%d", *scaler),
1090 scaler, test_timer_set_periodic_load_to_0);
1091 qtest_add_data_func(
1092 g_strdup_printf("mptimer/zero_load_mode_switch scaler=%d", *scaler),
1093 scaler, test_timer_zero_load_mode_switch);
1095 if (scaler == &nonscaled) {
1097 goto tests_with_prescaler_arg;
1100 qtest_start("-machine vexpress-a9");