altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / kernel / ao_debounce.c
1 /*
2  * Copyright © 2013 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_debounce.h>
21 #include <ao_fast_timer.h>
22
23 static uint8_t                  ao_debounce_initialized;
24 static uint8_t                  ao_debounce_running;
25 static struct ao_debounce       *ao_debounce;
26
27 static uint8_t  values[64];
28 static uint8_t  n;
29
30 #define d_step(n)       (((n) + 1) & 63)
31
32 static void
33 _ao_debounce_set(struct ao_debounce *debounce, uint8_t value)
34 {
35         if (value != debounce->value) {
36                 values[n] = value;
37                 n = (n + 1) & 63;
38                 debounce->value = value;
39                 debounce->_set(debounce, value);
40         }
41         _ao_debounce_stop(debounce);
42 }
43
44 void
45 ao_debounce_dump(void)
46 {
47         uint8_t s;
48
49         for (s = 0; s < n; s++) {
50                 printf ("%d: %d\n",
51                         s, values[s]);
52         }
53         n = 0;
54 }
55
56 /*
57  * Get the current value, set the result when we've
58  * reached the debounce count limit
59  */
60 static void
61 _ao_debounce_check(struct ao_debounce *debounce)
62 {
63         uint8_t next = debounce->_get(debounce);
64
65         if (next == debounce->current) {
66                 if (debounce->count < debounce->hold) {
67                         if (++debounce->count == debounce->hold)
68                                 _ao_debounce_set(debounce, debounce->current);
69                 }
70         } else {
71                 debounce->count = 0;
72                 debounce->current = next;
73         }
74 }
75
76 static void
77 _ao_debounce_isr(void)
78 {
79         struct ao_debounce *debounce, *next;
80
81         for (debounce = ao_debounce; debounce; debounce = next) {
82                 next = debounce->next;
83                 _ao_debounce_check(debounce);
84         }
85 }
86
87 static void
88 ao_debounce_on(void)
89 {
90         ao_fast_timer_on(_ao_debounce_isr);
91 }
92
93 static void
94 ao_debounce_off(void)
95 {
96         ao_fast_timer_off(_ao_debounce_isr);
97 }
98
99 /*
100  * Start monitoring one pin
101  */
102 void
103 _ao_debounce_start(struct ao_debounce *debounce)
104 {
105         uint32_t        m;
106
107         m = ao_arch_irqsave();
108         if (!debounce->running) {
109                 debounce->running = 1;
110
111                 /* Reset the counter */
112                 debounce->count = 0;
113
114                 /* Link into list */
115                 debounce->next = ao_debounce;
116                 ao_debounce = debounce;
117
118                 /* Make sure the timer is running */
119                 if (!ao_debounce_running++)
120                         ao_debounce_on();
121
122                 /* And go check the current value */
123                 _ao_debounce_check(debounce);
124         }
125         ao_arch_irqrestore(m);
126 }
127
128 /*
129  * Stop monitoring one pin
130  */
131 void
132 _ao_debounce_stop(struct ao_debounce *debounce)
133 {
134         struct ao_debounce **prev;
135         uint32_t m;
136
137         m = ao_arch_irqsave();
138         if (debounce->running) {
139                 debounce->running = 0;
140
141                 /* Unlink */
142                 for (prev = &ao_debounce; (*prev); prev = &((*prev)->next)) {
143                         if (*prev == debounce) {
144                                 *prev = debounce->next;
145                                 break;
146                         }
147                 }
148                 debounce->next = NULL;
149
150                 /* Turn off the timer if possible */
151                 if (!--ao_debounce_running)
152                         ao_debounce_off();
153         }
154         ao_arch_irqrestore(m);
155 }
156
157 void
158 ao_debounce_init(void)
159 {
160         if (ao_debounce_initialized)
161                 return;
162         ao_debounce_initialized = 1;
163         ao_fast_timer_init();
164 }