]> www.fi.muni.cz Git - bike-lights.git/commitdiff
adc: use only active pwmleds, on-demand switching of other channels
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 3 Jun 2013 15:52:14 +0000 (17:52 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 3 Jun 2013 15:52:14 +0000 (17:52 +0200)
firmware/adc.c
firmware/lights.h

index c6b113d4932cb538768fb8f94674d53af34a2483..1dbedfc2c2e36fbb4f19c369152efeb7e614d291 100644 (file)
@@ -6,8 +6,10 @@
 // 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
@@ -26,7 +28,9 @@ struct {
        { 0, 1, 0 },                    // gain20
 };
 
-volatile static unsigned char current_adc, current_slow_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;
@@ -66,25 +70,30 @@ static void setup_mux(unsigned char n)
 
 static void start_next_adc()
 {
-       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;
-       } else {
-               // next PWMLED
+       if (slow_adcs_wanted) {
+               current_adc = slow_adcs_wanted;
+               slow_adcs_wanted = 0;
+               goto found;
+       }
+
+       if (current_adc > N_PWMLEDS) {
                current_adc--;
+               goto found;
        }
 
-#if 0
-       log_byte(0x90 + current_adc); // debug ADC switching
-#endif
+       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
@@ -113,11 +122,14 @@ static void start_next_adc()
 
 void timer_start_slow_adcs()
 {
-       if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress
-               log_byte(0x80 + current_slow_adc);
+       if ((jiffies & 0x000F) == 0) {
+               slow_adcs_wanted = FIRST_1S_ADC;
        } else {
-               current_slow_adc = NUM_ADCS;
-               // TODO: kick the watchdog here
+               slow_adcs_wanted = FIRST_16HZ_ADC;
+       }
+
+       if (!adc_is_on) {
+               start_next_adc();
        }
 }
 
@@ -144,8 +156,8 @@ static uint16_t read_adc_sync()
 void init_adc()
 {
        unsigned char i;
-       current_slow_adc = NUM_ADCS;
        current_adc = 0;
+       adc_is_on = 1;
 
        ADCSRA = _BV(ADEN)                      // enable
                | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
index 72b719b321eaff5907a604ef2c0f2b7d528cf013..41ee4d6aebcdaa9804184101b8b0f6bbf6022de2 100644 (file)
@@ -29,6 +29,7 @@ void inline log_word(uint16_t word) { }
 void init_adc();
 void susp_adc();
 void timer_start_slow_adcs();
+extern volatile unsigned char adc_is_on;
 
 /* pwm.c */
 /*