]> www.fi.muni.cz Git - bike-lights.git/blobdiff - firmware/adc.c
mudflap for dual rearlights
[bike-lights.git] / firmware / adc.c
index f55a42792570670e464c1e66d035b809b6ecc298..2bca0cf2e8cd8951acca37bbf04c6266fe787a5b 100644 (file)
@@ -3,11 +3,14 @@
 
 #include "lights.h"
 
-#define AMBIENT_ADC N_PWMLEDS
-#define BATTERY_ADC (N_PWMLEDS + 1)
-#define ADC1_GAIN20 (N_PWMLEDS + 2)
-#define BUTTON_ADC  (N_PWMLEDS + 3)
-#define ZERO_ADC    (N_PWMLEDS + 4)
+// 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
 
@@ -19,13 +22,15 @@ struct {
        { 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, AMBIENT_ADC_SHIFT },    // ambient
+       { 0, 1, 0 },                    // buttons
        { 0, 1, 0 },                    // battery
        { 0, 1, 0 },                    // gain20
-       { 0, 1, 0 },                    // buttons
 };
 
-volatile static unsigned char current_adc;
+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;
@@ -47,16 +52,16 @@ static void setup_mux(unsigned char n)
        case AMBIENT_ADC: // ambient light: 1.1V, ADC5 (PA6), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0);
                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);
+               break;
        case BATTERY_ADC: // batt voltage: 1.1V, ADC6 (PA7), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX1);
                break;
        case ADC1_GAIN20: // gain stage offset: 1.1V, ADC1,1, gain 20
                ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
                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);
-               break;
        case ZERO_ADC: // zero: 1.1V, ADC1 (PA1), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX0);
                break;
@@ -65,14 +70,31 @@ static void setup_mux(unsigned char n)
 
 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--;
-       } else {
-               // TODO: kick the watchdog here.
-               current_adc = NUM_ADCS;
+               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:
@@ -98,12 +120,17 @@ static void start_next_adc()
        ADCSRA |= _BV(ADSC);
 }
 
-void timer_start_adcs()
+void timer_start_slow_adcs()
 {
-       if (current_adc == NUM_ADCS) // Don't start if in progress
+       if ((jiffies & 0x000F) == 0) {
+               slow_adcs_wanted = FIRST_1S_ADC;
+       } else {
+               slow_adcs_wanted = FIRST_16HZ_ADC;
+       }
+
+       if (!adc_is_on) {
                start_next_adc();
-       else
-               log_byte(0x99); // overrun
+       }
 }
 
 /*
@@ -129,7 +156,9 @@ static uint16_t read_adc_sync()
 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
@@ -175,6 +204,18 @@ static void adc1_gain20_adc(uint16_t adcsum)
 ISR(ADC_vect) { // IRQ handler
        uint16_t adcval = ADCW;
 
+       /*
+        * 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 (zero_count) {
                if (zero_count > 1) {
                        ADCSRA |= _BV(ADSC);
@@ -197,6 +238,7 @@ ISR(ADC_vect) { // IRQ handler
                ADCSRA |= _BV(ADSC);
                adc_sum += adcval;
                read_count--;
+               pwm_timer();
                return;
        }
 
@@ -225,12 +267,12 @@ ISR(ADC_vect) { // IRQ handler
        case AMBIENT_ADC:
                ambient_adc(adc_sum);
                break;
-       case BATTERY_ADC:
-               battery_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;