2 #include <avr/interrupt.h>
6 // pwmleds are measured continuously (when active)
7 #define AMBIENT_ADC N_PWMLEDS // measured every jiffy (16 Hz)
8 #define BUTTON_ADC (N_PWMLEDS + 1) // measured every jiffy (16 Hz)
9 #define FIRST_16HZ_ADC BUTTON_ADC
10 #define BATTERY_ADC (N_PWMLEDS + 2) // once per second
11 #define ADC1_GAIN20 (N_PWMLEDS + 3) // once per second
12 #define FIRST_1S_ADC ADC1_GAIN20
13 #define ZERO_ADC (N_PWMLEDS + 4) // must be last
15 #define NUM_ADCS ZERO_ADC
18 unsigned char read_zero_log : 2;
19 unsigned char read_drop_log : 2;
20 unsigned char read_keep_log : 4;
21 } adc_params[NUM_ADCS] = {
22 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 1
23 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 2
24 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 3
25 { 0, 1, AMBIENT_ADC_SHIFT }, // ambient
26 { 0, 1, 0 }, // buttons
27 { 0, 1, 0 }, // battery
28 { 0, 1, 0 }, // gain20
31 volatile unsigned char adc_is_on;
33 volatile static unsigned char current_adc, slow_adcs_wanted;
34 static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log;
35 #define ADC1_GAIN20_OFFSET_SHIFT 6
36 static uint16_t adc1_gain20_offset;
39 static void setup_mux(unsigned char n)
41 /* ADC numbering: PWM LEDs first, then others, zero at the end */
43 case 0: // pwmled 1: 1.1V, ADC0,1 (PA0,1), gain 20
44 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX1) | _BV(MUX0);
46 case 1: // pwmled 2: 1.1V, ADC2,1 (PA2,1), gain 20
47 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
49 case 2: // pwmled 3: 1.1V, ADC4 (PA5), single-ended
50 ADMUX = _BV(REFS1) | _BV(MUX2);
52 case AMBIENT_ADC: // ambient light: 1.1V, ADC5 (PA6), single-ended
53 ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX0);
55 case BUTTON_ADC: // buttons: 1.1V, ADC3, single-ended
56 PORTA |= _BV(PA3); // +5V to the voltage splitter
57 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
59 case BATTERY_ADC: // batt voltage: 1.1V, ADC6 (PA7), single-ended
60 ADMUX = _BV(REFS1) | _BV(MUX2) | _BV(MUX1);
62 case ADC1_GAIN20: // gain stage offset: 1.1V, ADC1,1, gain 20
63 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
65 case ZERO_ADC: // zero: 1.1V, ADC1 (PA1), single-ended
66 ADMUX = _BV(REFS1) | _BV(MUX0);
71 static void start_next_adc()
73 if (slow_adcs_wanted) {
74 current_adc = slow_adcs_wanted;
79 if (current_adc > N_PWMLEDS) {
84 if (!TIMER1_IS_ON()) {
91 current_adc = N_PWMLEDS;
93 } while (!PWM_IS_ON(current_adc));
99 // we use the last iteration of zero_count to set up the MUX
100 // to its final destination, hence the "1 +" below:
101 if (adc_params[current_adc].read_zero_log)
102 zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
106 if (adc_params[current_adc].read_drop_log)
107 drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
111 read_count = 1 << adc_params[current_adc].read_keep_log;
112 n_reads_log = adc_params[current_adc].read_keep_log;
114 // set up mux, start one-shot conversion
118 setup_mux(current_adc);
123 void timer_start_slow_adcs()
125 if ((jiffies & 0x000F) == 0) {
126 slow_adcs_wanted = FIRST_1S_ADC;
128 slow_adcs_wanted = FIRST_16HZ_ADC;
137 * Single synchronous ADC conversion.
138 * Has to be called with IRQs disabled (or with the ADC IRQ disabled).
140 static uint16_t read_adc_sync()
144 ADCSRA |= _BV(ADSC); // start the conversion
146 // wait for the conversion to finish
147 while((ADCSRA & _BV(ADIF)) == 0)
151 ADCSRA |= _BV(ADIF); // clear the IRQ flag
161 slow_adcs_wanted = FIRST_1S_ADC;
163 ADCSRA = _BV(ADEN) // enable
164 | _BV(ADPS1) | _BV(ADPS0) // CLK/8 = 125 kHz
165 // | _BV(ADPS2) // CLK/16 = 62.5 kHz
167 // ADCSRB |= _BV(GSEL); // gain 8 or 32
169 // Disable digital input on all bits used by ADC
170 DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D) | _BV(ADC3D)
171 | _BV(ADC4D) | _BV(ADC5D) | _BV(ADC6D);
173 // 1.1V, ADC1,1, gain 20
174 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
176 /* Do first conversion and drop the result */
179 adc1_gain20_offset = 0;
181 for (i = 0; i < (1 << ADC1_GAIN20_OFFSET_SHIFT); i++) {
182 adc1_gain20_offset += read_adc_sync()
183 - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
186 ADCSRA |= _BV(ADIE); // enable IRQ
197 static void adc1_gain20_adc(uint16_t adcsum)
200 adc1_gain20_offset += adcsum
201 - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
204 ISR(ADC_vect) { // IRQ handler
205 uint16_t adcval = ADCW;
208 * After the timer interrupt, drop the current reading.
209 * We may have changed the PWM outputs, so the value is
210 * probably useless anyway.
211 * FIXME: possible race condition - we should make an explicit
212 * notification inside the timer IRQ handler.
214 if (slow_adcs_wanted) {
220 if (zero_count > 1) {
225 setup_mux(current_adc);
232 ADCSRA |= _BV(ADSC); // drop this one, start the next
245 * Now we have performed read_count measurements and have them
249 // For inputs with gain, subtract the measured gain stage offset
250 if (current_adc < 2) {
251 uint16_t offset = adc1_gain20_offset
252 >> (ADC1_GAIN20_OFFSET_SHIFT - n_reads_log);
254 if (adc_sum > offset)
260 switch (current_adc) {
264 pwmled_adc(current_adc, adc_sum);
267 ambient_adc(adc_sum);
273 battery_adc(adc_sum);
276 adc1_gain20_adc(adcval);