From: Jan "Yenya" Kasprzak Date: Tue, 2 Jul 2013 10:03:19 +0000 (+0200) Subject: adc.c: abort the current reading when the timer expires X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=commitdiff_plain;h=b34429defa04d493a49e24ad8840e42600bd0f28;hp=d52307e8b0b0b1113ba928e19ffa91340d037eda adc.c: abort the current reading when the timer expires Inside the timer interrupt, we may have changed the PWM outputs, so the reading of ADC will be useless. Drop it in this case. FIXME: possible race condition against timer IRQ. --- diff --git a/firmware/adc.c b/firmware/adc.c index 9ed80f2..5ebc0a6 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -204,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);