Mark these test module with warning.
[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 Bernardo Innocenti <bernie@develer.com>
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
47 static void timer_test_constants(void)
48 {
49         kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", TIMER_HW_HPTICKS_PER_SEC);
50         #ifdef TIMER_PRESCALER
51                 kprintf("TIMER_PRESCALER=%d\n", TIMER_PRESCALER);
52         #endif
53         #ifdef TIMER1_OVF_COUNT
54                 kprintf("TIMER1_OVF_COUNT=%d\n", (int)TIMER1_OVF_COUNT);
55         #endif
56         kprintf("TIMER_TICKS_PER_SEC=%d\n", (int)TIMER_TICKS_PER_SEC);
57         kprintf("\n");
58         kprintf("ms_to_ticks(100)=%lu\n", ms_to_ticks(100));
59         kprintf("ms_to_ticks(10000)=%lu\n", ms_to_ticks(10000));
60         kprintf("us_to_ticks(100)=%lu\n", us_to_ticks(100));
61         kprintf("us_to_ticks(10000)=%lu\n", us_to_ticks(10000));
62         kprintf("\n");
63         kprintf("ticks_to_ms(100)=%lu\n", ticks_to_ms(100));
64         kprintf("ticks_to_ms(10000)=%lu\n", ticks_to_ms(10000));
65         kprintf("ticks_to_us(100)=%lu\n", ticks_to_us(100));
66         kprintf("ticks_to_us(10000)=%lu\n", ticks_to_us(10000));
67         kprintf("\n");
68         kprintf("hptime_to_us(100)=%lu\n", hptime_to_us(100));
69         kprintf("hptime_to_us(10000)=%lu\n", hptime_to_us(10000));
70         kprintf("us_to_hptime(100)=%lu\n", us_to_hptime(100));
71         kprintf("us_to_hptime(10000)=%lu\n", us_to_hptime(10000));
72 }
73
74 static void timer_test_delay(void)
75 {
76         int i;
77
78         kputs("Delay test\n");
79         for (i = 0; i < 1000; i += 100)
80         {
81                 kprintf("delay %d...", i);
82                 timer_delay(i);
83                 kputs("done\n");
84         }
85 }
86
87 static void timer_test_hook(iptr_t _timer)
88 {
89         Timer *timer = (Timer *)(void *)_timer;
90
91         kprintf("Timer %ld expired\n", ticks_to_ms(timer->_delay));
92         timer_add(timer);
93 }
94
95 static void timer_test_async(void)
96 {
97         static Timer test_timers[5];
98         static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
99         size_t i;
100
101         for (i = 0; i < countof(test_timers); ++i)
102         {
103                 Timer *timer = &test_timers[i];
104                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
105                 timer_set_event_softint(timer, timer_test_hook, (iptr_t)timer);
106                 timer_add(timer);
107         }
108 }
109
110 static void timer_test_poll(void)
111 {
112         int secs = 0;
113         mtime_t start_time = ticks_to_ms(timer_clock());
114         mtime_t now;
115
116         while (secs <= 10)
117         {
118                 now = ticks_to_ms(timer_clock());
119                 if (now - start_time >= 1000)
120                 {
121                         ++secs;
122                         start_time += 1000;
123                         kprintf("seconds = %d, ticks=%ld\n", secs, now);
124                 }
125                 wdt_reset();
126         }
127 }
128
129 #ifdef _TEST
130
131 #include "timer.c"
132 #include "mware/event.c"
133 #include "os/hptime.c"
134
135 int main(void)
136 {
137         wdt_init(7);
138         timer_init();
139         timer_test_constants();
140         timer_test_delay();
141         timer_test_async();
142         timer_test_poll();
143         return 0;
144 }
145 #endif
146