2 * Copyright (c) <2014>, Olivier Matz <zer0@droids-corp.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
31 #include <aversive/wait.h>
38 static struct callout_mgr global_cm;
39 static volatile uint16_t time_ref;
41 #define C1_TIME_MS 500
42 #define C2_TIME_MS 500
43 #define C3_TIME_MS 1000
44 #define C4_TIME_MS 4000
46 /* return the current time reference (the variable is incremented in the
48 static uint16_t get_time_ms(void)
59 /* printf with a timestamp */
60 static void ts_printf(const char *fmt, ...)
64 printf("%d: ", get_time_ms());
66 vfprintf(stdout, fmt, ap);
70 static void f1(struct callout_mgr *cm, struct callout *tim, void *arg)
73 ts_printf("%s\n", __FUNCTION__);
75 callout_reschedule(cm, tim, C1_TIME_MS);
78 static void f2(struct callout_mgr *cm, struct callout *tim, void *arg)
81 ts_printf("%s\n", __FUNCTION__);
83 callout_reschedule(cm, tim, C2_TIME_MS);
86 static void f3(struct callout_mgr *cm, struct callout *tim, void *arg)
89 ts_printf("%s START\n", __FUNCTION__);
91 ts_printf("%s END\n", __FUNCTION__);
93 callout_reschedule(cm, tim, C3_TIME_MS);
96 static void supp(struct callout_mgr *cm, struct callout *tim, void *arg)
98 struct callout *c2 = arg;
100 ts_printf("stopping c1\n");
101 callout_stop(cm, c2);
104 static void timer_interrupt(void)
107 static uint16_t cycles;
109 cycles += 256 * 8; /* 8 bits timer + 8 divisor */
110 if (cycles > (CONFIG_QUARTZ/1000)) {
111 cycles -= (CONFIG_QUARTZ/1000);
118 sei(); /* callout_manage() must be called with irq allowed */
119 callout_manage(&global_cm);
122 static void dump_stats(char c)
127 callout_dump_stats(&global_cm);
132 struct callout c1, c2, c3, c4;
137 hostsim_ittimer_add(timer_interrupt, 1 * 1000 * 1000); /* 1ms period */
138 hostsim_ittimer_enable(100); /* 100 us */
141 fdevopen(uart0_dev_send, uart0_dev_recv);
143 timer0_register_OV_intr(timer_interrupt);
145 uart_register_rx_event(0, dump_stats);
147 callout_mgr_init(&global_cm, get_time_ms);
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",
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");
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);
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);
168 while (get_time_ms() < 2900)
171 old_prio = callout_mgr_set_prio(&global_cm, 150);
172 ts_printf("set prio 150\n");
174 while (get_time_ms() < 3100)
177 ts_printf("set prio 0\n");
178 callout_mgr_restore_prio(&global_cm, old_prio);
180 while (get_time_ms() < 5000)
183 callout_stop(&global_cm, &c1);
184 callout_stop(&global_cm, &c3);
185 callout_stop(&global_cm, &c4);
187 callout_dump_stats(&global_cm);