altos/driver: Add support for one-step-per-click quadrature encoder
[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         (void) debounce;
59         return cur;
60 #endif
61 }
62
63 static uint16_t
64 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) {
65         uint16_t        v = ~stm_gpio_get_all(gpio);
66         uint8_t         a = (v >> pin_a) & 1;
67         uint8_t         b = (v >> pin_b) & 1;
68
69         a = ao_debounce(a, &debounce_state[0]);
70         b = ao_debounce(b, &debounce_state[1]);
71
72         return a | (b << 1);
73 }
74
75 #define _ao_quadrature_get(q)   ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q])
76
77 static void
78 _ao_quadrature_queue(uint8_t q, int8_t step)
79 {
80         ao_quadrature_count[q] += step;
81 #if AO_EVENT
82         ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
83 #endif
84         ao_wakeup(&ao_quadrature_count[q]);
85 }
86
87 #if AO_QUADRATURE_SINGLE_CODE
88 struct ao_quadrature_step {
89         uint8_t inc;
90         uint8_t dec;
91 };
92
93 static struct ao_quadrature_step ao_quadrature_steps[4] = {
94         [0] = { .inc = 1, .dec = 2 },
95         [1] = { .inc = 3, .dec = 0 },
96         [3] = { .inc = 2, .dec = 1 },
97         [2] = { .inc = 0, .dec = 3 },
98 };
99 #endif
100
101 static void
102 _ao_quadrature_set(uint8_t q, uint8_t new) {
103         uint8_t old = ao_quadrature_state[q];
104
105 #ifdef AO_QUADRATURE_SINGLE_CODE
106         if (new == ao_quadrature_steps[old].inc) {
107                 _ao_quadrature_queue(q, 1);
108         } else if (new == ao_quadrature_steps[old].dec) {
109                 _ao_quadrature_queue(q, -1);
110         }
111 #else
112         if (old != new && new == 0) {
113                 if (old == 2)
114                         _ao_quadrature_queue(q, 1);
115                 else if (old == 1)
116                         _ao_quadrature_queue(q, -1);
117         }
118 #endif
119         ao_quadrature_state[q] = new;
120 }
121
122 static void
123 ao_quadrature_isr(void)
124 {
125 #if AO_QUADRATURE_COUNT > 0
126         _ao_quadrature_set(0, _ao_quadrature_get(0));
127 #endif
128 #if AO_QUADRATURE_COUNT > 1
129         _ao_quadrature_set(1, _ao_quadrature_get(1));
130 #endif
131 }
132
133 int32_t
134 ao_quadrature_poll(uint8_t q)
135 {
136         int32_t ret;
137         ao_arch_critical(ret = ao_quadrature_count[q];);
138         return ret;
139 }
140
141 int32_t
142 ao_quadrature_wait(uint8_t q)
143 {
144         ao_sleep(&ao_quadrature_count[q]);
145         return ao_quadrature_poll(q);
146 }
147
148 static void
149 ao_quadrature_test(void)
150 {
151         uint8_t q;
152         int32_t c;
153         uint8_t s;
154
155         ao_cmd_decimal();
156         q = ao_cmd_lex_i;
157         if (q >= AO_QUADRATURE_COUNT) {
158                 ao_cmd_status = ao_cmd_syntax_error;
159                 return;
160         }
161
162         c = -10000;
163         s = 0;
164         while (ao_quadrature_count[q] != 10) {
165                 if (ao_quadrature_count[q] != c ||
166                     ao_quadrature_state[q] != s) {
167                         c = ao_quadrature_count[q];
168                         s = ao_quadrature_state[q];
169                         printf ("count %3d state %2x\n", c, s);
170                         flush();
171                 }
172         }
173 #if 0
174         for (;;) {
175                 int32_t c;
176                 flush();
177                 c = ao_quadrature_wait(q);
178                 printf ("new count %6d\n", c);
179                 if (c == 100)
180                         break;
181         }
182 #endif
183 }
184
185 static const struct ao_cmds ao_quadrature_cmds[] = {
186         { ao_quadrature_test,   "q <unit>\0Test quadrature" },
187         { 0, NULL }
188 };
189
190 #define init(q) do {                                    \
191                 ao_enable_input(port(q), bita(q), 0);   \
192                 ao_enable_input(port(q), bitb(q), 0);   \
193         } while (0)
194
195 void
196 ao_quadrature_init(void)
197 {
198 #if AO_QUADRATURE_COUNT > 0
199         init(0);
200 #endif
201 #if AO_QUADRATURE_COUNT > 1
202         init(1);
203 #endif
204         ao_fast_timer_init();
205         ao_fast_timer_on(ao_quadrature_isr);
206         ao_cmd_register(&ao_quadrature_cmds[0]);
207 }