X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=firmware%2Fmain.c;h=8166771478bd207e8cced92f6bf9f11eaef4a754;hb=fad6d0df251d09246816d5f6480543b8af588641;hp=25ea829511781e3e365c282a9bc0ae0e4357b8c3;hpb=b0e6a5b47836a04116631e3a5427b8a508fc5cbf;p=bike-lights.git diff --git a/firmware/main.c b/firmware/main.c index 25ea829..8166771 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -7,9 +7,10 @@ #include "lights.h" -void hw_setup() +static void hw_setup() { wdt_enable(WDTO_1S); + init_battery(); init_pwm(); init_adc(); @@ -24,75 +25,50 @@ void hw_setup() set_sleep_mode(SLEEP_MODE_IDLE); } -void hw_suspend() +static void hw_suspend() { susp_pwm(); susp_adc(); susp_tmr(); susp_gpio(); + susp_buttons(); + wdt_disable(); } - void power_down() { - uint16_t wake_count = 0; - unsigned char btn; - -sleep_again: - // enable PCINT14, so that user can wake up later - GIMSK |= _BV(PCIE1); - PCMSK1 |= _BV(PCINT14); - - // G'night - set_sleep_mode(SLEEP_MODE_PWR_DOWN); - sleep_enable(); - sleep_bod_disable(); - sei(); - sleep_cpu(); - - // G'morning - cli(); - - sleep_disable(); - - // Disable PCINT14 - GIMSK &= ~_BV(PCIE1); - PCMSK1 &= ~_BV(PCINT14); - - // allow wakeup by long button-press only - for (btn = 0; btn < 5; btn++) { - if ((PINB & _BV(PB6)) != 0) { - wake_count++; - goto sleep_again;; - } - _delay_ms(100); - } + hw_suspend(); - // ok, so I will wake up - log_byte(0xb1); - log_word(wake_count); - log_flush(); + do { + // G'night + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleep_enable(); + sleep_bod_disable(); + sei(); + sleep_cpu(); - hw_setup(); -} + // G'morning + cli(); + sleep_disable(); + // allow wakeup by long button-press only + } while (!buttons_wait_for_release()); -ISR(PCINT_vect) -{ - GIMSK &= ~_BV(PCIE1); - PCMSK1 &= ~_BV(PCINT14); + // ok, so I will wake up + hw_setup(); } int main(void) { log_init(); + power_usi_disable(); // Once for lifetime + ACSRA |= _BV(ACD); // disable analog comparator log_set_state(3); hw_setup(); - hw_suspend(); power_down(); sei();