]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - avr/main.c
Command 'go <startaddr>' works now
[z180-stamp.git] / avr / main.c
index 79ca3e02e16ed7cedce8fa9d664fb3cdf10fa818..5c4cd0c0bca48ebdd29bf0242c67e715df061f5e 100644 (file)
 
 /*--------------------------------------------------------------------------*/
 
+static uint8_t mcusr;
+
+static
 void setup_avr(void)
 {
+       /* save and clear reset reason(s) */
+       mcusr = MCUSR;
+       MCUSR = 0;
+       
        /* WD */
 
        /* CPU */
@@ -59,6 +66,30 @@ void setup_avr(void)
        TIMSK1 = _BV(OCIE1A); // Enable TC1.oca interrupt
 }
 
+static const FLASH char * const FLASH rreasons[] = {
+                       FSTR("Power on"),
+                       FSTR("External"),
+                       FSTR("Brown out"),
+                       FSTR("Watchdog"),
+                       FSTR("JTAG"),
+               };
+
+static
+void print_reset_reason(void)
+{
+       uint8_t r = mcusr & 0x1f;
+       const FLASH char * const FLASH *p = rreasons;
+
+       printf_P(PSTR("Reset reason(s): "));
+       for ( ; r; p++, r >>= 1) {
+               if (r & 1) {
+                       my_puts_P(*p);
+                       if (r & ~1)
+                               printf_P(PSTR(", "));
+               }
+       }
+       printf_P(PSTR(".\n"));
+}
 
 
 /*******************************************************************************/
@@ -120,6 +151,7 @@ static int abortboot(int  bootdelay)
        return abort;
 }
 
+static
 const char *bootdelay_process(void)
 {
        char *s;
@@ -137,6 +169,7 @@ const char *bootdelay_process(void)
        return s;
 }
 
+static
 void autoboot_command(const char *s)
 {
        debug("### main_loop: bootcmd=\"%s\"\n", s ? s : PSTR("<UNDEFINED>"));
@@ -148,6 +181,7 @@ void autoboot_command(const char *s)
 }
 
 
+static
 void main_loop(void)
 {
        const char *s;
@@ -166,6 +200,9 @@ int main(void)
        sei();
 
        debug("\n=========================<  (RE)START DEBUG  >=========================\n");
+#if DEBUG
+       print_reset_reason();
+#endif
                
        env_init();