]> www.fi.muni.cz Git - tinyboard.git/blobdiff - projects/step-up/main.c
Imported firmware from Project Bike Lights
[tinyboard.git] / projects / step-up / main.c
diff --git a/projects/step-up/main.c b/projects/step-up/main.c
new file mode 100644 (file)
index 0000000..90bea63
--- /dev/null
@@ -0,0 +1,92 @@
+#include <avr/io.h>
+#include <util/delay.h>
+#include <avr/sleep.h>
+#include <avr/interrupt.h>
+#include <avr/power.h>
+#include <avr/wdt.h>
+
+#include "lights.h"
+
+static void hw_setup()
+{
+       wdt_enable(WDTO_1S);
+
+       init_battery();
+       init_pwm();
+       init_adc();
+       init_tmr();
+       init_buttons();
+
+       init_pwmled();
+       init_gpio();
+       init_ambient();
+       init_pattern();
+       init_control();
+
+       set_sleep_mode(SLEEP_MODE_IDLE);
+}
+
+static void hw_suspend()
+{
+       susp_pwm();
+       susp_adc();
+       susp_tmr();
+       susp_gpio();
+       susp_buttons();
+
+       wdt_disable();
+}
+
+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, so I will wake up
+       hw_setup();
+}
+
+int main(void)
+{
+       init_log();
+
+       power_usi_disable(); // Once for lifetime
+       ACSRA |= _BV(ACD);   // disable analog comparator
+
+       log_set_state(3);
+
+       hw_setup();
+       power_down();
+
+       sei();
+#if 1
+       while (1) {
+               wdt_reset();
+               sleep_mode();
+       }
+#endif
+
+#if 0
+       DDRB |= _BV(PB2);
+       while (1) {
+               PORTB |=  _BV( PB2 );
+               _delay_ms(200);
+               PORTB &=~ _BV( PB2 );
+               _delay_ms(200);
+       }
+#endif
+}