Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
[bertos.git] / drv / timer_test.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Hardware independent timer driver (implementation)
9  *
10  * \version $Id$
11  * \author Bernardo Innocenti <bernie@develer.com>
12  */
13
14 /*#*
15  *#* $Log$
16  *#* Revision 1.2  2005/11/27 03:58:18  bernie
17  *#* Add POSIX timer emulator.
18  *#*
19  *#* Revision 1.1  2005/11/27 03:04:08  bernie
20  *#* Move test code to timer_test.c; Add OS_HOSTED support.
21  *#*
22  *#*/
23
24 #include "timer.c"
25 #include "mware/event.c"
26 #include "os/hptime.c"
27
28 static void timer_test_constants(void)
29 {
30         kprintf("TIMER_HW_HPTICKS_PER_SEC=%lu\n", TIMER_HW_HPTICKS_PER_SEC);
31         #ifdef TIMER_PRESCALER
32                 kprintf("TIMER_PRESCALER=%d\n", TIMER_PRESCALER);
33         #endif
34         #ifdef TIMER1_OVF_COUNT
35                 kprintf("TIMER1_OVF_COUNT=%d\n", (int)TIMER1_OVF_COUNT);
36         #endif
37         kprintf("TIMER_TICKS_PER_MSEC=%d\n", (int)TIMER_TICKS_PER_MSEC);
38         kprintf("\n");
39         kprintf("ms_to_ticks(100)=%lu\n", ms_to_ticks(100));
40         kprintf("ms_to_ticks(10000)=%lu\n", ms_to_ticks(10000));
41         kprintf("us_to_ticks(100)=%lu\n", us_to_ticks(100));
42         kprintf("us_to_ticks(10000)=%lu\n", us_to_ticks(10000));
43         kprintf("\n");
44         kprintf("ticks_to_ms(100)=%lu\n", ticks_to_ms(100));
45         kprintf("ticks_to_ms(10000)=%lu\n", ticks_to_ms(10000));
46         kprintf("ticks_to_us(100)=%lu\n", ticks_to_us(100));
47         kprintf("ticks_to_us(10000)=%lu\n", ticks_to_us(10000));
48         kprintf("\n");
49         kprintf("hptime_to_us(100)=%lu\n", hptime_to_us(100));
50         kprintf("hptime_to_us(10000)=%lu\n", hptime_to_us(10000));
51         kprintf("us_to_hptime(100)=%lu\n", us_to_hptime(100));
52         kprintf("us_to_hptime(10000)=%lu\n", us_to_hptime(10000));
53 }
54
55 static void timer_test_delay(void)
56 {
57         int i;
58
59         kputs("Delay test\n");
60         for (i = 0; i < 1000; i += 100)
61         {
62                 kprintf("delay %d...", i);
63                 timer_delay(i);
64                 kputs("done\n");
65         }
66 }
67
68 static void timer_test_hook(iptr_t _timer)
69 {
70         Timer *timer = (Timer *)(void *)_timer;
71
72         kprintf("Timer %ld expired\n", ticks_to_ms(timer->_delay));
73         timer_add(timer);
74 }
75
76 static void timer_test_async(void)
77 {
78         static Timer test_timers[5];
79         static const mtime_t test_delays[5] = { 170, 50, 310, 1500, 310 };
80         size_t i;
81
82         for (i = 0; i < countof(test_timers); ++i)
83         {
84                 Timer *timer = &test_timers[i];
85                 timer_setDelay(timer, ms_to_ticks(test_delays[i]));
86                 timer_set_event_softint(timer, timer_test_hook, (iptr_t)timer);
87                 timer_add(timer);
88         }
89 }
90
91 static void timer_test_poll(void)
92 {
93         int secs = 0;
94         mtime_t start_time = ticks_to_ms(timer_clock());
95         mtime_t now;
96
97         while (secs <= 10)
98         {
99                 now = ticks_to_ms(timer_clock());
100                 if (now - start_time >= 1000)
101                 {
102                         ++secs;
103                         start_time += 1000;
104                         kprintf("seconds = %d, ticks=%ld\n", secs, now);
105                 }
106                 wdt_reset();
107         }
108 }
109
110 int main(void)
111 {
112         wdt_init(7);
113         timer_init();
114         timer_test_constants();
115         timer_test_delay();
116         timer_test_async();
117         timer_test_poll();
118 }