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 int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
26 #ifndef AO_QUADRATURE_SINGLE_CODE
27 static int8_t ao_quadrature_step[AO_QUADRATURE_COUNT];
30 static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT];
37 static struct ao_debounce ao_debounce_state[AO_QUADRATURE_COUNT][2];
39 #define port(q) AO_QUADRATURE_ ## q ## _PORT
40 #define bita(q) AO_QUADRATURE_ ## q ## _A
41 #define bitb(q) AO_QUADRATURE_ ## q ## _B
42 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
43 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
44 #define isr(q) ao_quadrature_isr_ ## q
46 #ifndef AO_QUADRATURE_DEBOUNCE
47 #error must define AO_QUADRATURE_DEBOUNCE
51 ao_debounce(uint8_t cur, struct ao_debounce *debounce)
53 #if AO_QUADRATURE_DEBOUNCE > 0
54 if (debounce->count > 0) {
56 } else if (cur != debounce->state) {
57 debounce->state = cur;
58 debounce->count = AO_QUADRATURE_DEBOUNCE;
60 return debounce->state;
68 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) {
69 uint16_t v = ~stm_gpio_get_all(gpio);
70 uint8_t a = (v >> pin_a) & 1;
71 uint8_t b = (v >> pin_b) & 1;
73 a = ao_debounce(a, &debounce_state[0]);
74 b = ao_debounce(b, &debounce_state[1]);
79 #define _ao_quadrature_get(q) ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q])
82 _ao_quadrature_step(uint8_t q, int8_t step)
84 #ifndef AO_QUADRATURE_SINGLE_CODE
85 ao_quadrature_step[q] += step;
86 if (ao_quadrature_state[q] != 0)
88 if (ao_quadrature_step[q] >= 4) {
89 ao_quadrature_step[q] = 0;
91 } else if (ao_quadrature_step[q] <= -4) {
92 ao_quadrature_step[q] = 0;
97 ao_quadrature_count[q] += step;
99 ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
101 ao_wakeup(&ao_quadrature_count[q]);
104 static const struct {
106 } ao_quadrature_steps[4] = {
107 [0] { .prev = 2, .next = 1 },
108 [1] { .prev = 0, .next = 3 },
109 [3] { .prev = 1, .next = 2 },
110 [2] { .prev = 3, .next = 0 },
114 _ao_quadrature_set(uint8_t q, uint8_t new)
118 ao_arch_block_interrupts();
119 old = ao_quadrature_state[q];
120 ao_quadrature_state[q] = new;
121 ao_arch_release_interrupts();
123 if (new == ao_quadrature_steps[old].next)
124 _ao_quadrature_step(q, 1);
125 else if (new == ao_quadrature_steps[old].prev)
126 _ao_quadrature_step(q, -1);
130 ao_quadrature_isr(void)
132 #if AO_QUADRATURE_COUNT > 0
133 _ao_quadrature_set(0, _ao_quadrature_get(0));
135 #if AO_QUADRATURE_COUNT > 1
136 _ao_quadrature_set(1, _ao_quadrature_get(1));
141 _ao_quadrature_start_one(uint8_t q, uint8_t new)
143 ao_arch_block_interrupts();
144 ao_quadrature_state[q] = new;
145 ao_arch_release_interrupts();
149 ao_quadrature_poll(uint8_t q)
152 ao_arch_critical(ret = ao_quadrature_count[q];);
157 ao_quadrature_wait(uint8_t q)
159 ao_sleep(&ao_quadrature_count[q]);
160 return ao_quadrature_poll(q);
164 ao_quadrature_test(void)
169 #ifndef AO_QUADRATURE_SINGLE_CODE
173 q = ao_cmd_decimal();
174 if (q >= AO_QUADRATURE_COUNT)
175 ao_cmd_status = ao_cmd_syntax_error;
176 if (ao_cmd_status != ao_cmd_success)
181 while (ao_quadrature_count[q] != 10) {
182 if (ao_quadrature_count[q] != c ||
183 #ifndef AO_QUADRATURE_SINGLE_CODE
184 ao_quadrature_step[q] != t ||
186 ao_quadrature_state[q] != s)
188 c = ao_quadrature_count[q];
189 s = ao_quadrature_state[q];
190 #ifndef AO_QUADRATURE_SINGLE_CODE
191 t = ao_quadrature_step[q];
192 printf("step %3d ", t);
194 printf ("count %3d state %2x\n", c, s);
202 c = ao_quadrature_wait(q);
203 printf ("new count %6d\n", c);
210 static const struct ao_cmds ao_quadrature_cmds[] = {
211 { ao_quadrature_test, "q <unit>\0Test quadrature" },
215 #define init(q) do { \
216 ao_enable_input(port(q), bita(q), 0); \
217 ao_enable_input(port(q), bitb(q), 0); \
218 _ao_quadrature_start_one(q, _ao_quadrature_get(q)); \
222 ao_quadrature_init(void)
224 #if AO_QUADRATURE_COUNT > 0
227 #if AO_QUADRATURE_COUNT > 1
230 ao_fast_timer_init();
231 ao_fast_timer_on(ao_quadrature_isr);
232 ao_cmd_register(&ao_quadrature_cmds[0]);