]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/adc.c
mudflap for dual rearlights
[bike-lights.git] / firmware / adc.c
index 298256f51580efceadda2b7f843bc20aff0119ed..2bca0cf2e8cd8951acca37bbf04c6266fe787a5b 100644 (file)
 
 #include "lights.h"
 
-#define AMBIENT_ADC N_PWMLEDS
-#define BATTERY_ADC (N_PWMLEDS + 1)
-#define ADC1_GAIN20 (N_PWMLEDS + 2)
-
-#define NUM_ADCS 6
-volatile static unsigned char current_adc;
-static uint16_t adc_sum;
-static unsigned char sum_shift;
-static unsigned char adc_vals;
-static uint16_t adc1_gain20_offset_x16;
-
-static void inline setup_mux(unsigned char n)
-{
-       ADCSRA |= _BV(ADEN); // enable ADC
+// pwmleds are measured continuously (when active)
+#define AMBIENT_ADC N_PWMLEDS          // measured every jiffy (16 Hz)
+#define BUTTON_ADC  (N_PWMLEDS + 1)    // measured every jiffy (16 Hz)
+#define FIRST_16HZ_ADC BUTTON_ADC
+#define BATTERY_ADC (N_PWMLEDS + 2)    // once per second
+#define ADC1_GAIN20 (N_PWMLEDS + 3)    // once per second
+#define FIRST_1S_ADC   ADC1_GAIN20
+#define ZERO_ADC    (N_PWMLEDS + 4)    // must be last
+
+#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, AMBIENT_ADC_SHIFT },    // ambient
+       { 0, 1, 0 },                    // buttons
+       { 0, 1, 0 },                    // battery
+       { 0, 1, 0 },                    // gain20
+};
+
+volatile unsigned char adc_is_on;
+
+volatile static unsigned char current_adc, slow_adcs_wanted;
+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;
 
-       /* ADC numbering: PWM LEDs first, then ambient light sensor, battery sensor */
+
+static void setup_mux(unsigned char n)
+{
+       /* 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 = 3; // 8 measurements
                break;
        case 1: // pwmled 2: 1.1V, ADC2,1 (PA2,1), gain 20
                ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
-               sum_shift = 3; // 8 measurements
                break;
        case 2: // pwmled 3: 1.1V, ADC4 (PA5), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2);
-               sum_shift = 2; // 4 measurements
                break;
-       case 3: // ambient light: 1.1V, ADC5 (PA6), single-ended
+       case AMBIENT_ADC: // ambient light: 1.1V, ADC5 (PA6), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0);
-               sum_shift = 0; // 1 measurement
                break;
-       case 4: // batt voltage: 1.1V, ADC6 (PA7), single-ended
+       case BUTTON_ADC: // buttons: 1.1V, ADC3, single-ended
+               PORTA |= _BV(PA3); // +5V to the voltage splitter
+               ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
+               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 5: // gain stage offset: 1.1V, ADC1,1, gain 20
+       case ADC1_GAIN20: // gain stage offset: 1.1V, ADC1,1, gain 20
                ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
-               sum_shift = 3; // 8 measurements
+               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()
 {
-       if (current_adc > 0) {
+       if (slow_adcs_wanted) {
+               current_adc = slow_adcs_wanted;
+               slow_adcs_wanted = 0;
+               goto found;
+       }
+
+       if (current_adc > N_PWMLEDS) {
                current_adc--;
-               // set up mux, start one-shot conversion
+               goto found;
+       }
+
+       if (!TIMER1_IS_ON()) {
+               adc_is_on = 0;
+               return;
+       }
+
+       do {
+               if (!current_adc)
+                       current_adc = N_PWMLEDS;
+               --current_adc;
+       } while (!PWM_IS_ON(current_adc));
+
+found:
+       adc_is_on = 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
+       if (zero_count > 1)
+               setup_mux(ZERO_ADC);
+       else
                setup_mux(current_adc);
-               ADCSRA |= _BV(ADSC);
+
+       ADCSRA |= _BV(ADSC);
+}
+
+void timer_start_slow_adcs()
+{
+       if ((jiffies & 0x000F) == 0) {
+               slow_adcs_wanted = FIRST_1S_ADC;
        } else {
-               current_adc = NUM_ADCS;
-               // TODO: kick the watchdog here.
+               slow_adcs_wanted = FIRST_16HZ_ADC;
+       }
+
+       if (!adc_is_on) {
+               start_next_adc();
        }
 }
 
+/*
+ * Single synchronous ADC conversion.
+ * Has to be called with IRQs disabled (or with the ADC IRQ disabled).
+ */
+static uint16_t read_adc_sync()
+{
+       uint16_t rv;
+
+       ADCSRA |= _BV(ADSC); // start the conversion
+
+       // wait for the conversion to finish
+       while((ADCSRA & _BV(ADIF)) == 0)
+               ;
+
+       rv = ADCW;
+       ADCSRA |= _BV(ADIF); // clear the IRQ flag
+
+       return rv;
+}
+
 void init_adc()
 {
        unsigned char i;
-       current_adc = NUM_ADCS;
+       current_adc = 0;
+       adc_is_on = 1;
+       slow_adcs_wanted = FIRST_1S_ADC;
 
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
@@ -75,33 +167,25 @@ void init_adc()
        // ADCSRB |= _BV(GSEL); // gain 8 or 32
 
        // Disable digital input on all bits used by ADC
-       DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D)
+       DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D) | _BV(ADC3D)
                | _BV(ADC4D) | _BV(ADC5D) | _BV(ADC6D);
 
        // 1.1V, ADC1,1, gain 20
        ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
-       ADCSRA |= _BV(ADSC);
 
        /* Do first conversion and drop the result */
-       while ((ADCSRA & _BV(ADIF)) == 0)
-               ;
-       ADCSRA |= _BV(ADIF); // clear the IRQ flag
+       read_adc_sync();
 
-       adc1_gain20_offset_x16 = 0;
+       adc1_gain20_offset = 0;
 
-       for (i = 0; i < 16; i++) {
-               ADCSRA |= _BV(ADSC);
-
-               while ((ADCSRA & _BV(ADIF)) == 0)
-                       ;
-               adc1_gain20_offset_x16 += ADCW;
-
-               ADCSRA |= _BV(ADIF); // clear the IRQ flag
+       for (i = 0; i < (1 << ADC1_GAIN20_OFFSET_SHIFT); i++) {
+               adc1_gain20_offset += read_adc_sync()
+                       - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
        }
 
        ADCSRA |= _BV(ADIE); // enable IRQ
 
-       ADCSRA &= ~_BV(ADEN); // disable until needed
+       start_next_adc();
 }
 
 void susp_adc()
@@ -110,55 +194,90 @@ 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);
-       else
-               ADCSRA &= ~_BV(ADEN); // the last one, disable ADC
+       /*
+        * After the timer interrupt, drop the current reading.
+        * We may have changed the PWM outputs, so the value is
+        * probably useless anyway.
+        * FIXME: possible race condition - we should make an explicit
+        * notification inside the timer IRQ handler.
+        */
+       if (slow_adcs_wanted) {
+               start_next_adc();
+               return;
+       }
 
-       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 (drop_count) {
+               ADCSRA |= _BV(ADSC); // drop this one, start the next
+               drop_count--;
+               return;
+       }
 
-       if (adc_vals) {
-               adc_vals--;
+       if (read_count) {
+               ADCSRA |= _BV(ADSC);
+               adc_sum += adcval;
+               read_count--;
+               pwm_timer();
                return;
        }
 
-       // Now handle the (1 << sum_shift) measurements
+       /*
+        * Now we have performed read_count measurements and have them
+        * in adc_sum.
+        */
 
-       adcval = adc_sum >> sum_shift;
+       // For inputs with gain, subtract the measured gain stage offset
+       if (current_adc < 2) {
+               uint16_t offset = adc1_gain20_offset
+                       >> (ADC1_GAIN20_OFFSET_SHIFT - n_reads_log);
 
-       if (current_adc == ADC1_GAIN20) {
-               // running average
-               adc1_gain20_offset_x16 += adcval
-                       - (adc1_gain20_offset_x16 >> 4);
-       } else if (current_adc == 0 || current_adc == 1) {
-               uint16_t offset = adc1_gain20_offset_x16 >> 4;
-               if (adcval >= offset)
-                       adcval -= offset;
+               if (adc_sum > offset)
+                       adc_sum -= offset;
                else
-                       adcval = 0;
+                       adc_sum = 0;
        }
 
-       if (current_adc < N_PWMLEDS)
-               pwmled_adc(current_adc, adcval);
-       if (current_adc == AMBIENT_ADC)
-               ambient_adc(adcval);
-       if (current_adc == BATTERY_ADC)
-               battery_adc(adcval);
-       
-       start_next_adc();
-}
+       switch (current_adc) {
+       case 0:
+       case 1:
+       case 2:
+               pwmled_adc(current_adc, adc_sum);
+               break;
+       case AMBIENT_ADC:
+               ambient_adc(adc_sum);
+               break;
+       case BUTTON_ADC:
+               button_adc(adc_sum);
+               break;
+       case BATTERY_ADC:
+               battery_adc(adc_sum);
+               break;
+       case ADC1_GAIN20:
+               adc1_gain20_adc(adcval);
+               break;
+       }
 
-void timer_start_adcs()
-{
-       if (current_adc == NUM_ADCS) // Don't start if in progress
-               start_next_adc();
-       else
-               log_byte(0x99);
+       start_next_adc();
 }