Clean up and add new features
authorStephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Sat, 10 Nov 2012 09:59:11 +0000 (10:59 +0100)
committerStephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Sat, 10 Nov 2012 11:27:27 +0000 (11:27 +0000)
Signed-off-by: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
firmware/bootloaderconfig.h
firmware/main.c

index 97311dd..2b94da4 100644 (file)
@@ -1,7 +1,9 @@
 /* Name: bootloaderconfig.h
  * Project: USBaspLoader
  * Author: Christian Starkjohann
 /* Name: bootloaderconfig.h
  * 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)
  * Tabsize: 4
  * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
  * License: GNU GPL v2 (see License.txt)
@@ -36,25 +38,50 @@ names BOOTLOADER_INIT and BOOTLOADER_CONDITION for this functionality. If
 these macros are defined, the boot loader usees them.
 */
 
 these macros are defined, the boot loader usees them.
 */
 
+/* ---------------------------- Macro Magic ---------------------------- */
+#define                PIN_CONCAT(a,b)                 a ## b
+#define                PIN_CONCAT3(a,b,c)              a ## b ## c
+
+#define                PIN_PORT(a)                     PIN_CONCAT(PORT, a)
+#define                PIN_PIN(a)                      PIN_CONCAT(PIN, a)
+#define                PIN_DDR(a)                      PIN_CONCAT(DDR, a)
+
+#define                PIN(a, b)                       PIN_CONCAT3(P, a, b)
+
 /* ---------------------------- Hardware Config ---------------------------- */
 
 /* ---------------------------- Hardware Config ---------------------------- */
 
-#define USB_CFG_IOPORTNAME      D
+#ifndef USB_CFG_IOPORTNAME
+  #define USB_CFG_IOPORTNAME      D
+#endif
 /* This is the port where the USB bus is connected. When you configure it to
  * "B", the registers PORTB, PINB and DDRB will be used.
  */
 /* This is the port where the USB bus is connected. When you configure it to
  * "B", the registers PORTB, PINB and DDRB will be used.
  */
-#define JUMPER_BIT             7       /* old value was 0 */
-/* 
- * jumper is connected to this bit in port D, active low
- */
-#define USB_CFG_DMINUS_BIT      6      /* old value was 4 */
+#ifndef USB_CFG_DMINUS_BIT
+  #define USB_CFG_DMINUS_BIT      6    /* old value was 4 */
+#endif
 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
  * This may be any bit in the port.
  */
 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
  * This may be any bit in the port.
  */
-#define USB_CFG_DPLUS_BIT       2
+#ifndef USB_CFG_DPLUS_BIT
+  #define USB_CFG_DPLUS_BIT       2
+#endif
 /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
  * This may be any bit in the port. Please note that D+ must also be connected
  * to interrupt pin INT0!
  */
 /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
  * This may be any bit in the port. Please note that D+ must also be connected
  * to interrupt pin INT0!
  */
+#ifndef JUMPER_PORT
+  #define JUMPER_PORT          USB_CFG_IOPORTNAME
+#endif
+/* 
+ * jumper is connected to this port
+ */
+#ifndef JUMPER_BIT
+  #define JUMPER_BIT           7       /* old value was 0 */
+#endif
+/* 
+ * jumper is connected to this bit in port "JUMPER_PORT", active low
+ */
+
 #define USB_CFG_CLOCK_KHZ       (F_CPU/1000)
 /* Clock rate of the AVR in MHz. Legal values are 12000, 16000 or 16500.
  * The 16.5 MHz version of the code requires no crystal, it tolerates +/- 1%
 #define USB_CFG_CLOCK_KHZ       (F_CPU/1000)
 /* Clock rate of the AVR in MHz. Legal values are 12000, 16000 or 16500.
  * The 16.5 MHz version of the code requires no crystal, it tolerates +/- 1%
@@ -81,19 +108,31 @@ these macros are defined, the boot loader usees them.
 /* ---------------------- feature / code size options ---------------------- */
 /* ------------------------------------------------------------------------- */
 
 /* ---------------------- feature / code size options ---------------------- */
 /* ------------------------------------------------------------------------- */
 
-#define HAVE_READ_LOCK_FUSE        1
+#ifndef CONFIG_NO__HAVE_READ_LOCK_FUSE
+  #define HAVE_READ_LOCK_FUSE      1
+#else
+  #define HAVE_READ_LOCK_FUSE      0
+#endif
 /*
  * enable the loaders capability to load its lfuse, hfuse and lockbits
  * ...However, programming of these is prohibited...
  */
 
 /*
  * enable the loaders capability to load its lfuse, hfuse and lockbits
  * ...However, programming of these is prohibited...
  */
 
-#define HAVE_BLB11_SOFTW_LOCKBIT    1
+#ifndef CONFIG_NO__HAVE_BLB11_SOFTW_LOCKBIT
+  #define HAVE_BLB11_SOFTW_LOCKBIT    1
+#else
+  #define HAVE_BLB11_SOFTW_LOCKBIT    0
+#endif
 /*
  * The IC itself do not need to prgra BLB11, but the bootloader will avaoid 
  * to erase itself from the bootregion
  */
 
 /*
  * The IC itself do not need to prgra BLB11, but the bootloader will avaoid 
  * to erase itself from the bootregion
  */
 
-#define HAVE_SPMINTEREFACE         1
+#ifndef CONFIG_NO__HAVE_SPMINTEREFACE
+  #define HAVE_SPMINTEREFACE       1
+#else
+  #define HAVE_SPMINTEREFACE       0
+#endif
 /*
  * Since code within normal section of application memory (rww-section) is
  * not able to call spm for programming flash-pages, this option (when
 /*
  * Since code within normal section of application memory (rww-section) is
  * not able to call spm for programming flash-pages, this option (when
@@ -117,14 +156,29 @@ these macros are defined, the boot loader usees them.
  * signature) does not support page mode for EEPROM. It is required for
  * accessing the EEPROM on the ATMega8. Costs ~54 bytes.
  */
  * signature) does not support page mode for EEPROM. It is required for
  * accessing the EEPROM on the ATMega8. Costs ~54 bytes.
  */
-#define BOOTLOADER_CAN_EXIT         0
+#ifndef CONFIG_NO__BOOTLOADER_CAN_EXIT
+  #define BOOTLOADER_CAN_EXIT         1
+#else
+  #define BOOTLOADER_CAN_EXIT         0
+#endif
 /* If this macro is defined to 1, the boot loader will exit shortly after the
 /* If this macro is defined to 1, the boot loader will exit shortly after the
- * programmer closes the connection to the device. Costs ~36 bytes.
+ * programmer closes the connection to the device. Costs extra bytes.
  */
 #define HAVE_CHIP_ERASE             0
 /* If this macro is defined to 1, the boot loader implements the Chip Erase
  * ISP command. Otherwise pages are erased on demand before they are written.
  */
  */
 #define HAVE_CHIP_ERASE             0
 /* If this macro is defined to 1, the boot loader implements the Chip Erase
  * ISP command. Otherwise pages are erased on demand before they are written.
  */
+#ifndef CONFIG_NO__NEED_WATCHDOG
+  #define NEED_WATCHDOG                1
+#else
+  #define NEED_WATCHDOG                0
+#endif
+/* ATTANTION: This macro MUST BE 1, if the MCU has reset enabled watchdog (WDTON is 0).
+ * If this macro is defined to 1, the bootloader implements an additional "wdt_disable()"
+ * after its contional entry point.
+ * If the used MCU is fused not to enable watchdog after reset (WDTON is 1 - safty level 1)
+ * then "NEED_WATCHDOG" may be deactivated in order to save some memory.
+ */
 //#define SIGNATURE_BYTES             0x1e, 0x93, 0x07, 0     /* ATMega8 */
 /* This macro defines the signature bytes returned by the emulated USBasp to
  * the programmer software. They should match the actual device at least in
 //#define SIGNATURE_BYTES             0x1e, 0x93, 0x07, 0     /* ATMega8 */
 /* This macro defines the signature bytes returned by the emulated USBasp to
  * the programmer software. They should match the actual device at least in
@@ -132,20 +186,6 @@ these macros are defined, the boot loader usees them.
  * ATMega88, ATMega168 and ATMega328 are guessed correctly.
  */
 
  * ATMega88, ATMega168 and ATMega328 are guessed correctly.
  */
 
-/* The following block guesses feature options so that the resulting code
- * should fit into 2k bytes boot block with the given device and clock rate.
- * Activate by passing "-DUSE_AUTOCONFIG=1" to the compiler.
- * This requires gcc 3.4.6 for small enough code size!
- */
-#if USE_AUTOCONFIG
-#   undef HAVE_EEPROM_PAGED_ACCESS
-#   define HAVE_EEPROM_PAGED_ACCESS     (USB_CFG_CLOCK_KHZ >= 16000)
-#   undef HAVE_EEPROM_BYTE_ACCESS
-#   define HAVE_EEPROM_BYTE_ACCESS      1
-#   undef BOOTLOADER_CAN_EXIT
-#   define BOOTLOADER_CAN_EXIT          1
-#   undef SIGNATURE_BYTES
-#endif /* USE_AUTOCONFIG */
 
 /* ------------------------------------------------------------------------- */
 
 
 /* ------------------------------------------------------------------------- */
 
@@ -165,20 +205,22 @@ these macros are defined, the boot loader usees them.
 
 static inline void  bootLoaderInit(void)
 {
 
 static inline void  bootLoaderInit(void)
 {
-    DDRD   = 0;
-    PORTD  = (1 << JUMPER_BIT);     /* activate pull-up */
+    PIN_DDR(JUMPER_PORT)  = 0;
+    PIN_PORT(JUMPER_PORT) = (1<< PIN(JUMPER_PORT, JUMPER_BIT)); /* activate pull-up */
+
 //     deactivated by Stephan - reset after each avrdude op is annoing!
 //     if(!(MCUCSR & (1 << EXTRF)))    /* If this was not an external reset, ignore */
 //         leaveBootloader();
 //     deactivated by Stephan - reset after each avrdude op is annoing!
 //     if(!(MCUCSR & (1 << EXTRF)))    /* If this was not an external reset, ignore */
 //         leaveBootloader();
+
     MCUCSR = 0;                     /* clear all reset flags for next time */
 }
 
 static inline void  bootLoaderExit(void)
 {
     MCUCSR = 0;                     /* clear all reset flags for next time */
 }
 
 static inline void  bootLoaderExit(void)
 {
-    PORTD = 0;                      /* undo bootLoaderInit() changes */
+    PIN_PORT(JUMPER_PORT) = 0;         /* undo bootLoaderInit() changes */
 }
 
 }
 
-#define bootLoaderCondition()          ((PIND & (1 << JUMPER_BIT)) == 0)
+#define bootLoaderCondition()          ((PIN_PIN(JUMPER_PORT) & (1 << PIN(JUMPER_PORT, JUMPER_BIT))) == 0)
 
 #endif /* __ASSEMBLER__ */
 
 
 #endif /* __ASSEMBLER__ */
 
index eb83d4e..c80ab7d 100644 (file)
@@ -1,7 +1,9 @@
 /* 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)
  * Tabsize: 4
  * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
  * License: GNU GPL v2 (see License.txt)
@@ -80,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
@@ -123,9 +115,8 @@ typedef union longConverter{
 
 
 #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;
@@ -299,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;
 }
@@ -413,21 +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 NEED_WATCHDOG
        wdt_disable();    /* main app may have enabled watchdog */
        wdt_disable();    /* main app may have enabled watchdog */
-#if BOOTLOADER_CAN_EXIT
-        uchar i = 0, j = 0;
 #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;
@@ -437,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();
 }