altos: Make quadrature debounce per-pin rather than per-device
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_quadrature.h>
20 #include <ao_exti.h>
21 #include <ao_fast_timer.h>
22 #include <ao_event.h>
23
24 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
25 static uint8_t  ao_quadrature_state[AO_QUADRATURE_COUNT];
26
27 struct ao_debounce {
28         uint8_t state;
29         uint8_t count;
30 };
31
32 static struct ao_debounce ao_debounce_state[AO_QUADRATURE_COUNT][2];
33
34 #define port(q) AO_QUADRATURE_ ## q ## _PORT
35 #define bita(q) AO_QUADRATURE_ ## q ## _A
36 #define bitb(q) AO_QUADRATURE_ ## q ## _B
37 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
38 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
39 #define isr(q)  ao_quadrature_isr_ ## q
40
41 #define DEBOUNCE        10
42
43 static uint8_t
44 ao_debounce(uint8_t cur, struct ao_debounce *debounce)
45 {
46         if (cur == debounce->state)
47                 debounce->count = 0;
48         else {
49                 if (++debounce->count == DEBOUNCE) {
50                         debounce->state = cur;
51                         debounce->count = 0;
52                 }
53         }
54         return debounce->state;
55 }
56
57 static uint16_t
58 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) {
59         uint16_t        v = ~stm_gpio_get_all(gpio);
60         uint8_t         a = (v >> pin_a) & 1;
61         uint8_t         b = (v >> pin_b) & 1;
62
63         a = ao_debounce(a, &debounce_state[0]);
64         b = ao_debounce(b, &debounce_state[1]);
65
66         return a | (b << 1);
67 }
68
69 #define _ao_quadrature_get(q)   ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q])
70
71 static void
72 _ao_quadrature_queue(uint8_t q, int8_t step)
73 {
74         ao_quadrature_count[q] += step;
75 #if AO_EVENT
76         ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
77 #endif
78         ao_wakeup(&ao_quadrature_count[q]);
79 }
80
81 static void
82 _ao_quadrature_set(uint8_t q, uint8_t new) {
83         uint8_t old = ao_quadrature_state[q];
84
85         if (old != new && new == 0) {
86                 if (old & 2)
87                         _ao_quadrature_queue(q, 1);
88                 else if (old & 1)
89                         _ao_quadrature_queue(q, -1);
90         }
91         ao_quadrature_state[q] = new;
92 }
93
94 static void
95 ao_quadrature_isr(void)
96 {
97 #if AO_QUADRATURE_COUNT > 0
98         _ao_quadrature_set(0, _ao_quadrature_get(0));
99 #endif
100 #if AO_QUADRATURE_COUNT > 1
101         _ao_quadrature_set(1, _ao_quadrature_get(1));
102 #endif
103 }
104
105 int32_t
106 ao_quadrature_poll(uint8_t q)
107 {
108         int32_t ret;
109         ao_arch_critical(ret = ao_quadrature_count[q];);
110         return ret;
111 }
112
113 int32_t
114 ao_quadrature_wait(uint8_t q)
115 {
116         ao_sleep(&ao_quadrature_count[q]);
117         return ao_quadrature_poll(q);
118 }
119
120 static void
121 ao_quadrature_test(void)
122 {
123         uint8_t q;
124         int32_t c;
125         uint8_t s;
126
127         ao_cmd_decimal();
128         q = ao_cmd_lex_i;
129         if (q >= AO_QUADRATURE_COUNT) {
130                 ao_cmd_status = ao_cmd_syntax_error;
131                 return;
132         }
133
134         c = -10000;
135         s = 0;
136         while (ao_quadrature_count[q] != 10) {
137                 if (ao_quadrature_count[q] != c ||
138                     ao_quadrature_state[q] != s) {
139                         c = ao_quadrature_count[q];
140                         s = ao_quadrature_state[q];
141                         printf ("count %3d state %2x\n", c, s);
142                         flush();
143                 }
144         }
145 #if 0
146         for (;;) {
147                 int32_t c;
148                 flush();
149                 c = ao_quadrature_wait(q);
150                 printf ("new count %6d\n", c);
151                 if (c == 100)
152                         break;
153         }
154 #endif
155 }
156
157 static const struct ao_cmds ao_quadrature_cmds[] = {
158         { ao_quadrature_test,   "q <unit>\0Test quadrature" },
159         { 0, NULL }
160 };
161
162 #define init(q) do {                                    \
163                 ao_enable_input(port(q), bita(q), 0);   \
164                 ao_enable_input(port(q), bitb(q), 0);   \
165         } while (0)
166
167 void
168 ao_quadrature_init(void)
169 {
170 #if AO_QUADRATURE_COUNT > 0
171         init(0);
172 #endif
173 #if AO_QUADRATURE_COUNT > 1
174         init(1);
175 #endif
176         ao_fast_timer_init();
177         ao_fast_timer_on(ao_quadrature_isr);
178         ao_cmd_register(&ao_quadrature_cmds[0]);
179 }