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