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
42 #ifndef AO_QUADRATURE_DEBOUNCE
43 #define AO_QUADRATURE_DEBOUNCE 30
47 ao_debounce(uint8_t cur, struct ao_debounce *debounce)
49 #if AO_QUADRATURE_DEBOUNCE > 0
50 if (debounce->count > 0) {
52 } else if (cur != debounce->state) {
53 debounce->state = cur;
54 debounce->count = AO_QUADRATURE_DEBOUNCE;
56 return debounce->state;
64 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) {
65 uint16_t v = ~stm_gpio_get_all(gpio);
66 uint8_t a = (v >> pin_a) & 1;
67 uint8_t b = (v >> pin_b) & 1;
69 a = ao_debounce(a, &debounce_state[0]);
70 b = ao_debounce(b, &debounce_state[1]);
75 #define _ao_quadrature_get(q) ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q])
78 _ao_quadrature_queue(uint8_t q, int8_t step)
80 ao_quadrature_count[q] += step;
82 ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
84 ao_wakeup(&ao_quadrature_count[q]);
87 #if AO_QUADRATURE_SINGLE_CODE
88 struct ao_quadrature_step {
93 static struct ao_quadrature_step ao_quadrature_steps[4] = {
94 [0] = { .inc = 1, .dec = 2 },
95 [1] = { .inc = 3, .dec = 0 },
96 [3] = { .inc = 2, .dec = 1 },
97 [2] = { .inc = 0, .dec = 3 },
102 _ao_quadrature_set(uint8_t q, uint8_t new) {
103 uint8_t old = ao_quadrature_state[q];
105 #ifdef AO_QUADRATURE_SINGLE_CODE
106 if (new == ao_quadrature_steps[old].inc) {
107 _ao_quadrature_queue(q, 1);
108 } else if (new == ao_quadrature_steps[old].dec) {
109 _ao_quadrature_queue(q, -1);
112 if (old != new && new == 0) {
114 _ao_quadrature_queue(q, 1);
116 _ao_quadrature_queue(q, -1);
119 ao_quadrature_state[q] = new;
123 ao_quadrature_isr(void)
125 #if AO_QUADRATURE_COUNT > 0
126 _ao_quadrature_set(0, _ao_quadrature_get(0));
128 #if AO_QUADRATURE_COUNT > 1
129 _ao_quadrature_set(1, _ao_quadrature_get(1));
134 ao_quadrature_poll(uint8_t q)
137 ao_arch_critical(ret = ao_quadrature_count[q];);
142 ao_quadrature_wait(uint8_t q)
144 ao_sleep(&ao_quadrature_count[q]);
145 return ao_quadrature_poll(q);
149 ao_quadrature_test(void)
157 if (q >= AO_QUADRATURE_COUNT) {
158 ao_cmd_status = ao_cmd_syntax_error;
164 while (ao_quadrature_count[q] != 10) {
165 if (ao_quadrature_count[q] != c ||
166 ao_quadrature_state[q] != s) {
167 c = ao_quadrature_count[q];
168 s = ao_quadrature_state[q];
169 printf ("count %3d state %2x\n", c, s);
177 c = ao_quadrature_wait(q);
178 printf ("new count %6d\n", c);
185 static const struct ao_cmds ao_quadrature_cmds[] = {
186 { ao_quadrature_test, "q <unit>\0Test quadrature" },
190 #define init(q) do { \
191 ao_enable_input(port(q), bita(q), 0); \
192 ao_enable_input(port(q), bitb(q), 0); \
196 ao_quadrature_init(void)
198 #if AO_QUADRATURE_COUNT > 0
201 #if AO_QUADRATURE_COUNT > 1
204 ao_fast_timer_init();
205 ao_fast_timer_on(ao_quadrature_isr);
206 ao_cmd_register(&ao_quadrature_cmds[0]);