X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=firmware%2Fadc.c;h=0ec32af77c0a54db8bbc4b0f8a6ac8476086e15f;hb=5d0f6ceb47eed2282aeb11eb57eebc390add234e;hp=fa0782c4248c6ee67917901cc1340dedfefea087;hpb=295d6d70b233e295c5080c998b9f54306c1170cf;p=bike-lights.git diff --git a/firmware/adc.c b/firmware/adc.c index fa0782c..0ec32af 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -21,19 +21,19 @@ static void inline setup_mux(unsigned char n) 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); - sum_shift = 0; // 1 measurement + sum_shift = 3; // 3 measurements break; case 4: // batt voltage: 1.1V, ADC6 (PA7), single-ended ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX1); @@ -51,15 +51,35 @@ 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() @@ -79,27 +99,20 @@ void init_adc() // 1.1V, ADC1,1, gain 20 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0); - ADCSRA |= _BV(ADSC); /* 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++) { - ADCSRA |= _BV(ADSC); - - while ((ADCSRA & _BV(ADIF)) == 0) - ; - adc1_gain20_offset += ADCW + adc1_gain20_offset += read_adc_sync() - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT); - - ADCSRA |= _BV(ADIF); // clear the IRQ flag } ADCSRA |= _BV(ADIE); // enable IRQ + + start_next_adc(); } void susp_adc() @@ -144,20 +157,10 @@ ISR(ADC_vect) { // IRQ handler if (current_adc < N_PWMLEDS) pwmled_adc(current_adc, adc_sum); if (current_adc == AMBIENT_ADC) - ambient_adc(adcval); + ambient_adc(adc_sum); if (current_adc == BATTERY_ADC) battery_adc(adcval); start_next_adc(); } -void timer_start_adcs() -{ - if (current_adc == NUM_ADCS) // Don't start if in progress - start_next_adc(); -#if 0 - else - log_byte(0x99); -#endif -} -