altos/drivers: Make quadrature debounce longer by default. Fix state tracking
[fw/altos] / src / drivers / ao_quadrature.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_quadrature.h>
21 #include <ao_exti.h>
22 #include <ao_fast_timer.h>
23 #include <ao_event.h>
24
25 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
26 static uint8_t  ao_quadrature_state[AO_QUADRATURE_COUNT];
27
28 struct ao_debounce {
29         uint8_t state;
30         uint8_t count;
31 };
32
33 static struct ao_debounce ao_debounce_state[AO_QUADRATURE_COUNT][2];
34
35 #define port(q) AO_QUADRATURE_ ## q ## _PORT
36 #define bita(q) AO_QUADRATURE_ ## q ## _A
37 #define bitb(q) AO_QUADRATURE_ ## q ## _B
38 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
39 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
40 #define isr(q)  ao_quadrature_isr_ ## q
41
42 #ifndef AO_QUADRATURE_DEBOUNCE
43 #define AO_QUADRATURE_DEBOUNCE  30
44 #endif
45
46 static uint8_t
47 ao_debounce(uint8_t cur, struct ao_debounce *debounce)
48 {
49 #if AO_QUADRATURE_DEBOUNCE > 0
50         if (debounce->count > 0) {
51                 debounce->count--;
52         } else if (cur != debounce->state) {
53                 debounce->state = cur;
54                 debounce->count = AO_QUADRATURE_DEBOUNCE;
55         }
56         return debounce->state;
57 #else
58         return cur;
59 #endif
60 }
61
62 static uint16_t
63 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) {
64         uint16_t        v = ~stm_gpio_get_all(gpio);
65         uint8_t         a = (v >> pin_a) & 1;
66         uint8_t         b = (v >> pin_b) & 1;
67
68         a = ao_debounce(a, &debounce_state[0]);
69         b = ao_debounce(b, &debounce_state[1]);
70
71         return a | (b << 1);
72 }
73
74 #define _ao_quadrature_get(q)   ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q])
75
76 static void
77 _ao_quadrature_queue(uint8_t q, int8_t step)
78 {
79         ao_quadrature_count[q] += step;
80 #if AO_EVENT
81         ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
82 #endif
83         ao_wakeup(&ao_quadrature_count[q]);
84 }
85
86 static void
87 _ao_quadrature_set(uint8_t q, uint8_t new) {
88         uint8_t old = ao_quadrature_state[q];
89
90         if (old != new && new == 0) {
91                 if (old == 2)
92                         _ao_quadrature_queue(q, 1);
93                 else if (old == 1)
94                         _ao_quadrature_queue(q, -1);
95         }
96         ao_quadrature_state[q] = new;
97 }
98
99 static void
100 ao_quadrature_isr(void)
101 {
102 #if AO_QUADRATURE_COUNT > 0
103         _ao_quadrature_set(0, _ao_quadrature_get(0));
104 #endif
105 #if AO_QUADRATURE_COUNT > 1
106         _ao_quadrature_set(1, _ao_quadrature_get(1));
107 #endif
108 }
109
110 int32_t
111 ao_quadrature_poll(uint8_t q)
112 {
113         int32_t ret;
114         ao_arch_critical(ret = ao_quadrature_count[q];);
115         return ret;
116 }
117
118 int32_t
119 ao_quadrature_wait(uint8_t q)
120 {
121         ao_sleep(&ao_quadrature_count[q]);
122         return ao_quadrature_poll(q);
123 }
124
125 static void
126 ao_quadrature_test(void)
127 {
128         uint8_t q;
129         int32_t c;
130         uint8_t s;
131
132         ao_cmd_decimal();
133         q = ao_cmd_lex_i;
134         if (q >= AO_QUADRATURE_COUNT) {
135                 ao_cmd_status = ao_cmd_syntax_error;
136                 return;
137         }
138
139         c = -10000;
140         s = 0;
141         while (ao_quadrature_count[q] != 10) {
142                 if (ao_quadrature_count[q] != c ||
143                     ao_quadrature_state[q] != s) {
144                         c = ao_quadrature_count[q];
145                         s = ao_quadrature_state[q];
146                         printf ("count %3d state %2x\n", c, s);
147                         flush();
148                 }
149         }
150 #if 0
151         for (;;) {
152                 int32_t c;
153                 flush();
154                 c = ao_quadrature_wait(q);
155                 printf ("new count %6d\n", c);
156                 if (c == 100)
157                         break;
158         }
159 #endif
160 }
161
162 static const struct ao_cmds ao_quadrature_cmds[] = {
163         { ao_quadrature_test,   "q <unit>\0Test quadrature" },
164         { 0, NULL }
165 };
166
167 #define init(q) do {                                    \
168                 ao_enable_input(port(q), bita(q), 0);   \
169                 ao_enable_input(port(q), bitb(q), 0);   \
170         } while (0)
171
172 void
173 ao_quadrature_init(void)
174 {
175 #if AO_QUADRATURE_COUNT > 0
176         init(0);
177 #endif
178 #if AO_QUADRATURE_COUNT > 1
179         init(1);
180 #endif
181         ao_fast_timer_init();
182         ao_fast_timer_on(ao_quadrature_isr);
183         ao_cmd_register(&ao_quadrature_cmds[0]);
184 }