Test.
[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 /*#*
40  *#* $Log$
41  *#* Revision 1.4  2006/07/19 12:56:26  bernie
42  *#* Convert to new Doxygen style.
43  *#*
44  *#* Revision 1.3  2006/02/21 21:28:02  bernie
45  *#* New time handling based on TIMER_TICKS_PER_SEC to support slow timers with ticks longer than 1ms.
46  *#*
47  *#* Revision 1.2  2005/11/27 03:58:18  bernie
48  *#* Add POSIX timer emulator.
49  *#*
50  *#* Revision 1.1  2005/11/27 03:04:08  bernie
51  *#* Move test code to timer_test.c; Add OS_HOSTED support.
52  *#*
53  *#*/
54
55 #include "timer.c"
56 #include "mware/event.c"
57 #include "os/hptime.c"
58
59 static void timer_test_constants(void)
60 {
61         kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", TIMER_HW_HPTICKS_PER_SEC);
62         #ifdef TIMER_PRESCALER
63                 kprintf("TIMER_PRESCALER=%d\n", TIMER_PRESCALER);
64         #endif
65         #ifdef TIMER1_OVF_COUNT
66                 kprintf("TIMER1_OVF_COUNT=%d\n", (int)TIMER1_OVF_COUNT);
67         #endif
68         kprintf("TIMER_TICKS_PER_SEC=%d\n", (int)TIMER_TICKS_PER_SEC);
69         kprintf("\n");
70         kprintf("ms_to_ticks(100)=%lu\n", ms_to_ticks(100));
71         kprintf("ms_to_ticks(10000)=%lu\n", ms_to_ticks(10000));
72         kprintf("us_to_ticks(100)=%lu\n", us_to_ticks(100));
73         kprintf("us_to_ticks(10000)=%lu\n", us_to_ticks(10000));
74         kprintf("\n");
75         kprintf("ticks_to_ms(100)=%lu\n", ticks_to_ms(100));
76         kprintf("ticks_to_ms(10000)=%lu\n", ticks_to_ms(10000));
77         kprintf("ticks_to_us(100)=%lu\n", ticks_to_us(100));
78         kprintf("ticks_to_us(10000)=%lu\n", ticks_to_us(10000));
79         kprintf("\n");
80         kprintf("hptime_to_us(100)=%lu\n", hptime_to_us(100));
81         kprintf("hptime_to_us(10000)=%lu\n", hptime_to_us(10000));
82         kprintf("us_to_hptime(100)=%lu\n", us_to_hptime(100));
83         kprintf("us_to_hptime(10000)=%lu\n", us_to_hptime(10000));
84 }
85
86 static void timer_test_delay(void)
87 {
88         int i;
89
90         kputs("Delay test\n");
91         for (i = 0; i < 1000; i += 100)
92         {
93                 kprintf("delay %d...", i);
94                 timer_delay(i);
95                 kputs("done\n");
96         }
97 }
98
99 static void timer_test_hook(iptr_t _timer)
100 {
101         Timer *timer = (Timer *)(void *)_timer;
102
103         kprintf("Timer %ld expired\n", ticks_to_ms(timer->_delay));
104         timer_add(timer);
105 }
106
107 static void timer_test_async(void)
108 {
109         static Timer test_timers[5];
110         static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
111         size_t i;
112
113         for (i = 0; i < countof(test_timers); ++i)
114         {
115                 Timer *timer = &test_timers[i];
116                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
117                 timer_set_event_softint(timer, timer_test_hook, (iptr_t)timer);
118                 timer_add(timer);
119         }
120 }
121
122 static void timer_test_poll(void)
123 {
124         int secs = 0;
125         mtime_t start_time = ticks_to_ms(timer_clock());
126         mtime_t now;
127
128         while (secs <= 10)
129         {
130                 now = ticks_to_ms(timer_clock());
131                 if (now - start_time >= 1000)
132                 {
133                         ++secs;
134                         start_time += 1000;
135                         kprintf("seconds = %d, ticks=%ld\n", secs, now);
136                 }
137                 wdt_reset();
138         }
139 }
140
141 int main(void)
142 {
143         wdt_init(7);
144         timer_init();
145         timer_test_constants();
146         timer_test_delay();
147         timer_test_async();
148         timer_test_poll();
149         return 0;
150 }