static inline void long_press(unsigned char button)
{
if (button == 0) {
- power_down();
+ power_down(0);
return;
}
/* logging.c */
#ifdef USE_LOGGING
-void init_log();
+void init_log(unsigned char mcusr);
void log_set_state(unsigned char val);
void log_flush();
void log_byte(unsigned char byte);
void log_word(uint16_t word);
#else
-void inline init_log() { }
+void inline init_log(unsigned char mcusr) { }
void inline log_set_state(unsigned char val) { }
void inline log_flush() { }
void inline log_byte(unsigned char byte) { }
pattern_t *laser_pattern_select();
/* main.c */
-void power_down();
+void power_down(unsigned char err);
#endif /* !LIGHTS_H__ */
eeprom_write_byte(&log_state, val);
}
-void init_log()
+void init_log(unsigned char mcusr)
{
unsigned char r_count;
if (r_count < 5) {
r_count++;
eeprom_write_byte(&reboot_count,
- (r_count << 4) | (MCUSR & 0xF));
- MCUSR = 0;
+ (r_count << 4) | (mcusr & 0xF));
can_write_eeprom = 1;
} else {
//eeprom_write_byte(&log_state, 0xFF);
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/power.h>
+#include <avr/wdt.h>
#include "lights.h"
susp_buttons();
}
-void power_down()
+void power_down(unsigned char err)
{
hw_suspend();
do {
+ if (err)
+ gpio_set(0, 1);
+
// G'night
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
int main(void)
{
- init_log();
+ unsigned char mcusr_save;
+
+ // disable the WDT if running
+ wdt_reset();
+ mcusr_save = MCUSR;
+ MCUSR = 0;
+ wdt_disable();
+
+ if (mcusr_save & _BV(WDRF)) // was watchdog reset?
+ gpio_set(0, 1);
+
+ init_log(mcusr_save);
power_usi_disable(); // Once for lifetime
ACSRA |= _BV(ACD); // disable analog comparator
log_set_state(3);
hw_setup();
- power_down();
+ power_down(mcusr_save & _BV(WDRF));
sei();
#if 1