BUGfix: Abort timeout when bootLoaderConditionSimple()
[pub/USBaspLoader.git] / firmware / main.c
index fe7d049..9558089 100644 (file)
@@ -112,8 +112,26 @@ typedef union longConverter{
 }longConverter_t;
 
 
-#if BOOTLOADER_CAN_EXIT
-static volatile unsigned char  stayinloader = 0xfe;
+#if (BOOTLOADER_CAN_EXIT)
+#      if (BOOTLOADER_LOOPCYCLES_TIMEOUT)
+#              if (BOOTLOADER_LOOPCYCLES_TIMEOUT < 256)
+#                      if ((HAVE_UNPRECISEWAIT))
+volatile register uint8_t timeout_remaining __asm__("r2");
+#                      else
+static volatile uint8_t timeout_remaining;
+#                      endif
+#              else
+static volatile uint16_t timeout_remaining;
+#              endif
+#      endif
+
+#      define stayinloader_initialValue 0xfe
+#      if ((HAVE_UNPRECISEWAIT))
+/* here we have to assume we need to optimize for every byte */
+volatile register uint8_t stayinloader __asm__("r17");
+#      else
+static volatile uint8_t stayinloader;
+#      endif
 #endif
 
 static longConverter_t         currentAddress; /* in bytes */
@@ -407,6 +425,23 @@ defined (__AVR_ATmega2561__)
 #   endif
       }
 #endif
+#if ((HAVE_BOOTLOADER_HIDDENEXITCOMMAND) && (BOOTLOADER_CAN_EXIT))
+#      if ((HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xac) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x20) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x28) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x40) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x48) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x4c) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xa0) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xc0) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x58) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x5c) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x30) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0xac) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x50) && (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x58) && \
+           (HAVE_BOOTLOADER_HIDDENEXITCOMMAND != 0x38))
+  }else if(rq->wValue.bytes[0] == (HAVE_BOOTLOADER_HIDDENEXITCOMMAND)){  /* cause a bootLoaderExit at disconnect */
+      stayinloader = 0xf1;  /* we need to be connected - so assume it */
+#      endif
+#endif
   }else{
       /* ignore all others, return default value == 0 */
   }
@@ -447,7 +482,12 @@ static uchar    replyBuffer[4];
     }else if(rq->bRequest == USBASP_FUNC_DISCONNECT){
 
 #if BOOTLOADER_CAN_EXIT
+#      ifdef CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT
+      /* let the main loop know for ever that here was activity */
+      stayinloader        &= (0xfc);
+#      else
       stayinloader        &= (0xfe);
+#      endif
 #endif
     }else{
         /* ignore: others, but could be USBASP_FUNC_CONNECT */
@@ -686,6 +726,10 @@ static void initForUsbConnectivity(void)
 
 int __attribute__((__noreturn__)) main(void)
 {
+#if ((BOOTLOADER_LOOPCYCLES_TIMEOUT) && (BOOTLOADER_CAN_EXIT))
+    uint16_t __loopscycles;
+    timeout_remaining = BOOTLOADER_LOOPCYCLES_TIMEOUT;
+#endif
     /* initialize  */
     bootLoaderInit();
     odDebugInit();
@@ -695,6 +739,31 @@ int __attribute__((__noreturn__)) main(void)
     GICR = (1 << IVSEL); /* move interrupts to boot flash section */
 #endif
     if(bootLoaderCondition()){
+#if (BOOTLOADER_CAN_EXIT)
+#      if (USE_EXCESSIVE_ASSEMBLER)
+asm  volatile  (
+  "ldi         %[sil],         %[normval]\n\t"
+#              if ((defined(CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT)) && (!(BOOTLOADER_IGNOREPROGBUTTON)) && (BOOTLOADER_LOOPCYCLES_TIMEOUT))
+  "sbis                %[pin],         %[bit]\n\t"
+  "subi                %[sil],         0x02\n\t"
+#              endif
+  : [sil]        "=d" (stayinloader)
+  : [normval]     "M" (stayinloader_initialValue)
+#              if (!(BOOTLOADER_IGNOREPROGBUTTON))
+                                                    ,
+    [pin]         "I" (_SFR_IO_ADDR(PIN_PIN(JUMPER_PORT))),
+    [bit]         "I" (PIN(JUMPER_PORT, JUMPER_BIT))
+#              endif    
+);
+#      else
+#              if ((defined(CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT)) && (!(BOOTLOADER_IGNOREPROGBUTTON)) && (BOOTLOADER_LOOPCYCLES_TIMEOUT))
+      if (bootLoaderConditionSimple()) {
+       stayinloader = stayinloader_initialValue - 0x02;
+      } else
+#              endif
+             stayinloader = stayinloader_initialValue;
+#      endif
+#endif
 #if NEED_WATCHDOG
 #      if (defined(MCUSR) && defined(WDRF))
        /* 
@@ -710,8 +779,39 @@ int __attribute__((__noreturn__)) main(void)
        MCUCSR = 0;       /* clear all reset flags for next time */
         initForUsbConnectivity();
         do{
+#if ((BOOTLOADER_LOOPCYCLES_TIMEOUT) && (BOOTLOADER_CAN_EXIT))
+#      ifdef CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT
+       if (stayinloader != 0x0e) {
+         /* can be reached, since high-nibble is decreased every cycle... */
+#else
+       if (stayinloader & 0x01) {
+#endif
+         timeout_remaining = BOOTLOADER_LOOPCYCLES_TIMEOUT;
+       } else {
+         __loopscycles++;
+         if (!(__loopscycles)) {
+           if(timeout_remaining)       timeout_remaining--;
+           else                        stayinloader&=0xf1;
+         }
+       }
+#endif
             usbPoll();
 #if BOOTLOADER_CAN_EXIT
+#if BOOTLOADER_IGNOREPROGBUTTON
+  /* 
+   * remove the high nibble as it would be subtracted due to:
+   * "(!bootLoaderConditionSimple())"
+   */ 
+#if USE_EXCESSIVE_ASSEMBLER
+asm  volatile  (
+  "andi                %[sil],         0x0f\n\t"
+  : [sil]        "+d" (stayinloader)
+  :
+);
+#else
+  stayinloader &= 0x0f;
+#endif
+#else
 #if USE_EXCESSIVE_ASSEMBLER
 asm  volatile  (
   "cpi         %[sil],         0x10\n\t"
@@ -743,6 +843,7 @@ asm  volatile  (
        }
 #endif
 #endif
+#endif
 
 #if BOOTLOADER_CAN_EXIT
         }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/