]> www.fi.muni.cz Git - tinyboard.git/blobdiff - projects/step-up/adc.c
Experimental step-up driver for chain of 5630 LEDs.
[tinyboard.git] / projects / step-up / adc.c
index 9daf99158ad26aa9391d24f3731409624c2cc9f4..55cd8610ab441cacc7663ddc8e605dde49869d7c 100644 (file)
@@ -1,35 +1,20 @@
 #include <avr/io.h>
 #include <avr/interrupt.h>
+#include <avr/power.h>
 #include <avr/sleep.h>
 
 #include "lights.h"
 
-#define BATTERY_ADC (N_PWMLEDS + 0)
-#define BUTTON_ADC  (N_PWMLEDS + 1)
-#define ZERO_ADC    (N_PWMLEDS + 2)
+#define ZERO_ADC    2
 
 //#define NUM_ADCS     ZERO_ADC
-#define NUM_ADCS       1
-
-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
-#if 0
-       { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 2
-       { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 3
-       { 0, 1, AMBIENT_ADC_SHIFT },    // ambient
-       { 0, 1, 0 },                    // battery
-       { 0, 1, 0 },                    // gain20
-       { 0, 1, 0 },                    // buttons
-#endif
-};
-
-volatile static unsigned char current_adc, current_slow_adc;
-static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log;
+#define NUM_ADCS       2
 
+volatile static unsigned char current_adc;
+volatile unsigned char adc_enabled;
+volatile unsigned char need_battery_adc, need_pwmled_adc;
+static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log;
+volatile uint16_t jiffies;
 
 static void setup_mux(unsigned char n)
 {
@@ -38,6 +23,9 @@ static void setup_mux(unsigned char n)
        case 0: // pwmled 1: 1.1V, ADC3 (PB3), single-ended
                ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
                break;
+       case 1: // battery voltage: 1.1V, ADC1 (PB2), single-ended
+               ADMUX = _BV(REFS1) | _BV(MUX0);
+               break;
        case ZERO_ADC: // zero: 1.1V, GND, single-ended
                ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
                break;
@@ -46,49 +34,36 @@ static void setup_mux(unsigned char n)
 
 void start_next_adc()
 {
-#if 0
-       if (current_adc == 0) {
-               if (current_slow_adc > N_PWMLEDS) {
-                       // read one of the non-PWMLED ADCs
-                       current_adc = --current_slow_adc;
-               } else {
-                       // no more non-PWMLEDs to do, start with PWMLEDs
-                       current_adc = N_PWMLEDS-1;
-               }
-       } else if (current_adc >= N_PWMLEDS) {
-               // one of the non-PWMLED ADCs just finished, skip to PWMLEDs.
-               current_adc = N_PWMLEDS-1;
+       if (need_battery_adc) {
+               need_battery_adc = 0;
+               current_adc = 1;
+               read_zero = 1;
+               drop_count = 1;
+               read_count = 1;
+               n_reads_log = 0;
+       } else if (need_pwmled_adc) {
+               current_adc = 0;
+               read_zero = 0;
+               drop_count = 1;
+               read_count = 1 << PWMLED_ADC_SHIFT;
+               n_reads_log = PWMLED_ADC_SHIFT;
        } else {
-               // next PWMLED
-               current_adc--;
+               ADCSRA &= ~_BV(ADEN);
+               power_adc_disable();
+               adc_enabled = 0;
+               return;
        }
-#else
-       // single ADC for testing only
-       current_adc = 0;
-#endif
 
-#if 0
-       log_byte(0x90 + current_adc); // debug ADC switching
-#endif
+       if (!adc_enabled) {
+               power_adc_enable();
+               ADCSRA |= _BV(ADEN);
+               adc_enabled = 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)
+       if (read_zero)
                setup_mux(ZERO_ADC);
        else
                setup_mux(current_adc);
@@ -96,6 +71,7 @@ void start_next_adc()
        ADCSRA |= _BV(ADSC);
 }
 
+#if 0
 void timer_start_slow_adcs()
 {
        if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress
@@ -105,6 +81,7 @@ void timer_start_slow_adcs()
                // TODO: kick the watchdog here
        }
 }
+#endif
 
 /*
  * Single synchronous ADC conversion.
@@ -128,9 +105,13 @@ static uint16_t read_adc_sync()
 
 void init_adc()
 {
-       unsigned char i;
-       current_slow_adc = NUM_ADCS;
+       need_battery_adc = 0;
+       need_pwmled_adc = 0;
        current_adc = 0;
+       adc_enabled = 1;
+
+       power_adc_enable();
+       ACSR |= _BV(ACD);       // but disable the analog comparator
 
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
@@ -152,34 +133,22 @@ void init_adc()
        start_next_adc();
 }
 
-#if 0
 void susp_adc()
 {
        ADCSRA = 0;
        DIDR0 = 0;
+       power_adc_disable();
+       adc_enabled = 0;
 }
 
-static void adc1_gain20_adc(uint16_t adcsum)
-{
-       // running average
-       adc1_gain20_offset += adcsum
-                       - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
-}
-#endif
-
 ISR(ADC_vect) { // IRQ handler
        uint16_t adcval = ADCW;
 
-       if (zero_count) {
-               if (zero_count > 1) {
-                       ADCSRA |= _BV(ADSC);
-                       zero_count--;
-                       return;
-               } else {
-                       setup_mux(current_adc);
-                       zero_count = 0;
-                       /* fall through */
-               }
+       if (read_zero) {
+               setup_mux(current_adc);
+               read_zero = 0;
+               ADCSRA |= _BV(ADSC); // drop this one, start the next
+               return;
        }
 
        if (drop_count) {
@@ -189,7 +158,7 @@ ISR(ADC_vect) { // IRQ handler
        }
 
        if (read_count) {
-               ADCSRA |= _BV(ADSC);
+               ADCSRA |= _BV(ADSC); // immediately start the next conversion
                adc_sum += adcval;
                read_count--;
                return;
@@ -202,8 +171,10 @@ ISR(ADC_vect) { // IRQ handler
        switch (current_adc) {
        case 0:
                // pwmled_adc(current_adc, adc_sum);
-               log_word(0x9000+adc_sum);
-               return;
+               pwmled_adc(adc_sum);
+               break;
+       case 1:
+               battery_adc(adc_sum);
                break;
        }