]> www.fi.muni.cz Git - heater.git/blob - firmware/main.c
Code cleanup and overview
[heater.git] / firmware / main.c
1 /*
2  * OVERVIEW
3  *
4  * Powering up:
5  * Immediately after reset, we power down the entire system.
6  * We wake up only after the button is pressed for a sufficiently long time.
7  *
8  * Heater output:
9  * The heater output is driven by Timer/Counter 1 in PWM mode.
10  * We want to be able to measure the battery voltage both when the
11  * output is on, and when the output is off. So we set the T/C1 clock
12  * prescaler so that the T/C1 is slow enough, we enable the T/C1 interrupts
13  * both on compare match and on overflow. After the interrupt, we trigger
14  * the battery voltage measurement with ADC.
15  *
16  * ADC:
17  * To avoid transients, we measure each battery state (when the heater is on
18  * and when it is off) separately, and we drop the first few readings.
19  * We calculate a running average of the readings to achieve higher accuracy.
20  *
21  * Buttons:
22  * There are two buttons (+ and -). Any button can wake the system up from
23  * the power-down state.
24  * TODO: When the system is woken up by the "-" button,
25  * it starts with the minimum output power, when it is woken up by the "+"
26  * button, it start with the maximum output power.
27  * When running, the "-" button is used for decreasing the output power,
28  * the "+" button is for increasing it.
29  * When on the lowest power state, the "-" button switches the system off.
30  * TODO: Long "-" button press switches the system off, long "+" button
31  * press sets the output power to maximum.
32  *
33  * Status LED:
34  * When powering up by a button press, the LED goes on to provide a visual
35  * feedback, and is switched off after the button is released.
36  * TODO: After a button press, the # of blinks of the LED reflects the
37  * chosen output power level for some time. Afterwards, it displays
38  * the battery level.
39  * TODO: When the battery is completely exhausted, the output power is switched
40  * off, the LED keeps blinking for some time, and then the whole system is
41  * switched off to avoid deep discharge of the battery.
42  *
43  * Timing:
44  * The firmware is timed by the Watchdog Timer interrupt. Most of the
45  * processing is done from the main loop, IRQs only set various flags
46  * or trigger other events.
47  */
48
49 #include <avr/interrupt.h>
50 #include <avr/io.h>
51 #include <avr/power.h>
52 #include <avr/sleep.h>
53 #include <avr/wdt.h>
54 #include <util/delay.h>
55
56 #include "logging.h"
57
58 /* waking up from the power down state by a button press */
59 #define WAKEUP_POLL 50  // msec
60 #define WAKEUP_LIMIT 5  // times WAKEUP_POLL
61
62 /* output power levels */
63 #define N_STEPS 5
64 static unsigned char steps[] = { 60, 85, 121, 171, 242 };
65 static unsigned char intensity = 0; // selected power level
66
67 /* which state (output on or output off) are we measuring now */
68 static volatile unsigned char adc_type, adc_drop;
69 static volatile uint16_t batt_on, batt_off; // measured voltage
70
71 /* timing by WDT */
72 static volatile unsigned char jiffies, next_clock_tick;
73
74 /* ========= Analog to Digital Converter (battery voltage) ========== */
75 static void adc_init()
76 {
77         power_adc_enable();
78
79         ADCSRA = _BV(ADEN)                      // enable
80                 | _BV(ADPS1) | _BV(ADPS0)       // clk/8 = 125 kHz
81                 | _BV(ADIE);                    // enable IRQ
82         ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
83                 // 1.1V reference, PB3 pin, single-ended
84         DIDR0 |= _BV(ADC3D);    // PB3 pin as analog input
85 }
86
87 static void adc_susp()
88 {
89         ADCSRA &= ~_BV(ADEN);   // disable ADC
90         DIDR0 &= ~_BV(ADC3D);   // disable analog input on PB3
91
92         power_adc_disable();
93 }
94
95 static void adc_start_measurement()
96 {
97         ADCSRA |= _BV(ADSC);
98 }
99
100 ISR(ADC_vect)
101 {
102         uint16_t adcw = ADCW;
103
104         if (adc_drop) {
105                 adc_drop--;
106                 ADCSRA |= _BV(ADSC);
107                 return;
108         }
109
110         // TODO: We may want to disable ADC after here to save power,
111         // but compared to the heater power it would be negligible,
112         // so don't bother with it.
113         if (adc_type == 0) {
114                 if (batt_off) {
115                         batt_off += adcw - (batt_off >> 5);
116                 } else {
117                         batt_off = adcw << 5;
118                 }
119         } else {
120                 if (batt_on) {
121                         batt_on += adcw - (batt_on >> 5);
122                 } else {
123                         batt_on = adcw << 5;
124                 }
125         }
126 }
127
128 /* ===================== Timer/Counter1 for PWM ===================== */
129 static void pwm_init()
130 {
131         power_timer1_enable();
132
133         DDRB |= _BV(PB4);
134
135         // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
136         TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
137         GTCCR = _BV(COM1B1) | _BV(PWM1B);
138         OCR1C = 255;
139         OCR1B = steps[0];
140         TIMSK = _BV(OCIE1B) | _BV(TOIE1);
141 }
142
143 static void pwm_susp()
144 {
145         TCCR1 = 0;
146 }
147
148 ISR(TIM1_OVF_vect)
149 {
150         adc_drop = 2;
151         adc_type = 1;
152         adc_start_measurement();
153 }
154
155 ISR(TIM1_COMPB_vect)
156 {
157         adc_drop = 2;
158         adc_type = 0;
159         adc_start_measurement();
160 }
161
162 static void pwm_set(unsigned char pwm)
163 {
164         OCR1B = pwm;
165 }
166
167 /* ===================== Status LED on pin PB2 ======================= */
168 static void status_led_init()
169 {
170         DDRB |= _BV(PB2);
171         PORTB &= ~_BV(PB2);
172 }
173
174 static void status_led_on()
175 {
176         PORTB |= _BV(PB2);
177 }
178
179 static void status_led_off()
180 {
181         PORTB &= ~_BV(PB2);
182 }
183
184 static unsigned char status_led_is_on()
185 {
186         return PORTB & _BV(PB2) ? 1 : 0;
187 }
188
189 /* ================== Buttons on pin PB0 and PB1 ===================== */
190 static void buttons_init()
191 {
192         DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input
193         PORTB |= _BV(PB0) | _BV(PB1);   // internal pull-up
194
195         GIMSK &= ~_BV(PCIE); // disable pin-change IRQs
196         PCMSK = 0; // disable pin-change IRQs on all pins of port B
197 }
198
199 static void buttons_susp()
200 {
201         buttons_init();
202
203         GIMSK |= _BV(PCIE);
204         PCMSK |= _BV(PCINT0) | _BV(PCINT1);
205 }
206
207 static unsigned char buttons_pressed()
208 {
209         return (
210                 (PINB & _BV(PB0) ? 0 : 1)
211                 |
212                 (PINB & _BV(PB1) ? 0 : 2)
213         );
214 }
215
216 static unsigned char buttons_wait_for_release()
217 {
218         uint16_t wake_count = 0;
219
220         do {
221                 if (++wake_count > WAKEUP_LIMIT)
222                         status_led_on(); // inform the user
223
224                 _delay_ms(WAKEUP_POLL);
225         } while (buttons_pressed());
226
227         status_led_off();
228
229         return wake_count > WAKEUP_LIMIT;
230 }
231
232 ISR(PCINT0_vect)
233 {
234         // empty - let it wake us from sleep, but do nothing else
235 }
236
237 /* ==== Watchdog Timer for timing blinks and other periodic tasks ==== */
238 static void wdt_init()
239 {
240         next_clock_tick = 0;
241         jiffies = 0;
242         WDTCR = _BV(WDIE) | _BV(WDP1); // interrupt mode, 64 ms
243 }
244
245 static void wdt_susp()
246 {
247         wdt_disable();
248 }
249
250 ISR(WDT_vect) {
251         next_clock_tick = 1;
252         jiffies++;
253 }
254
255 /* ====== Hardware init, teardown, powering down and waking up ====== */
256 static void hw_setup()
257 {
258         power_all_disable();
259
260         pwm_init();
261         adc_init();
262         status_led_init();
263         wdt_init();
264 }
265
266 static void hw_suspend()
267 {
268         adc_susp();
269         pwm_susp();
270         status_led_init(); // we don't have a separate _susp() here
271         buttons_susp();
272         wdt_susp();
273
274         power_all_disable();
275 }
276
277 static void power_down()
278 {
279         hw_suspend();
280
281         do {
282                 // G'night
283                 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
284                 sleep_enable();
285                 sleep_bod_disable();
286                 sei();
287                 sleep_cpu();
288
289                 // G'morning
290                 cli();
291                 sleep_disable();
292
293                 // allow wakeup by long button-press only
294         } while (!buttons_wait_for_release());
295
296         // OK, wake up now
297         hw_setup();
298 }
299
300 /* ======== Button press detection and  handling ===================== */
301 static void button_one_pressed()
302 {
303         if (intensity > 0) {
304                 pwm_set(steps[--intensity]);
305         } else {
306                 power_down();
307         }
308 }
309
310 static void button_two_pressed()
311 {
312         if (intensity < N_STEPS-1) {
313                 pwm_set(steps[++intensity]);
314         }
315 }
316
317 static unsigned char button_state, button_state_time;
318
319 static void timer_check_buttons()
320 {
321         unsigned char newstate = buttons_pressed();
322
323         if (newstate == button_state) {
324                 if (newstate && button_state_time < 4)
325                         ++button_state_time;
326                 return;
327         }
328
329         if (newstate) {
330                 button_state = newstate;
331                 button_state_time = 0;
332                 return;
333         }
334
335         // just released
336         switch (button_state) {
337         case 1: button_one_pressed();
338                 break;
339         case 2: button_two_pressed();
340                 break;
341         default: // ignore when both are preseed
342                 break;
343         }
344
345         button_state = newstate;
346 }
347
348 /* ============ Status LED blinking =================================== */
349 static unsigned char blink_on_time, blink_off_time, n_blinks;
350 static unsigned char blink_counter;
351
352 static void status_led_next_pattern()
353 {
354         // for now, display the selected intensity
355         n_blinks = intensity + 1;
356         blink_on_time = 0;
357         blink_off_time = 2;
358         blink_counter = 10;
359 }
360
361 static void timer_blink()
362 {
363         if (blink_counter) {
364                 blink_counter--;
365         } else if (status_led_is_on()) {
366                 status_led_off();
367                 blink_counter = blink_off_time;
368         } else if (n_blinks) {
369                 --n_blinks;
370                 status_led_on();
371                 blink_counter = blink_on_time;
372         } else {
373                 status_led_next_pattern();
374         }
375 }
376
377 int main()
378 {
379         log_init();
380
381         power_down();
382
383         sei();
384
385         // we try to be completely IRQ-driven, so just wait for IRQs here
386         while(1) {
387                 cli();
388                 set_sleep_mode(SLEEP_MODE_IDLE);
389                 sleep_enable();
390                 // keep BOD active, no sleep_bod_disable();
391                 sei();
392                 sleep_cpu();
393                 sleep_disable();
394
395                 // FIXME: Maybe handle new ADC readings as well?
396                 if (next_clock_tick) {
397                         next_clock_tick = 0;
398                         timer_check_buttons();
399                         timer_blink();
400                         log_flush();
401                 }
402         }
403 }