2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include <ao_quadrature.h>
21 #include <ao_debounce.h>
24 #define AO_QUADRATURE_DEBOUNCE_HOLD 3
26 static __xdata struct ao_debounce ao_quadrature_debounce[AO_QUADRATURE_COUNT];
28 #define debounce_id(d) ((d) - ao_quadrature_debounce)
30 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
32 static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT];
34 #define BIT(a,b) ((a) | ((b) << 1))
35 #define STATE(old_a, old_b, new_a, new_b) (((BIT(old_a, old_b) << 2) | BIT(new_a, new_b)))
37 #define port(q) AO_QUADRATURE_ ## q ## _PORT
38 #define bita(q) AO_QUADRATURE_ ## q ## _A
39 #define bitb(q) AO_QUADRATURE_ ## q ## _B
40 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
41 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
43 #define q_case(q) case q: v = (!ao_gpio_get(port(q), bita(q), pina(q))) | ((!ao_gpio_get(port(q), bitb(q), pinb(q))) << 1); break
49 _ao_quadrature_get(struct ao_debounce *debounce) {
50 uint8_t q = debounce_id(debounce);
54 #if AO_QUADRATURE_COUNT > 0
57 #if AO_QUADRATURE_COUNT > 1
63 quad_r = (quad_r + 1) & 63;
69 _ao_quadrature_queue(uint8_t q, int8_t step)
71 ao_quadrature_count[q] += step;
73 ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
75 ao_wakeup(&ao_quadrature_count[q]);
78 uint8_t quad_history[64];
82 _ao_quadrature_set(struct ao_debounce *debounce, uint8_t value) {
83 uint8_t q = debounce_id(debounce);
85 ao_quadrature_state[q] = ((ao_quadrature_state[q] & 3) << 2);
86 ao_quadrature_state[q] |= value;
89 quad_history[quad_h] = ao_quadrature_state[0];
90 quad_h = (quad_h + 1) & 63;
93 switch (ao_quadrature_state[q]) {
94 case STATE(0, 1, 0, 0):
95 _ao_quadrature_queue(q, 1);
97 case STATE(1, 0, 0, 0):
98 _ao_quadrature_queue(q, -1);
104 ao_quadrature_isr(void)
107 for (q = 0; q < AO_QUADRATURE_COUNT; q++)
108 _ao_debounce_start(&ao_quadrature_debounce[q]);
112 ao_quadrature_poll(uint8_t q)
115 ao_arch_critical(ret = ao_quadrature_count[q];);
120 ao_quadrature_wait(uint8_t q)
122 ao_sleep(&ao_quadrature_count[q]);
123 return ao_quadrature_poll(q);
127 ao_quadrature_test(void)
136 c = ao_quadrature_wait(q);
137 printf ("new count %6d\n", c);
143 static const struct ao_cmds ao_quadrature_cmds[] = {
144 { ao_quadrature_test, "q <unit>\0Test quadrature" },
149 ao_quadrature_debounce_init(struct ao_debounce *debounce) {
150 debounce->hold = AO_QUADRATURE_DEBOUNCE_HOLD;
151 debounce->_get = _ao_quadrature_get;
152 debounce->_set = _ao_quadrature_set;
155 #define init(q) do { \
156 ao_enable_port(port(q)); \
157 ao_quadrature_debounce_init(&ao_quadrature_debounce[q]); \
158 ao_exti_setup(port(q), bita(q), \
159 AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
160 ao_quadrature_isr); \
161 ao_exti_enable(port(q), bita(q)); \
163 ao_exti_setup(port(q), bitb(q), \
164 AO_QUADRATURE_MODE|AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_MED, \
165 ao_quadrature_isr); \
166 ao_exti_enable(port(q), bitb(q)); \
170 ao_quadrature_init(void)
173 #if AO_QUADRATURE_COUNT > 0
176 #if AO_QUADRATURE_COUNT > 1
179 ao_cmd_register(&ao_quadrature_cmds[0]);