altos/lpc: Make ADC inputs work
authorKeith Packard <keithp@keithp.com>
Fri, 14 Jun 2013 11:38:11 +0000 (04:38 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 15 Jun 2013 08:21:39 +0000 (01:21 -0700)
They're still very unstable (bouncing around a lot), but at least they
seem to report useful stuff now.

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

index 9ecc7c13f1e313d572d98e1bad45d4eb4aefcbd0..25a121b53ca6eb4391991fd645323927db101859 100644 (file)
 # endif
 #endif
 
+#define AO_ADC_MASK    ((AO_ADC_0 << 0) |      \
+                        (AO_ADC_1 << 1) |      \
+                        (AO_ADC_2 << 2) |      \
+                        (AO_ADC_3 << 3) |      \
+                        (AO_ADC_4 << 4) |      \
+                        (AO_ADC_5 << 5) |      \
+                        (AO_ADC_6 << 6) |      \
+                        (AO_ADC_7 << 7))
+
+#define AO_ADC_CLKDIV  (AO_LPC_SYSCLK / 4500000 - 1)
+
 static uint8_t                 ao_adc_ready;
 
 void  lpc_adc_isr(void)
 {
        uint32_t        stat = lpc_adc.stat;
-       uint16_t        *out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
-       vuint32_t       *in = &lpc_adc.dr[0];
+       uint16_t        *out;
 
-       lpc_adc.cr = 0;
+       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));
        lpc_adc.stat = 0;
 
        /* Store converted values in packet */
 
+       out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
 #if AO_ADC_0
        *out++ = lpc_adc.dr[0] >> 1;
 #endif
@@ -141,16 +155,6 @@ void  lpc_adc_isr(void)
        ao_adc_ready = 1;
 }
 
-#define AO_ADC_MASK    ((AO_ADC_0 << 0) |      \
-                        (AO_ADC_1 << 1) |      \
-                        (AO_ADC_2 << 2) |      \
-                        (AO_ADC_3 << 3) |      \
-                        (AO_ADC_4 << 4) |      \
-                        (AO_ADC_5 << 5) |      \
-                        (AO_ADC_6 << 6) |      \
-                        (AO_ADC_7 << 7))
-
-#define AO_ADC_CLKDIV  (AO_LPC_CLKOUT / 4500000)
 
 /*
  * Start the ADC sequence using the DMA engine
@@ -204,29 +208,35 @@ ao_adc_init(void)
        lpc_nvic_set_enable(LPC_ISR_ADC_POS);
        lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
 #if AO_ADC_0
-       ao_enable_analog(0, 11);
+       ao_enable_analog(0, 11, 0);
 #endif
 #if AO_ADC_1
-       ao_enable_analog(0, 12);
+       ao_enable_analog(0, 12, 1);
 #endif
 #if AO_ADC_2
-       ao_enable_analog(0, 13);
+       ao_enable_analog(0, 13, 2);
 #endif
 #if AO_ADC_3
-       ao_enable_analog(0, 14);
+       ao_enable_analog(0, 14, 3);
 #endif
 #if AO_ADC_4
-       ao_enable_analog(0, 14);
+       ao_enable_analog(0, 15, 4);
 #endif
 #if AO_ADC_5
-       ao_enable_analog(0, 14);
+       ao_enable_analog(0, 16, 5);
 #endif
 #if AO_ADC_6
-       ao_enable_analog(0, 14);
+       ao_enable_analog(0, 22, 6);
 #endif
 #if AO_ADC_7
-       ao_enable_analog(0, 14);
+       ao_enable_analog(0, 23, 7);
 #endif
+
+       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));
+
        ao_cmd_register(&ao_adc_cmds[0]);
 
        ao_adc_ready = 1;
index 204d1227dd5f525d02579efd85dac30db4a55096..179b2f4d0ae3963dba54febef71ab8889f058004 100644 (file)
                            (1 << LPC_IOCONF_ADMODE));                  \
        } while (0)
 
-#define ao_enable_analog(port,bit) do {                                        \
-               vuint32_t *_ioconf = &lpc_ioconf.pio0_0 + ((port)*24+(bit)); \
+#define lpc_token_paster_2(x,y)                x ## y
+#define lpc_token_evaluator_2(x,y)     lpc_token_paster_2(x,y)
+#define lpc_token_paster_3(x,y,z)      x ## y ## z
+#define lpc_token_evaluator_3(x,y,z)   lpc_token_paster_3(x,y,z)
+#define lpc_token_paster_4(w,x,y,z)    w ## x ## y ## z
+#define lpc_token_evaluator_4(w,x,y,z) lpc_token_paster_4(w,x,y,z)
+#define analog_reg(port,bit)           lpc_token_evaluator_4(pio,port,_,bit)
+#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);                      \
-               *_ioconf = *_ioconf & ~((1 << LPC_IOCONF_ADMODE) |      \
-                                       (LPC_IOCONF_MODE_MASK << LPC_IOCONF_MODE)); \
+               _mode = ((analog_func(id) << LPC_IOCONF_FUNC) |         \
+                        (0 << LPC_IOCONF_ADMODE));                     \
+               lpc_ioconf.analog_reg(port,bit) = _mode;                \
        } while (0)
        
 #define ARM_PUSH32(stack, val) (*(--(stack)) = (val))