USBaspLoader v0.97 stable release
[pub/USBaspLoader.git] / firmware / main.c
index 9558089..fccec56 100644 (file)
@@ -11,7 +11,8 @@
 
 #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>
@@ -164,6 +165,8 @@ 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_ATmega164P__) || defined (__AVR_ATmega164PA__)
 #elif defined (__AVR_ATmega164A__)
     0x1e, 0x94, 0x0f, 0
 #elif defined (__AVR_ATmega164P__) || defined (__AVR_ATmega164PA__)
@@ -363,6 +366,7 @@ 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_ATmega164A__) || defined (__AVR_ATmega164P__)  ||                                                                     \
 defined (__AVR_ATmega168__)  || defined (__AVR_ATmega168A__)  || defined (__AVR_ATmega168P__)  || defined (__AVR_ATmega168PA__) ||  \
 defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__)  ||                                                                     \
@@ -691,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
@@ -738,6 +745,13 @@ 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 (BOOTLOADER_CAN_EXIT)
 #      if (USE_EXCESSIVE_ASSEMBLER)
     if(bootLoaderCondition()){
 #if (BOOTLOADER_CAN_EXIT)
 #      if (USE_EXCESSIVE_ASSEMBLER)