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