altos: Support multiple quadrature encoders.
[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
22 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
23
24 static uint8_t  ao_quadrature_state[AO_QUADRATURE_COUNT];
25
26 #define BIT(a,b)        ((a) | ((b) << 1))
27 #define STATE(old_a, old_b, new_a, new_b)       (((BIT(old_a, old_b) << 2) | BIT(new_a, new_b)))
28
29 #define port(q) AO_QUADRATURE_ ## q ## _PORT
30 #define bita(q) AO_QUADRATURE_ ## q ## _A
31 #define bitb(q) AO_QUADRATURE_ ## q ## _B
32
33 #define ao_quadrature_update(q) do {                                    \
34                 ao_quadrature_state[q] = ((ao_quadrature_state[q] & 3) << 2); \
35                 ao_quadrature_state[q] |= ao_gpio_get(port(q), bita(q), 0); \
36                 ao_quadrature_state[q] |= ao_gpio_get(port(q), bitb(q), 0) << 1; \
37         } while (0)
38         
39
40 static void
41 ao_quadrature_isr(void)
42 {
43         uint8_t q;
44 #if AO_QUADRATURE_COUNT > 0
45         ao_quadrature_update(0);
46 #endif
47 #if AO_QUADRATURE_COUNT > 1
48         ao_quadrature_update(1);
49 #endif
50
51         for (q = 0; q < AO_QUADRATURE_COUNT; q++) {
52                 switch (ao_quadrature_state[q]) {
53                 case STATE(0, 1, 0, 0):
54                         ao_quadrature_count[q]++;
55                         break;
56                 case STATE(1, 0, 0, 0):
57                         ao_quadrature_count[q]--;
58                         break;
59                 default:
60                         continue;
61                 }
62                 ao_wakeup(&ao_quadrature_count[q]);
63         }
64 }
65
66 int32_t
67 ao_quadrature_poll(uint8_t q)
68 {
69         int32_t ret;
70         ao_arch_critical(ret = ao_quadrature_count[q];);
71         return ret;
72 }
73
74 int32_t
75 ao_quadrature_wait(uint8_t q)
76 {
77         ao_sleep(&ao_quadrature_count[q]);
78         return ao_quadrature_poll(q);
79 }
80
81 static void
82 ao_quadrature_test(void)
83 {
84 #if 1
85         for (;;) {
86                 int32_t c;
87                 flush();
88                 c = ao_quadrature_wait(0);
89                 printf ("new count %6d\n", c);
90                 if (c == 100)
91                         break;
92         }
93 #endif
94 #if 0
95         uint8_t a, old_a, b, old_b;
96
97         old_a = 2; old_b = 2;
98         for (;;) {
99                 a = ao_gpio_get(AO_QUADRATURE_PORT, AO_QUADRATURE_A, AO_QUADRATURE_A_PIN);
100                 b = ao_gpio_get(AO_QUADRATURE_PORT, AO_QUADRATURE_B, AO_QUADRATURE_B_PIN);
101                 if (a != old_a || b != old_b) {
102                         printf ("A %d B %d count %ld\n", a, b, ao_quadrature_count);
103                         flush();
104                         ao_yield();
105                         old_a = a;
106                         old_b = b;
107                 }
108                 if (ao_stdin_ready)
109                         break;
110         }
111 #endif          
112 }
113
114 static const struct ao_cmds ao_quadrature_cmds[] = {
115         { ao_quadrature_test,   "q\0Test quadrature" },
116         { 0, NULL }
117 };
118
119 #define init(q) do {                                                    \
120                 ao_enable_port(port(q));                                \
121                                                                         \
122                 ao_exti_setup(port(q), bita(q),                         \
123                               AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
124                               ao_quadrature_isr);                       \
125                 ao_exti_enable(port(q), bita(q));                       \
126                                                                         \
127                 ao_exti_setup(port(q), bitb(q),                         \
128                               AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
129                               ao_quadrature_isr);                       \
130                 ao_exti_enable(port(q), bitb(q));                       \
131         } while (0)
132
133 void
134 ao_quadrature_init(void)
135 {
136 #if AO_QUADRATURE_COUNT > 0
137         init(0);
138 #endif
139 #if AO_QUADRATURE_COUNT > 1
140         init(1);
141 #endif
142         ao_cmd_register(&ao_quadrature_cmds[0]);
143 }