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>
23 #define ao_quadrature_queue(q) ao_event_put_isr(AO_EVENT_QUADRATURE, q, ao_quadrature_count[q])
25 #define ao_quadrature_queue(q)
28 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
30 static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT];
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)))
35 #define port(q) AO_QUADRATURE_ ## q ## _PORT
36 #define bita(q) AO_QUADRATURE_ ## q ## _A
37 #define bitb(q) AO_QUADRATURE_ ## q ## _B
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; \
47 ao_quadrature_isr(void)
50 #if AO_QUADRATURE_COUNT > 0
51 ao_quadrature_update(0);
53 #if AO_QUADRATURE_COUNT > 1
54 ao_quadrature_update(1);
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]++;
62 case STATE(1, 0, 0, 0):
63 ao_quadrature_count[q]--;
68 ao_quadrature_queue(q);
69 ao_wakeup(&ao_quadrature_count[q]);
74 ao_quadrature_poll(uint8_t q)
77 ao_arch_critical(ret = ao_quadrature_count[q];);
82 ao_quadrature_wait(uint8_t q)
84 ao_sleep(&ao_quadrature_count[q]);
85 return ao_quadrature_poll(q);
89 ao_quadrature_test(void)
98 c = ao_quadrature_wait(q);
99 printf ("new count %6d\n", c);
105 static const struct ao_cmds ao_quadrature_cmds[] = {
106 { ao_quadrature_test, "q <unit>\0Test quadrature" },
110 #define init(q) do { \
111 ao_enable_port(port(q)); \
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)); \
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)); \
125 ao_quadrature_init(void)
127 #if AO_QUADRATURE_COUNT > 0
130 #if AO_QUADRATURE_COUNT > 1
133 ao_cmd_register(&ao_quadrature_cmds[0]);