altos: Simplify quadrature tracking
authorKeith Packard <keithp@keithp.com>
Fri, 9 May 2014 07:05:39 +0000 (00:05 -0700)
committerKeith Packard <keithp@keithp.com>
Fri, 9 May 2014 07:05:39 +0000 (00:05 -0700)
Set the timer to 200Hz for a 5ms debounce interval. Then, simply look
for transitions ending in both bits in the encoder being off, which
indicates the the encoder is resting in a detent. If bit '2' is
turning off, the encoder was rotated clockwise, otherwise the encoder
was rotated counter clockwise.

This is a lot more reliable, although still not perfect.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/drivers/ao_quadrature.c
src/stm/ao_fast_timer.c
src/telelco-v0.2/ao_pins.h

index d07488d08d4f9307b661a0f690f9447dab4fec73..66a77dfaa997b22824805cbbdc4bda9c63b5618d 100644 (file)
 #include <ao_event.h>
 
 __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT];
-static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT];
-static int8_t  ao_quadrature_raw[AO_QUADRATURE_COUNT];
-
-#define BIT(a,b)       ((a) | ((b) << 1))
-#define STATE(old_a, old_b, new_a, new_b)      (((BIT(old_a, old_b) << 2) | BIT(new_a, new_b)))
+static uint8_t  ao_quadrature_state[AO_QUADRATURE_COUNT];
 
 #define port(q)        AO_QUADRATURE_ ## q ## _PORT
 #define bita(q) AO_QUADRATURE_ ## q ## _A
@@ -54,51 +50,29 @@ _ao_quadrature_queue(uint8_t q, int8_t step)
        ao_wakeup(&ao_quadrature_count[q]);
 }
 
-static const int8_t    step[16] = {
-       [STATE(0,0,0,0)] = 0,
-       [STATE(0,0,0,1)] = -1,
-       [STATE(0,0,1,0)] = 1,
-       [STATE(0,0,1,1)] = 0,
-       [STATE(0,1,0,0)] = 1,
-       [STATE(0,1,1,0)] = 0,
-       [STATE(0,1,1,1)] = -1,
-       [STATE(1,0,0,0)] = -1,
-       [STATE(1,0,0,1)] = 0,
-       [STATE(1,0,1,0)] = 0,
-       [STATE(1,0,1,1)] = 1,
-       [STATE(1,1,0,0)] = 0,
-       [STATE(1,1,0,1)] = 1,
-       [STATE(1,1,1,0)] = -1,
-       [STATE(1,1,1,1)] = 0
-};
 
 static void
-_ao_quadrature_set(uint8_t q, uint8_t value) {
-       uint8_t v;
-       
-       v = ao_quadrature_state[q] & 3;
-       value = value & 3;
+_ao_quadrature_set(uint8_t q, uint8_t new) {
+       uint8_t old = ao_quadrature_state[q];
 
-       if (v == value)
-               return;
-       
-       ao_quadrature_state[q] = (v << 2) | value;
-
-       ao_quadrature_raw[q] += step[ao_quadrature_state[q]];
-       if (value == 0) {
-               if (ao_quadrature_raw[q] == 4)
+       if (old != new && new == 0) {
+               if (old & 2)
                        _ao_quadrature_queue(q, 1);
-               else if (ao_quadrature_raw[q] == -4)
+               else if (old & 1)
                        _ao_quadrature_queue(q, -1);
-               ao_quadrature_raw[q] = 0;
        }
+       ao_quadrature_state[q] = new;
 }
 
 static void
 ao_quadrature_isr(void)
 {
+#if AO_QUADRATURE_COUNT > 0
        _ao_quadrature_set(0, _ao_quadrature_get(0));
+#endif
+#if AO_QUADRATURE_COUNT > 1
        _ao_quadrature_set(1, _ao_quadrature_get(1));
+#endif
 }
 
 int32_t
@@ -120,6 +94,8 @@ static void
 ao_quadrature_test(void)
 {
        uint8_t q;
+       int32_t c;
+       uint8_t s;
 
        ao_cmd_decimal();
        q = ao_cmd_lex_i;
@@ -127,10 +103,18 @@ ao_quadrature_test(void)
                ao_cmd_status = ao_cmd_syntax_error;
                return;
        }
-       printf ("count %d state %x raw %d\n",
-               ao_quadrature_count[q],
-               ao_quadrature_state[q],
-               ao_quadrature_raw[q]);
+
+       c = -10000;
+       s = 0;
+       while (ao_quadrature_count[q] != 10) {
+               if (ao_quadrature_count[q] != c ||
+                   ao_quadrature_state[q] != s) {
+                       c = ao_quadrature_count[q];
+                       s = ao_quadrature_state[q];
+                       printf ("count %3d state %2x\n", c, s);
+                       flush();
+               }
+       }
 #if 0
        for (;;) {
                int32_t c;
index d61b40c99bed32f5a25af6010f41ad1e93ef9869..9a73eb51c30c3c2e1562907124e9d44c3768cb8c 100644 (file)
@@ -88,7 +88,11 @@ void stm_tim6_isr(void)
 #define TIMER_23467_SCALER 1
 #endif
 
-#define TIMER_10kHz    ((AO_PCLK1 * TIMER_23467_SCALER) / 10000)
+#ifndef FAST_TIMER_FREQ
+#define FAST_TIMER_FREQ        10000
+#endif
+
+#define TIMER_FAST     ((AO_PCLK1 * TIMER_23467_SCALER) / FAST_TIMER_FREQ)
 
 void
 ao_fast_timer_init(void)
@@ -100,7 +104,7 @@ ao_fast_timer_init(void)
                /* Turn on timer 6 */
                stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_TIM6EN);
 
-               stm_tim6.psc = TIMER_10kHz;
+               stm_tim6.psc = TIMER_FAST;
                stm_tim6.arr = 9;
                stm_tim6.cnt = 0;
 
index 62f221a1da8979402697e6da3dfbebe94db05bbe..609e549416ba1c2b048865e28f1ff86f91309cfd 100644 (file)
@@ -72,6 +72,8 @@
 #define PACKET_HAS_SLAVE       0
 #define PACKET_HAS_MASTER      0
 
+#define FAST_TIMER_FREQ                200     /* 5ms for debouncing */
+
 /*
  * Radio is a cc1120 connected via SPI
  */