170105dea8a1ca9ba809e4305faa5bb10eeb01b2
[bertos.git] / drv / timer_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Hardware independent timer driver (implementation)
34  *
35  * \version $Id$
36  * \author Bernardo Innocenti <bernie@develer.com>
37  */
38
39 #include "timer.c"
40 #include "mware/event.c"
41 #include "os/hptime.c"
42
43 static void timer_test_constants(void)
44 {
45         kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", TIMER_HW_HPTICKS_PER_SEC);
46         #ifdef TIMER_PRESCALER
47                 kprintf("TIMER_PRESCALER=%d\n", TIMER_PRESCALER);
48         #endif
49         #ifdef TIMER1_OVF_COUNT
50                 kprintf("TIMER1_OVF_COUNT=%d\n", (int)TIMER1_OVF_COUNT);
51         #endif
52         kprintf("TIMER_TICKS_PER_SEC=%d\n", (int)TIMER_TICKS_PER_SEC);
53         kprintf("\n");
54         kprintf("ms_to_ticks(100)=%lu\n", ms_to_ticks(100));
55         kprintf("ms_to_ticks(10000)=%lu\n", ms_to_ticks(10000));
56         kprintf("us_to_ticks(100)=%lu\n", us_to_ticks(100));
57         kprintf("us_to_ticks(10000)=%lu\n", us_to_ticks(10000));
58         kprintf("\n");
59         kprintf("ticks_to_ms(100)=%lu\n", ticks_to_ms(100));
60         kprintf("ticks_to_ms(10000)=%lu\n", ticks_to_ms(10000));
61         kprintf("ticks_to_us(100)=%lu\n", ticks_to_us(100));
62         kprintf("ticks_to_us(10000)=%lu\n", ticks_to_us(10000));
63         kprintf("\n");
64         kprintf("hptime_to_us(100)=%lu\n", hptime_to_us(100));
65         kprintf("hptime_to_us(10000)=%lu\n", hptime_to_us(10000));
66         kprintf("us_to_hptime(100)=%lu\n", us_to_hptime(100));
67         kprintf("us_to_hptime(10000)=%lu\n", us_to_hptime(10000));
68 }
69
70 static void timer_test_delay(void)
71 {
72         int i;
73
74         kputs("Delay test\n");
75         for (i = 0; i < 1000; i += 100)
76         {
77                 kprintf("delay %d...", i);
78                 timer_delay(i);
79                 kputs("done\n");
80         }
81 }
82
83 static void timer_test_hook(iptr_t _timer)
84 {
85         Timer *timer = (Timer *)(void *)_timer;
86
87         kprintf("Timer %ld expired\n", ticks_to_ms(timer->_delay));
88         timer_add(timer);
89 }
90
91 static void timer_test_async(void)
92 {
93         static Timer test_timers[5];
94         static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
95         size_t i;
96
97         for (i = 0; i < countof(test_timers); ++i)
98         {
99                 Timer *timer = &test_timers[i];
100                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
101                 timer_set_event_softint(timer, timer_test_hook, (iptr_t)timer);
102                 timer_add(timer);
103         }
104 }
105
106 static void timer_test_poll(void)
107 {
108         int secs = 0;
109         mtime_t start_time = ticks_to_ms(timer_clock());
110         mtime_t now;
111
112         while (secs <= 10)
113         {
114                 now = ticks_to_ms(timer_clock());
115                 if (now - start_time >= 1000)
116                 {
117                         ++secs;
118                         start_time += 1000;
119                         kprintf("seconds = %d, ticks=%ld\n", secs, now);
120                 }
121                 wdt_reset();
122         }
123 }
124
125 int main(void)
126 {
127         wdt_init(7);
128         timer_init();
129         timer_test_constants();
130         timer_test_delay();
131         timer_test_async();
132         timer_test_poll();
133         return 0;
134 }