#include <avr/io.h>
#include <avr/interrupt.h>
+#include <util/atomic.h>
#include "lights.h"
static uint16_t adc_sum;
static unsigned char sum_shift;
static unsigned char adc_vals;
-static uint16_t adc1_gain20_offset_x16;
+#define ADC1_GAIN20_OFFSET_SHIFT 6
+static uint16_t adc1_gain20_offset;
+static unsigned char handler_running;
static void inline setup_mux(unsigned char n)
{
- ADCSRA |= _BV(ADEN); // enable ADC
-
/* ADC numbering: PWM LEDs first, then ambient light sensor, battery sensor */
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 = 2; // 4 measurements
+ sum_shift = PWMLED_ADC_SHIFT;
break;
case 3: // ambient light: 1.1V, ADC5 (PA6), single-ended
ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0);
break;
case 5: // gain stage offset: 1.1V, ADC1,1, gain 20
ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
- sum_shift = 3; // 8 measurements
+ sum_shift = 0; // 1 measurement
break;
}
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()
{
unsigned char i;
current_adc = NUM_ADCS;
+ handler_running = 0;
ADCSRA = _BV(ADEN) // enable
| _BV(ADPS1) | _BV(ADPS0) // CLK/8 = 125 kHz
// 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
-
- adc1_gain20_offset_x16 = 0;
-
- for (i = 0; i < 16; i++) {
- ADCSRA |= _BV(ADSC);
+ read_adc_sync();
- while ((ADCSRA & _BV(ADIF)) == 0)
- ;
- adc1_gain20_offset_x16 += ADCW;
+ adc1_gain20_offset = 0;
- ADCSRA |= _BV(ADIF); // clear the IRQ flag
+ for (i = 0; i < (1 << ADC1_GAIN20_OFFSET_SHIFT); i++) {
+ adc1_gain20_offset += read_adc_sync()
+ - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
}
ADCSRA |= _BV(ADIE); // enable IRQ
- ADCSRA &= ~_BV(ADEN); // disable until needed
+ start_next_adc();
}
void susp_adc()
if (adc_vals)
// start the next conversion immediately
ADCSRA |= _BV(ADSC);
- else
- ADCSRA &= ~_BV(ADEN); // the last one, disable ADC
if (adc_vals < (1 << sum_shift))
// drop the first conversion, use all others
if (current_adc == ADC1_GAIN20) {
// running average
- adc1_gain20_offset_x16 += adcval
- - (adc1_gain20_offset_x16 >> 4);
+ adc1_gain20_offset += adcval
+ - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
} else if (current_adc == 0 || current_adc == 1) {
- uint16_t offset = adc1_gain20_offset_x16 >> 4;
- if (adcval >= offset)
- adcval -= offset;
+ uint16_t offset = adc1_gain20_offset
+ >> (ADC1_GAIN20_OFFSET_SHIFT - sum_shift);
+ if (adc_sum > offset)
+ adc_sum -= offset;
else
- adcval = 0;
+ adc_sum = 0;
}
- if (current_adc < N_PWMLEDS)
- pwmled_adc(current_adc, adcval);
- if (current_adc == AMBIENT_ADC)
- ambient_adc(adcval);
- if (current_adc == BATTERY_ADC)
- battery_adc(adcval);
-
- start_next_adc();
-}
+ if (handler_running & (1 << current_adc)) {
+ log_byte(0xB0 + current_adc);
+
+ // drop the result, what else to do?
-void timer_start_adcs()
-{
- if (current_adc == NUM_ADCS) // Don't start if in progress
start_next_adc();
- else
- log_byte(0x99);
+ } else {
+ unsigned char current_adc_copy = current_adc;
+ uint16_t adc_sum_copy = adc_sum;
+
+ start_next_adc();
+
+ handler_running |= (1 << current_adc_copy);
+ NONATOMIC_BLOCK(NONATOMIC_FORCEOFF) {
+ if (current_adc_copy < N_PWMLEDS)
+ pwmled_adc(current_adc_copy, adc_sum_copy);
+ if (current_adc_copy == AMBIENT_ADC)
+ ambient_adc(adc_sum_copy);
+ if (current_adc_copy == BATTERY_ADC)
+ battery_adc(adc_sum_copy);
+ }
+ handler_running &= ~(1 << current_adc_copy);
+ }
}