]> www.fi.muni.cz Git - bike-lights.git/blob - adc.c
a6547e30e81f74c64b82fd73b1c9fc39515fe324
[bike-lights.git] / adc.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3
4 #include "lights.h"
5
6 static unsigned char adc_mux[] = { // pwmleds should be first
7         // 0: pwmled 0: 1.1V, ADC3 (PA4), single-ended
8         _BV(REFS1) | _BV(MUX1) | _BV(MUX0),
9         // 1: pwmled 1: 1.1V, ADC0,1 (PA0,1), gain 1 or 8
10         _BV(REFS1) | _BV(MUX3) | _BV(MUX2),
11         // 2: pwmled 2: 1.1V, ADC2,1 (PA2,1), gain 1 or 8
12         _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0),
13         // 3: ambient light: 1.1V, ADC4 (PA5), single-ended
14         _BV(REFS1) | _BV(MUX2),
15         // 4: batt voltage: 1.1V, ADC5 (PA6), single-ended
16         _BV(REFS1) | _BV(MUX2) | _BV(MUX0),
17 };
18
19 #define AMBIENT_ADC N_PWMLEDS
20
21 #define LAST_ADC (sizeof(adc_mux)/sizeof(adc_mux[0]))
22 volatile static unsigned char current_adc;
23 static unsigned char adc_ignore;
24
25 static void start_next_adc()
26 {
27         while (current_adc > 0) {
28                 --current_adc;
29
30                 // test if current_adc should be measured
31                 if (current_adc < N_PWMLEDS && pwmled_needs_adc(current_adc))
32                         goto found;
33                 if (current_adc == AMBIENT_ADC)
34                         goto found;
35                 // TODO battery sense, etc.
36         }
37
38         // all ADCs have been handled
39         current_adc = LAST_ADC;
40         return;
41 found:
42         // ADCSRB |= _BV(GSEL); // gain 8 or 32
43         ADMUX = adc_mux[current_adc]; // set up mux, start one-shot conversion
44         adc_ignore = 1; // ignore first reading after mux change
45         ADCSRA |= _BV(ADSC);
46 }
47
48 void init_adc()
49 {
50         current_adc = LAST_ADC;
51         adc_ignore = 1;
52
53         ADCSRA = _BV(ADEN)                      // enable
54                 | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
55                 // | _BV(ADPS2)                 // CLK/16 = 62.5 kHz
56                 ;
57         ADCSRB |= _BV(GSEL); // gain 8 or 32
58
59         // Disable digital input on all bits used by ADC
60         DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D) | _BV(ADC3D)
61                 | _BV(ADC4D) | _BV(ADC5D);
62
63         ADCSRA |= _BV(ADSC);
64
65         /* Do first conversion and drop the result */
66         while ((ADCSRA & _BV(ADIF)) == 0)
67                 ;
68         ADCSRA |= _BV(ADIF); // clear the IRQ flag
69
70         ADCSRA |= _BV(ADIE); // enable IRQ
71 }
72
73 ISR(ADC_vect) { // IRQ handler
74         uint16_t adcval = ADCW;
75
76 #if 0
77         log_byte(0xF3);
78         log_byte(current_adc);
79         log_word(adcval);
80 #endif
81
82         if (adc_ignore) {
83                 ADCSRA |= _BV(ADSC);
84                 adc_ignore = 0;
85                 return;
86         }
87
88         if (current_adc < N_PWMLEDS)
89                 pwmled_adc(current_adc, adcval);
90         if (current_adc == AMBIENT_ADC)
91                 ambient_adc(adcval);
92         // TODO battery sense, etc.
93         
94         start_next_adc();
95 }
96
97 void timer_start_adcs()
98 {
99         if (current_adc == LAST_ADC) // Don't start if in progress
100                 start_next_adc();
101 }
102