callout: add a new module which is an alternative to the scheduler
[aversive.git] / modules / base / callout / test / main.c
1 /*
2  * Copyright (c) <2014>, Olivier Matz <zer0@droids-corp.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of the University of California, Berkeley nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <stdarg.h>
30
31 #include <aversive/wait.h>
32
33 #include <callout.h>
34 #include <timer.h>
35 #include <uart.h>
36 #include <hostsim.h>
37
38 static struct callout_mgr global_cm;
39 static volatile uint16_t time_ref;
40
41 #define C1_TIME_MS 500
42 #define C2_TIME_MS 500
43 #define C3_TIME_MS 1000
44 #define C4_TIME_MS 4000
45
46 /* return the current time reference (the variable is incremented in the
47  * interuption */
48 static uint16_t get_time_ms(void)
49 {
50         uint8_t flags;
51         uint16_t ret;
52
53         IRQ_LOCK(flags);
54         ret = time_ref;
55         IRQ_UNLOCK(flags);
56         return ret;
57 }
58
59 /* printf with a timestamp */
60 static void ts_printf(const char *fmt, ...)
61 {
62         va_list ap;
63
64         printf("%d: ", get_time_ms());
65         va_start(ap, fmt);
66         vfprintf(stdout, fmt, ap);
67         va_end(ap);
68 }
69
70 static void f1(struct callout_mgr *cm, struct callout *tim, void *arg)
71 {
72         (void)arg;
73         ts_printf("%s\n", __FUNCTION__);
74
75         callout_reschedule(cm, tim, C1_TIME_MS);
76 }
77
78 static void f2(struct callout_mgr *cm, struct callout *tim, void *arg)
79 {
80         (void)arg;
81         ts_printf("%s\n", __FUNCTION__);
82
83         callout_reschedule(cm, tim, C2_TIME_MS);
84 }
85
86 static void f3(struct callout_mgr *cm, struct callout *tim, void *arg)
87 {
88         (void)arg;
89         ts_printf("%s START\n", __FUNCTION__);
90         wait_ms(600);
91         ts_printf("%s END\n", __FUNCTION__);
92
93         callout_reschedule(cm, tim, C3_TIME_MS);
94 }
95
96 static void supp(struct callout_mgr *cm, struct callout *tim, void *arg)
97 {
98         struct callout *c2 = arg;
99
100         ts_printf("stopping c1\n");
101         callout_stop(cm, c2);
102 }
103
104 static void timer_interrupt(void)
105 {
106 #ifndef HOST_VERSION
107         static uint16_t cycles;
108
109         cycles += 256 * 8; /* 8 bits timer + 8 divisor */
110         if (cycles > (CONFIG_QUARTZ/1000)) {
111                 cycles -= (CONFIG_QUARTZ/1000);
112                 time_ref++;
113         }
114 #else
115         time_ref++;
116 #endif
117
118         sei(); /* callout_manage() must be called with irq allowed */
119         callout_manage(&global_cm);
120 }
121
122 static void dump_stats(char c)
123 {
124         if (c != 's')
125                 return;
126
127         callout_dump_stats(&global_cm);
128 }
129
130 int main(void)
131 {
132         struct callout c1, c2, c3, c4;
133         uint8_t old_prio;
134
135 #ifdef HOST_VERSION
136         hostsim_uart_init();
137         hostsim_ittimer_add(timer_interrupt, 1 * 1000 * 1000); /* 1ms period */
138         hostsim_ittimer_enable(100); /* 100 us */
139 #else
140         uart_init();
141         fdevopen(uart0_dev_send, uart0_dev_recv);
142         timer_init();
143         timer0_register_OV_intr(timer_interrupt);
144 #endif
145         uart_register_rx_event(0, dump_stats);
146
147         callout_mgr_init(&global_cm, get_time_ms);
148         sei();
149
150         printf("f1 every %d ms, high prio (200)\n", C1_TIME_MS);
151         printf("f2 every %d ms, low prio (50)\n", C2_TIME_MS);
152         printf("f3 every %d ms, med prio (100), the function lasts 600ms\n",
153                 C3_TIME_MS);
154         printf("f4 only once after %d ms, very high prio (250), "
155                 "stop task f2\n", C4_TIME_MS);
156         printf("type s to dump stats\n");
157
158         callout_init(&c1, f1, NULL, 200);
159         callout_init(&c2, f2, NULL, 50);
160         callout_init(&c3, f3, NULL, 100);
161         callout_init(&c4, supp, &c2, 250);
162
163         callout_schedule(&global_cm, &c1, C1_TIME_MS);
164         callout_schedule(&global_cm, &c2, C2_TIME_MS);
165         callout_schedule(&global_cm, &c3, C3_TIME_MS);
166         callout_schedule(&global_cm, &c4, C4_TIME_MS);
167
168         while (get_time_ms() < 2900)
169                 ;
170
171         old_prio = callout_mgr_set_prio(&global_cm, 150);
172         ts_printf("set prio 150\n");
173
174         while (get_time_ms() < 3100)
175                 ;
176
177         ts_printf("set prio 0\n");
178         callout_mgr_restore_prio(&global_cm, old_prio);
179
180         while (get_time_ms() < 5000)
181                 ;
182
183         callout_stop(&global_cm, &c1);
184         callout_stop(&global_cm, &c3);
185         callout_stop(&global_cm, &c4);
186
187         callout_dump_stats(&global_cm);
188         wait_ms(10);
189
190 #ifdef HOST_VERSION
191         hostsim_uart_exit();
192 #endif
193
194         return 0;
195 }