]> www.fi.muni.cz Git - heater.git/commitdiff
firmware: button handling, pwr down
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 17:25:40 +0000 (18:25 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Thu, 30 Jan 2014 17:25:40 +0000 (18:25 +0100)
firmware/logging.c
firmware/main.c

index 82c98774881a00183b17cb13d5e2e91bcf6cffac..b59f6975655493f67cd1e7110f63820ac0074949 100644 (file)
@@ -33,6 +33,9 @@ void log_init()
                r_count++;
                eeprom_write_byte(&reboot_count,
                        (r_count << 4) | (MCUSR & 0xF));
                r_count++;
                eeprom_write_byte(&reboot_count,
                        (r_count << 4) | (MCUSR & 0xF));
+
+               MCUSR = 0;
+
                log_init_common();
        }
 }
                log_init_common();
        }
 }
index 78777866d1f88354bccdc2c2e68f21a1cf2132a2..9a041500be8aefd19088472b2aa1ae2d8e50ca9d 100644 (file)
+#include <avr/interrupt.h>
 #include <avr/io.h>
 #include <avr/io.h>
+#include <avr/power.h>
+#include <avr/sleep.h>
+#include <avr/wdt.h>
 #include <util/delay.h>
 
 #include "logging.h"
 
 #include <util/delay.h>
 
 #include "logging.h"
 
-int main()
+static unsigned char pwm = 1;
+
+static void timer_init()
 {
 {
-       log_init();
+       power_timer1_enable();
+
+       DDRB |= _BV(PB4);
 
 
-       DDRB |= _BV(PB2) | _BV(PB4);
        TCCR1 = _BV(CS10); // clk/1 = 1 MHz
        // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = 255;
        TCCR1 = _BV(CS10); // clk/1 = 1 MHz
        // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = 255;
-       OCR1B = 9;
+       OCR1B = pwm;
+}
+
+static void adc_init()
+{
+       power_adc_enable();
 
        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);
 
        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);
+}
+
+static void status_led_init()
+{
+       DDRB |= _BV(PB2);
+       PORTB &= ~_BV(PB2);
+}
+
+static void status_led_on()
+{
+       PORTB |= _BV(PB2);
+}
+
+static void status_led_off()
+{
+       PORTB &= ~_BV(PB2);
+}
+
+static void buttons_init()
+{
+       DDRB &= ~(_BV(PB0) | _BV(PB1)); // set as input
+       PORTB |= _BV(PB0) | _BV(PB1);   // internal pull-up
+
+        GIMSK &= ~_BV(PCIE); // disable pin-change IRQs
+        PCMSK = 0; // disable pin-change IRQs on all pins of port B
+}
+
+static void buttons_susp()
+{
+       buttons_init();
+
+       GIMSK |= _BV(PCIE);
+       PCMSK |= _BV(PCINT0) | _BV(PCINT1);
+}
+
+static unsigned char buttons_pressed()
+{
+       return (
+               (PINB & _BV(PB0) ? 0 : 1)
+               |
+               (PINB & _BV(PB1) ? 0 : 2)
+       );
+}
+
+#define WAKEUP_POLL 100        // msec
+#define WAKEUP_LIMIT 5 // times WAKEUP_POLL
+
+static unsigned char buttons_wait_for_release()
+{
+       uint16_t wake_count = 0;
+
+       do {
+               if (++wake_count > WAKEUP_LIMIT)
+                       status_led_on(); // inform the user
+
+               _delay_ms(WAKEUP_POLL);
+       } while (buttons_pressed());
+
+       status_led_off();
+
+       return wake_count > WAKEUP_LIMIT;
+}
+
+ISR(PCINT0_vect)
+{
+        // empty - let it wake us from sleep, but do nothing else
+}
+
+static void wdt_init()
+{
+       WDTCR = _BV(WDIE) | _BV(WDP1); // interrupt mode, 64 ms
+}
+
+static void wdt_susp()
+{
+       wdt_disable();
+}
+
+static void hw_setup()
+{
+       power_all_disable();
+
+       timer_init();
+       adc_init();
+       status_led_init();
+       wdt_init();
+}
+
+static void hw_suspend()
+{
+       ADCSRA &= ~_BV(ADEN); // disable ADC
+       TCCR1 = 0; // disable T/C 1
+
+       status_led_init();
+       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();
+
+       do {
+               // G'night
+               set_sleep_mode(SLEEP_MODE_PWR_DOWN);
+               sleep_enable();
+               sleep_bod_disable();
+               sei();
+               sleep_cpu();
+
+               // G'morning
+               cli();
+               sleep_disable();
+
+               // allow wakeup by long button-press only
+       } while (!buttons_wait_for_release());
+
+       // OK, wake up now
+       hw_setup();
+}
+
+static void button_one_pressed()
+{
+       if (pwm > 1) {
+               pwm >>= 1;
+               OCR1B = pwm;
+       } else {
+               power_down();
+       }
+}
+
+static void button_two_pressed()
+{
+       if (pwm < 0x80) {
+               pwm <<= 1;
+               OCR1B = pwm;
+       }
+}
+
+static unsigned char button_state, button_state_time;
+
+static void timer_check_buttons()
+{
+       unsigned char newstate = buttons_pressed();
+
+       if (newstate == button_state) {
+               if (newstate && button_state_time < 4)
+                       ++button_state_time;
+               return;
+       }
+
+       if (newstate) {
+               button_state = newstate;
+               button_state_time = 0;
+               return;
+       }
+
+       // just released
+       switch (button_state) {
+       case 1: button_one_pressed();
+               break;
+       case 2: button_two_pressed();
+               break;
+       default: // ignore when both are preseed
+               break;
+       }
+
+       button_state = newstate;
+}
+
+int main()
+{
+       log_init();
+
+       power_down();
+
+#if 0
        ADCSRA |= _BV(ADSC);
        while (!(ADCSRA & _BV(ADIF)))
                ;
        ADCSRA |= _BV(ADSC);
        while (!(ADCSRA & _BV(ADIF)))
                ;
@@ -26,11 +223,22 @@ int main()
                ;
        log_word(ADCW);
        log_flush();
                ;
        log_word(ADCW);
        log_flush();
+#endif
+       sei();
 
 
+       // we try to be completely IRQ-driven, so just wait for IRQs here
        while(1) {
        while(1) {
-               PORTB |= _BV(PB2);
-               _delay_ms(500);
-               PORTB &= ~_BV(PB2);
-               _delay_ms(1000);
+               cli();
+               set_sleep_mode(SLEEP_MODE_IDLE);
+               sleep_enable();
+               // keep BOD active, no sleep_bod_disable();
+               sei();
+               sleep_cpu();
+               sleep_disable();
+
+               if (wdt_timer_fired) {
+                       wdt_timer_fired = 0;
+                       timer_check_buttons();
+               }
        }
 }
        }
 }