altos: Simplify quadrature 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; 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 #define port(q) AO_QUADRATURE_ ## q ## _PORT
28 #define bita(q) AO_QUADRATURE_ ## q ## _A
29 #define bitb(q) AO_QUADRATURE_ ## q ## _B
30 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
31 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
32 #define isr(q)  ao_quadrature_isr_ ## q
33
34 static inline uint16_t
35 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b) {
36         uint16_t        v = stm_gpio_get_all(gpio);
37
38         return ~((((v >> pin_a) & 1) | (((v >> pin_b) & 1) << 1))) & 3;
39 }
40
41 #define _ao_quadrature_get(q)   ao_quadrature_read(port(q), bita(q), bitb(q))
42
43 static void
44 _ao_quadrature_queue(uint8_t q, int8_t step)
45 {
46         ao_quadrature_count[q] += step;
47 #if AO_EVENT
48         ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
49 #endif
50         ao_wakeup(&ao_quadrature_count[q]);
51 }
52
53
54 static void
55 _ao_quadrature_set(uint8_t q, uint8_t new) {
56         uint8_t old = ao_quadrature_state[q];
57
58         if (old != new && new == 0) {
59                 if (old & 2)
60                         _ao_quadrature_queue(q, 1);
61                 else if (old & 1)
62                         _ao_quadrature_queue(q, -1);
63         }
64         ao_quadrature_state[q] = new;
65 }
66
67 static void
68 ao_quadrature_isr(void)
69 {
70 #if AO_QUADRATURE_COUNT > 0
71         _ao_quadrature_set(0, _ao_quadrature_get(0));
72 #endif
73 #if AO_QUADRATURE_COUNT > 1
74         _ao_quadrature_set(1, _ao_quadrature_get(1));
75 #endif
76 }
77
78 int32_t
79 ao_quadrature_poll(uint8_t q)
80 {
81         int32_t ret;
82         ao_arch_critical(ret = ao_quadrature_count[q];);
83         return ret;
84 }
85
86 int32_t
87 ao_quadrature_wait(uint8_t q)
88 {
89         ao_sleep(&ao_quadrature_count[q]);
90         return ao_quadrature_poll(q);
91 }
92
93 static void
94 ao_quadrature_test(void)
95 {
96         uint8_t q;
97         int32_t c;
98         uint8_t s;
99
100         ao_cmd_decimal();
101         q = ao_cmd_lex_i;
102         if (q >= AO_QUADRATURE_COUNT) {
103                 ao_cmd_status = ao_cmd_syntax_error;
104                 return;
105         }
106
107         c = -10000;
108         s = 0;
109         while (ao_quadrature_count[q] != 10) {
110                 if (ao_quadrature_count[q] != c ||
111                     ao_quadrature_state[q] != s) {
112                         c = ao_quadrature_count[q];
113                         s = ao_quadrature_state[q];
114                         printf ("count %3d state %2x\n", c, s);
115                         flush();
116                 }
117         }
118 #if 0
119         for (;;) {
120                 int32_t c;
121                 flush();
122                 c = ao_quadrature_wait(q);
123                 printf ("new count %6d\n", c);
124                 if (c == 100)
125                         break;
126         }
127 #endif
128 }
129
130 static const struct ao_cmds ao_quadrature_cmds[] = {
131         { ao_quadrature_test,   "q <unit>\0Test quadrature" },
132         { 0, NULL }
133 };
134
135 #define init(q) do {                                    \
136                 ao_enable_input(port(q), bita(q), 0);   \
137                 ao_enable_input(port(q), bitb(q), 0);   \
138         } while (0)
139
140 void
141 ao_quadrature_init(void)
142 {
143 #if AO_QUADRATURE_COUNT > 0
144         init(0);
145 #endif
146 #if AO_QUADRATURE_COUNT > 1
147         init(1);
148 #endif
149         ao_fast_timer_init();
150         ao_fast_timer_on(ao_quadrature_isr);
151         ao_cmd_register(&ao_quadrature_cmds[0]);
152 }