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 static volatile uint16_t batt_on, batt_off; // measured voltage
72 static volatile unsigned char jiffies, next_clock_tick;
74 /* ========= Analog to Digital Converter (battery voltage) ========== */
75 static void adc_init()
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
87 static void adc_susp()
89 ADCSRA &= ~_BV(ADEN); // disable ADC
90 DIDR0 &= ~_BV(ADC3D); // disable analog input on PB3
95 static void adc_start_measurement()
102 uint16_t adcw = ADCW;
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.
115 batt_off += adcw - (batt_off >> 5);
117 batt_off = adcw << 5;
121 batt_on += adcw - (batt_on >> 5);
128 /* ===================== Timer/Counter1 for PWM ===================== */
129 static void pwm_init()
131 power_timer1_enable();
135 // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
136 TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
137 GTCCR = _BV(COM1B1) | _BV(PWM1B);
140 TIMSK = _BV(OCIE1B) | _BV(TOIE1);
143 static void pwm_susp()
152 adc_start_measurement();
159 adc_start_measurement();
162 static void pwm_set(unsigned char pwm)
167 /* ===================== Status LED on pin PB2 ======================= */
168 static void status_led_init()
174 static void status_led_on()
179 static void status_led_off()
184 static unsigned char status_led_is_on()
186 return PORTB & _BV(PB2) ? 1 : 0;
189 /* ================== Buttons on pin PB0 and PB1 ===================== */
190 static void buttons_init()
192 DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input
193 PORTB |= _BV(PB0) | _BV(PB1); // internal pull-up
195 GIMSK &= ~_BV(PCIE); // disable pin-change IRQs
196 PCMSK = 0; // disable pin-change IRQs on all pins of port B
199 static void buttons_susp()
204 PCMSK |= _BV(PCINT0) | _BV(PCINT1);
207 static unsigned char buttons_pressed()
210 (PINB & _BV(PB0) ? 0 : 1)
212 (PINB & _BV(PB1) ? 0 : 2)
216 static unsigned char buttons_wait_for_release()
218 uint16_t wake_count = 0;
221 if (++wake_count > WAKEUP_LIMIT)
222 status_led_on(); // inform the user
224 _delay_ms(WAKEUP_POLL);
225 } while (buttons_pressed());
229 return wake_count > WAKEUP_LIMIT;
234 // empty - let it wake us from sleep, but do nothing else
237 /* ==== Watchdog Timer for timing blinks and other periodic tasks ==== */
238 static void wdt_init()
242 WDTCR = _BV(WDIE) | _BV(WDP1); // interrupt mode, 64 ms
245 static void wdt_susp()
255 /* ====== Hardware init, teardown, powering down and waking up ====== */
256 static void hw_setup()
266 static void hw_suspend()
270 status_led_init(); // we don't have a separate _susp() here
277 static void power_down()
283 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
293 // allow wakeup by long button-press only
294 } while (!buttons_wait_for_release());
300 /* ======== Button press detection and handling ===================== */
301 static void button_one_pressed()
304 pwm_set(steps[--intensity]);
310 static void button_two_pressed()
312 if (intensity < N_STEPS-1) {
313 pwm_set(steps[++intensity]);
317 static unsigned char button_state, button_state_time;
319 static void timer_check_buttons()
321 unsigned char newstate = buttons_pressed();
323 if (newstate == button_state) {
324 if (newstate && button_state_time < 4)
330 button_state = newstate;
331 button_state_time = 0;
336 switch (button_state) {
337 case 1: button_one_pressed();
339 case 2: button_two_pressed();
341 default: // ignore when both are preseed
345 button_state = newstate;
348 /* ============ Status LED blinking =================================== */
349 static unsigned char blink_on_time, blink_off_time, n_blinks;
350 static unsigned char blink_counter;
352 static void status_led_next_pattern()
354 // for now, display the selected intensity
355 n_blinks = intensity + 1;
361 static void timer_blink()
365 } else if (status_led_is_on()) {
367 blink_counter = blink_off_time;
368 } else if (n_blinks) {
371 blink_counter = blink_on_time;
373 status_led_next_pattern();
385 // we try to be completely IRQ-driven, so just wait for IRQs here
388 set_sleep_mode(SLEEP_MODE_IDLE);
390 // keep BOD active, no sleep_bod_disable();
395 // FIXME: Maybe handle new ADC readings as well?
396 if (next_clock_tick) {
398 timer_check_buttons();