Add test commands for configuration.
[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  * $test$: cp bertos/cfg/cfg_kern.h $cfgdir/
39  * $test$: echo  "#undef CONFIG_KERN" >> $cfgdir/cfg_kern.h
40  * $test$: echo "#define CONFIG_KERN 0" >> $cfgdir/cfg_kern.h
41  * $test$: echo  "#undef CONFIG_KERN_SCHED" >> $cfgdir/cfg_kern.h
42  * $test$: echo "#define CONFIG_KERN_SCHED 0" >> $cfgdir/cfg_kern.h
43  * $test$: echo  "#undef CONFIG_KERN_SIGNALS" >> $cfgdir/cfg_kern.h
44  * $test$: echo "#define CONFIG_KERN_SIGNALS 0" >> $cfgdir/cfg_kern.h
45  * $test$: echo  "#undef CONFIG_KERN_SEMAPHORES" >> $cfgdir/cfg_kern.h
46  * $test$: echo "#define CONFIG_KERN_SEMAPHORES 0" >> $cfgdir/cfg_kern.h
47  * $test$: echo  "#undef CONFIG_KERN_MONITOR" >> $cfgdir/cfg_kern.h
48  * $test$: echo "#define CONFIG_KERN_MONITOR 0" >> $cfgdir/cfg_kern.h
49  */
50
51 #include <cfg/test.h>
52
53 #include <drv/timer.h>
54 #include <drv/wdt.h>
55
56 #include <mware/event.h>
57
58 #include <cfg/debug.h>
59
60 static void timer_test_constants(void)
61 {
62         kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", (unsigned long)TIMER_HW_HPTICKS_PER_SEC);
63         #ifdef TIMER_PRESCALER
64                 kprintf("TIMER_PRESCALER    = %lu\n", (unsigned long)TIMER_PRESCALER);
65         #endif
66         #ifdef TIMER1_OVF_COUNT
67                 kprintf("TIMER1_OVF_COUNT   = %lu\n", (unsigned long)TIMER1_OVF_COUNT);
68         #endif
69         kprintf("TIMER_TICKS_PER_SEC= %lu\n",  (unsigned long)TIMER_TICKS_PER_SEC);
70         kprintf("\n");
71         kprintf("ms_to_ticks(100)   = %lu\n",   (unsigned long)ms_to_ticks(100));
72         kprintf("ms_to_ticks(10000) = %lu\n",   (unsigned long)ms_to_ticks(10000));
73         kprintf("us_to_ticks(100)   = %lu\n",   (unsigned long)us_to_ticks(100));
74         kprintf("us_to_ticks(10000) = %lu\n",   (unsigned long)us_to_ticks(10000));
75         kprintf("\n");
76         kprintf("ticks_to_ms(100)   = %lu\n",   (unsigned long)ticks_to_ms(100));
77         kprintf("ticks_to_ms(10000) = %lu\n",   (unsigned long)ticks_to_ms(10000));
78         kprintf("ticks_to_us(100)   = %lu\n",   (unsigned long)ticks_to_us(100));
79         kprintf("ticks_to_us(10000) = %lu\n",   (unsigned long)ticks_to_us(10000));
80         kprintf("\n");
81         kprintf("hptime_to_us(100)  = %lu\n",   (unsigned long)hptime_to_us(100));
82         kprintf("hptime_to_us(10000)= %lu\n",   (unsigned long)hptime_to_us(10000));
83         kprintf("us_to_hptime(100)  = %lu\n",   (unsigned long)us_to_hptime(100));
84         kprintf("us_to_hptime(10000)= %lu\n",   (unsigned long)us_to_hptime(10000));
85 }
86
87 static void timer_test_delay(void)
88 {
89         int i;
90
91         kputs("Delay test\n");
92         for (i = 0; i < 1000; i += 100)
93         {
94                 kprintf("delay %d...", i);
95                 timer_delay(i);
96                 kputs("done\n");
97         }
98 }
99
100 static void timer_test_hook(iptr_t _timer)
101 {
102         Timer *timer = (Timer *)(void *)_timer;
103
104         kprintf("Timer %lu expired\n", (unsigned long)ticks_to_ms(timer->_delay));
105         timer_add(timer);
106 }
107
108 static Timer test_timers[5];
109 static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
110
111 static void timer_test_async(void)
112 {
113         size_t i;
114
115         for (i = 0; i < countof(test_timers); ++i)
116         {
117                 Timer *timer = &test_timers[i];
118                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
119                 timer_setSoftint(timer, timer_test_hook, (iptr_t)timer);
120                 timer_add(timer);
121         }
122 }
123
124 static void timer_test_poll(void)
125 {
126         int secs = 0;
127         mtime_t start_time = ticks_to_ms(timer_clock());
128         mtime_t now;
129
130         while (secs <= 10)
131         {
132                 now = ticks_to_ms(timer_clock());
133                 if (now - start_time >= 1000)
134                 {
135                         ++secs;
136                         start_time += 1000;
137                         kprintf("seconds = %d, ticks=%lu\n", secs, (unsigned long)now);
138                 }
139                 wdt_reset();
140         }
141 }
142
143 int timer_testSetup(void)
144 {
145         IRQ_ENABLE;
146         wdt_start(7);
147         timer_init();
148         kdbg_init();
149         return 0;
150 }
151
152 int timer_testRun(void)
153 {
154         timer_test_constants();
155         timer_test_delay();
156         timer_test_async();
157         timer_test_poll();
158         return 0;
159 }
160
161 int timer_testTearDown(void)
162 {
163         unsigned i;
164         for (i = 0; i < countof(test_timers); ++i)
165                 timer_abort(&test_timers[i]);
166         return 0;
167 }
168
169 TEST_MAIN(timer);
170