smpinterface fix: missing rampZ-write for atmega1284p
[pub/USBaspLoader.git] / firmware / main.c
index 5c1233b..fd5dd48 100644 (file)
@@ -8,6 +8,8 @@
  * This Revision: $Id: main.c 786 2010-05-30 20:41:40Z cs $
  */
 
  * 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>
@@ -18,6 +20,8 @@
 
 #include <avr/cpufunc.h>
 
 
 #include <avr/cpufunc.h>
 
+#include <avr/boot.h>
+
 #include <string.h>
 
 
 #include <string.h>
 
 
@@ -44,6 +48,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
@@ -95,37 +108,6 @@ typedef union longConverter{
 }longConverter_t;
 
 
 }longConverter_t;
 
 
-#if HAVE_DOSPM_TUNNELCMD
-/*
-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[15] PROGMEM = {0xb68f , 0x94f8, 0xb677, 0xfc70, 0xcffd, 0xbe97, 0x95e8, 0xbe8f, 0x9508, 0x00, 0xFFFF, 0x95e8, 0x9508, 0x0000,  0xFFFF};
-#else
-  #error "bootloader__do_spm has to be adapted, since there is no guaranty for SREG==0x3f, SPMCR==0x37, SPMEN==0x00"
-#endif
-#endif
-
-
 #if BOOTLOADER_CAN_EXIT
 static uchar                   requestBootLoaderExit;
 #endif
 #if BOOTLOADER_CAN_EXIT
 static uchar                   requestBootLoaderExit;
 #endif
@@ -153,6 +135,8 @@ static const uchar  signatureBytes[4] = {
     0x1e, 0x94, 0x06, 0
 #elif defined (__AVR_ATmega328P__)
     0x1e, 0x95, 0x0f, 0
     0x1e, 0x94, 0x06, 0
 #elif defined (__AVR_ATmega328P__)
     0x1e, 0x95, 0x0f, 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
@@ -172,6 +156,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
@@ -232,7 +217,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){
@@ -282,11 +267,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;
@@ -362,7 +343,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);
@@ -371,6 +351,7 @@ 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()){
+       wdt_disable();    /* main app may have enabled watchdog */
 #if BOOTLOADER_CAN_EXIT
         uchar i = 0, j = 0;
 #endif
 #if BOOTLOADER_CAN_EXIT
         uchar i = 0, j = 0;
 #endif