]> www.fi.muni.cz Git - heater.git/blobdiff - firmware/main.c
Code cleanup and overview
[heater.git] / firmware / main.c
index 9a041500be8aefd19088472b2aa1ae2d8e50ca9d..ddbd2d3cf54471fd8edf97dc0dbc87ae670f3dca 100644 (file)
@@ -1,3 +1,51 @@
+/*
+ * OVERVIEW
+ *
+ * Powering up:
+ * Immediately after reset, we power down the entire system.
+ * We wake up only after the button is pressed for a sufficiently long time.
+ *
+ * Heater output:
+ * The heater output is driven by Timer/Counter 1 in PWM mode.
+ * We want to be able to measure the battery voltage both when the
+ * output is on, and when the output is off. So we set the T/C1 clock
+ * prescaler so that the T/C1 is slow enough, we enable the T/C1 interrupts
+ * both on compare match and on overflow. After the interrupt, we trigger
+ * the battery voltage measurement with ADC.
+ *
+ * ADC:
+ * To avoid transients, we measure each battery state (when the heater is on
+ * and when it is off) separately, and we drop the first few readings.
+ * We calculate a running average of the readings to achieve higher accuracy.
+ *
+ * Buttons:
+ * There are two buttons (+ and -). Any button can wake the system up from
+ * the power-down state.
+ * TODO: When the system is woken up by the "-" button,
+ * it starts with the minimum output power, when it is woken up by the "+"
+ * button, it start with the maximum output power.
+ * When running, the "-" button is used for decreasing the output power,
+ * the "+" button is for increasing it.
+ * When on the lowest power state, the "-" button switches the system off.
+ * TODO: Long "-" button press switches the system off, long "+" button
+ * press sets the output power to maximum.
+ *
+ * Status LED:
+ * When powering up by a button press, the LED goes on to provide a visual
+ * feedback, and is switched off after the button is released.
+ * TODO: After a button press, the # of blinks of the LED reflects the
+ * chosen output power level for some time. Afterwards, it displays
+ * the battery level.
+ * TODO: When the battery is completely exhausted, the output power is switched
+ * off, the LED keeps blinking for some time, and then the whole system is
+ * switched off to avoid deep discharge of the battery.
+ *
+ * Timing:
+ * The firmware is timed by the Watchdog Timer interrupt. Most of the
+ * processing is done from the main loop, IRQs only set various flags
+ * or trigger other events.
+ */
+
 #include <avr/interrupt.h>
 #include <avr/io.h>
 #include <avr/power.h>
 #include <avr/interrupt.h>
 #include <avr/io.h>
 #include <avr/power.h>
 
 #include "logging.h"
 
 
 #include "logging.h"
 
-static unsigned char pwm = 1;
+/* waking up from the power down state by a button press */
+#define WAKEUP_POLL 50 // msec
+#define WAKEUP_LIMIT 5 // times WAKEUP_POLL
+
+/* output power levels */
+#define N_STEPS 5
+static unsigned char steps[] = { 60, 85, 121, 171, 242 };
+static unsigned char intensity = 0; // selected power level
+
+/* which state (output on or output off) are we measuring now */
+static volatile unsigned char adc_type, adc_drop;
+static volatile uint16_t batt_on, batt_off; // measured voltage
+
+/* timing by WDT */
+static volatile unsigned char jiffies, next_clock_tick;
+
+/* ========= Analog to Digital Converter (battery voltage) ========== */
+static void adc_init()
+{
+       power_adc_enable();
+
+       ADCSRA = _BV(ADEN)                      // enable
+               | _BV(ADPS1) | _BV(ADPS0)       // clk/8 = 125 kHz
+               | _BV(ADIE);                    // enable IRQ
+       ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
+               // 1.1V reference, PB3 pin, single-ended
+       DIDR0 |= _BV(ADC3D);    // PB3 pin as analog input
+}
+
+static void adc_susp()
+{
+       ADCSRA &= ~_BV(ADEN);   // disable ADC
+       DIDR0 &= ~_BV(ADC3D);   // disable analog input on PB3
+
+       power_adc_disable();
+}
+
+static void adc_start_measurement()
+{
+       ADCSRA |= _BV(ADSC);
+}
+
+ISR(ADC_vect)
+{
+       uint16_t adcw = ADCW;
 
 
-static void timer_init()
+       if (adc_drop) {
+               adc_drop--;
+               ADCSRA |= _BV(ADSC);
+               return;
+       }
+
+       // TODO: We may want to disable ADC after here to save power,
+       // but compared to the heater power it would be negligible,
+       // so don't bother with it.
+       if (adc_type == 0) {
+               if (batt_off) {
+                       batt_off += adcw - (batt_off >> 5);
+               } else {
+                       batt_off = adcw << 5;
+               }
+       } else {
+               if (batt_on) {
+                       batt_on += adcw - (batt_on >> 5);
+               } else {
+                       batt_on = adcw << 5;
+               }
+       }
+}
+
+/* ===================== Timer/Counter1 for PWM ===================== */
+static void pwm_init()
 {
        power_timer1_enable();
 
        DDRB |= _BV(PB4);
 
 {
        power_timer1_enable();
 
        DDRB |= _BV(PB4);
 
-       TCCR1 = _BV(CS10); // clk/1 = 1 MHz
-       // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
+       // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
+       TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = 255;
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = 255;
-       OCR1B = pwm;
+       OCR1B = steps[0];
+       TIMSK = _BV(OCIE1B) | _BV(TOIE1);
 }
 
 }
 
-static void adc_init()
+static void pwm_susp()
 {
 {
-       power_adc_enable();
+       TCCR1 = 0;
+}
 
 
-       ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0); // clk/8 = 125 kHz
-       ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); // 1.1V ref., PB3 single-ended
-       DIDR0 = _BV(ADC3D);
+ISR(TIM1_OVF_vect)
+{
+       adc_drop = 2;
+       adc_type = 1;
+       adc_start_measurement();
 }
 
 }
 
+ISR(TIM1_COMPB_vect)
+{
+       adc_drop = 2;
+       adc_type = 0;
+       adc_start_measurement();
+}
+
+static void pwm_set(unsigned char pwm)
+{
+       OCR1B = pwm;
+}
+
+/* ===================== Status LED on pin PB2 ======================= */
 static void status_led_init()
 {
        DDRB |= _BV(PB2);
 static void status_led_init()
 {
        DDRB |= _BV(PB2);
@@ -47,6 +181,12 @@ static void status_led_off()
        PORTB &= ~_BV(PB2);
 }
 
        PORTB &= ~_BV(PB2);
 }
 
+static unsigned char status_led_is_on()
+{
+       return PORTB & _BV(PB2) ? 1 : 0;
+}
+
+/* ================== Buttons on pin PB0 and PB1 ===================== */
 static void buttons_init()
 {
        DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input
 static void buttons_init()
 {
        DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input
@@ -73,9 +213,6 @@ static unsigned char buttons_pressed()
        );
 }
 
        );
 }
 
-#define WAKEUP_POLL 100        // msec
-#define WAKEUP_LIMIT 5 // times WAKEUP_POLL
-
 static unsigned char buttons_wait_for_release()
 {
        uint16_t wake_count = 0;
 static unsigned char buttons_wait_for_release()
 {
        uint16_t wake_count = 0;
@@ -97,8 +234,11 @@ ISR(PCINT0_vect)
         // empty - let it wake us from sleep, but do nothing else
 }
 
         // empty - let it wake us from sleep, but do nothing else
 }
 
+/* ==== Watchdog Timer for timing blinks and other periodic tasks ==== */
 static void wdt_init()
 {
 static void wdt_init()
 {
+       next_clock_tick = 0;
+       jiffies = 0;
        WDTCR = _BV(WDIE) | _BV(WDP1); // interrupt mode, 64 ms
 }
 
        WDTCR = _BV(WDIE) | _BV(WDP1); // interrupt mode, 64 ms
 }
 
@@ -107,11 +247,17 @@ static void wdt_susp()
        wdt_disable();
 }
 
        wdt_disable();
 }
 
+ISR(WDT_vect) {
+       next_clock_tick = 1;
+       jiffies++;
+}
+
+/* ====== Hardware init, teardown, powering down and waking up ====== */
 static void hw_setup()
 {
        power_all_disable();
 
 static void hw_setup()
 {
        power_all_disable();
 
-       timer_init();
+       pwm_init();
        adc_init();
        status_led_init();
        wdt_init();
        adc_init();
        status_led_init();
        wdt_init();
@@ -119,22 +265,15 @@ static void hw_setup()
 
 static void hw_suspend()
 {
 
 static void hw_suspend()
 {
-       ADCSRA &= ~_BV(ADEN); // disable ADC
-       TCCR1 = 0; // disable T/C 1
-
-       status_led_init();
+       adc_susp();
+       pwm_susp();
+       status_led_init(); // we don't have a separate _susp() here
        buttons_susp();
        wdt_susp();
 
        power_all_disable();
 }
 
        buttons_susp();
        wdt_susp();
 
        power_all_disable();
 }
 
-static volatile unsigned char wdt_timer_fired;
-
-ISR(WDT_vect) {
-       wdt_timer_fired = 1;
-}
-
 static void power_down()
 {
        hw_suspend();
 static void power_down()
 {
        hw_suspend();
@@ -158,11 +297,11 @@ static void power_down()
        hw_setup();
 }
 
        hw_setup();
 }
 
+/* ======== Button press detection and  handling ===================== */
 static void button_one_pressed()
 {
 static void button_one_pressed()
 {
-       if (pwm > 1) {
-               pwm >>= 1;
-               OCR1B = pwm;
+       if (intensity > 0) {
+               pwm_set(steps[--intensity]);
        } else {
                power_down();
        }
        } else {
                power_down();
        }
@@ -170,9 +309,8 @@ static void button_one_pressed()
 
 static void button_two_pressed()
 {
 
 static void button_two_pressed()
 {
-       if (pwm < 0x80) {
-               pwm <<= 1;
-               OCR1B = pwm;
+       if (intensity < N_STEPS-1) {
+               pwm_set(steps[++intensity]);
        }
 }
 
        }
 }
 
@@ -207,23 +345,41 @@ static void timer_check_buttons()
        button_state = newstate;
 }
 
        button_state = newstate;
 }
 
+/* ============ Status LED blinking =================================== */
+static unsigned char blink_on_time, blink_off_time, n_blinks;
+static unsigned char blink_counter;
+
+static void status_led_next_pattern()
+{
+       // for now, display the selected intensity
+       n_blinks = intensity + 1;
+       blink_on_time = 0;
+       blink_off_time = 2;
+       blink_counter = 10;
+}
+
+static void timer_blink()
+{
+       if (blink_counter) {
+               blink_counter--;
+       } else if (status_led_is_on()) {
+               status_led_off();
+               blink_counter = blink_off_time;
+       } else if (n_blinks) {
+               --n_blinks;
+               status_led_on();
+               blink_counter = blink_on_time;
+       } else {
+               status_led_next_pattern();
+       }
+}
+
 int main()
 {
        log_init();
 
        power_down();
 
 int main()
 {
        log_init();
 
        power_down();
 
-#if 0
-       ADCSRA |= _BV(ADSC);
-       while (!(ADCSRA & _BV(ADIF)))
-               ;
-       log_word(ADCW);
-       ADCSRA |= _BV(ADSC);
-       while (!(ADCSRA & _BV(ADIF)))
-               ;
-       log_word(ADCW);
-       log_flush();
-#endif
        sei();
 
        // we try to be completely IRQ-driven, so just wait for IRQs here
        sei();
 
        // we try to be completely IRQ-driven, so just wait for IRQs here
@@ -236,9 +392,12 @@ int main()
                sleep_cpu();
                sleep_disable();
 
                sleep_cpu();
                sleep_disable();
 
-               if (wdt_timer_fired) {
-                       wdt_timer_fired = 0;
+               // FIXME: Maybe handle new ADC readings as well?
+               if (next_clock_tick) {
+                       next_clock_tick = 0;
                        timer_check_buttons();
                        timer_check_buttons();
+                       timer_blink();
+                       log_flush();
                }
        }
 }
                }
        }
 }