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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_quadrature.h>
22 #include <ao_fast_timer.h>
25 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
26 static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT];
33 static struct ao_debounce ao_debounce_state[AO_QUADRATURE_COUNT][2];
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 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
39 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
40 #define isr(q) ao_quadrature_isr_ ## q
45 ao_debounce(uint8_t cur, struct ao_debounce *debounce)
47 if (cur == debounce->state)
50 if (++debounce->count == DEBOUNCE) {
51 debounce->state = cur;
55 return debounce->state;
59 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) {
60 uint16_t v = ~stm_gpio_get_all(gpio);
61 uint8_t a = (v >> pin_a) & 1;
62 uint8_t b = (v >> pin_b) & 1;
64 a = ao_debounce(a, &debounce_state[0]);
65 b = ao_debounce(b, &debounce_state[1]);
70 #define _ao_quadrature_get(q) ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q])
73 _ao_quadrature_queue(uint8_t q, int8_t step)
75 ao_quadrature_count[q] += step;
77 ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
79 ao_wakeup(&ao_quadrature_count[q]);
83 _ao_quadrature_set(uint8_t q, uint8_t new) {
84 uint8_t old = ao_quadrature_state[q];
86 if (old != new && new == 0) {
88 _ao_quadrature_queue(q, 1);
90 _ao_quadrature_queue(q, -1);
92 ao_quadrature_state[q] = new;
96 ao_quadrature_isr(void)
98 #if AO_QUADRATURE_COUNT > 0
99 _ao_quadrature_set(0, _ao_quadrature_get(0));
101 #if AO_QUADRATURE_COUNT > 1
102 _ao_quadrature_set(1, _ao_quadrature_get(1));
107 ao_quadrature_poll(uint8_t q)
110 ao_arch_critical(ret = ao_quadrature_count[q];);
115 ao_quadrature_wait(uint8_t q)
117 ao_sleep(&ao_quadrature_count[q]);
118 return ao_quadrature_poll(q);
122 ao_quadrature_test(void)
130 if (q >= AO_QUADRATURE_COUNT) {
131 ao_cmd_status = ao_cmd_syntax_error;
137 while (ao_quadrature_count[q] != 10) {
138 if (ao_quadrature_count[q] != c ||
139 ao_quadrature_state[q] != s) {
140 c = ao_quadrature_count[q];
141 s = ao_quadrature_state[q];
142 printf ("count %3d state %2x\n", c, s);
150 c = ao_quadrature_wait(q);
151 printf ("new count %6d\n", c);
158 static const struct ao_cmds ao_quadrature_cmds[] = {
159 { ao_quadrature_test, "q <unit>\0Test quadrature" },
163 #define init(q) do { \
164 ao_enable_input(port(q), bita(q), 0); \
165 ao_enable_input(port(q), bitb(q), 0); \
169 ao_quadrature_init(void)
171 #if AO_QUADRATURE_COUNT > 0
174 #if AO_QUADRATURE_COUNT > 1
177 ao_fast_timer_init();
178 ao_fast_timer_on(ao_quadrature_isr);
179 ao_cmd_register(&ao_quadrature_cmds[0]);