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.
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.
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.
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 * Long "-" button press switches the system off, long "+" button
31 * press sets the output power to maximum.
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
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.
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.
49 #include <avr/interrupt.h>
51 #include <avr/power.h>
52 #include <avr/sleep.h>
54 #include <util/delay.h>
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
62 /* output power levels */
63 #define N_POWER_LEVELS 5
64 static unsigned char power_levels[N_POWER_LEVELS] = {
68 static unsigned char power_level = 0; // selected power level
70 /* which state (output on or output off) are we measuring now */
71 static volatile unsigned char adc_type, adc_drop;
72 #define ADC_RUNAVG_SHIFT 5 // running average shift on batt_on, batt_off
73 static volatile uint16_t batt_on, batt_off; // measured voltage
76 * The voltage divider has 1M5 and 300K resistors (i.e. it measures 1/6th of
77 * the real voltage), ADC uses 1.1V internal reference.
78 * Macro to calculate upper eight bits of the ADC running-averaged value
79 * from the voltage in milivolts.
81 #define ADC_1100MV_VALUE 1071 // measured, not exactly 1100
82 #define MV_TO_ADC8(mV) ((unsigned char)(((uint32_t)(1UL << ADC_RUNAVG_SHIFT) \
84 / (6UL * ADC_1100MV_VALUE)) >> 8))
85 #define BATT_N_LEVELS 3
86 static unsigned char batt_levels[BATT_N_LEVELS] = {
93 static volatile unsigned char jiffies, next_clock_tick;
95 /* button press duration (in jiffies) */
96 #define BUTTON_SHORT_MIN 1
97 #define BUTTON_LONG_MIN 10
99 /* ========= Analog to Digital Converter (battery voltage) ========== */
100 static void adc_init()
104 ADCSRA = _BV(ADEN) // enable
105 | _BV(ADPS1) | _BV(ADPS0) // clk/8 = 125 kHz
106 | _BV(ADIE); // enable IRQ
107 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
108 // 1.1V reference, PB3 pin, single-ended
109 DIDR0 |= _BV(ADC3D); // PB3 pin as analog input
112 static void adc_susp()
114 ADCSRA &= ~_BV(ADEN); // disable ADC
115 DIDR0 &= ~_BV(ADC3D); // disable analog input on PB3
120 static void adc_start_measurement()
127 uint16_t adcw = ADCW;
135 // TODO: We may want to disable ADC after here to save power,
136 // but compared to the heater power it would be negligible,
137 // so don't bother with it.
140 batt_off += adcw - (batt_off >> ADC_RUNAVG_SHIFT);
142 batt_off = adcw << ADC_RUNAVG_SHIFT;
146 batt_on += adcw - (batt_on >> ADC_RUNAVG_SHIFT);
148 batt_on = adcw << ADC_RUNAVG_SHIFT;
153 /* ===================== Timer/Counter1 for PWM ===================== */
154 static void pwm_init()
156 power_timer1_enable();
160 // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
161 TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
162 GTCCR = _BV(COM1B1) | _BV(PWM1B);
166 TIMSK = _BV(OCIE1B) | _BV(TOIE1);
169 static void pwm_susp()
178 adc_start_measurement();
185 adc_start_measurement();
188 static void pwm_set(unsigned char pwm)
193 /* ===================== Status LED on pin PB2 ======================= */
194 static void status_led_init()
200 static void status_led_on()
205 static void status_led_off()
210 static unsigned char status_led_is_on()
212 return PORTB & _BV(PB2) ? 1 : 0;
215 /* ================== Buttons on pin PB0 and PB1 ===================== */
216 static void buttons_init()
218 DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input
219 PORTB |= _BV(PB0) | _BV(PB1); // internal pull-up
221 GIMSK &= ~_BV(PCIE); // disable pin-change IRQs
222 PCMSK = 0; // disable pin-change IRQs on all pins of port B
225 static void buttons_susp()
230 PCMSK |= _BV(PCINT0) | _BV(PCINT1);
233 static unsigned char buttons_pressed()
236 (PINB & _BV(PB0) ? 0 : 1)
238 (PINB & _BV(PB1) ? 0 : 2)
242 static unsigned char buttons_wait_for_release()
244 uint16_t wake_count = 0;
247 if (++wake_count > WAKEUP_LIMIT)
248 status_led_on(); // inform the user
250 _delay_ms(WAKEUP_POLL);
251 } while (buttons_pressed());
255 return wake_count > WAKEUP_LIMIT;
260 // empty - let it wake us from sleep, but do nothing else
263 /* ==== Watchdog Timer for timing blinks and other periodic tasks ==== */
264 static void wdt_init()
268 WDTCR = _BV(WDIE) | _BV(WDP1); // interrupt mode, 64 ms
271 static void wdt_susp()
281 /* ====== Hardware init, teardown, powering down and waking up ====== */
282 static void hw_setup()
292 static void hw_suspend()
296 status_led_init(); // we don't have a separate _susp() here
303 static void power_down()
309 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
319 // allow wakeup by long button-press only
320 } while (!buttons_wait_for_release());
326 /* ======== Button press detection and handling ===================== */
327 static void button_pressed(unsigned char button, unsigned char long_press)
329 // ignore simlultaneous button 1 and 2 press
333 } else if (button == 2) {
334 power_level = N_POWER_LEVELS-1;
336 } else { // short press
338 if (power_level > 0) {
343 } else if (button == 2) {
344 if (power_level < N_POWER_LEVELS-1) {
351 static unsigned char button_state, button_state_time;
353 static void timer_check_buttons()
355 unsigned char newstate = buttons_pressed();
357 if (newstate == button_state) {
358 if (newstate && button_state_time < BUTTON_LONG_MIN)
361 if (newstate && button_state_time >= BUTTON_LONG_MIN) {
368 button_state = newstate;
369 button_state_time = 0;
374 if (button_state_time >= BUTTON_SHORT_MIN)
375 button_pressed(button_state,
376 button_state_time >= BUTTON_LONG_MIN ? 1 : 0);
378 button_state = newstate;
379 button_state_time = 0;
382 /* ============ Status LED blinking =================================== */
383 static unsigned char blink_on_time, blink_off_time, n_blinks;
384 static unsigned char blink_counter;
386 static unsigned char battery_level()
388 unsigned char i, adc8;
390 // NOTE: we use 8-bit value only, so we don't need lock to protect
391 // us against concurrently running ADC IRQ handler:
392 adc8 = batt_off >> 8;
394 for (i = 0; i < BATT_N_LEVELS; i++)
395 if (batt_levels[i] > adc8)
401 static void status_led_next_pattern()
404 // for now, display the selected intensity
405 n_blinks = power_level + 1;
406 // n_blinks = battery_level() + 1;
412 static void timer_blink()
416 } else if (status_led_is_on()) {
418 blink_counter = blink_off_time;
419 } else if (n_blinks) {
422 blink_counter = blink_on_time;
424 status_led_next_pattern();
433 log_word(batt_levels[0]);
434 log_word(batt_levels[1]);
435 log_word(batt_levels[2]);
443 // we try to be completely IRQ-driven, so just wait for IRQs here
446 set_sleep_mode(SLEEP_MODE_IDLE);
448 // keep BOD active, no sleep_bod_disable();
453 // FIXME: Maybe handle new ADC readings as well?
454 if (next_clock_tick) {
457 // this has to be after the timer_blink() call
458 // to override the status LED during long button press
459 timer_check_buttons();
461 if ((jiffies & 0x0F) == 0) {
464 for (i = 0; i < BATT_N_LEVELS; i++)
465 if (batt_levels[i] > batt_off)
471 log_byte(batt_off >> 8);
472 log_byte(batt_on >> 8);