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 #if 1
92         for (;;) {
93                 int32_t c;
94                 flush();
95                 c = ao_quadrature_wait(0);
96                 printf ("new count %6d\n", c);
97                 if (c == 100)
98                         break;
99         }
100 #endif
101 #if 0
102         uint8_t a, old_a, b, old_b;
103
104         old_a = 2; old_b = 2;
105         for (;;) {
106                 a = ao_gpio_get(AO_QUADRATURE_PORT, AO_QUADRATURE_A, AO_QUADRATURE_A_PIN);
107                 b = ao_gpio_get(AO_QUADRATURE_PORT, AO_QUADRATURE_B, AO_QUADRATURE_B_PIN);
108                 if (a != old_a || b != old_b) {
109                         printf ("A %d B %d count %ld\n", a, b, ao_quadrature_count);
110                         flush();
111                         ao_yield();
112                         old_a = a;
113                         old_b = b;
114                 }
115                 if (ao_stdin_ready)
116                         break;
117         }
118 #endif          
119 }
120
121 static const struct ao_cmds ao_quadrature_cmds[] = {
122         { ao_quadrature_test,   "q\0Test quadrature" },
123         { 0, NULL }
124 };
125
126 #define init(q) do {                                                    \
127                 ao_enable_port(port(q));                                \
128                                                                         \
129                 ao_exti_setup(port(q), bita(q),                         \
130                               AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
131                               ao_quadrature_isr);                       \
132                 ao_exti_enable(port(q), bita(q));                       \
133                                                                         \
134                 ao_exti_setup(port(q), bitb(q),                         \
135                               AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
136                               ao_quadrature_isr);                       \
137                 ao_exti_enable(port(q), bitb(q));                       \
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_cmd_register(&ao_quadrature_cmds[0]);
150 }