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];
27 #define port(q) AO_QUADRATURE_ ## q ## _PORT
28 #define bita(q) AO_QUADRATURE_ ## q ## _A
29 #define bitb(q) AO_QUADRATURE_ ## q ## _B
30 #define pina(q) AO_QUADRATURE_ ## q ## _A ## _PIN
31 #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN
32 #define isr(q) ao_quadrature_isr_ ## q
34 static inline uint16_t
35 ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b) {
36 uint16_t v = stm_gpio_get_all(gpio);
38 return ~((((v >> pin_a) & 1) | (((v >> pin_b) & 1) << 1))) & 3;
41 #define _ao_quadrature_get(q) ao_quadrature_read(port(q), bita(q), bitb(q))
44 _ao_quadrature_queue(uint8_t q, int8_t step)
46 ao_quadrature_count[q] += step;
48 ao_event_put_isr(AO_EVENT_QUADRATURE, q, step);
50 ao_wakeup(&ao_quadrature_count[q]);
55 _ao_quadrature_set(uint8_t q, uint8_t new) {
56 uint8_t old = ao_quadrature_state[q];
58 if (old != new && new == 0) {
60 _ao_quadrature_queue(q, 1);
62 _ao_quadrature_queue(q, -1);
64 ao_quadrature_state[q] = new;
68 ao_quadrature_isr(void)
70 #if AO_QUADRATURE_COUNT > 0
71 _ao_quadrature_set(0, _ao_quadrature_get(0));
73 #if AO_QUADRATURE_COUNT > 1
74 _ao_quadrature_set(1, _ao_quadrature_get(1));
79 ao_quadrature_poll(uint8_t q)
82 ao_arch_critical(ret = ao_quadrature_count[q];);
87 ao_quadrature_wait(uint8_t q)
89 ao_sleep(&ao_quadrature_count[q]);
90 return ao_quadrature_poll(q);
94 ao_quadrature_test(void)
102 if (q >= AO_QUADRATURE_COUNT) {
103 ao_cmd_status = ao_cmd_syntax_error;
109 while (ao_quadrature_count[q] != 10) {
110 if (ao_quadrature_count[q] != c ||
111 ao_quadrature_state[q] != s) {
112 c = ao_quadrature_count[q];
113 s = ao_quadrature_state[q];
114 printf ("count %3d state %2x\n", c, s);
122 c = ao_quadrature_wait(q);
123 printf ("new count %6d\n", c);
130 static const struct ao_cmds ao_quadrature_cmds[] = {
131 { ao_quadrature_test, "q <unit>\0Test quadrature" },
135 #define init(q) do { \
136 ao_enable_input(port(q), bita(q), 0); \
137 ao_enable_input(port(q), bitb(q), 0); \
141 ao_quadrature_init(void)
143 #if AO_QUADRATURE_COUNT > 0
146 #if AO_QUADRATURE_COUNT > 1
149 ao_fast_timer_init();
150 ao_fast_timer_on(ao_quadrature_isr);
151 ao_cmd_register(&ao_quadrature_cmds[0]);