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_fast_timer.h>
24 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
25 static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT];
32 static struct ao_debounce ao_debounce_state[AO_QUADRATURE_COUNT][2];
34 #define port(q) AO_QUADRATURE_ ## q ## _PORT
35 #define bita(q) AO_QUADRATURE_ ## q ## _A
36 #define bitb(q) AO_QUADRATURE_ ## q ## _B
37 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
38 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
39 #define isr(q) ao_quadrature_isr_ ## q
44 ao_debounce(uint8_t cur, struct ao_debounce *debounce)
46 if (cur == debounce->state)
49 if (++debounce->count == DEBOUNCE) {
50 debounce->state = cur;
54 return debounce->state;
58 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) {
59 uint16_t v = ~stm_gpio_get_all(gpio);
60 uint8_t a = (v >> pin_a) & 1;
61 uint8_t b = (v >> pin_b) & 1;
63 a = ao_debounce(a, &debounce_state[0]);
64 b = ao_debounce(b, &debounce_state[1]);
69 #define _ao_quadrature_get(q) ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q])
72 _ao_quadrature_queue(uint8_t q, int8_t step)
74 ao_quadrature_count[q] += step;
76 ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
78 ao_wakeup(&ao_quadrature_count[q]);
82 _ao_quadrature_set(uint8_t q, uint8_t new) {
83 uint8_t old = ao_quadrature_state[q];
85 if (old != new && new == 0) {
87 _ao_quadrature_queue(q, 1);
89 _ao_quadrature_queue(q, -1);
91 ao_quadrature_state[q] = new;
95 ao_quadrature_isr(void)
97 #if AO_QUADRATURE_COUNT > 0
98 _ao_quadrature_set(0, _ao_quadrature_get(0));
100 #if AO_QUADRATURE_COUNT > 1
101 _ao_quadrature_set(1, _ao_quadrature_get(1));
106 ao_quadrature_poll(uint8_t q)
109 ao_arch_critical(ret = ao_quadrature_count[q];);
114 ao_quadrature_wait(uint8_t q)
116 ao_sleep(&ao_quadrature_count[q]);
117 return ao_quadrature_poll(q);
121 ao_quadrature_test(void)
129 if (q >= AO_QUADRATURE_COUNT) {
130 ao_cmd_status = ao_cmd_syntax_error;
136 while (ao_quadrature_count[q] != 10) {
137 if (ao_quadrature_count[q] != c ||
138 ao_quadrature_state[q] != s) {
139 c = ao_quadrature_count[q];
140 s = ao_quadrature_state[q];
141 printf ("count %3d state %2x\n", c, s);
149 c = ao_quadrature_wait(q);
150 printf ("new count %6d\n", c);
157 static const struct ao_cmds ao_quadrature_cmds[] = {
158 { ao_quadrature_test, "q <unit>\0Test quadrature" },
162 #define init(q) do { \
163 ao_enable_input(port(q), bita(q), 0); \
164 ao_enable_input(port(q), bitb(q), 0); \
168 ao_quadrature_init(void)
170 #if AO_QUADRATURE_COUNT > 0
173 #if AO_QUADRATURE_COUNT > 1
176 ao_fast_timer_init();
177 ao_fast_timer_on(ao_quadrature_isr);
178 ao_cmd_register(&ao_quadrature_cmds[0]);