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>
22 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
24 static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT];
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)))
29 #define port(q) AO_QUADRATURE_ ## q ## _PORT
30 #define bita(q) AO_QUADRATURE_ ## q ## _A
31 #define bitb(q) AO_QUADRATURE_ ## q ## _B
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; \
41 ao_quadrature_isr(void)
44 #if AO_QUADRATURE_COUNT > 0
45 ao_quadrature_update(0);
47 #if AO_QUADRATURE_COUNT > 1
48 ao_quadrature_update(1);
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]++;
56 case STATE(1, 0, 0, 0):
57 ao_quadrature_count[q]--;
62 ao_wakeup(&ao_quadrature_count[q]);
67 ao_quadrature_poll(uint8_t q)
70 ao_arch_critical(ret = ao_quadrature_count[q];);
75 ao_quadrature_wait(uint8_t q)
77 ao_sleep(&ao_quadrature_count[q]);
78 return ao_quadrature_poll(q);
82 ao_quadrature_test(void)
88 c = ao_quadrature_wait(0);
89 printf ("new count %6d\n", c);
95 uint8_t a, old_a, b, old_b;
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);
114 static const struct ao_cmds ao_quadrature_cmds[] = {
115 { ao_quadrature_test, "q\0Test quadrature" },
119 #define init(q) do { \
120 ao_enable_port(port(q)); \
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)); \
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)); \
134 ao_quadrature_init(void)
136 #if AO_QUADRATURE_COUNT > 0
139 #if AO_QUADRATURE_COUNT > 1
142 ao_cmd_register(&ao_quadrature_cmds[0]);