Add synchronous timer scheduler.
[bertos.git] / bertos / 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 Bernie Innocenti <bernie@codewiz.org>
37  *
38  */
39
40 #include <cfg/test.h>
41
42 #include <drv/timer.h>
43 #include <drv/wdt.h>
44
45 #include <mware/event.h>
46
47 #include <cfg/debug.h>
48
49 static void timer_test_constants(void)
50 {
51         kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", (unsigned long)TIMER_HW_HPTICKS_PER_SEC);
52         #ifdef TIMER_PRESCALER
53                 kprintf("TIMER_PRESCALER    = %lu\n", (unsigned long)TIMER_PRESCALER);
54         #endif
55         #ifdef TIMER1_OVF_COUNT
56                 kprintf("TIMER1_OVF_COUNT   = %lu\n", (unsigned long)TIMER1_OVF_COUNT);
57         #endif
58         kprintf("TIMER_TICKS_PER_SEC= %lu\n",  (unsigned long)TIMER_TICKS_PER_SEC);
59         kprintf("\n");
60         kprintf("ms_to_ticks(100)   = %lu\n",   (unsigned long)ms_to_ticks(100));
61         kprintf("ms_to_ticks(10000) = %lu\n",   (unsigned long)ms_to_ticks(10000));
62         kprintf("us_to_ticks(100)   = %lu\n",   (unsigned long)us_to_ticks(100));
63         kprintf("us_to_ticks(10000) = %lu\n",   (unsigned long)us_to_ticks(10000));
64         kprintf("\n");
65         kprintf("ticks_to_ms(100)   = %lu\n",   (unsigned long)ticks_to_ms(100));
66         kprintf("ticks_to_ms(10000) = %lu\n",   (unsigned long)ticks_to_ms(10000));
67         kprintf("ticks_to_us(100)   = %lu\n",   (unsigned long)ticks_to_us(100));
68         kprintf("ticks_to_us(10000) = %lu\n",   (unsigned long)ticks_to_us(10000));
69         kprintf("\n");
70         kprintf("hptime_to_us(100)  = %lu\n",   (unsigned long)hptime_to_us(100));
71         #if (SIZEOF_HPTIME_T > 1)
72                 kprintf("hptime_to_us(10000)= %lu\n",   (unsigned long)hptime_to_us(10000));
73         #endif
74         kprintf("us_to_hptime(100)  = %lu\n",   (unsigned long)us_to_hptime(100));
75         kprintf("us_to_hptime(10000)= %lu\n",   (unsigned long)us_to_hptime(10000));
76 }
77
78 static void timer_test_delay(void)
79 {
80         int i;
81
82         kputs("Delay test\n");
83         for (i = 0; i < 1000; i += 100)
84         {
85                 kprintf("delay %d...", i);
86                 timer_delay(i);
87                 kputs("done\n");
88         }
89 }
90
91 static void timer_test_hook(iptr_t _timer)
92 {
93         Timer *timer = (Timer *)(void *)_timer;
94
95         kprintf("Timer %lu expired\n", (unsigned long)ticks_to_ms(timer->_delay));
96         timer_add(timer);
97 }
98
99 static Timer test_timers[5];
100
101 List synctimer_list;
102 static Timer synctimer_timers[5];
103
104 static void synctimer_test_hook(iptr_t _timer)
105 {
106         Timer *timer = (Timer *)(void *)_timer;
107         kprintf("Sync timer process %lu expired\n", (unsigned long)ticks_to_ms(timer->_delay));
108         synctimer_add(timer, &synctimer_list);
109 }
110
111
112 static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
113
114 static void timer_test_async(void)
115 {
116         size_t i;
117
118         for (i = 0; i < countof(test_timers); ++i)
119         {
120                 Timer *timer = &test_timers[i];
121                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
122                 timer_setSoftint(timer, timer_test_hook, (iptr_t)timer);
123                 timer_add(timer);
124         }
125 }
126
127 static void timer_test_poll(void)
128 {
129         int secs = 0;
130         mtime_t start_time = ticks_to_ms(timer_clock());
131         mtime_t now;
132
133         while (secs <= 10)
134         {
135                 now = ticks_to_ms(timer_clock());
136                 if (now - start_time >= 1000)
137                 {
138                         ++secs;
139                         start_time += 1000;
140                         kprintf("seconds = %d, ticks=%lu\n", secs, (unsigned long)now);
141                 }
142                 wdt_reset();
143         }
144 }
145
146 static void synctimer_test(void)
147 {
148         size_t i;
149
150         for (i = 0; i < countof(synctimer_timers); ++i)
151         {
152                 Timer *timer = &synctimer_timers[i];
153                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
154                 timer_setSoftint(timer, synctimer_test_hook, (iptr_t)timer);
155                 synctimer_add(timer, &synctimer_list);
156         }
157
158         int secs = 0;
159         mtime_t start_time = ticks_to_ms(timer_clock());
160         mtime_t now;
161
162         while (secs <= 10)
163         {
164                 now = ticks_to_ms(timer_clock());
165                 synctimer_poll(&synctimer_list);
166                 if (now - start_time >= 1000)
167                 {
168                         ++secs;
169                         start_time += 1000;
170                         kprintf("seconds = %d, ticks=%lu\n", secs, (unsigned long)now);
171                 }
172                 wdt_reset();
173         }
174
175         for (i = 0; i < countof(synctimer_timers); ++i)
176         {
177                 synctimer_abort(&synctimer_timers[i]);
178         }
179 }
180
181 int timer_testSetup(void)
182 {
183         IRQ_ENABLE;
184         wdt_start(7);
185         timer_init();
186         LIST_INIT(&synctimer_list);
187         kdbg_init();
188         return 0;
189 }
190
191 int timer_testRun(void)
192 {
193         timer_test_constants();
194         timer_test_delay();
195         timer_test_async();
196         timer_test_poll();
197         synctimer_test();
198         return 0;
199 }
200
201 int timer_testTearDown(void)
202 {
203         unsigned i;
204         for (i = 0; i < countof(test_timers); ++i)
205                 timer_abort(&test_timers[i]);
206         return 0;
207 }
208
209 TEST_MAIN(timer);
210