X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=heater.git;a=blobdiff_plain;f=firmware%2Fmain.c;h=fcb74261d6df2df28c1034c95da4a85053fb0eba;hp=30f904b5e463cd2c335557e9e17cb9b6867afac1;hb=4c4fe8554a4c770d68dba4b45df716c4d0308a2f;hpb=2755b4c6000c4de64d83921bdb109077f4b38597 diff --git a/firmware/main.c b/firmware/main.c index 30f904b..fcb7426 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -1,33 +1,316 @@ +#include #include -#include +#include +#include +#include #include -static uint16_t adcval EEMEM; +#include "logging.h" -int main() +#define N_STEPS 5 +static unsigned char steps[] = { 60, 85, 121, 171, 242 }; +static unsigned char intensity = 0; + +static void timer_init() { - DDRB |= _BV(PB2) | _BV(PB4); - TCCR1 = _BV(CS10); // clk/1 = 1 MHz - // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz + power_timer1_enable(); + + DDRB |= _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; - OCR1B = 9; + OCR1B = steps[0]; + TIMSK = _BV(OCIE1B) | _BV(TOIE1); +} - 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); +volatile unsigned char adc_type, adc_drop; + +ISR(TIM1_OVF_vect) +{ + adc_drop = 2; + adc_type = 1; ADCSRA |= _BV(ADSC); - while (!(ADCSRA & _BV(ADIF))) - ; +} + +ISR(TIM1_COMPB_vect) +{ + adc_drop = 2; + adc_type = 0; ADCSRA |= _BV(ADSC); - while (!(ADCSRA & _BV(ADIF))) - ; - eeprom_write_word(&adcval, ADCW); +} + +static void set_pwm(unsigned char pwm) +{ + OCR1B = pwm; +} + +static void adc_init() +{ + power_adc_enable(); + + ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0) | _BV(ADIE); // clk/8 = 125 kHz + ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); // 1.1V ref., PB3 single-ended + DIDR0 = _BV(ADC3D); +} + +volatile uint16_t batt_on, batt_off; + +ISR(ADC_vect) +{ + uint16_t adcw = ADCW; + + if (adc_drop) { + adc_drop--; + ADCSRA |= _BV(ADSC); + return; + } + + 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; + } + } +} +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 unsigned char status_led_is_on() +{ + return PORTB & _BV(PB2) ? 1 : 0; +} + +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 (intensity > 0) { + set_pwm(steps[--intensity]); + } else { + power_down(); + } +} + +static void button_two_pressed() +{ + if (intensity < N_STEPS-1) { + set_pwm(steps[++intensity]); + } +} + +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; +} + +static unsigned char blink_on_time, blink_off_time, n_blinks; +static unsigned char blink_counter; + +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 { + n_blinks = intensity + 1; + blink_on_time = 0; + blink_off_time = 2; + blink_counter = 10; +#if 0 + log_byte(0xbb); + log_byte(batt_on >> 7); + log_byte(batt_off >> 7); +#endif + } +} + +int main() +{ + log_init(); + + power_down(); + + sei(); + + // we try to be completely IRQ-driven, so just wait for IRQs here 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(); + timer_blink(); + log_flush(); + } } }