4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
33 * \brief Hardware independent timer driver (implementation)
35 * \author Bernie Innocenti <bernie@codewiz.org>
41 #include <drv/timer.h>
44 #include <mware/event.h>
46 #include <cfg/debug.h>
48 static void timer_test_constants(void)
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);
54 #ifdef TIMER1_OVF_COUNT
55 kprintf("TIMER1_OVF_COUNT = %lu\n", (unsigned long)TIMER1_OVF_COUNT);
57 kprintf("TIMER_TICKS_PER_SEC= %lu\n", (unsigned long)TIMER_TICKS_PER_SEC);
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));
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));
69 kprintf("hptime_to_us(100) = %lu\n", (unsigned long)hptime_to_us(100));
70 #if (SIZEOF_HPTIME_T > 1)
71 kprintf("hptime_to_us(10000)= %lu\n", (unsigned long)hptime_to_us(10000));
73 kprintf("us_to_hptime(100) = %lu\n", (unsigned long)us_to_hptime(100));
74 kprintf("us_to_hptime(10000)= %lu\n", (unsigned long)us_to_hptime(10000));
77 static void timer_test_delay(void)
81 kputs("Delay test\n");
82 for (i = 0; i < 1000; i += 100)
84 kprintf("delay %d...", i);
90 static void timer_test_hook(iptr_t _timer)
92 Timer *timer = (Timer *)(void *)_timer;
94 kprintf("Timer %lu expired\n", (unsigned long)ticks_to_ms(timer->_delay));
98 static Timer test_timers[5];
101 static Timer synctimer_timers[5];
103 static void synctimer_test_hook(iptr_t _timer)
105 Timer *timer = (Timer *)(void *)_timer;
106 kprintf("Sync timer process %lu expired\n", (unsigned long)ticks_to_ms(timer->_delay));
107 synctimer_add(timer, &synctimer_list);
111 static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
113 static void timer_test_async(void)
117 for (i = 0; i < countof(test_timers); ++i)
119 Timer *timer = &test_timers[i];
120 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
121 timer_setSoftint(timer, timer_test_hook, (iptr_t)timer);
126 static void timer_test_poll(void)
129 mtime_t start_time = ticks_to_ms(timer_clock());
134 now = ticks_to_ms(timer_clock());
135 if (now - start_time >= 1000)
139 kprintf("seconds = %d, ticks=%lu\n", secs, (unsigned long)now);
145 static void synctimer_test(void)
149 LIST_INIT(&synctimer_list);
150 for (i = 0; i < countof(synctimer_timers); ++i)
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);
159 mtime_t start_time = ticks_to_ms(timer_clock());
164 now = ticks_to_ms(timer_clock());
165 synctimer_poll(&synctimer_list);
166 if (now - start_time >= 1000)
170 kprintf("seconds = %d, ticks=%lu\n", secs, (unsigned long)now);
175 for (i = 0; i < countof(synctimer_timers); ++i)
177 synctimer_abort(&synctimer_timers[i]);
181 int timer_testSetup(void)
190 int timer_testRun(void)
192 timer_test_constants();
200 int timer_testTearDown(void)
203 for (i = 0; i < countof(test_timers); ++i)
204 timer_abort(&test_timers[i]);