cc15f11c8a88cab5d320fb53f7720fd660f4b924
[bertos.git] / drv / timer_test.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Hardware independent timer driver (implementation)
9  *
10  * \version $Id$
11  * \author Bernardo Innocenti <bernie@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.1  2005/11/27 03:04:08  bernie
17  *#* Move test code to timer_test.c; Add OS_HOSTED support.
18  *#*
19  *#*/
20
21 #include "timer.c"
22 #include "mware/event.c"
23
24 static void timer_test_constants(void)
25 {
26         kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", TIMER_HW_HPTICKS_PER_SEC);
27         #ifdef TIMER_PRESCALER
28                 kprintf("TIMER_PRESCALER=%d\n", TIMER_PRESCALER);
29         #endif
30         #ifdef TIMER1_OVF_COUNT
31                 kprintf("TIMER1_OVF_COUNT=%d\n", (int)TIMER1_OVF_COUNT);
32         #endif
33         kprintf("TIMER_TICKS_PER_MSEC=%d\n", (int)TIMER_TICKS_PER_MSEC);
34         kprintf("\n");
35         kprintf("ms_to_ticks(100)=%lu\n", ms_to_ticks(100));
36         kprintf("ms_to_ticks(10000)=%lu\n", ms_to_ticks(10000));
37         kprintf("us_to_ticks(100)=%lu\n", us_to_ticks(100));
38         kprintf("us_to_ticks(10000)=%lu\n", us_to_ticks(10000));
39         kprintf("\n");
40         kprintf("ticks_to_ms(100)=%lu\n", ticks_to_ms(100));
41         kprintf("ticks_to_ms(10000)=%lu\n", ticks_to_ms(10000));
42         kprintf("ticks_to_us(100)=%lu\n", ticks_to_us(100));
43         kprintf("ticks_to_us(10000)=%lu\n", ticks_to_us(10000));
44         kprintf("\n");
45         kprintf("hptime_to_us(100)=%lu\n", hptime_to_us(100));
46         kprintf("hptime_to_us(10000)=%lu\n", hptime_to_us(10000));
47         kprintf("us_to_hptime(100)=%lu\n", us_to_hptime(100));
48         kprintf("us_to_hptime(10000)=%lu\n", us_to_hptime(10000));
49 }
50
51 static void timer_test_delay(void)
52 {
53         int i;
54
55         kputs("Delay test\n");
56         for (i = 0; i < 1000; i += 100)
57         {
58                 kprintf("delay %d...", i);
59                 timer_delay(i);
60                 kputs("done\n");
61         }
62 }
63
64 static void timer_test_hook(iptr_t _timer)
65 {
66         Timer *timer = (Timer *)(void *)_timer;
67
68         kprintf("Timer %ld expired\n", ticks_to_ms(timer->_delay));
69         timer_add(timer);
70 }
71
72 static void timer_test_async(void)
73 {
74         static Timer test_timers[5];
75         static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
76         size_t i;
77
78         for (i = 0; i < countof(test_timers); ++i)
79         {
80                 Timer *timer = &test_timers[i];
81                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
82                 timer_set_event_softint(timer, timer_test_hook, (iptr_t)timer);
83                 timer_add(timer);
84         }
85 }
86
87 static void timer_test_poll(void)
88 {
89         int secs = 0;
90         mtime_t start_time = ticks_to_ms(timer_clock());
91         mtime_t now;
92
93         while (secs <= 10)
94         {
95                 now = ticks_to_ms(timer_clock());
96                 if (now - start_time >= 1000)
97                 {
98                         ++secs;
99                         start_time += 1000;
100                         kprintf("seconds = %d, ticks=%ld\n", secs, now);
101                 }
102                 wdt_reset();
103         }
104 }
105
106 int main(void)
107 {
108         wdt_init(7);
109         timer_init();
110         timer_test_constants();
111         timer_test_delay();
112         timer_test_async();
113         timer_test_poll();
114 }