have updater check HAVE_SPMINTEREFACE and raise error if not supported
[pub/USBaspLoader.git] / firmware / main.c
index d8386e2..c80ab7d 100644 (file)
@@ -1,13 +1,17 @@
 /* Name: main.c
  * Project: USBaspLoader
  * Author: Christian Starkjohann
 /* Name: main.c
  * Project: USBaspLoader
  * Author: Christian Starkjohann
+ * Author: Stephan Baerwolf
  * Creation Date: 2007-12-08
  * Creation Date: 2007-12-08
+ * Modification Date: 2012-11-10
  * 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 $
  */
 
  * 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 <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/pgmspace.h>
 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/pgmspace.h>
 #include <avr/eeprom.h>
 #include <util/delay.h>
 
 #include <avr/eeprom.h>
 #include <util/delay.h>
 
+
+#if 0
+/*
+ * 29.09.2012 /  30.09.2012
+ * 
+ * Since cpufunc.h is not needed in this context and
+ * since it is not available in all toolchains, this include
+ * becomes deactivated by github issue-report.
+ * (In case of trouble it remains in sourcecode for reactivation.)
+ * 
+ * The autor would like to thank Lena-M for reporting this
+ * issue (https://github.com/baerwolf/USBaspLoader/issues/1).
+ */
 #include <avr/cpufunc.h>
 #include <avr/cpufunc.h>
+#endif
+
+#include <avr/boot.h>
 
 #include <string.h>
 
 
 #include <string.h>
 
@@ -44,6 +64,15 @@ static void leaveBootloader() __attribute__((__noreturn__));
 #define USBASP_FUNC_WRITEEEPROM     8
 #define USBASP_FUNC_SETLONGADDRESS  9
 
 #define USBASP_FUNC_WRITEEEPROM     8
 #define USBASP_FUNC_SETLONGADDRESS  9
 
+// additional USBasp Commands
+#define USBASP_FUNC_SETISPSCK       10
+#define USBASP_FUNC_TPI_CONNECT      11
+#define USBASP_FUNC_TPI_DISCONNECT   12
+#define USBASP_FUNC_TPI_RAWREAD      13
+#define USBASP_FUNC_TPI_RAWWRITE     14
+#define USBASP_FUNC_TPI_READBLOCK    15
+#define USBASP_FUNC_TPI_WRITEBLOCK   16
+#define USBASP_FUNC_GETCAPABILITIES 127
 /* ------------------------------------------------------------------------ */
 
 #ifndef ulong
 /* ------------------------------------------------------------------------ */
 
 #ifndef ulong
@@ -53,16 +82,6 @@ static void leaveBootloader() __attribute__((__noreturn__));
 #   define uint     unsigned int
 #endif
 
 #   define uint     unsigned int
 #endif
 
-/* defaults if not in config file: */
-#ifndef HAVE_EEPROM_PAGED_ACCESS
-#   define HAVE_EEPROM_PAGED_ACCESS 0
-#endif
-#ifndef HAVE_EEPROM_BYTE_ACCESS
-#   define HAVE_EEPROM_BYTE_ACCESS  0
-#endif
-#ifndef BOOTLOADER_CAN_EXIT
-#   define  BOOTLOADER_CAN_EXIT     0
-#endif
 
 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
 #ifdef BOOTLOADER_INIT
 
 /* allow compatibility with avrusbboot's bootloaderconfig.h: */
 #ifdef BOOTLOADER_INIT
@@ -95,45 +114,9 @@ typedef union longConverter{
 }longConverter_t;
 
 
 }longConverter_t;
 
 
-#if HAVE_DOSPM_TUNNELCMD
-#if HAVE_BLB11_SOFTW_BACKDOOR
-  const uint16_t bootloader__do_spm[13] PROGMEM = {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000};
-#else
-/*
-do_spm:
-;input:        spmcrval determines SPM action
-;disable interrupts if enabled, store status
-;temp1 will be register:       r7
-;temp2 will be register:       r8
-;spmcrval will be register:    r9
-
-in     temp2, SREG     ; --> has to be done before calling
-cli                    ; --> has to be done before calling
-                       ;check for previous SPM complete
-wait:
-in     temp1, SPMCR
-sbrc   temp1, SPMEN
-rjmp   wait
-                       ;SPM timed sequence
-out    SPMCR, spmcrval
-spm
-                       ;restore SREG (to enable interrupts if originally enabled)
-out    SREG, temp2
-ret
-*/ 
-#if defined (__AVR_ATmega8__)
-  const uint16_t bootloader__do_spm[13] PROGMEM = {0xb68f , 0x94f8, 0xb677, 0xfc70, 0xcffd, 0xbe97, 0x95e8, 0xbe8f, 0x9508, 0x00, 0xFFFF, 0xFFFF, 0xFFFF};
-#else
-  #error "bootloader__do_spm has to be adapted, since there is no guaranty for SREG==0x3f, SPMCR==0x37, SPMEN==0x00"
-#endif
-#endif
-#endif
-
-
 #if BOOTLOADER_CAN_EXIT
 #if BOOTLOADER_CAN_EXIT
-static uchar                   requestBootLoaderExit;
-#endif
 static volatile unsigned char  stayinloader = 0xfe;
 static volatile unsigned char  stayinloader = 0xfe;
+#endif
 
 static longConverter_t         currentAddress; /* in bytes */
 static uchar                   bytesRemaining;
 
 static longConverter_t         currentAddress; /* in bytes */
 static uchar                   bytesRemaining;
@@ -147,16 +130,46 @@ 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_ATmega8__) || defined (__AVR_ATmega8HVA__)
+#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega8HVA__)
     0x1e, 0x93, 0x07, 0
     0x1e, 0x93, 0x07, 0
-#elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48P__)
+#elif defined (__AVR_ATmega32__)
+    0x1e, 0x95, 0x02, 0
+#elif defined (__AVR_ATmega48__) || defined (__AVR_ATmega48A__) || defined (__AVR_ATmega48P__)
+    #error ATmega48 does not support bootloaders!
     0x1e, 0x92, 0x05, 0
     0x1e, 0x92, 0x05, 0
-#elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88P__)
+#elif defined (__AVR_ATmega48PA__)
+    #error ATmega48 does not support bootloaders!
+    0x1e, 0x92, 0x0A, 0
+#elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega88A__) || defined (__AVR_ATmega88P__)
     0x1e, 0x93, 0x0a, 0
     0x1e, 0x93, 0x0a, 0
-#elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168P__)
+#elif defined (__AVR_ATmega88PA__)
+    0x1e, 0x93, 0x0F, 0
+#elif defined (__AVR_ATmega164A__)
+    0x1e, 0x94, 0x0f, 0
+#elif defined (__AVR_ATmega164P__)
+    0x1e, 0x94, 0x0a, 0
+#elif defined (__AVR_ATmega168__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega168P__)
     0x1e, 0x94, 0x06, 0
     0x1e, 0x94, 0x06, 0
+#elif defined (__AVR_ATmega168PA__)
+    0x1e, 0x94, 0x0B, 0
+#elif defined (__AVR_ATmega324A__)
+    0x1e, 0x95, 0x15, 0
+#elif defined (__AVR_ATmega324P__)
+    0x1e, 0x95, 0x08, 0
+#elif defined (__AVR_ATmega328__)
+    0x1e, 0x95, 0x14, 0
 #elif defined (__AVR_ATmega328P__)
     0x1e, 0x95, 0x0f, 0
 #elif defined (__AVR_ATmega328P__)
     0x1e, 0x95, 0x0f, 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_ATmega1284__)
+    0x1e, 0x97, 0x06, 0
+#elif defined (__AVR_ATmega1284P__)
+    0x1e, 0x97, 0x05, 0
 #else
 #   error "Device signature is not known, please edit main.c!"
 #endif
 #else
 #   error "Device signature is not known, please edit main.c!"
 #endif
@@ -176,6 +189,7 @@ static void leaveBootloader()
     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 */
+    
 /* 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
 /* 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
@@ -202,13 +216,33 @@ static uchar    replyBuffer[4];
             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__)
+#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || 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 */
+            rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
+        }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){  /* read hfuse bits */
+            rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
+
+#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_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_ATmega644__)  || defined (__AVR_ATmega644A__)  || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)  ||  \
+      defined (__AVR_ATmega128__)  ||                                                                                                        \
+      defined (__AVR_ATmega1284__) || defined (__AVR_ATmega1284P__)
         }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 */
             rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
         }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){  /* read hfuse bits */
             rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_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 */
             rval = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
         }else if(rq->wValue.bytes[0] == 0x58 && rq->wValue.bytes[1] == 0x08){  /* read hfuse bits */
             rval = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
+        }else if(rq->wValue.bytes[0] == 0x50 && rq->wValue.bytes[1] == 0x08){  /* read efuse bits */
+            rval = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS );
+#else
+       #warning "HAVE_READ_LOCK_FUSE is activated but MCU unknown -> will not support this feature"
 #endif
 #endif
 #if HAVE_EEPROM_BYTE_ACCESS
 #endif
 #endif
 #if HAVE_EEPROM_BYTE_ACCESS
@@ -236,7 +270,7 @@ static uchar    replyBuffer[4];
         }
         replyBuffer[3] = rval;
         len = 4;
         }
         replyBuffer[3] = rval;
         len = 4;
-    }else if(rq->bRequest == USBASP_FUNC_ENABLEPROG){
+    }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;
     }else if(rq->bRequest >= USBASP_FUNC_READFLASH && rq->bRequest <= USBASP_FUNC_SETLONGADDRESS){
         /* replyBuffer[0] = 0; is never touched and thus always 0 which means success */
         len = 1;
     }else if(rq->bRequest >= USBASP_FUNC_READFLASH && rq->bRequest <= USBASP_FUNC_SETLONGADDRESS){
@@ -256,13 +290,15 @@ static uchar    replyBuffer[4];
         }
 
     }else if(rq->bRequest == USBASP_FUNC_DISCONNECT){
         }
 
     }else if(rq->bRequest == USBASP_FUNC_DISCONNECT){
-      stayinloader        &= (0xfe);
+
 #if BOOTLOADER_CAN_EXIT
 #if BOOTLOADER_CAN_EXIT
-      requestBootLoaderExit = 1;      /* allow proper shutdown/close of connection */
+      stayinloader        &= (0xfe);
 #endif
     }else{
         /* ignore: others, but could be USBASP_FUNC_CONNECT */
 #endif
     }else{
         /* ignore: others, but could be USBASP_FUNC_CONNECT */
+#if BOOTLOADER_CAN_EXIT
        stayinloader       |= (0x01);
        stayinloader       |= (0x01);
+#endif
     }
     return len;
 }
     }
     return len;
 }
@@ -286,11 +322,7 @@ uchar   isLast;
         for(i = 0; i < len;){
 #if HAVE_BLB11_SOFTW_LOCKBIT
            if (CURRENT_ADDRESS >= (addr_t)(BOOTLOADER_ADDRESS)) {
         for(i = 0; i < len;){
 #if HAVE_BLB11_SOFTW_LOCKBIT
            if (CURRENT_ADDRESS >= (addr_t)(BOOTLOADER_ADDRESS)) {
-#if HAVE_BLB11_SOFTW_BACKDOOR
-             if (!((stayinloader >= 0x10) && (bootLoaderCondition()))) return 1;
-#else
              return 1;
              return 1;
-#endif
            }
 #endif
             i += 2;
            }
 #endif
             i += 2;
@@ -306,9 +338,9 @@ uchar   isLast;
                 DBG1(0x33, 0, 0);
 #   ifndef NO_FLASH_WRITE
                 cli();
                 DBG1(0x33, 0, 0);
 #   ifndef NO_FLASH_WRITE
                 cli();
-                boot_page_erase((currentAddress.w[0]-2) & (SPM_PAGESIZE - 1));   /* erase page */
+                boot_page_erase(CURRENT_ADDRESS - 2);   /* erase page */
                 sei();
                 sei();
-                boot_spm_busy_wait();               /* wait until page is erased */
+                boot_spm_busy_wait();                   /* wait until page is erased */
 #   endif
 #endif
                 DBG1(0x34, 0, 0);
 #   endif
 #endif
                 DBG1(0x34, 0, 0);
@@ -366,7 +398,6 @@ uchar   i = 0;
 int __attribute__((noreturn)) main(void)
 {
     /* initialize  */
 int __attribute__((noreturn)) main(void)
 {
     /* initialize  */
-    wdt_disable();      /* main app may have enabled watchdog */
     bootLoaderInit();
     odDebugInit();
     DBG1(0x00, 0, 0);
     bootLoaderInit();
     odDebugInit();
     DBG1(0x00, 0, 0);
@@ -375,20 +406,13 @@ int __attribute__((noreturn)) main(void)
     GICR = (1 << IVSEL); /* move interrupts to boot flash section */
 #endif
     if(bootLoaderCondition()){
     GICR = (1 << IVSEL); /* move interrupts to boot flash section */
 #endif
     if(bootLoaderCondition()){
-#if BOOTLOADER_CAN_EXIT
-        uchar i = 0, j = 0;
+#if NEED_WATCHDOG
+       wdt_disable();    /* main app may have enabled watchdog */
 #endif
         initForUsbConnectivity();
         do{
             usbPoll();
 #if BOOTLOADER_CAN_EXIT
 #endif
         initForUsbConnectivity();
         do{
             usbPoll();
 #if BOOTLOADER_CAN_EXIT
-            if(requestBootLoaderExit){
-                if(--i == 0){
-                    if(--j == 0)
-                        break;
-                }
-            }
-#endif
        if (stayinloader >= 0x10) {
          if (!bootLoaderCondition()) {
            stayinloader-=0x10;
        if (stayinloader >= 0x10) {
          if (!bootLoaderCondition()) {
            stayinloader-=0x10;
@@ -398,8 +422,13 @@ int __attribute__((noreturn)) main(void)
            if (stayinloader > 1) stayinloader-=2;
          }
        }
            if (stayinloader > 1) stayinloader-=2;
          }
        }
+#endif
 
 
-        }while (stayinloader);  /* main event loop */
+#if BOOTLOADER_CAN_EXIT
+        }while (stayinloader); /* main event loop, if BOOTLOADER_CAN_EXIT*/
+#else
+        }while (1);            /* main event loop */
+#endif
     }
     leaveBootloader();
 }
     }
     leaveBootloader();
 }