]> www.fi.muni.cz Git - tinyboard.git/blob - projects/step-up/adc.c
Power management - make every module enable power for its own HW.
[tinyboard.git] / projects / step-up / adc.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <avr/power.h>
4 #include <avr/sleep.h>
5
6 #include "lights.h"
7
8 #define BATTERY_ADC (N_PWMLEDS + 0)
9 #define BUTTON_ADC  (N_PWMLEDS + 1)
10 #define ZERO_ADC    (N_PWMLEDS + 2)
11
12 //#define NUM_ADCS      ZERO_ADC
13 #define NUM_ADCS        1
14
15 struct {
16         unsigned char read_zero_log : 2;
17         unsigned char read_drop_log : 2;
18         unsigned char read_keep_log : 4;
19 } adc_params[NUM_ADCS] = {
20         { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 1
21 #if 0
22         { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 2
23         { 0, 1, PWMLED_ADC_SHIFT },     // pwmled 3
24         { 0, 1, AMBIENT_ADC_SHIFT },    // ambient
25         { 0, 1, 0 },                    // battery
26         { 0, 1, 0 },                    // gain20
27         { 0, 1, 0 },                    // buttons
28 #endif
29 };
30
31 volatile static unsigned char current_adc, current_slow_adc;
32 static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log;
33
34
35 static void setup_mux(unsigned char n)
36 {
37         /* ADC numbering: PWM LEDs first, then others, zero at the end */
38         switch (n) {
39         case 0: // pwmled 1: 1.1V, ADC3 (PB3), single-ended
40                 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
41                 break;
42         case ZERO_ADC: // zero: 1.1V, GND, single-ended
43                 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
44                 break;
45         }
46 }
47
48 void start_next_adc()
49 {
50 #if 0
51         if (current_adc == 0) {
52                 if (current_slow_adc > N_PWMLEDS) {
53                         // read one of the non-PWMLED ADCs
54                         current_adc = --current_slow_adc;
55                 } else {
56                         // no more non-PWMLEDs to do, start with PWMLEDs
57                         current_adc = N_PWMLEDS-1;
58                 }
59         } else if (current_adc >= N_PWMLEDS) {
60                 // one of the non-PWMLED ADCs just finished, skip to PWMLEDs.
61                 current_adc = N_PWMLEDS-1;
62         } else {
63                 // next PWMLED
64                 current_adc--;
65         }
66 #else
67         // single ADC for testing only
68         current_adc = 0;
69 #endif
70
71 #if 0
72         log_byte(0x90 + current_adc); // debug ADC switching
73 #endif
74
75         adc_sum = 0;
76         // we use the last iteration of zero_count to set up the MUX
77         // to its final destination, hence the "1 +" below:
78         if (adc_params[current_adc].read_zero_log)
79                 zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
80         else
81                 zero_count = 1;
82
83         if (adc_params[current_adc].read_drop_log)
84                 drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
85         else
86                 drop_count = 0;
87
88         read_count = 1 << adc_params[current_adc].read_keep_log;
89         n_reads_log = adc_params[current_adc].read_keep_log;
90
91         // set up mux, start one-shot conversion
92         if (zero_count > 1)
93                 setup_mux(ZERO_ADC);
94         else
95                 setup_mux(current_adc);
96
97         ADCSRA |= _BV(ADSC);
98 }
99
100 void timer_start_slow_adcs()
101 {
102         if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress
103                 log_byte(0x80 + current_slow_adc);
104         } else {
105                 current_slow_adc = NUM_ADCS;
106                 // TODO: kick the watchdog here
107         }
108 }
109
110 /*
111  * Single synchronous ADC conversion.
112  * Has to be called with IRQs disabled (or with the ADC IRQ disabled).
113  */
114 static uint16_t read_adc_sync()
115 {
116         uint16_t rv;
117
118         ADCSRA |= _BV(ADSC); // start the conversion
119
120         // wait for the conversion to finish
121         while((ADCSRA & _BV(ADIF)) == 0)
122                 ;
123
124         rv = ADCW;
125         ADCSRA |= _BV(ADIF); // clear the IRQ flag
126
127         return rv;
128 }
129
130 void init_adc()
131 {
132         unsigned char i;
133         current_slow_adc = NUM_ADCS;
134         current_adc = 0;
135
136         power_adc_enable();
137         ACSR |= _BV(ACD);       // but disable the analog comparator
138
139         ADCSRA = _BV(ADEN)                      // enable
140                 | _BV(ADPS1) | _BV(ADPS0)       // CLK/8 = 125 kHz
141                 // | _BV(ADPS2)                 // CLK/16 = 62.5 kHz
142                 ;
143         // ADCSRB |= _BV(GSEL); // gain 8 or 32
144
145         // Disable digital input on all bits used by ADC
146         DIDR0 = _BV(ADC3D) | _BV(ADC2D);
147         
148         // 1.1V, GND
149         ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
150
151         /* Do first conversion and drop the result */
152         read_adc_sync();
153
154         ADCSRA |= _BV(ADIE); // enable IRQ
155
156         start_next_adc();
157 }
158
159 #if 0
160 void susp_adc()
161 {
162         ADCSRA = 0;
163         DIDR0 = 0;
164 }
165
166 static void adc1_gain20_adc(uint16_t adcsum)
167 {
168         // running average
169         adc1_gain20_offset += adcsum
170                         - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
171 }
172 #endif
173
174 ISR(ADC_vect) { // IRQ handler
175         uint16_t adcval = ADCW;
176
177         if (zero_count) {
178                 if (zero_count > 1) {
179                         ADCSRA |= _BV(ADSC);
180                         zero_count--;
181                         return;
182                 } else {
183                         setup_mux(current_adc);
184                         zero_count = 0;
185                         /* fall through */
186                 }
187         }
188
189         if (drop_count) {
190                 ADCSRA |= _BV(ADSC); // drop this one, start the next
191                 drop_count--;
192                 return;
193         }
194
195         if (read_count) {
196                 ADCSRA |= _BV(ADSC);
197                 adc_sum += adcval;
198                 read_count--;
199                 return;
200         }
201
202         /*
203          * Now we have performed read_count measurements and have them
204          * in adc_sum.
205          */
206         switch (current_adc) {
207         case 0:
208                 // pwmled_adc(current_adc, adc_sum);
209                 pwmled_adc(1, adc_sum);
210                 break;
211         }
212
213         start_next_adc();
214 }