From 8e3842660274ac4bcd7b5a78f5db215222b1c4de Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 30 Apr 2014 22:14:37 -0700 Subject: altos: For telelco discovery packets, retry 5 times with shorter timeout A timeout of 10ms is more than enough to receive a query packet, but if we miss it during device discovery, it's a pain, so retry 5 times to make sure we find everyone. Signed-off-by: Keith Packard --- src/telelco-v0.2/ao_lco.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/telelco-v0.2') diff --git a/src/telelco-v0.2/ao_lco.c b/src/telelco-v0.2/ao_lco.c index 0bbb76f1..b3f5bb16 100644 --- a/src/telelco-v0.2/ao_lco.c +++ b/src/telelco-v0.2/ao_lco.c @@ -251,18 +251,22 @@ ao_lco_search(void) { uint16_t tick_offset; int8_t r; + int8_t try; uint8_t box; ao_lco_box_reset_present(); for (box = 0; box < AO_PAD_MAX_BOXES; box++) { if ((box % 10) == 0) ao_lco_set_box(box); - tick_offset = 0; - r = ao_lco_query(box, &ao_pad_query, &tick_offset); - PRINTD("box %d result %d\n", box, r); - if (r == AO_RADIO_CMAC_OK) { - ao_lco_box_set_present(box); - ao_delay(AO_MS_TO_TICKS(30)); + for (try = 0; try < 5; try++) { + tick_offset = 0; + r = ao_lco_query(box, &ao_pad_query, &tick_offset); + PRINTD("box %d result %d\n", box, r); + if (r == AO_RADIO_CMAC_OK) { + ao_lco_box_set_present(box); + ao_delay(AO_MS_TO_TICKS(30)); + break; + } } } if (ao_lco_min_box <= ao_lco_max_box) -- cgit v1.2.3 From 8d9c79f5c162e07d57d42c6ba5825a3327a911d5 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 9 May 2014 00:05:39 -0700 Subject: altos: Simplify quadrature tracking 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 --- src/drivers/ao_quadrature.c | 66 +++++++++++++++++---------------------------- src/stm/ao_fast_timer.c | 8 ++++-- src/telelco-v0.2/ao_pins.h | 2 ++ 3 files changed, 33 insertions(+), 43 deletions(-) (limited to 'src/telelco-v0.2') diff --git a/src/drivers/ao_quadrature.c b/src/drivers/ao_quadrature.c index d07488d0..66a77dfa 100644 --- a/src/drivers/ao_quadrature.c +++ b/src/drivers/ao_quadrature.c @@ -22,11 +22,7 @@ #include __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; diff --git a/src/stm/ao_fast_timer.c b/src/stm/ao_fast_timer.c index d61b40c9..9a73eb51 100644 --- a/src/stm/ao_fast_timer.c +++ b/src/stm/ao_fast_timer.c @@ -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; diff --git a/src/telelco-v0.2/ao_pins.h b/src/telelco-v0.2/ao_pins.h index 62f221a1..609e5494 100644 --- a/src/telelco-v0.2/ao_pins.h +++ b/src/telelco-v0.2/ao_pins.h @@ -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 */ -- cgit v1.2.3 From 819f73698f57e76dca50fe4fadccebd23ffb776d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Thu, 15 May 2014 09:31:24 -0600 Subject: altos: Make quadrature debounce per-pin rather than per-device Debouncing per-pin means we don't lose transitions, which makes counting a lot more precise. Signed-off-by: Keith Packard --- src/drivers/ao_quadrature.c | 39 +++++++++++++++++++++++++++++++++------ src/telelco-v0.2/ao_pins.h | 2 +- 2 files changed, 34 insertions(+), 7 deletions(-) (limited to 'src/telelco-v0.2') diff --git a/src/drivers/ao_quadrature.c b/src/drivers/ao_quadrature.c index 66a77dfa..0cdcc9fb 100644 --- a/src/drivers/ao_quadrature.c +++ b/src/drivers/ao_quadrature.c @@ -24,6 +24,13 @@ __xdata int32_t ao_quadrature_count[AO_QUADRATURE_COUNT]; static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT]; +struct ao_debounce { + uint8_t state; + uint8_t count; +}; + +static struct ao_debounce ao_debounce_state[AO_QUADRATURE_COUNT][2]; + #define port(q) AO_QUADRATURE_ ## q ## _PORT #define bita(q) AO_QUADRATURE_ ## q ## _A #define bitb(q) AO_QUADRATURE_ ## q ## _B @@ -31,14 +38,35 @@ static uint8_t ao_quadrature_state[AO_QUADRATURE_COUNT]; #define pinb(q) AO_QUADRATURE_ ## q ## _B ## _PIN #define isr(q) ao_quadrature_isr_ ## q -static inline uint16_t -ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b) { - uint16_t v = stm_gpio_get_all(gpio); +#define DEBOUNCE 10 - return ~((((v >> pin_a) & 1) | (((v >> pin_b) & 1) << 1))) & 3; +static uint8_t +ao_debounce(uint8_t cur, struct ao_debounce *debounce) +{ + if (cur == debounce->state) + debounce->count = 0; + else { + if (++debounce->count == DEBOUNCE) { + debounce->state = cur; + debounce->count = 0; + } + } + return debounce->state; } -#define _ao_quadrature_get(q) ao_quadrature_read(port(q), bita(q), bitb(q)) +static uint16_t +ao_quadrature_read(struct stm_gpio *gpio, uint8_t pin_a, uint8_t pin_b, struct ao_debounce debounce_state[2]) { + uint16_t v = ~stm_gpio_get_all(gpio); + uint8_t a = (v >> pin_a) & 1; + uint8_t b = (v >> pin_b) & 1; + + a = ao_debounce(a, &debounce_state[0]); + b = ao_debounce(b, &debounce_state[1]); + + return a | (b << 1); +} + +#define _ao_quadrature_get(q) ao_quadrature_read(port(q), bita(q), bitb(q), ao_debounce_state[q]) static void _ao_quadrature_queue(uint8_t q, int8_t step) @@ -50,7 +78,6 @@ _ao_quadrature_queue(uint8_t q, int8_t step) ao_wakeup(&ao_quadrature_count[q]); } - static void _ao_quadrature_set(uint8_t q, uint8_t new) { uint8_t old = ao_quadrature_state[q]; diff --git a/src/telelco-v0.2/ao_pins.h b/src/telelco-v0.2/ao_pins.h index 609e5494..a6fd4ff8 100644 --- a/src/telelco-v0.2/ao_pins.h +++ b/src/telelco-v0.2/ao_pins.h @@ -72,7 +72,7 @@ #define PACKET_HAS_SLAVE 0 #define PACKET_HAS_MASTER 0 -#define FAST_TIMER_FREQ 200 /* 5ms for debouncing */ +#define FAST_TIMER_FREQ 10000 /* .1ms for debouncing */ /* * Radio is a cc1120 connected via SPI -- cgit v1.2.3 From b278a73cb54ba2f107bf91089f87c11528f017ab Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 21 May 2014 01:41:38 -0700 Subject: altos/stm: Make stm applications depend on ao_boot.h This should make sure they get recompiled when boot stuff changes. Signed-off-by: Keith Packard --- src/easymega-v0.1/Makefile | 1 + src/megadongle-v0.1/Makefile | 1 + src/stm-demo/Makefile | 1 + src/stm-flash/Makefile | 1 + src/teleballoon-v2.0/Makefile | 1 + src/telegps-v0.1/Makefile | 1 + src/telelco-v0.1/Makefile | 1 + src/telelco-v0.2/Makefile | 1 + src/telemega-v1.0/Makefile | 1 + src/telemetrum-v2.0/Makefile | 1 + 10 files changed, 10 insertions(+) (limited to 'src/telelco-v0.2') diff --git a/src/easymega-v0.1/Makefile b/src/easymega-v0.1/Makefile index 6e440fe4..66619852 100644 --- a/src/easymega-v0.1/Makefile +++ b/src/easymega-v0.1/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_companion.h \ ao_data.h \ ao_sample.h \ diff --git a/src/megadongle-v0.1/Makefile b/src/megadongle-v0.1/Makefile index 6035c348..ade8e496 100644 --- a/src/megadongle-v0.1/Makefile +++ b/src/megadongle-v0.1/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_pins.h \ ao_product.h \ ao_cc1120_CC1120.h \ diff --git a/src/stm-demo/Makefile b/src/stm-demo/Makefile index 8260abee..869fb32f 100644 --- a/src/stm-demo/Makefile +++ b/src/stm-demo/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_pins.h \ ao_product.h diff --git a/src/stm-flash/Makefile b/src/stm-flash/Makefile index 5c0699e1..568ab85a 100644 --- a/src/stm-flash/Makefile +++ b/src/stm-flash/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_pins.h \ ao_product.h diff --git a/src/teleballoon-v2.0/Makefile b/src/teleballoon-v2.0/Makefile index a17a06d6..28588778 100644 --- a/src/teleballoon-v2.0/Makefile +++ b/src/teleballoon-v2.0/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_companion.h \ ao_data.h \ ao_sample.h \ diff --git a/src/telegps-v0.1/Makefile b/src/telegps-v0.1/Makefile index 77ef9c4a..46eb0ac5 100644 --- a/src/telegps-v0.1/Makefile +++ b/src/telegps-v0.1/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_pins.h \ ao_product.h \ ao_task.h \ diff --git a/src/telelco-v0.1/Makefile b/src/telelco-v0.1/Makefile index 44d9237f..f7628c30 100644 --- a/src/telelco-v0.1/Makefile +++ b/src/telelco-v0.1/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_companion.h \ ao_data.h \ ao_sample.h \ diff --git a/src/telelco-v0.2/Makefile b/src/telelco-v0.2/Makefile index f28bdd32..7a21f099 100644 --- a/src/telelco-v0.2/Makefile +++ b/src/telelco-v0.2/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_companion.h \ ao_data.h \ ao_sample.h \ diff --git a/src/telemega-v1.0/Makefile b/src/telemega-v1.0/Makefile index 7a0c1195..46c768e4 100644 --- a/src/telemega-v1.0/Makefile +++ b/src/telemega-v1.0/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_companion.h \ ao_data.h \ ao_sample.h \ diff --git a/src/telemetrum-v2.0/Makefile b/src/telemetrum-v2.0/Makefile index 83a364dc..d77e9585 100644 --- a/src/telemetrum-v2.0/Makefile +++ b/src/telemetrum-v2.0/Makefile @@ -9,6 +9,7 @@ INC = \ ao.h \ ao_arch.h \ ao_arch_funcs.h \ + ao_boot.h \ ao_companion.h \ ao_data.h \ ao_sample.h \ -- cgit v1.2.3