]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/adc.c
adc.c: make the ADC handlers non-atomic
[bike-lights.git] / firmware / adc.c
index 8bc171a390bd684ba79a345c39f377bb909cbbd2..5f7849fb1128ed47414f71fd1072dddab68e3e80 100644 (file)
@@ -1,34 +1,37 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
+#include <util/atomic.h>
 
 #include "lights.h"
 
 #define AMBIENT_ADC N_PWMLEDS
 #define BATTERY_ADC (N_PWMLEDS + 1)
+#define ADC1_GAIN20 (N_PWMLEDS + 2)
 
-#define NUM_ADCS 5
+#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;
+#define ADC1_GAIN20_OFFSET_SHIFT       6
+static uint16_t adc1_gain20_offset;
+static unsigned char handler_running;
 
 static void inline setup_mux(unsigned char n)
 {
-       ADCSRA |= _BV(ADEN); // enable ADC
-
        /* ADC numbering: PWM LEDs first, then ambient light sensor, battery sensor */
        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
+               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 = 3; // 8 measurements
+               sum_shift = PWMLED_ADC_SHIFT;
                break;
        case 2: // pwmled 3: 1.1V, ADC4 (PA5), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2);
-               sum_shift = 3; // 8 measurements
+               sum_shift = PWMLED_ADC_SHIFT;
                break;
        case 3: // ambient light: 1.1V, ADC5 (PA6), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0);
@@ -38,6 +41,10 @@ static void inline setup_mux(unsigned char n)
                ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX1);
                sum_shift = 0; // 1 measurement
                break;
+       case 5: // gain stage offset: 1.1V, ADC1,1, gain 20
+               ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
+               sum_shift = 0; // 1 measurement
+               break;
        }
 
        adc_sum = 0;
@@ -46,20 +53,42 @@ static void inline setup_mux(unsigned char n)
 
 static void start_next_adc()
 {
-       if (current_adc > 0) {
+       if (current_adc > 0)
                current_adc--;
-               // set up mux, start one-shot conversion
-               setup_mux(current_adc);
-               ADCSRA |= _BV(ADSC);
-       } else {
-               current_adc = NUM_ADCS;
+       else
                // TODO: kick the watchdog here.
-       }
+               current_adc = NUM_ADCS-1;
+
+       // set up mux, start one-shot conversion
+       setup_mux(current_adc);
+       ADCSRA |= _BV(ADSC);
+}
+
+/*
+ * 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;
+       handler_running = 0;
 
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
@@ -71,15 +100,22 @@ void init_adc()
        DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D)
                | _BV(ADC4D) | _BV(ADC5D) | _BV(ADC6D);
 
-       ADCSRA |= _BV(ADSC);
+       // 1.1V, ADC1,1, gain 20
+       ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
 
        /* 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 = 0;
+
+       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()
@@ -94,8 +130,6 @@ ISR(ADC_vect) { // IRQ handler
        if (adc_vals)
                // start the next conversion immediately
                ADCSRA |= _BV(ADSC);
-       else
-               ADCSRA &= ~_BV(ADEN); // the last one, disable ADC
 
        if (adc_vals < (1 << sum_shift))
                 // drop the first conversion, use all others
@@ -110,19 +144,41 @@ ISR(ADC_vect) { // IRQ handler
 
        adcval = adc_sum >> sum_shift;
 
-       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();
-}
+       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) {
+               uint16_t offset = adc1_gain20_offset
+                       >> (ADC1_GAIN20_OFFSET_SHIFT - sum_shift);
+               if (adc_sum > offset)
+                       adc_sum -= offset;
+               else
+                       adc_sum = 0;
+       }
+
+       if (handler_running & (1 << current_adc)) {
+               log_byte(0xB0 + current_adc);
+
+               // drop the result, what else to do?
 
-void timer_start_adcs()
-{
-       if (current_adc == NUM_ADCS) // Don't start if in progress
                start_next_adc();
+       } else {
+               unsigned char current_adc_copy = current_adc;
+               uint16_t adc_sum_copy = adc_sum;
+
+               start_next_adc();
+
+               handler_running |= (1 << current_adc_copy);
+               NONATOMIC_BLOCK(NONATOMIC_FORCEOFF) {
+                       if (current_adc_copy < N_PWMLEDS)
+                               pwmled_adc(current_adc_copy, adc_sum_copy);
+                       if (current_adc_copy == AMBIENT_ADC)
+                               ambient_adc(adc_sum_copy);
+                       if (current_adc_copy == BATTERY_ADC)
+                               battery_adc(adc_sum_copy);
+               }
+               handler_running &= ~(1 << current_adc_copy);
+       }
 }