1 /* Name: bootloaderconfig.h
2 * Project: USBaspLoader
3 * Author: Christian Starkjohann
4 * Author: Stephan Baerwolf
5 * Creation Date: 2007-12-08
6 * Modification Date: 2013-03-31
8 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
9 * License: GNU GPL v2 (see License.txt)
12 #ifndef __bootloaderconfig_h_included__
13 #define __bootloaderconfig_h_included__
18 This file (together with some settings in Makefile) configures the boot loader
19 according to the hardware.
21 This file contains (besides the hardware configuration normally found in
22 usbconfig.h) two functions or macros: bootLoaderInit() and
23 bootLoaderCondition(). Whether you implement them as macros or as static
24 inline functions is up to you, decide based on code size and convenience.
26 bootLoaderInit() is called as one of the first actions after reset. It should
27 be a minimum initialization of the hardware so that the boot loader condition
28 can be read. This will usually consist of activating a pull-up resistor for an
29 external jumper which selects boot loader mode.
31 bootLoaderCondition() is called immediately after initialization and in each
32 main loop iteration. If it returns TRUE, the boot loader will be active. If it
33 returns FALSE, the boot loader jumps to address 0 (the loaded application)
36 For compatibility with Thomas Fischl's avrusbboot, we also support the macro
37 names BOOTLOADER_INIT and BOOTLOADER_CONDITION for this functionality. If
38 these macros are defined, the boot loader usees them.
41 /* ---------------------------- Macro Magic ---------------------------- */
42 #define PIN_CONCAT(a,b) a ## b
43 #define PIN_CONCAT3(a,b,c) a ## b ## c
45 #define PIN_PORT(a) PIN_CONCAT(PORT, a)
46 #define PIN_PIN(a) PIN_CONCAT(PIN, a)
47 #define PIN_DDR(a) PIN_CONCAT(DDR, a)
49 #define PIN(a, b) PIN_CONCAT3(P, a, b)
51 /* ---------------------------- Hardware Config ---------------------------- */
53 #ifndef USB_CFG_IOPORTNAME
54 #define USB_CFG_IOPORTNAME D
56 /* This is the port where the USB bus is connected. When you configure it to
57 * "B", the registers PORTB, PINB and DDRB will be used.
59 #ifndef USB_CFG_INTPORT_BIT
60 #if (defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__))
61 #define USB_CFG_INTPORT_BIT 0
63 #define USB_CFG_INTPORT_BIT 2
66 /* Not all devices have their INT0 on PD2.
67 * Since "INT0" and "USB_CFG_DPLUS_BIT" should get the same signals,
68 * map them to be ideally the same:
69 * So abstract "USB_CFG_DPLUS_BIT" to this one here.
72 #ifndef USB_CFG_DMINUS_BIT
73 /* This is Revision 3 and later (where PD6 and PD7 were swapped */
74 #define USB_CFG_DMINUS_BIT 7 /* Rev.2 and previous was 6 */
76 /* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
77 * This may be any bit in the port.
79 #ifndef USB_CFG_DPLUS_BIT
80 #define USB_CFG_DPLUS_BIT USB_CFG_INTPORT_BIT
82 /* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
83 * This may be any bit in the port. Please note that D+ must also be connected
84 * to interrupt pin INT0!
87 #define JUMPER_PORT USB_CFG_IOPORTNAME
90 * jumper is connected to this port
93 /* This is Revision 3 and later (where PD6 and PD7 were swapped */
94 #define JUMPER_BIT 6 /* Rev.2 and previous was 7 */
97 * jumper is connected to this bit in port "JUMPER_PORT", active low
100 #define USB_CFG_CLOCK_KHZ (F_CPU/1000)
101 /* Clock rate of the AVR in MHz. Legal values are 12000, 16000 or 16500.
102 * The 16.5 MHz version of the code requires no crystal, it tolerates +/- 1%
103 * deviation from the nominal frequency. All other rates require a precision
104 * of 2000 ppm and thus a crystal!
105 * Default if not specified: 12 MHz
108 /* ----------------------- Optional Hardware Config ------------------------ */
110 /* #define USB_CFG_PULLUP_IOPORTNAME D */
111 /* If you connect the 1.5k pullup resistor from D- to a port pin instead of
112 * V+, you can connect and disconnect the device from firmware by calling
113 * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h).
114 * This constant defines the port on which the pullup resistor is connected.
116 /* #define USB_CFG_PULLUP_BIT 4 */
117 /* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined
118 * above) where the 1.5k pullup resistor is connected. See description
122 /* ------------------------------------------------------------------------- */
123 /* ---------------------- feature / code size options ---------------------- */
124 /* ------------------------------------------------------------------------- */
126 #ifndef CONFIG_NO__HAVE_READ_LOCK_FUSE
127 #define HAVE_READ_LOCK_FUSE 1
129 #define HAVE_READ_LOCK_FUSE 0
132 * enable the loaders capability to load its lfuse, hfuse and lockbits
133 * ...However, programming of these is prohibited...
136 #ifndef CONFIG_NO__HAVE_BLB11_SOFTW_LOCKBIT
137 #define HAVE_BLB11_SOFTW_LOCKBIT 1
139 #define HAVE_BLB11_SOFTW_LOCKBIT 0
142 * The IC itself do not need to prgra BLB11, but the bootloader will avaoid
143 * to erase itself from the bootregion
146 #ifndef CONFIG_NO__HAVE_SPMINTEREFACE
147 #define HAVE_SPMINTEREFACE 1
149 #define HAVE_SPMINTEREFACE 0
152 * Since code within normal section of application memory (rww-section) is
153 * not able to call spm for programming flash-pages, this option (when
154 * enabled) will insert a small subroutine into the bootloader-section
155 * to enable applications to circumvent this limitation and make them
156 * able to program the flash in a similar way as the bootloader does, too.
157 * For further details see "spminterface.h", which implements this
161 #define HAVE_SPMINTEREFACE_NORETMAGIC 1
163 * If sth. went wrong within "bootloader__do_spm" and this macro is ACTIVATED,
164 * then "bootloader__do_spm" will not return the call and loop infinity instead.
166 * This feature prevents old updaters to do sth. undefined on wrong magic.
169 /* all boards should use a magic to make it safe to confuse updatefiles :-) */
170 #define HAVE_SPMINTEREFACE_MAGICVALUE 0
171 /* If this feature is enabled (value != 0), the configured 32bit value is
172 * used as a magic value within spminterface. "bootloader__do_spm" will check
173 * additional four (4) registers for this value and only proceed, if they contain
174 * the right value. With this feature you can identify your board and avoid
175 * updating the wrong bootloader to the wrong board!
177 * Not all values are possible - "SPMINTEREFACE_MAGICVALUE" must be very sparse!
178 * To avoid collisions, magic-values will be organized centrally by Stephan
179 * Following values are definitly blocked or reserved and must not be used:
180 * 0x00000000, 0x12345678,
181 * 0x00a500a5, 0x00a5a500, 0xa50000a5, 0xa500a500,
182 * 0x005a005a, 0x005a5a00, 0x5a00005a, 0x5a005a00,
183 * 0x5aa55aa5, 0x5aa5a55a, 0xa55a5aa5, 0xa55aa55a,
184 * 0x5a5a5a5a, 0xa5a5a5a5,
185 * 0xffa5ffa5, 0xffa5a5ff, 0xa5ffffa5, 0xa5ffa5ff,
186 * 0xff5aff5a, 0xff5a5aff, 0x5affff5a, 0x5aff5aff,
187 * 0x00ff00ff, 0x00ffff00, 0xff0000ff, 0xff00ff00,
190 * To request your own magic, please send at least following information
191 * about yourself and your board together within an informal request to:
192 * stephan@matrixstorm.com / matrixstorm@gmx.de / stephan.baerwolf@tu-ilmenau.de
195 * - your project (maybe an url?)
196 * - your type of MCU used
197 * --> your used "BOOTLOADER_ADDRESS" (since same magics can be reused for different "BOOTLOADER_ADDRESS")
199 * There may be no garanty for it, but Stephan will then send you an
200 * response with a "SPMINTEREFACE_MAGICVALUE" just for your board/project...
201 * WITH REQUESTING A MAGIC YOU AGREE TO PUBLISHED YOUR DATA SEND WITHIN THE REQUEST
204 #ifndef CONFIG_NO__EEPROM_PAGED_ACCESS
205 # define HAVE_EEPROM_PAGED_ACCESS 1
207 # define HAVE_EEPROM_PAGED_ACCESS 0
209 /* If HAVE_EEPROM_PAGED_ACCESS is defined to 1, page mode access to EEPROM is
210 * compiled in. Whether page mode or byte mode access is used by AVRDUDE
211 * depends on the target device. Page mode is only used if the device supports
212 * it, e.g. for the ATMega88, 168 etc. You can save quite a bit of memory by
213 * disabling page mode EEPROM access. Costs ~ 138 bytes.
216 #ifndef CONFIG_NO__EEPROM_BYTE_ACCESS
217 # define HAVE_EEPROM_BYTE_ACCESS 1
219 # define HAVE_EEPROM_BYTE_ACCESS 0
221 /* If HAVE_EEPROM_BYTE_ACCESS is defined to 1, byte mode access to EEPROM is
222 * compiled in. Byte mode is only used if the device (as identified by its
223 * signature) does not support page mode for EEPROM. It is required for
224 * accessing the EEPROM on the ATMega8. Costs ~54 bytes.
227 #ifndef CONFIG_NO__BOOTLOADER_CAN_EXIT
228 # define BOOTLOADER_CAN_EXIT 1
230 # define BOOTLOADER_CAN_EXIT 0
232 /* If this macro is defined to 1, the boot loader will exit shortly after the
233 * programmer closes the connection to the device. Costs extra bytes.
236 #ifndef CONFIG_NO__CHIP_ERASE
237 # define HAVE_CHIP_ERASE 1
239 # define HAVE_CHIP_ERASE 0
241 /* If this macro is defined to 1, the boot loader implements the Chip Erase
242 * ISP command. Otherwise pages are erased on demand before they are written.
244 #ifndef CONFIG_NO__ONDEMAND_PAGEERASE
245 # define HAVE_ONDEMAND_PAGEERASE 1
247 # define HAVE_ONDEMAND_PAGEERASE 0
249 /* Even if "HAVE_CHIP_ERASE" is avtivated - enabling the "HAVE_ONDEMAND_PAGEERASE"-
250 * feature the bootloader will erase pages on demand short before writing new data
252 * If pages are not erase before reprogram (for example because user call avrdude -D)
253 * then data may become inconsistent since writing only allow to unset bits in the flash.
254 * This feature may prevent this...
257 #ifndef CONFIG_NO__NEED_WATCHDOG
258 # define NEED_WATCHDOG 1
260 # define NEED_WATCHDOG 0
262 /* ATTANTION: This macro MUST BE 1, if the MCU has reset enabled watchdog (WDTON is 0).
263 * If this macro is defined to 1, the bootloader implements an additional "wdt_disable()"
264 * after its contional entry point.
265 * If the used MCU is fused not to enable watchdog after reset (WDTON is 1 - safty level 1)
266 * then "NEED_WATCHDOG" may be deactivated in order to save some memory.
269 #ifndef CONFIG_NO__PRECISESLEEP
270 # define HAVE_UNPRECISEWAIT 0
272 # define HAVE_UNPRECISEWAIT 1
274 /* This macro enables hand-optimized assembler code
275 * instead to use _sleep_ms for delaying USB enumeration.
276 * Because normally these timings do not need to be exact,
277 * the optimized assembler code does not need to be precise.
278 * Therefore it is very small, which saves some PROGMEM bytes!
281 #ifndef CONFIG_NO__FLASH_BYTE_READACCESS
282 # define HAVE_FLASH_BYTE_READACCESS 1
284 # define HAVE_FLASH_BYTE_READACCESS 0
286 /* If HAVE_FLASH_BYTE_READACCESS is defined to 1, byte mode access to FLASH is
287 * compiled in. Byte mode sometimes might be used by some programming softwares
288 * (avrdude in terminal mode). Without this feature the device would return "0"
289 * instead the right content of the flash memory.
292 #ifdef CONFIG_USE__EXCESSIVE_ASSEMBLER
293 # define USE_EXCESSIVE_ASSEMBLER 1
295 # define USE_EXCESSIVE_ASSEMBLER 0
297 /* This macro enables large codeareas of hand-optimized assembler code.
299 * It will only work properly on devices with <64k of flash memory and SRAM.
300 * Some configuration macros (when changed) may not be applied correctly
301 * (since their behaviour is raced within asm)!
302 * Nevertheless this feature saves lots of memory.
305 #ifdef CONFIG_USE__BOOTUP_CLEARRAM
306 # define USE_BOOTUP_CLEARRAM 1
308 # define USE_BOOTUP_CLEARRAM 0
310 /* This macro enables some (init3) code, executed at bootup.
311 * This codefragment will safely overwrite the whole SRAM with "0"
312 * (except registers and IO), since RESET will NOT clear old RAM content.
315 #ifdef CONFIG_NO__BOOTLOADERENTRY_FROMSOFTWARE
316 # define HAVE_BOOTLOADERENTRY_FROMSOFTWARE 0
318 # define HAVE_BOOTLOADERENTRY_FROMSOFTWARE 1
321 * Enable firmware to boot the bootloader without
325 #ifdef CONFIG_NO__BOOTLOADER_HIDDENEXITCOMMAND
326 # define HAVE_BOOTLOADER_HIDDENEXITCOMMAND 0
328 # define HAVE_BOOTLOADER_HIDDENEXITCOMMAND 0xff
331 * When enabling "BOOTLOADER_HIDDENEXITCOMMAND", then
332 * sending the RAW-ISP command "0xff 0xXX 0xXX 0xXX"
333 * will cause the bootloader to start the firmware
334 * as soon as the programming software disconnects.
337 #ifndef BOOTLOADER_LOOPCYCLES_TIMEOUT
338 # define BOOTLOADER_LOOPCYCLES_TIMEOUT 0
341 * When greater than "0", "BOOTLOADER_LOOPCYCLES_TIMEOUT"
342 * defines how many 16bit loopcycles can be cycled,
343 * before bootloader times out and starts user
345 * Of course "BOOTLOADER_CAN_EXIT" must be enabled.
346 * If value is even too small, bootloader will not
347 * exit as long as bootLoaderConditionSimple stays on.
350 //#define SIGNATURE_BYTES 0x1e, 0x93, 0x07, 0 /* ATMega8 */
351 /* This macro defines the signature bytes returned by the emulated USBasp to
352 * the programmer software. They should match the actual device at least in
353 * memory size and features. If you don't define this, values for ATMega8,
354 * ATMega88, ATMega168 and ATMega328 are guessed correctly.
358 /* ------------------------------------------------------------------------- */
360 /* Example configuration: Port D bit 3 is connected to a jumper which ties
361 * this pin to GND if the boot loader is requested. Initialization allows
362 * several clock cycles for the input voltage to stabilize before
363 * bootLoaderCondition() samples the value.
364 * We use a function for bootLoaderInit() for convenience and a macro for
365 * bootLoaderCondition() for efficiency.
368 #ifndef __ASSEMBLER__ /* assembler cannot parse function definitions */
370 #ifndef MCUCSR /* compatibility between ATMega8 and ATMega88 */
371 # define MCUCSR MCUSR
375 * following commands and macros may not be evaluated properly when 'USE_EXCESSIVE_ASSEMBLER"
378 static inline void bootLoaderInit(void)
380 PIN_DDR(JUMPER_PORT
) = 0;
381 PIN_PORT(JUMPER_PORT
) = (1<< PIN(JUMPER_PORT
, JUMPER_BIT
)); /* activate pull-up */
383 // deactivated by Stephan - reset after each avrdude op is annoing!
384 // if(!(MCUCSR & (1 << EXTRF))) /* If this was not an external reset, ignore */
385 // leaveBootloader();
388 static inline void bootLoaderExit(void)
390 PIN_PORT(JUMPER_PORT
) = 0; /* undo bootLoaderInit() changes */
394 #define bootLoaderConditionSimple() ((PIN_PIN(JUMPER_PORT) & (1 << PIN(JUMPER_PORT, JUMPER_BIT))) == 0)
396 #if (HAVE_BOOTLOADERENTRY_FROMSOFTWARE)
398 * How it works: The idea
400 * During normal C initialization, the stackpointer (SP) always is pointed to
401 * SRAMs end, where it grows towards RAMSTART.
403 * Check if last possible pushed address in stack is bootloaders address.
404 * Store investigation result into "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch"
405 * Result will be "0xff" in case of mismatch.
411 #define __BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS (BOOTLOADER_ADDRESS>>1)
412 static volatile uint8_t __BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch
__attribute__ ((section(".noinit")));
413 static volatile uint8_t __BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR
__attribute__ ((section(".noinit")));
415 static inline bool bootLoaderCondition(void)
417 if (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR
& (~(_BV(WDRF
)))) {
419 if (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch
== (__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS
& 0xff)) {
420 // anything else: match - the firmware is calling the bootloader
424 return bootLoaderConditionSimple();
427 #define bootLoaderCondition bootLoaderConditionSimple
430 #endif /* __ASSEMBLER__ */
432 /* ------------------------------------------------------------------------- */
434 #endif /* __bootloader_h_included__ */