]> www.fi.muni.cz Git - tinyboard.git/commitdiff
Merge branch 'master' of ssh://anxur.fi.muni.cz/~kas/html/git/tinyboard
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 1 May 2013 13:25:26 +0000 (15:25 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 1 May 2013 13:25:26 +0000 (15:25 +0200)
1  2 
projects/step-up/adc.c

diff --combined projects/step-up/adc.c
index daf7bfe4bdb305c9cb05ce65d036a346ccb03b1a,2bd4bfff81804ad3ad00d87fb790ea7c1f9da269..fc4c7caac3a201b3526c2f5a4908aabc24c5f67b
@@@ -1,36 -1,16 +1,17 @@@
  #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    1
  
  //#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;
+ static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log;
  
  static void setup_mux(unsigned char n)
  {
@@@ -73,23 -53,14 +54,14 @@@ void start_next_adc(
  #endif
  
        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;
+       read_zero = 0;
+       drop_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;
+       read_count = 1 << PWMLED_ADC_SHIFT;
+       n_reads_log = PWMLED_ADC_SHIFT;
  
        // set up mux, start one-shot conversion
-       if (zero_count > 1)
+       if (read_zero)
                setup_mux(ZERO_ADC);
        else
                setup_mux(current_adc);
@@@ -97,6 -68,7 +69,7 @@@
        ADCSRA |= _BV(ADSC);
  }
  
+ #if 0
  void timer_start_slow_adcs()
  {
        if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress
                // TODO: kick the watchdog here
        }
  }
+ #endif
  
  /*
   * Single synchronous ADC conversion.
@@@ -129,13 -102,9 +103,12 @@@ static uint16_t read_adc_sync(
  
  void init_adc()
  {
-       unsigned char i;
        current_slow_adc = NUM_ADCS;
        current_adc = 0;
  
 +      power_adc_enable();
 +      ACSR |= _BV(ACD);       // but disable the analog comparator
 +
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
                // | _BV(ADPS2)                 // CLK/16 = 62.5 kHz
@@@ -171,19 -140,26 +144,26 @@@ static void adc1_gain20_adc(uint16_t ad
  }
  #endif
  
+ static void inline adc_based_timer()
+ {
+       static uint16_t pattern_counter;
+       if (++pattern_counter > 250) {
+               pattern_counter = 0;
+               patterns_next_tick();
+       }
+ }
  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 */
-               }
+       adc_based_timer();
+       if (read_zero) {
+               setup_mux(current_adc);
+               read_zero = 0;
+               ADCSRA |= _BV(ADSC); // drop this one, start the next
+               return;
        }
  
        if (drop_count) {
        }
  
        if (read_count) {
-               ADCSRA |= _BV(ADSC);
+               ADCSRA |= _BV(ADSC); // immediately start the next conversion
                adc_sum += adcval;
                read_count--;
                return;
        switch (current_adc) {
        case 0:
                // pwmled_adc(current_adc, adc_sum);
-               pwmled_adc(1, adc_sum);
+               pwmled_adc(adc_sum);
                break;
        }