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