altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / drivers / ao_event.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_event.h>
21
22 #define AO_EVENT_QUEUE  64
23
24 #define ao_event_queue_next(n)  (((n) + 1) & (AO_EVENT_QUEUE - 1))
25 #define ao_event_queue_prev(n)  (((n) - 1) & (AO_EVENT_QUEUE - 1))
26 #define ao_event_queue_empty()  (ao_event_queue_insert == ao_event_queue_remove)
27 #define ao_event_queue_full()   (ao_event_queue_next(ao_event_queue_insert) == ao_event_queue_remove)
28
29 struct ao_event ao_event_queue[AO_EVENT_QUEUE];
30 uint8_t         ao_event_queue_insert;
31 uint8_t         ao_event_queue_remove;
32
33
34 void
35 ao_event_get(struct ao_event *ev)
36 {
37         ao_arch_critical(
38                 while (ao_event_queue_empty())
39                         ao_sleep(&ao_event_queue);
40                 *ev = ao_event_queue[ao_event_queue_remove];
41                 ao_event_queue_remove = ao_event_queue_next(ao_event_queue_remove);
42                 );
43 }
44
45 uint8_t
46 ao_event_get_for(struct ao_event *ev, AO_TICK_TYPE timeout)
47 {
48         uint8_t empty = 1;
49         ao_arch_critical(
50                 while ((empty = ao_event_queue_empty()))
51                         if (ao_sleep_for(&ao_event_queue, timeout))
52                                 break;
53                 if (!empty) {
54                         *ev = ao_event_queue[ao_event_queue_remove];
55                         ao_event_queue_remove = ao_event_queue_next(ao_event_queue_remove);
56                 }
57                 );
58         return empty;
59 }
60
61 /* called with interrupts disabled */
62 void
63 ao_event_put_isr(uint8_t type, uint8_t unit, int32_t value)
64 {
65         if (!ao_event_queue_full()) {
66                 ao_event_queue[ao_event_queue_insert] = (struct ao_event) {
67                         .type = type,
68                         .unit = unit,
69                         .tick = ao_tick_count,
70                         .value = value
71                 };
72                 ao_event_queue_insert = ao_event_queue_next(ao_event_queue_insert);
73                 ao_wakeup(&ao_event_queue);
74         }
75 }
76
77 void
78 ao_event_put(uint8_t type, uint8_t unit, int32_t value)
79 {
80         ao_arch_critical(ao_event_put_isr(type, unit, value););
81 }