]> www.fi.muni.cz Git - bike-lights.git/commitdiff
adc.c: configurable number of pre-readings
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 17 Mar 2013 16:59:23 +0000 (17:59 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 17 Mar 2013 16:59:23 +0000 (17:59 +0100)
When there is a supposed ADC noise problem, it is handful to drop
several first readings, and even discharge the ADC capacitor
by reading the single-ended wire connected to zero. This commit
rewrites the ADC handling to allow exactly this, for each ADC input.

firmware/adc.c

index 9cc1374452eed00f11c75b0ee384d811485b7eca..02af6b380724ae6f92ccca9407d1e1951a75c53f 100644 (file)
@@ -7,52 +7,60 @@
 #define BATTERY_ADC (N_PWMLEDS + 1)
 #define ADC1_GAIN20 (N_PWMLEDS + 2)
 #define BUTTON_ADC  (N_PWMLEDS + 3)
+#define ZERO_ADC    (N_PWMLEDS + 4)
+
+#define NUM_ADCS       ZERO_ADC
+
+struct {
+       unsigned char read_zero_log : 2;
+       unsigned char read_drop_log : 2;
+       unsigned char read_keep_log : 4;
+} adc_params[NUM_ADCS] = {
+       { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 1
+       { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 2
+       { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 3
+       { 0, 1, 3 },                    // ambient
+       { 0, 1, 0 },                    // battery
+       { 0, 1, 0 },                    // gain20
+       { 0, 1, 0 },                    // buttons
+};
 
-#define NUM_ADCS 7
 volatile static unsigned char current_adc;
-static uint16_t adc_sum;
-static unsigned char sum_shift;
-static unsigned char adc_vals;
+static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log;
 #define ADC1_GAIN20_OFFSET_SHIFT       6
 static uint16_t adc1_gain20_offset;
 
-static void inline setup_mux(unsigned char n)
+
+static void setup_mux(unsigned char n)
 {
-       /* ADC numbering: PWM LEDs first, then ambient light sensor, battery sensor */
+       /* ADC numbering: PWM LEDs first, then others, zero at the end */
        switch (n) {
        case 0: // pwmled 1: 1.1V, ADC0,1 (PA0,1), gain 20
                ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX1) | _BV(MUX0);
-               sum_shift = PWMLED_ADC_SHIFT;
                break;
        case 1: // pwmled 2: 1.1V, ADC2,1 (PA2,1), gain 20
                ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
-               sum_shift = PWMLED_ADC_SHIFT;
                break;
        case 2: // pwmled 3: 1.1V, ADC4 (PA5), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2);
-               sum_shift = PWMLED_ADC_SHIFT;
                break;
        case AMBIENT_ADC: // ambient light: 1.1V, ADC5 (PA6), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0);
-               sum_shift = 3; // 3 measurements
                break;
        case BATTERY_ADC: // batt voltage: 1.1V, ADC6 (PA7), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX1);
-               sum_shift = 0; // 1 measurement
                break;
        case ADC1_GAIN20: // gain stage offset: 1.1V, ADC1,1, gain 20
                ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
-               sum_shift = 0; // 1 measurement
                break;
        case BUTTON_ADC: // buttons: 1.1V, ADC3, single-ended
                PORTA |= _BV(PA3); // +5V to the voltage splitter
                ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
-               sum_shift = 0;
+               break;
+       case ZERO_ADC: // zero: 1.1V, ADC1 (PA1), single-ended
+               ADMUX = _BV(REFS1) | _BV(MUX0);
                break;
        }
-
-       adc_sum = 0;
-       adc_vals = 1 << sum_shift;
 }
 
 static void start_next_adc()
@@ -63,8 +71,28 @@ static void start_next_adc()
                // TODO: kick the watchdog here.
                current_adc = NUM_ADCS-1;
 
+       adc_sum = 0;
+       // we use the last iteration of zero_count to set up the MUX
+       // to its final destination, hence the "1 +" below:
+       if (adc_params[current_adc].read_zero_log)
+               zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
+       else
+               zero_count = 1;
+
+       if (adc_params[current_adc].read_drop_log)
+               drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
+       else
+               drop_count = 0;
+
+       read_count = 1 << adc_params[current_adc].read_keep_log;
+       n_reads_log = adc_params[current_adc].read_keep_log;
+
        // set up mux, start one-shot conversion
-       setup_mux(current_adc);
+       if (zero_count > 1)
+               setup_mux(ZERO_ADC);
+       else
+               setup_mux(current_adc);
+
        ADCSRA |= _BV(ADSC);
 }
 
@@ -127,48 +155,77 @@ void susp_adc()
        DIDR0 = 0;
 }
 
+static void adc1_gain20_adc(uint16_t adcsum)
+{
+       // running average
+       adc1_gain20_offset += adcsum
+                       - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
+}
+
 ISR(ADC_vect) { // IRQ handler
        uint16_t adcval = ADCW;
 
-       if (adc_vals)
-               // start the next conversion immediately
-               ADCSRA |= _BV(ADSC);
-
-       if (adc_vals < (1 << sum_shift))
-                // drop the first conversion, use all others
-                adc_sum += adcval;
+       if (zero_count) {
+               if (zero_count > 1) {
+                       ADCSRA |= _BV(ADSC);
+                       zero_count--;
+                       return;
+               } else {
+                       setup_mux(current_adc);
+                       zero_count = 0;
+                       /* fall through */
+               }
+       }
 
-       if (adc_vals) {
-               adc_vals--;
+       if (drop_count) {
+               ADCSRA |= _BV(ADSC); // drop this one, start the next
+               drop_count--;
                return;
        }
 
-       // Now handle the (1 << sum_shift) measurements
+       if (read_count) {
+               ADCSRA |= _BV(ADSC);
+               adc_sum += adcval;
+               read_count--;
+               return;
+       }
 
-       adcval = adc_sum >> sum_shift;
+       /*
+        * Now we have performed read_count measurements and have them
+        * in adc_sum.
+        */
 
-       if (current_adc == ADC1_GAIN20) {
-               // running average
-               adc1_gain20_offset += adcval
-                       - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
-       } else if (current_adc == 0 || current_adc == 1) {
+       // For inputs with gain, subtract the measured gain stage offset
+       if (current_adc < 2) {
                uint16_t offset = adc1_gain20_offset
-                       >> (ADC1_GAIN20_OFFSET_SHIFT - sum_shift);
+                       >> (ADC1_GAIN20_OFFSET_SHIFT - n_reads_log);
+
                if (adc_sum > offset)
                        adc_sum -= offset;
                else
                        adc_sum = 0;
        }
 
-       if (current_adc < N_PWMLEDS)
+       switch (current_adc) {
+       case 0:
+       case 1:
+       case 2:
                pwmled_adc(current_adc, adc_sum);
-       if (current_adc == AMBIENT_ADC)
+               break;
+       case AMBIENT_ADC:
                ambient_adc(adc_sum);
-       if (current_adc == BATTERY_ADC)
-               battery_adc(adcval);
-       if (current_adc == BUTTON_ADC)
-               button_adc(adcval);
-       
+               break;
+       case BATTERY_ADC:
+               battery_adc(adc_sum);
+               break;
+       case BUTTON_ADC:
+               button_adc(adc_sum);
+               break;
+       case ADC1_GAIN20:
+               adc1_gain20_adc(adcval);
+               break;
+       }
+
        start_next_adc();
 }