2 #include <avr/interrupt.h>
6 #define AMBIENT_ADC N_PWMLEDS
7 #define BATTERY_ADC (N_PWMLEDS + 1)
8 #define ADC1_GAIN20 (N_PWMLEDS + 2)
9 #define BUTTON_ADC (N_PWMLEDS + 3)
10 #define ZERO_ADC (N_PWMLEDS + 4)
12 #define NUM_ADCS ZERO_ADC
15 unsigned char read_zero_log : 2;
16 unsigned char read_drop_log : 2;
17 unsigned char read_keep_log : 4;
18 } adc_params[NUM_ADCS] = {
19 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 1
20 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 2
21 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 3
22 { 0, 1, AMBIENT_ADC_SHIFT }, // ambient
23 { 0, 1, 0 }, // battery
24 { 0, 1, 0 }, // gain20
25 { 0, 1, 0 }, // buttons
28 volatile static unsigned char current_adc;
29 static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log;
30 #define ADC1_GAIN20_OFFSET_SHIFT 6
31 static uint16_t adc1_gain20_offset;
34 static void setup_mux(unsigned char n)
36 /* ADC numbering: PWM LEDs first, then others, zero at the end */
38 case 0: // pwmled 1: 1.1V, ADC0,1 (PA0,1), gain 20
39 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX1) | _BV(MUX0);
41 case 1: // pwmled 2: 1.1V, ADC2,1 (PA2,1), gain 20
42 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
44 case 2: // pwmled 3: 1.1V, ADC4 (PA5), single-ended
45 ADMUX = _BV(REFS1) | _BV(MUX2);
47 case AMBIENT_ADC: // ambient light: 1.1V, ADC5 (PA6), single-ended
48 ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0);
50 case BATTERY_ADC: // batt voltage: 1.1V, ADC6 (PA7), single-ended
51 ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX1);
53 case ADC1_GAIN20: // gain stage offset: 1.1V, ADC1,1, gain 20
54 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
56 case BUTTON_ADC: // buttons: 1.1V, ADC3, single-ended
57 PORTA |= _BV(PA3); // +5V to the voltage splitter
58 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
60 case ZERO_ADC: // zero: 1.1V, ADC1 (PA1), single-ended
61 ADMUX = _BV(REFS1) | _BV(MUX0);
66 static void start_next_adc()
68 if (current_adc > 0) {
71 // TODO: kick the watchdog here.
72 current_adc = NUM_ADCS;
77 // we use the last iteration of zero_count to set up the MUX
78 // to its final destination, hence the "1 +" below:
79 if (adc_params[current_adc].read_zero_log)
80 zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
84 if (adc_params[current_adc].read_drop_log)
85 drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
89 read_count = 1 << adc_params[current_adc].read_keep_log;
90 n_reads_log = adc_params[current_adc].read_keep_log;
92 // set up mux, start one-shot conversion
96 setup_mux(current_adc);
101 void timer_start_adcs()
103 if (current_adc == NUM_ADCS) // Don't start if in progress
106 log_byte(0x99); // overrun
110 * Single synchronous ADC conversion.
111 * Has to be called with IRQs disabled (or with the ADC IRQ disabled).
113 static uint16_t read_adc_sync()
117 ADCSRA |= _BV(ADSC); // start the conversion
119 // wait for the conversion to finish
120 while((ADCSRA & _BV(ADIF)) == 0)
124 ADCSRA |= _BV(ADIF); // clear the IRQ flag
132 current_adc = NUM_ADCS;
134 ADCSRA = _BV(ADEN) // enable
135 | _BV(ADPS1) | _BV(ADPS0) // CLK/8 = 125 kHz
136 // | _BV(ADPS2) // CLK/16 = 62.5 kHz
138 // ADCSRB |= _BV(GSEL); // gain 8 or 32
140 // Disable digital input on all bits used by ADC
141 DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D) | _BV(ADC3D)
142 | _BV(ADC4D) | _BV(ADC5D) | _BV(ADC6D);
144 // 1.1V, ADC1,1, gain 20
145 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
147 /* Do first conversion and drop the result */
150 adc1_gain20_offset = 0;
152 for (i = 0; i < (1 << ADC1_GAIN20_OFFSET_SHIFT); i++) {
153 adc1_gain20_offset += read_adc_sync()
154 - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
157 ADCSRA |= _BV(ADIE); // enable IRQ
168 static void adc1_gain20_adc(uint16_t adcsum)
171 adc1_gain20_offset += adcsum
172 - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
175 ISR(ADC_vect) { // IRQ handler
176 uint16_t adcval = ADCW;
179 if (zero_count > 1) {
184 setup_mux(current_adc);
191 ADCSRA |= _BV(ADSC); // drop this one, start the next
204 * Now we have performed read_count measurements and have them
208 // For inputs with gain, subtract the measured gain stage offset
209 if (current_adc < 2) {
210 uint16_t offset = adc1_gain20_offset
211 >> (ADC1_GAIN20_OFFSET_SHIFT - n_reads_log);
213 if (adc_sum > offset)
219 switch (current_adc) {
223 pwmled_adc(current_adc, adc_sum);
226 ambient_adc(adc_sum);
229 battery_adc(adc_sum);
235 adc1_gain20_adc(adcval);