USBaspLoader v0.97 stable release
[pub/USBaspLoader.git] / firmware / main.c
index c450095..fccec56 100644 (file)
@@ -3,16 +3,16 @@
  * Author: Christian Starkjohann
  * Author: Stephan Baerwolf
  * Creation Date: 2007-12-08
  * Author: Christian Starkjohann
  * Author: Stephan Baerwolf
  * Creation Date: 2007-12-08
- * Modification Date: 2012-11-10
+ * Modification Date: 2013-03-31
  * Tabsize: 4
  * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
  * License: GNU GPL v2 (see License.txt)
  * Tabsize: 4
  * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
  * License: GNU GPL v2 (see License.txt)
- * This Revision: $Id: main.c 786 2010-05-30 20:41:40Z cs $
  */
 
 #include "spminterface.h"  /* must be included as first! */
 
  */
 
 #include "spminterface.h"  /* must be included as first! */
 
-#include <avr/io.h>
+#include "../misc/iofixes.h"
+
 #include <avr/interrupt.h>
 #include <avr/pgmspace.h>
 #include <avr/wdt.h>
 #include <avr/interrupt.h>
 #include <avr/pgmspace.h>
 #include <avr/wdt.h>
@@ -113,8 +113,26 @@ typedef union longConverter{
 }longConverter_t;
 
 
 }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 */
 #endif
 
 static longConverter_t         currentAddress; /* in bytes */
@@ -129,8 +147,12 @@ static const uchar         currentRequest = 0;
 static const uchar  signatureBytes[4] = {
 #ifdef SIGNATURE_BYTES
     SIGNATURE_BYTES
 static const uchar  signatureBytes[4] = {
 #ifdef SIGNATURE_BYTES
     SIGNATURE_BYTES
+#elif defined (__AVR_ATmega8535__)
+    0x1e, 0x93, 0x08, 0
 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
     0x1e, 0x93, 0x07, 0
 #elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
     0x1e, 0x93, 0x07, 0
+#elif defined (__AVR_ATmega16__)
+    0x1e, 0x94, 0x03, 0
 #elif defined (__AVR_ATmega32__)
     0x1e, 0x95, 0x02, 0
 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
 #elif defined (__AVR_ATmega32__)
     0x1e, 0x95, 0x02, 0
 #elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
@@ -143,9 +165,11 @@ static const uchar  signatureBytes[4] = {
     0x1e, 0x93, 0x0a, 0
 #elif defined (__AVR_ATmega88PA__)
     0x1e, 0x93, 0x0F, 0
     0x1e, 0x93, 0x0a, 0
 #elif defined (__AVR_ATmega88PA__)
     0x1e, 0x93, 0x0F, 0
+#elif defined (__AVR_ATmega162__)
+    0x1e, 0x94, 0x04, 0
 #elif defined (__AVR_ATmega164A__)
     0x1e, 0x94, 0x0f, 0
 #elif defined (__AVR_ATmega164A__)
     0x1e, 0x94, 0x0f, 0
-#elif defined (__AVR_ATmega164P__)
+#elif defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
     0x1e, 0x94, 0x0a, 0
 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
     0x1e, 0x94, 0x06, 0
     0x1e, 0x94, 0x0a, 0
 #elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
     0x1e, 0x94, 0x06, 0
@@ -161,42 +185,121 @@ static const uchar  signatureBytes[4] = {
     0x1e, 0x95, 0x14, 0
 #elif defined (__AVR_ATmega328P__)
     0x1e, 0x95, 0x0f, 0
     0x1e, 0x95, 0x14, 0
 #elif defined (__AVR_ATmega328P__)
     0x1e, 0x95, 0x0f, 0
+#elif defined (__AVR_ATmega640__)
+    0x1e, 0x96, 0x08, 0
 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
     0x1e, 0x96, 0x09, 0
 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
     0x1e, 0x96, 0x0a, 0
 #elif defined (__AVR_ATmega128__)
     0x1e, 0x97, 0x02, 0
 #elif defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__)
     0x1e, 0x96, 0x09, 0
 #elif defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
     0x1e, 0x96, 0x0a, 0
 #elif defined (__AVR_ATmega128__)
     0x1e, 0x97, 0x02, 0
+#elif defined (__AVR_ATmega1280__)
+    0x1e, 0x97, 0x03, 0
+#elif defined (__AVR_ATmega1281__)
+    0x1e, 0x97, 0x04, 0
 #elif defined (__AVR_ATmega1284__)
     0x1e, 0x97, 0x06, 0
 #elif defined (__AVR_ATmega1284P__)
     0x1e, 0x97, 0x05, 0
 #elif defined (__AVR_ATmega1284__)
     0x1e, 0x97, 0x06, 0
 #elif defined (__AVR_ATmega1284P__)
     0x1e, 0x97, 0x05, 0
+#elif defined (__AVR_ATmega2560__)
+    0x1e, 0x98, 0x01, 0
+#elif defined (__AVR_ATmega2561__)
+    0x1e, 0x98, 0x02, 0
 #else
 #else
-#   error "Device signature is not known, please edit main.c!"
+#   if (defined(SIGNATURE_0) && defined(SIGNATURE_1) && defined(SIGNATURE_2))
+#     warning "Device signature is not known - using AVR Libc suggestion..."
+    SIGNATURE_0, SIGNATURE_1, SIGNATURE_2, 0
+#   else
+#     error "Device signature is not known, please edit main.c!"
+#   endif
 #endif
 };
 
 /* ------------------------------------------------------------------------ */
 
 #endif
 };
 
 /* ------------------------------------------------------------------------ */
 
+#if (HAVE_BOOTLOADERENTRY_FROMSOFTWARE)
+void __attribute__ ((section(".init3"),naked,used,no_instrument_function)) __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND(void);
+void __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND(void) {
+  asm volatile (
+    "in                %[mcucsrval]    ,       %[mcucsrio]\n\t"
+    "ldi       r29             ,       %[ramendhi]\n\t"
+    "ldi       r28             ,       %[ramendlo]\n\t"
+#if (FLASHEND>131071)
+    "ld                %[result]       ,       Y+\n\t"
+    "cpi       %[result]       ,       %[bootaddrhi]\n\t"
+    "brne      __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_mismatch%=\n\t"
+#endif
+    "ld                %[result]       ,       Y+\n\t"
+    "cpi       %[result]       ,       %[bootaddrme]\n\t"
+    "ld                %[result]       ,       Y+\n\t"
+    "breq      __BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_done%=\n\t"
+
+    "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_mismatch%=:\n\t"
+    "ldi       %[result]       ,       0xff\n\t"
+
+    "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_investigate_RAMEND_done%=:\n\t"
+    : [result]         "=a" (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch),
+      [mcucsrval]      "=a" (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR)
+    : [mcucsrio]       "I"  (_SFR_IO_ADDR(MCUCSR)),
+#if (FLASHEND>131071)
+      [ramendhi]       "M" (((RAMEND - 2) >> 8) & 0xff),
+      [ramendlo]       "M" (((RAMEND - 2) >> 0) & 0xff),
+      [bootaddrhi]     "M" (((__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS) >>16) & 0xff),
+#else
+      [ramendhi]       "M" (((RAMEND - 1) >> 8) & 0xff),
+      [ramendlo]       "M" (((RAMEND - 1) >> 0) & 0xff),
+#endif
+      [bootaddrme]     "M" (((__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS) >> 8) & 0xff)
+    
+  );
+}
+#endif
+
+#if (USE_BOOTUP_CLEARRAM)
+/*
+* Under normal circumstances, RESET will not clear contents of RAM.
+* As always, if you want it done - do it yourself...
+*/
+void __attribute__ ((section(".init3"),naked,used,no_instrument_function)) __func_clearram(void);
+void __func_clearram(void) {
+  extern size_t __bss_end;
+  asm volatile (
+    "__clearram:\n\t"
+#if (!(HAVE_BOOTLOADERENTRY_FROMSOFTWARE))
+    "ldi r29, %[ramendhi]\n\t"
+    "ldi r28, %[ramendlo]\n\t"
+#endif
+    "__clearramloop%=:\n\t"
+    "st -Y , __zero_reg__\n\t"
+    "cp r28, %A[bssend]\n\t"
+    "cpc r29, %B[bssend]\n\t"
+    "brne __clearramloop%=\n\t"
+    :
+    : [ramendhi] "M" (((RAMEND+1)>>8) & 0xff),
+      [ramendlo] "M" (((RAMEND+1)>>0) & 0xff),
+      [bssend] "r" (&__bss_end)
+    : "memory"
+      );
+}
+#endif
+
 #if (!USE_EXCESSIVE_ASSEMBLER) || (!(defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)))
 static void (*nullVector)(void) __attribute__((__noreturn__));
 #endif
 
 #if (!USE_EXCESSIVE_ASSEMBLER) || (!(defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)))
 static void (*nullVector)(void) __attribute__((__noreturn__));
 #endif
 
-static void __attribute__((__noreturn__)) leaveBootloader()
-{
 #if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
 #if (USE_EXCESSIVE_ASSEMBLER) && (defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__))
-asm  volatile  (
+static void __attribute__((naked,__noreturn__)) leaveBootloader(void);
+static void leaveBootloader(void) {
+  asm  volatile  (
   "cli\n\t"
   "cli\n\t"
-  "clr         r30\n\t"
   "sbi         %[usbddr],      %[usbminus]\n\t"  
   "cbi         %[port],        %[bit]\n\t"
   "sbi         %[usbddr],      %[usbminus]\n\t"  
   "cbi         %[port],        %[bit]\n\t"
-  "out         %[usbintrenab], r30\n\t"
-  "out         %[usbintrcfg],  r30\n\t"
+  "out         %[usbintrenab], __zero_reg__\n\t"
+  "out         %[usbintrcfg],  __zero_reg__\n\t"
   "ldi         r31,            %[ivce]\n\t"
   "out         %[mygicr],      r31\n\t"
   "ldi         r31,            %[ivce]\n\t"
   "out         %[mygicr],      r31\n\t"
-  "out         %[mygicr],      r30\n\t"  
-  "clr         r31\n\t"
-  "icall\n\t"
+  "out         %[mygicr],      __zero_reg__\n\t"  
+  "rjmp                nullVector\n\t"
   :
   : [port]        "I" (_SFR_IO_ADDR(PIN_PORT(JUMPER_PORT))),
     [bit]         "I" (PIN(JUMPER_PORT, JUMPER_BIT)),
   :
   : [port]        "I" (_SFR_IO_ADDR(PIN_PORT(JUMPER_PORT))),
     [bit]         "I" (PIN(JUMPER_PORT, JUMPER_BIT)),
@@ -207,8 +310,10 @@ asm  volatile  (
     [mygicr]      "I" (_SFR_IO_ADDR(GICR)),          
     [ivce]        "I" (1<<IVCE)
 );
     [mygicr]      "I" (_SFR_IO_ADDR(GICR)),          
     [ivce]        "I" (1<<IVCE)
 );
-/* TODO: compiler will put an unnecessary "ret" at this end ! */
+}
 #else
 #else
+static void __attribute__((__noreturn__)) leaveBootloader(void);
+static void leaveBootloader(void) {
     DBG1(0x01, 0, 0);
     cli();
     usbDeviceDisconnect();
     DBG1(0x01, 0, 0);
     cli();
     usbDeviceDisconnect();
@@ -217,15 +322,24 @@ asm  volatile  (
     USB_INTR_CFG = 0;       /* also reset config bits */
     GICR = (1 << IVCE);     /* enable change of interrupt vectors */
     GICR = (0 << IVSEL);    /* move interrupts to application flash section */
     USB_INTR_CFG = 0;       /* also reset config bits */
     GICR = (1 << IVCE);     /* enable change of interrupt vectors */
     GICR = (0 << IVSEL);    /* move interrupts to application flash section */
-    
+
+/*
+ * There seems to be another funny compiler Bug.
+ * When gcc is using "eicall" opcode it forgets to modify EIND.
+ * On devices with large flash memory there are some target address bits
+ * missing. In this case some zero bits...
+ */
+#if (defined(EIND) && ((FLASHEND)>131071))
+  EIND=0;
+#endif
 /* We must go through a global function pointer variable instead of writing
  *  ((void (*)(void))0)();
  * because the compiler optimizes a constant 0 to "rcall 0" which is not
  * handled correctly by the assembler.
  */
     nullVector();
 /* We must go through a global function pointer variable instead of writing
  *  ((void (*)(void))0)();
  * because the compiler optimizes a constant 0 to "rcall 0" which is not
  * handled correctly by the assembler.
  */
     nullVector();
-#endif
 }
 }
+#endif
 
 /* ------------------------------------------------------------------------ */
 
 
 /* ------------------------------------------------------------------------ */
 
@@ -240,7 +354,9 @@ uchar usbFunctionSetup_USBASP_FUNC_TRANSMIT(usbRequest_t *rq) {
     rval = rq->wIndex.bytes[0] & 3;
     rval = signatureBytes[rval];
 #if HAVE_READ_LOCK_FUSE
     rval = rq->wIndex.bytes[0] & 3;
     rval = signatureBytes[rval];
 #if HAVE_READ_LOCK_FUSE
-#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega32__)
+#if defined (__AVR_ATmega8535__) ||                                    \
+    defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) ||                 \
+    defined (__AVR_ATmega16__) || defined (__AVR_ATmega32__)
   }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
       rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
   }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
   }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
       rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
   }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
@@ -250,13 +366,19 @@ uchar usbFunctionSetup_USBASP_FUNC_TRANSMIT(usbRequest_t *rq) {
 
 #elif defined (__AVR_ATmega48__)   || defined (__AVR_ATmega48A__)   || defined (__AVR_ATmega48P__)   || defined (__AVR_ATmega48PA__)  ||  \
 defined (__AVR_ATmega88__)   || defined (__AVR_ATmega88A__)   || defined (__AVR_ATmega88P__)   || defined (__AVR_ATmega88PA__)  ||  \
 
 #elif defined (__AVR_ATmega48__)   || defined (__AVR_ATmega48A__)   || defined (__AVR_ATmega48P__)   || defined (__AVR_ATmega48PA__)  ||  \
 defined (__AVR_ATmega88__)   || defined (__AVR_ATmega88A__)   || defined (__AVR_ATmega88P__)   || defined (__AVR_ATmega88PA__)  ||  \
+defined (__AVR_ATmega162__)  ||                                                                                                      \
 defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__)  ||                                                                     \
 defined (__AVR_ATmega168__)  || defined (__AVR_ATmega168A__)  || defined (__AVR_ATmega168P__)  || defined (__AVR_ATmega168PA__) ||  \
 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__)  ||                                                                     \
 defined (__AVR_ATmega328__)  || defined (__AVR_ATmega328P__)  ||                                                                     \
 defined (__AVR_ATmega164A__) || defined (__AVR_ATmega164P__)  ||                                                                     \
 defined (__AVR_ATmega168__)  || defined (__AVR_ATmega168A__)  || defined (__AVR_ATmega168P__)  || defined (__AVR_ATmega168PA__) ||  \
 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__)  ||                                                                     \
 defined (__AVR_ATmega328__)  || defined (__AVR_ATmega328P__)  ||                                                                     \
+defined (__AVR_ATmega640__)  ||                                                                                                        \
 defined (__AVR_ATmega644__)  || defined (__AVR_ATmega644A__)  || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)  ||  \
 defined (__AVR_ATmega128__)  ||                                                                                                              \
 defined (__AVR_ATmega644__)  || defined (__AVR_ATmega644A__)  || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)  ||  \
 defined (__AVR_ATmega128__)  ||                                                                                                              \
-defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
+defined (__AVR_ATmega1280__) ||                                                                                                        \
+defined (__AVR_ATmega1281__) ||                                                                                                        \
+defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)  ||                                                                                                      \
+defined (__AVR_ATmega2560__) ||                                                                                                        \
+defined (__AVR_ATmega2561__)
   }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
       rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
   }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
   }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x00){  /* read lock bits */
       rval = boot_lock_fuse_bits_get(GET_LOCK_BITS);
   }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x00){  /* read lfuse bits */
@@ -307,6 +429,23 @@ defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
 #   endif
       }
 #endif
 #   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 */
   }
   }else{
       /* ignore all others, return default value == 0 */
   }
@@ -315,19 +454,19 @@ defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
 }
 
 
 }
 
 
-uchar   usbFunctionSetup(uchar data[8])
+usbMsgLen_t usbFunctionSetup(uchar data[8])
 {
 usbRequest_t    *rq = (void *)data;
 {
 usbRequest_t    *rq = (void *)data;
-uchar           len = 0;
+usbMsgLen_t     len = 0;
 static uchar    replyBuffer[4];
 
 static uchar    replyBuffer[4];
 
-    usbMsgPtr = replyBuffer;
+    usbMsgPtr = (usbMsgPtr_t)replyBuffer;
     if(rq->bRequest == USBASP_FUNC_TRANSMIT){   /* emulate parts of ISP protocol */
         replyBuffer[3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT(rq);
     if(rq->bRequest == USBASP_FUNC_TRANSMIT){   /* emulate parts of ISP protocol */
         replyBuffer[3] = usbFunctionSetup_USBASP_FUNC_TRANSMIT(rq);
-        len = 4;
+        len = (usbMsgLen_t)4;
     }else if((rq->bRequest == USBASP_FUNC_ENABLEPROG) || (rq->bRequest == USBASP_FUNC_SETISPSCK)){
         /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
     }else if((rq->bRequest == USBASP_FUNC_ENABLEPROG) || (rq->bRequest == USBASP_FUNC_SETISPSCK)){
         /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
-        len = 1;
+        len = (usbMsgLen_t)1;
     }else if(rq->bRequest >= USBASP_FUNC_READFLASH && rq->bRequest <= USBASP_FUNC_SETLONGADDRESS){
         currentAddress.w[0] = rq->wValue.word;
         if(rq->bRequest == USBASP_FUNC_SETLONGADDRESS){
     }else if(rq->bRequest >= USBASP_FUNC_READFLASH && rq->bRequest <= USBASP_FUNC_SETLONGADDRESS){
         currentAddress.w[0] = rq->wValue.word;
         if(rq->bRequest == USBASP_FUNC_SETLONGADDRESS){
@@ -341,13 +480,18 @@ static uchar    replyBuffer[4];
 #if HAVE_EEPROM_PAGED_ACCESS
             currentRequest = rq->bRequest;
 #endif
 #if HAVE_EEPROM_PAGED_ACCESS
             currentRequest = rq->bRequest;
 #endif
-            len = 0xff; /* hand over to usbFunctionRead() / usbFunctionWrite() */
+            len = USB_NO_MSG; /* hand over to usbFunctionRead() / usbFunctionWrite() */
         }
 
     }else if(rq->bRequest == USBASP_FUNC_DISCONNECT){
 
 #if BOOTLOADER_CAN_EXIT
         }
 
     }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);
       stayinloader        &= (0xfe);
+#      endif
 #endif
     }else{
         /* ignore: others, but could be USBASP_FUNC_CONNECT */
 #endif
     }else{
         /* ignore: others, but could be USBASP_FUNC_CONNECT */
@@ -551,32 +695,35 @@ uchar   i;
 
 /* ------------------------------------------------------------------------ */
 
 
 /* ------------------------------------------------------------------------ */
 
-static void initForUsbConnectivity(void)
-{
-#if HAVE_UNPRECISEWAIT
-    /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
-     * F_CPU/(16*65536) ~ waitloopcnt
-     * F_CPU / 1048576 ~ waitloopcnt
-     */
-    uint8_t waitloopcnt = 1 + (F_CPU/1048576);
-#endif
-    usbInit();
-    /* enforce USB re-enumerate: */
-    usbDeviceDisconnect();  /* do this while interrupts are disabled */
 #if HAVE_UNPRECISEWAIT
 #if HAVE_UNPRECISEWAIT
+static void _mywait(uint8_t waitloopcnt) {
     asm volatile (
       /*we really don't care what value Z has...
        * ...if we loop 65536/F_CPU more or less...
        * ...unimportant - just save some opcodes
        */
     asm volatile (
       /*we really don't care what value Z has...
        * ...if we loop 65536/F_CPU more or less...
        * ...unimportant - just save some opcodes
        */
-"initForUsbConnectivity_sleeploop:                     \n\t"
+"_mywait_sleeploop%=:                                  \n\t"
       "sbiw    r30,    1                               \n\t"
       "sbci    %0,     0                               \n\t"
       "sbiw    r30,    1                               \n\t"
       "sbci    %0,     0                               \n\t"
-      "brne    initForUsbConnectivity_sleeploop        \n\t"
+      "brne    _mywait_sleeploop%=                     \n\t"
       : "+d" (waitloopcnt)
       :
       : "r30","r31"
     );
       : "+d" (waitloopcnt)
       :
       : "r30","r31"
     );
+}
+#endif
+
+static void initForUsbConnectivity(void)
+{
+    usbInit();
+    /* enforce USB re-enumerate: */
+    usbDeviceDisconnect();  /* do this while interrupts are disabled */
+#if HAVE_UNPRECISEWAIT
+    /* (0.25s*F_CPU)/(4 cycles per loop) ~ (65536*waitloopcnt)
+     * F_CPU/(16*65536) ~ waitloopcnt
+     * F_CPU / 1048576 ~ waitloopcnt
+     */
+    _mywait(1 + (F_CPU/1048576));
 #else
     _delay_ms(260);         /* fake USB disconnect for > 250 ms */
 #endif
 #else
     _delay_ms(260);         /* fake USB disconnect for > 250 ms */
 #endif
@@ -586,6 +733,10 @@ static void initForUsbConnectivity(void)
 
 int __attribute__((__noreturn__)) main(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();
     /* initialize  */
     bootLoaderInit();
     odDebugInit();
@@ -594,14 +745,87 @@ int __attribute__((__noreturn__)) main(void)
     GICR = (1 << IVCE);  /* enable change of interrupt vectors */
     GICR = (1 << IVSEL); /* move interrupts to boot flash section */
 #endif
     GICR = (1 << IVCE);  /* enable change of interrupt vectors */
     GICR = (1 << IVSEL); /* move interrupts to boot flash section */
 #endif
+#if (HAVE_BOOTLOADER_ADDITIONALMSDEVICEWAIT>0)    
+#      if HAVE_UNPRECISEWAIT
+    _mywait(1+((HAVE_BOOTLOADER_ADDITIONALMSDEVICEWAIT*F_CPU)/262144000));
+#      else
+    _delay_ms(HAVE_BOOTLOADER_ADDITIONALMSDEVICEWAIT);
+#      endif
+#endif
     if(bootLoaderCondition()){
     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 NEED_WATCHDOG
+#      if (defined(MCUSR) && defined(WDRF))
+       /* 
+        * Fix issue 6: (special thanks to coldtobi)
+        * 
+        * The WDRF bit in the MCUSR needs to be cleared first,
+        * otherwise it is not possible to disable the watchdog
+        */
+       MCUSR &= ~(_BV(WDRF));
+#      endif
        wdt_disable();    /* main app may have enabled watchdog */
 #endif
        wdt_disable();    /* main app may have enabled watchdog */
 #endif
+       MCUCSR = 0;       /* clear all reset flags for next time */
         initForUsbConnectivity();
         do{
         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
             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"
 #if USE_EXCESSIVE_ASSEMBLER
 asm  volatile  (
   "cpi         %[sil],         0x10\n\t"
@@ -623,16 +847,17 @@ asm  volatile  (
 );
 #else
        if (stayinloader >= 0x10) {
 );
 #else
        if (stayinloader >= 0x10) {
-         if (!bootLoaderCondition()) {
+         if (!bootLoaderConditionSimple()) {
            stayinloader-=0x10;
          } 
        } else {
            stayinloader-=0x10;
          } 
        } else {
-         if (bootLoaderCondition()) {
+         if (bootLoaderConditionSimple()) {
            if (stayinloader > 1) stayinloader-=2;
          }
        }
 #endif
 #endif
            if (stayinloader > 1) stayinloader-=2;
          }
        }
 #endif
 #endif
+#endif
 
 #if BOOTLOADER_CAN_EXIT
         }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/
 
 #if BOOTLOADER_CAN_EXIT
         }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/