Merge remote-tracking branch 'mjb/master'
[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 #if AO_EVENT
22 #include <ao_event.h>
23 #define ao_quadrature_queue(q)  ao_event_put_isr(AO_EVENT_QUADRATURE, q, ao_quadrature_count[q])
24 #else
25 #define ao_quadrature_queue(q)
26 #endif
27
28 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
29
30 static uint8_t  ao_quadrature_state[AO_QUADRATURE_COUNT];
31
32 #define BIT(a,b)        ((a) | ((b) << 1))
33 #define STATE(old_a, old_b, new_a, new_b)       (((BIT(old_a, old_b) << 2) | BIT(new_a, new_b)))
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
39 #define ao_quadrature_update(q) do {                                    \
40                 ao_quadrature_state[q] = ((ao_quadrature_state[q] & 3) << 2); \
41                 ao_quadrature_state[q] |= ao_gpio_get(port(q), bita(q), 0); \
42                 ao_quadrature_state[q] |= ao_gpio_get(port(q), bitb(q), 0) << 1; \
43         } while (0)
44         
45
46 static void
47 ao_quadrature_isr(void)
48 {
49         uint8_t q;
50 #if AO_QUADRATURE_COUNT > 0
51         ao_quadrature_update(0);
52 #endif
53 #if AO_QUADRATURE_COUNT > 1
54         ao_quadrature_update(1);
55 #endif
56
57         for (q = 0; q < AO_QUADRATURE_COUNT; q++) {
58                 switch (ao_quadrature_state[q]) {
59                 case STATE(0, 1, 0, 0):
60                         ao_quadrature_count[q]++;
61                         break;
62                 case STATE(1, 0, 0, 0):
63                         ao_quadrature_count[q]--;
64                         break;
65                 default:
66                         continue;
67                 }
68                 ao_quadrature_queue(q);
69                 ao_wakeup(&ao_quadrature_count[q]);
70         }
71 }
72
73 int32_t
74 ao_quadrature_poll(uint8_t q)
75 {
76         int32_t ret;
77         ao_arch_critical(ret = ao_quadrature_count[q];);
78         return ret;
79 }
80
81 int32_t
82 ao_quadrature_wait(uint8_t q)
83 {
84         ao_sleep(&ao_quadrature_count[q]);
85         return ao_quadrature_poll(q);
86 }
87
88 static void
89 ao_quadrature_test(void)
90 {
91         uint8_t q;
92
93         ao_cmd_decimal();
94         q = ao_cmd_lex_i;
95         for (;;) {
96                 int32_t c;
97                 flush();
98                 c = ao_quadrature_wait(q);
99                 printf ("new count %6d\n", c);
100                 if (c == 100)
101                         break;
102         }
103 }
104
105 static const struct ao_cmds ao_quadrature_cmds[] = {
106         { ao_quadrature_test,   "q <unit>\0Test quadrature" },
107         { 0, NULL }
108 };
109
110 #define init(q) do {                                                    \
111                 ao_enable_port(port(q));                                \
112                                                                         \
113                 ao_exti_setup(port(q), bita(q),                         \
114                               AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
115                               ao_quadrature_isr);                       \
116                 ao_exti_enable(port(q), bita(q));                       \
117                                                                         \
118                 ao_exti_setup(port(q), bitb(q),                         \
119                               AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
120                               ao_quadrature_isr);                       \
121                 ao_exti_enable(port(q), bitb(q));                       \
122         } while (0)
123
124 void
125 ao_quadrature_init(void)
126 {
127 #if AO_QUADRATURE_COUNT > 0
128         init(0);
129 #endif
130 #if AO_QUADRATURE_COUNT > 1
131         init(1);
132 #endif
133         ao_cmd_register(&ao_quadrature_cmds[0]);
134 }