altos/lpc: Filter ADC inputs
authorKeith Packard <keithp@keithp.com>
Sat, 15 Jun 2013 08:20:49 +0000 (01:20 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 15 Jun 2013 08:21:45 +0000 (01:21 -0700)
They're amazingly noisy on EasyMini, so just filter them as the only
thing we use them for is battery and pyro numbers.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/lpc/ao_adc_lpc.c
src/lpc/ao_arch_funcs.h
src/lpc/lpc.h

index 25a121b53ca6eb4391991fd645323927db101859..40173aa451315e371044a5c30e5d868b0369df3f 100644 (file)
                         (AO_ADC_6 << 6) |      \
                         (AO_ADC_7 << 7))
 
-#define AO_ADC_CLKDIV  (AO_LPC_SYSCLK / 4500000 - 1)
+#define AO_ADC_CLKDIV  (AO_LPC_SYSCLK / 4500000)
 
-static uint8_t                 ao_adc_ready;
+static uint8_t         ao_adc_ready;
+
+static uint16_t                ao_adc_prev[AO_NUM_ADC];
+
+#define sample(id)     do {                            \
+               uint16_t        p = *prev;              \
+               uint16_t        v = lpc_adc.dr[id];     \
+               p -= p >> 4;                            \
+               p += v >> 4;                            \
+               *prev++ = p;                            \
+               *out++ = p >> 1;                        \
+       } while (0)
 
 void  lpc_adc_isr(void)
 {
        uint32_t        stat = lpc_adc.stat;
        uint16_t        *out;
+       uint16_t        *prev;
 
-       lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) |
-                     (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
-                     (0 << LPC_ADC_CR_BURST) |
-                     (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
+       /* Turn off burst mode */
+       lpc_adc.cr = 0;
        lpc_adc.stat = 0;
 
        /* Store converted values in packet */
 
        out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
+       prev = ao_adc_prev;
 #if AO_ADC_0
-       *out++ = lpc_adc.dr[0] >> 1;
+       sample(0);
 #endif
 #if AO_ADC_1
-       *out++ = lpc_adc.dr[1] >> 1;
+       sample(1);
 #endif
 #if AO_ADC_2
-       *out++ = lpc_adc.dr[2] >> 1;
+       sample(2);
 #endif
 #if AO_ADC_3
-       *out++ = lpc_adc.dr[3] >> 1;
+       sample(3);
 #endif
 #if AO_ADC_4
-       *out++ = lpc_adc.dr[4] >> 1;
+       sample(4);
 #endif
 #if AO_ADC_5
-       *out++ = lpc_adc.dr[5] >> 1;
+       sample(5); 
 #endif
 #if AO_ADC_6
-       *out++ = lpc_adc.dr[6] >> 1;
+       sample(6);
 #endif
 #if AO_ADC_7
-       *out++ = lpc_adc.dr[7] >> 1;
+       sample(7);
 #endif
 
        AO_DATA_PRESENT(AO_DATA_ADC);
index 179b2f4d0ae3963dba54febef71ab8889f058004..1bbb14f5b8773338a643ea1b7488cde0f8e319a1 100644 (file)
 #define analog_func(id)                        lpc_token_evaluator_2(LPC_IOCONF_FUNC_AD,id)
 
 #define ao_enable_analog(port,bit,id) do {                             \
-               uint32_t _mode;                                         \
                ao_enable_port(port);                                   \
                lpc_gpio.dir[port] &= ~(1 << bit);                      \
-               _mode = ((analog_func(id) << LPC_IOCONF_FUNC) |         \
-                        (0 << LPC_IOCONF_ADMODE));                     \
-               lpc_ioconf.analog_reg(port,bit) = _mode;                \
+               lpc_ioconf.analog_reg(port,bit) = ((analog_func(id) << LPC_IOCONF_FUNC) | \
+                                                  (0 << LPC_IOCONF_ADMODE)); \
        } while (0)
        
 #define ARM_PUSH32(stack, val) (*(--(stack)) = (val))
index 49034c1c564e71107ff5586c5acdbe74fb3c621c..d66f0dd00bdf2b98d328a6a06713bfbe62be0b8b 100644 (file)
@@ -1169,6 +1169,13 @@ extern struct lpc_adc lpc_adc;
 #define  LPC_ADC_CR_CLKS_6             5
 #define  LPC_ADC_CR_CLKS_5             6
 #define  LPC_ADC_CR_CLKS_4             7
+#define LPC_ADC_CR_START       24
+#define  LPC_ADC_CR_START_NONE         0
+#define  LPC_ADC_CR_START_NOW          1
+
+#define LPC_ADC_GDR_CHN                        24
+#define LPC_ADC_GDR_OVERRUN            30
+#define LPC_ADC_GDR_DONE               31
 
 #define LPC_ADC_INTEN_ADINTEN  0
 #define LPC_ADC_INTEN_ADGINTEN 8