1 /* Name: bootloaderconfig.h 
   2  * Project: USBaspLoader 
   3  * Author: Christian Starkjohann 
   4  * Author: Stephan Baerwolf 
   5  * Creation Date: 2007-12-08 
   6  * Modification Date: 2012-11-10 
   8  * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH 
   9  * License: GNU GPL v2 (see License.txt) 
  10  * This Revision: $Id: bootloaderconfig.h 729 2009-03-20 09:03:58Z cs $ 
  13 #ifndef __bootloaderconfig_h_included__ 
  14 #define __bootloaderconfig_h_included__ 
  19 This file (together with some settings in Makefile) configures the boot loader 
  20 according to the hardware. 
  22 This file contains (besides the hardware configuration normally found in 
  23 usbconfig.h) two functions or macros: bootLoaderInit() and 
  24 bootLoaderCondition(). Whether you implement them as macros or as static 
  25 inline functions is up to you, decide based on code size and convenience. 
  27 bootLoaderInit() is called as one of the first actions after reset. It should 
  28 be a minimum initialization of the hardware so that the boot loader condition 
  29 can be read. This will usually consist of activating a pull-up resistor for an 
  30 external jumper which selects boot loader mode. 
  32 bootLoaderCondition() is called immediately after initialization and in each 
  33 main loop iteration. If it returns TRUE, the boot loader will be active. If it 
  34 returns FALSE, the boot loader jumps to address 0 (the loaded application) 
  37 For compatibility with Thomas Fischl's avrusbboot, we also support the macro 
  38 names BOOTLOADER_INIT and BOOTLOADER_CONDITION for this functionality. If 
  39 these macros are defined, the boot loader usees them. 
  42 /* ---------------------------- Macro Magic ---------------------------- */ 
  43 #define         PIN_CONCAT(a,b)                 a ## b 
  44 #define         PIN_CONCAT3(a,b,c)              a ## b ## c 
  46 #define         PIN_PORT(a)                     PIN_CONCAT(PORT, a) 
  47 #define         PIN_PIN(a)                      PIN_CONCAT(PIN, a) 
  48 #define         PIN_DDR(a)                      PIN_CONCAT(DDR, a) 
  50 #define         PIN(a, b)                       PIN_CONCAT3(P, a, b) 
  52 /* ---------------------------- Hardware Config ---------------------------- */ 
  54 #ifndef USB_CFG_IOPORTNAME 
  55   #define USB_CFG_IOPORTNAME      D 
  57 /* This is the port where the USB bus is connected. When you configure it to 
  58  * "B", the registers PORTB, PINB and DDRB will be used. 
  60 #ifndef USB_CFG_DMINUS_BIT 
  61   #define USB_CFG_DMINUS_BIT      6     /* old value was 4 */ 
  63 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected. 
  64  * This may be any bit in the port. 
  66 #ifndef USB_CFG_DPLUS_BIT 
  67   #define USB_CFG_DPLUS_BIT       2 
  69 /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected. 
  70  * This may be any bit in the port. Please note that D+ must also be connected 
  71  * to interrupt pin INT0! 
  74   #define JUMPER_PORT           USB_CFG_IOPORTNAME 
  77  * jumper is connected to this port 
  80   #define JUMPER_BIT            7       /* old value was 0 */ 
  83  * jumper is connected to this bit in port "JUMPER_PORT", active low 
  86 #define USB_CFG_CLOCK_KHZ       (F_CPU/1000) 
  87 /* Clock rate of the AVR in MHz. Legal values are 12000, 16000 or 16500. 
  88  * The 16.5 MHz version of the code requires no crystal, it tolerates +/- 1% 
  89  * deviation from the nominal frequency. All other rates require a precision 
  90  * of 2000 ppm and thus a crystal! 
  91  * Default if not specified: 12 MHz 
  94 /* ----------------------- Optional Hardware Config ------------------------ */ 
  96 /* #define USB_CFG_PULLUP_IOPORTNAME   D */ 
  97 /* If you connect the 1.5k pullup resistor from D- to a port pin instead of 
  98  * V+, you can connect and disconnect the device from firmware by calling 
  99  * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h). 
 100  * This constant defines the port on which the pullup resistor is connected. 
 102 /* #define USB_CFG_PULLUP_BIT          4 */ 
 103 /* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined 
 104  * above) where the 1.5k pullup resistor is connected. See description 
 108 /* ------------------------------------------------------------------------- */ 
 109 /* ---------------------- feature / code size options ---------------------- */ 
 110 /* ------------------------------------------------------------------------- */ 
 112 #ifndef CONFIG_NO__HAVE_READ_LOCK_FUSE 
 113   #define HAVE_READ_LOCK_FUSE       1 
 115   #define HAVE_READ_LOCK_FUSE       0 
 118  * enable the loaders capability to load its lfuse, hfuse and lockbits 
 119  * ...However, programming of these is prohibited... 
 122 #ifndef CONFIG_NO__HAVE_BLB11_SOFTW_LOCKBIT 
 123   #define HAVE_BLB11_SOFTW_LOCKBIT    1 
 125   #define HAVE_BLB11_SOFTW_LOCKBIT    0 
 128  * The IC itself do not need to prgra BLB11, but the bootloader will avaoid  
 129  * to erase itself from the bootregion 
 132 #ifndef CONFIG_NO__HAVE_SPMINTEREFACE 
 133   #define HAVE_SPMINTEREFACE        1 
 135   #define HAVE_SPMINTEREFACE        0 
 138  * Since code within normal section of application memory (rww-section) is 
 139  * not able to call spm for programming flash-pages, this option (when 
 140  * enabled) will insert a small subroutine into the bootloader-section 
 141  * to enable applications to circumvent this limitation and make them 
 142  * able to program the flash in a similar way as the bootloader does, too. 
 143  * For further details see "spminterface.h", which implements this  
 147 #define HAVE_SPMINTEREFACE_NORETMAGIC   1 
 149  * If sth. went wrong within "bootloader__do_spm" and this macro is ACTIVATED, 
 150  * then "bootloader__do_spm" will not return the call and loop infinity instead. 
 152  * This feature prevents old updaters to do sth. undefined on wrong magic. 
 155 /* all boards should use a magic to make it safe to confuse updatefiles :-)  */ 
 156 #define HAVE_SPMINTEREFACE_MAGICVALUE    0 
 157 /* If this feature is enabled (value != 0), the configured 32bit value is  
 158  * used as a magic value within spminterface. "bootloader__do_spm" will check 
 159  * additional four (4) registers for this value and only proceed, if they contain 
 160  * the right value. With this feature you can identify your board and avoid 
 161  * updating the wrong bootloader to the wrong board! 
 163  * Not all values are possible - "SPMINTEREFACE_MAGICVALUE" must be very sparse! 
 164  * To avoid collisions, magic-values will be organized centrally by Stephan 
 165  * Following values are definitly blocked or reserved and must not be used: 
 166  *      0x00000000, 0x12345678, 
 167  *      0x00a500a5, 0x00a5a500, 0xa50000a5, 0xa500a500, 
 168  *      0x005a005a, 0x005a5a00, 0x5a00005a, 0x5a005a00, 
 169  *      0x5aa55aa5, 0x5aa5a55a, 0xa55a5aa5, 0xa55aa55a, 
 170  *      0x5a5a5a5a, 0xa5a5a5a5, 
 171  *      0xffa5ffa5, 0xffa5a5ff, 0xa5ffffa5, 0xa5ffa5ff, 
 172  *      0xff5aff5a, 0xff5a5aff, 0x5affff5a, 0x5aff5aff, 
 173  *      0x00ff00ff, 0x00ffff00, 0xff0000ff, 0xff00ff00, 
 176  * To request your own magic, please send at least following information 
 177  * about yourself and your board together within an informal request to: 
 178  * stephan@matrixstorm.com / matrixstorm@gmx.de / stephan.baerwolf@tu-ilmenau.de 
 181  *        - your project (maybe an url?) 
 182  *        - your type of MCU used 
 183  *      --> your used "BOOTLOADER_ADDRESS" (since same magics can be reused for different "BOOTLOADER_ADDRESS") 
 185  * There may be no garanty for it, but Stephan will then send you an 
 186  * response with a "SPMINTEREFACE_MAGICVALUE" just for your board/project... 
 187  * WITH REQUESTING A MAGIC YOU AGREE TO PUBLISHED YOUR DATA SEND WITHIN THE REQUEST  
 190 #ifndef CONFIG_NO__EEPROM_PAGED_ACCESS 
 191 #       define HAVE_EEPROM_PAGED_ACCESS    1 
 193 #       define HAVE_EEPROM_PAGED_ACCESS    0 
 195 /* If HAVE_EEPROM_PAGED_ACCESS is defined to 1, page mode access to EEPROM is 
 196  * compiled in. Whether page mode or byte mode access is used by AVRDUDE 
 197  * depends on the target device. Page mode is only used if the device supports 
 198  * it, e.g. for the ATMega88, 168 etc. You can save quite a bit of memory by 
 199  * disabling page mode EEPROM access. Costs ~ 138 bytes. 
 202 #ifndef CONFIG_NO__EEPROM_BYTE_ACCESS 
 203 #       define HAVE_EEPROM_BYTE_ACCESS     1 
 205 #       define HAVE_EEPROM_BYTE_ACCESS     0 
 207 /* If HAVE_EEPROM_BYTE_ACCESS is defined to 1, byte mode access to EEPROM is 
 208  * compiled in. Byte mode is only used if the device (as identified by its 
 209  * signature) does not support page mode for EEPROM. It is required for 
 210  * accessing the EEPROM on the ATMega8. Costs ~54 bytes. 
 213 #ifndef CONFIG_NO__BOOTLOADER_CAN_EXIT 
 214 #       define BOOTLOADER_CAN_EXIT         1 
 216 #       define BOOTLOADER_CAN_EXIT         0 
 218 /* If this macro is defined to 1, the boot loader will exit shortly after the 
 219  * programmer closes the connection to the device. Costs extra bytes. 
 222 #ifndef CONFIG_NO__CHIP_ERASE 
 223 #       define HAVE_CHIP_ERASE             1 
 225 #       define HAVE_CHIP_ERASE             0 
 227 /* If this macro is defined to 1, the boot loader implements the Chip Erase 
 228  * ISP command. Otherwise pages are erased on demand before they are written. 
 230 #ifndef CONFIG_NO__ONDEMAND_PAGEERASE 
 231 #       define HAVE_ONDEMAND_PAGEERASE            1 
 233 #       define HAVE_ONDEMAND_PAGEERASE            0 
 235 /* Even if "HAVE_CHIP_ERASE" is avtivated - enabling the "HAVE_ONDEMAND_PAGEERASE"- 
 236  * feature the bootloader will erase pages on demand short before writing new data 
 238  * If pages are not erase before reprogram (for example because user call avrdude -D) 
 239  * then data may become inconsistent since writing only allow to unset bits in the flash. 
 240  * This feature may prevent this... 
 243 #ifndef CONFIG_NO__NEED_WATCHDOG 
 244 #       define NEED_WATCHDOG            1 
 246 #       define NEED_WATCHDOG            0 
 248 /* ATTANTION: This macro MUST BE 1, if the MCU has reset enabled watchdog (WDTON is 0). 
 249  * If this macro is defined to 1, the bootloader implements an additional "wdt_disable()" 
 250  * after its contional entry point. 
 251  * If the used MCU is fused not to enable watchdog after reset (WDTON is 1 - safty level 1) 
 252  * then "NEED_WATCHDOG" may be deactivated in order to save some memory. 
 255 #ifndef CONFIG_NO__PRECISESLEEP 
 256 #       define HAVE_UNPRECISEWAIT       0 
 258 #       define HAVE_UNPRECISEWAIT       1 
 260 /* This macro enables hand-optimized assembler code 
 261  * instead to use _sleep_ms for delaying USB enumeration. 
 262  * Because normally these timings do not need to be exact, 
 263  * the optimized assembler code does not need to be precise. 
 264  * Therefore it is very small, which saves some PROGMEM bytes! 
 267 #ifndef CONFIG_NO__FLASH_BYTE_READACCESS 
 268 #       define HAVE_FLASH_BYTE_READACCESS       1 
 270 #       define HAVE_FLASH_BYTE_READACCESS       0 
 272 /* If HAVE_FLASH_BYTE_READACCESS is defined to 1, byte mode access to FLASH is 
 273  * compiled in. Byte mode sometimes might be used by some programming softwares 
 274  * (avrdude in terminal mode). Without this feature the device would return "0" 
 275  * instead the right content of the flash memory. 
 278 //#define SIGNATURE_BYTES             0x1e, 0x93, 0x07, 0     /* ATMega8 */ 
 279 /* This macro defines the signature bytes returned by the emulated USBasp to 
 280  * the programmer software. They should match the actual device at least in 
 281  * memory size and features. If you don't define this, values for ATMega8, 
 282  * ATMega88, ATMega168 and ATMega328 are guessed correctly. 
 286 /* ------------------------------------------------------------------------- */ 
 288 /* Example configuration: Port D bit 3 is connected to a jumper which ties 
 289  * this pin to GND if the boot loader is requested. Initialization allows 
 290  * several clock cycles for the input voltage to stabilize before 
 291  * bootLoaderCondition() samples the value. 
 292  * We use a function for bootLoaderInit() for convenience and a macro for 
 293  * bootLoaderCondition() for efficiency. 
 296 #ifndef __ASSEMBLER__   /* assembler cannot parse function definitions */ 
 298 #ifndef MCUCSR          /* compatibility between ATMega8 and ATMega88 */ 
 299 #   define MCUCSR   MCUSR 
 302 static inline void  bootLoaderInit(void) 
 304     PIN_DDR(JUMPER_PORT
)  = 0; 
 305     PIN_PORT(JUMPER_PORT
) = (1<< PIN(JUMPER_PORT
, JUMPER_BIT
)); /* activate pull-up */ 
 307 //     deactivated by Stephan - reset after each avrdude op is annoing! 
 308 //     if(!(MCUCSR & (1 << EXTRF)))    /* If this was not an external reset, ignore */ 
 309 //         leaveBootloader(); 
 311     MCUCSR 
= 0;                     /* clear all reset flags for next time */ 
 314 static inline void  bootLoaderExit(void) 
 316     PIN_PORT(JUMPER_PORT
) = 0;          /* undo bootLoaderInit() changes */ 
 319 #define bootLoaderCondition()           ((PIN_PIN(JUMPER_PORT) & (1 << PIN(JUMPER_PORT, JUMPER_BIT))) == 0) 
 321 #endif /* __ASSEMBLER__ */ 
 323 /* ------------------------------------------------------------------------- */ 
 325 #endif /* __bootloader_h_included__ */