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 * TODO: 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 */
64 static unsigned char steps[] = { 60, 85, 121, 171, 242 };
65 static unsigned char intensity = 0; // selected power level
67 /* which state (output on or output off) are we measuring now */
68 static volatile unsigned char adc_type, adc_drop;
69 #define ADC_RUNAVG_SHIFT 5 // running average shift on batt_on, batt_off
70 static volatile uint16_t batt_on, batt_off; // measured voltage
73 * The voltage divider has 1M5 and 300K resistors (i.e. it measures 1/6th of
74 * the real voltage), ADC uses 1.1V internal reference.
75 * Macro to calculate upper eight bits of the ADC running-averaged value
76 * from the voltage in milivolts.
78 #define ADC_1100MV_VALUE 1071 // measured, not exactly 1100
79 #define MV_TO_ADC8(mV) ((unsigned char)(((uint32_t)(1UL << ADC_RUNAVG_SHIFT) \
81 / (6UL * ADC_1100MV_VALUE)) >> 8))
82 #define BATT_N_LEVELS 3
83 static unsigned char batt_levels[BATT_N_LEVELS] = {
90 static volatile unsigned char jiffies, next_clock_tick;
92 /* ========= Analog to Digital Converter (battery voltage) ========== */
93 static void adc_init()
97 ADCSRA = _BV(ADEN) // enable
98 | _BV(ADPS1) | _BV(ADPS0) // clk/8 = 125 kHz
99 | _BV(ADIE); // enable IRQ
100 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
101 // 1.1V reference, PB3 pin, single-ended
102 DIDR0 |= _BV(ADC3D); // PB3 pin as analog input
105 static void adc_susp()
107 ADCSRA &= ~_BV(ADEN); // disable ADC
108 DIDR0 &= ~_BV(ADC3D); // disable analog input on PB3
113 static void adc_start_measurement()
120 uint16_t adcw = ADCW;
128 // TODO: We may want to disable ADC after here to save power,
129 // but compared to the heater power it would be negligible,
130 // so don't bother with it.
133 batt_off += adcw - (batt_off >> ADC_RUNAVG_SHIFT);
135 batt_off = adcw << ADC_RUNAVG_SHIFT;
139 batt_on += adcw - (batt_on >> ADC_RUNAVG_SHIFT);
141 batt_on = adcw << ADC_RUNAVG_SHIFT;
146 /* ===================== Timer/Counter1 for PWM ===================== */
147 static void pwm_init()
149 power_timer1_enable();
153 // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
154 TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
155 GTCCR = _BV(COM1B1) | _BV(PWM1B);
159 TIMSK = _BV(OCIE1B) | _BV(TOIE1);
162 static void pwm_susp()
171 adc_start_measurement();
178 adc_start_measurement();
181 static void pwm_set(unsigned char pwm)
186 /* ===================== Status LED on pin PB2 ======================= */
187 static void status_led_init()
193 static void status_led_on()
198 static void status_led_off()
203 static unsigned char status_led_is_on()
205 return PORTB & _BV(PB2) ? 1 : 0;
208 /* ================== Buttons on pin PB0 and PB1 ===================== */
209 static void buttons_init()
211 DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input
212 PORTB |= _BV(PB0) | _BV(PB1); // internal pull-up
214 GIMSK &= ~_BV(PCIE); // disable pin-change IRQs
215 PCMSK = 0; // disable pin-change IRQs on all pins of port B
218 static void buttons_susp()
223 PCMSK |= _BV(PCINT0) | _BV(PCINT1);
226 static unsigned char buttons_pressed()
229 (PINB & _BV(PB0) ? 0 : 1)
231 (PINB & _BV(PB1) ? 0 : 2)
235 static unsigned char buttons_wait_for_release()
237 uint16_t wake_count = 0;
240 if (++wake_count > WAKEUP_LIMIT)
241 status_led_on(); // inform the user
243 _delay_ms(WAKEUP_POLL);
244 } while (buttons_pressed());
248 return wake_count > WAKEUP_LIMIT;
253 // empty - let it wake us from sleep, but do nothing else
256 /* ==== Watchdog Timer for timing blinks and other periodic tasks ==== */
257 static void wdt_init()
261 WDTCR = _BV(WDIE) | _BV(WDP1); // interrupt mode, 64 ms
264 static void wdt_susp()
274 /* ====== Hardware init, teardown, powering down and waking up ====== */
275 static void hw_setup()
285 static void hw_suspend()
289 status_led_init(); // we don't have a separate _susp() here
296 static void power_down()
302 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
312 // allow wakeup by long button-press only
313 } while (!buttons_wait_for_release());
319 /* ======== Button press detection and handling ===================== */
320 static void button_one_pressed()
323 pwm_set(steps[--intensity]);
329 static void button_two_pressed()
331 if (intensity < N_STEPS-1) {
332 pwm_set(steps[++intensity]);
336 static unsigned char button_state, button_state_time;
338 static void timer_check_buttons()
340 unsigned char newstate = buttons_pressed();
342 if (newstate == button_state) {
343 if (newstate && button_state_time < 4)
349 button_state = newstate;
350 button_state_time = 0;
355 switch (button_state) {
356 case 1: button_one_pressed();
358 case 2: button_two_pressed();
360 default: // ignore when both are preseed
364 button_state = newstate;
367 /* ============ Status LED blinking =================================== */
368 static unsigned char blink_on_time, blink_off_time, n_blinks;
369 static unsigned char blink_counter;
371 static unsigned char battery_level()
373 unsigned char i, adc8;
375 // NOTE: we use 8-bit value only, so we don't need lock to protect
376 // us against concurrently running ADC IRQ handler:
377 adc8 = batt_off >> 8;
379 for (i = 0; i < BATT_N_LEVELS; i++)
380 if (batt_levels[i] > adc8)
386 static void status_led_next_pattern()
389 // for now, display the selected intensity
390 // n_blinks = intensity + 1;
391 n_blinks = battery_level() + 1;
397 static void timer_blink()
401 } else if (status_led_is_on()) {
403 blink_counter = blink_off_time;
404 } else if (n_blinks) {
407 blink_counter = blink_on_time;
409 status_led_next_pattern();
418 log_word(batt_levels[0]);
419 log_word(batt_levels[1]);
420 log_word(batt_levels[2]);
428 // we try to be completely IRQ-driven, so just wait for IRQs here
431 set_sleep_mode(SLEEP_MODE_IDLE);
433 // keep BOD active, no sleep_bod_disable();
438 // FIXME: Maybe handle new ADC readings as well?
439 if (next_clock_tick) {
441 timer_check_buttons();
443 if ((jiffies & 0x0F) == 0) {
446 for (i = 0; i < BATT_N_LEVELS; i++)
447 if (batt_levels[i] > batt_off)
453 log_byte(batt_off >> 8);
454 log_byte(batt_on >> 8);