USBaspLoader v0.97 stable release
[pub/USBaspLoader.git] / firmware / bootloaderconfig.h
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
7 * Tabsize: 4
8 * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
9 * License: GNU GPL v2 (see License.txt)
10 */
11
12 #ifndef __bootloaderconfig_h_included__
13 #define __bootloaderconfig_h_included__
14 #include "../misc/iofixes.h"
15
16 /*
17 General Description:
18 This file (together with some settings in Makefile) configures the boot loader
19 according to the hardware.
20
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.
25
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.
30
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)
34 immediately.
35
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.
39 */
40
41 /* ---------------------------- Macro Magic ---------------------------- */
42 #define PIN_CONCAT(a,b) a ## b
43 #define PIN_CONCAT3(a,b,c) a ## b ## c
44
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)
48
49 #define PIN(a, b) PIN_CONCAT3(P, a, b)
50
51 /* ---------------------------- Hardware Config ---------------------------- */
52
53 #ifndef USB_CFG_IOPORTNAME
54 #define USB_CFG_IOPORTNAME D
55 #endif
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.
58 */
59 #ifndef USB_CFG_INTPORT_BIT
60 #if (defined(__AVR_ATmega640__) || defined (__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__))
61 #define USB_CFG_INTPORT_BIT 0
62 #else
63 #define USB_CFG_INTPORT_BIT 2
64 #endif
65 #endif
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.
70 */
71
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 */
75 #endif
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.
78 */
79 #ifndef USB_CFG_DPLUS_BIT
80 #define USB_CFG_DPLUS_BIT USB_CFG_INTPORT_BIT
81 #endif
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!
85 */
86 #ifndef JUMPER_PORT
87 #define JUMPER_PORT USB_CFG_IOPORTNAME
88 #endif
89 /*
90 * jumper is connected to this port
91 */
92 #ifndef JUMPER_BIT
93 /* This is Revision 3 and later (where PD6 and PD7 were swapped */
94 #define JUMPER_BIT 6 /* Rev.2 and previous was 7 */
95 #endif
96 /*
97 * jumper is connected to this bit in port "JUMPER_PORT", active low
98 */
99
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
106 */
107
108 /* ----------------------- Optional Hardware Config ------------------------ */
109
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.
115 */
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
119 * above for details.
120 */
121
122 /* ------------------------------------------------------------------------- */
123 /* ---------------------- feature / code size options ---------------------- */
124 /* ------------------------------------------------------------------------- */
125
126 #ifndef CONFIG_NO__HAVE_READ_LOCK_FUSE
127 #define HAVE_READ_LOCK_FUSE 1
128 #else
129 #define HAVE_READ_LOCK_FUSE 0
130 #endif
131 /*
132 * enable the loaders capability to load its lfuse, hfuse and lockbits
133 * ...However, programming of these is prohibited...
134 */
135
136 #ifndef CONFIG_NO__HAVE_BLB11_SOFTW_LOCKBIT
137 #define HAVE_BLB11_SOFTW_LOCKBIT 1
138 #else
139 #define HAVE_BLB11_SOFTW_LOCKBIT 0
140 #endif
141 /*
142 * The IC itself do not need to prgra BLB11, but the bootloader will avaoid
143 * to erase itself from the bootregion
144 */
145
146 #ifndef CONFIG_NO__HAVE_SPMINTEREFACE
147 #define HAVE_SPMINTEREFACE 1
148 #else
149 #define HAVE_SPMINTEREFACE 0
150 #endif
151 /*
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
158 * feature.
159 */
160
161 #define HAVE_SPMINTEREFACE_NORETMAGIC 1
162 /*
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.
165 *
166 * This feature prevents old updaters to do sth. undefined on wrong magic.
167 */
168
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!
176 *
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,
188 * 0xffffffff
189 *
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
193 * - your name
194 * - your e-mail
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")
198 *
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
202 *
203 *
204 * ***************************************************************************
205 * ***************************************************************************
206 *
207 * Because of pressing interest there will be following free-for-use scheme.
208 * It will always start with 0xf as the 4 most significant bits, and then
209 * can be encoded based of the signature of the AVR, the IO-Port used for USB,
210 * the USB dataline PINs, the external interrupt used for USB, the clock frequency
211 * the BOD voltage and if the watchdog is enabled by fuses.
212 *
213 * The magic will be: 0xf??????? in binary 0b1111ZZZZ YYYYYYYY WXXXVUUU TTSSSRRR
214 *
215 * --> where the 4 bit of Z correspond to the middle of the 3 byte AVR signature:
216 * SIGNATURE_1 == 0x90 <--> ZZZZ == 0b0000
217 * SIGNATURE_1 == 0x91 <--> ZZZZ == 0b0001
218 * SIGNATURE_1 == 0x92 <--> ZZZZ == 0b0010
219 * SIGNATURE_1 == 0x93 <--> ZZZZ == 0b0011
220 * SIGNATURE_1 == 0x94 <--> ZZZZ == 0b0100
221 * SIGNATURE_1 == 0x95 <--> ZZZZ == 0b0101
222 * SIGNATURE_1 == 0x96 <--> ZZZZ == 0b0110
223 * SIGNATURE_1 == 0x97 <--> ZZZZ == 0b0111
224 * SIGNATURE_1 == 0x98 <--> ZZZZ == 0b1000
225 * SIGNATURE_1 == 0xA6 <--> ZZZZ == 0b1001
226 * SIGNATURE_1 == 0xA7 <--> ZZZZ == 0b1010 *
227 * SIGNATURE_1 == 0xA8 <--> ZZZZ == 0b1011
228 * reserved ZZZZ == 0b1100 *
229 * not free (database magics) ZZZZ == 0b1101 *
230 * not free (database magics) ZZZZ == 0b1110 *
231 * not free (database magics) ZZZZ == 0b1111 *
232 * If the AVRs SIGNATURE_1 is different from the presented 12, this coding scheme is
233 * not applicable for you - please ask for a personal magic in this case.
234 *
235 *
236 * --> where the 8 bit of Y are the SIGNATURE_2 byte (for example 0b00000111 on ATmega8)
237 *
238 *
239 * --> where the 1 bit of W is watchdog enabled by fuse: 0=no, 1=yes (WD enabled)
240 *
241 *
242 * --> where the 3 bit of X are the (external) crystal clockspeed
243 * 12MHz external crystal <--> XXX == 0b000
244 * 12.8MHz external crystal <--> XXX == 0b001
245 * reserved <--> XXX == 0b010
246 * 15MHz external crystal <--> XXX == 0b011
247 * 16MHz external crystal <--> XXX == 0b100
248 * 16.5MHz external crystal <--> XXX == 0b101
249 * 18MHz external crystal <--> XXX == 0b110
250 * 20MHz external crystal <--> XXX == 0b111
251 * If your clockspeed is different from the presented 8, this coding scheme is
252 * not applicable for you - please ask for a personal magic in this case.
253 *
254 *
255 * --> where the 1 bit of V depends on the controllers Vcc:
256 * 4.5V (ususally 5V) <--> V == 0
257 * 3.3V <--> V == 1
258 *
259 *
260 * --> where the 3 bit of U define the PORT (USB_CFG_IOPORTNAME) for USB
261 * PORTA <--> UUU == 0b000
262 * PORTB <--> UUU == 0b001
263 * PORTC <--> UUU == 0b010
264 * PORTD <--> UUU == 0b011
265 * PORTE <--> UUU == 0b100
266 * PORTF <--> UUU == 0b101
267 * PORTG <--> UUU == 0b110
268 * PORTH <--> UUU == 0b111
269 * If your PORT is different from the presented 8:
270 * This coding scheme is more an example. UUU=0b000 really means, the first GPIO port
271 * on the specified MCU. (0b001 is the second, 0b010 the third...)
272 * Normally there aren't more then 8 GPIO ports on an AVR - so this coding works.
273 * If there are more then 8, only the first 8 ports can be encoded and otherwise
274 * this coding scheme is not applicable for you - please ask for a personal magic in this case.
275 *
276 *
277 * --> where the 2 bit of T define the external interrupt used for USBD
278 * INT0 <--> TT == 0b00
279 * INT1 <--> TT == 0b01
280 * INT2 <--> TT == 0b10
281 * INT3 <--> TT == 0b11
282 * If you use a different interrupt, this coding scheme is not applicable
283 * for you - please ask for a personal magic in this case.
284 *
285 *
286 * --> where the 3 bit of S define the PIN (on the previous encoded port) for USB+
287 * (basically the value of USB_CFG_DPLUS_BIT)
288 * 0 <--> SSS == 0b000
289 * 1 <--> SSS == 0b001
290 * ...
291 * 7 <--> SSS == 0b111
292 *
293 *
294 * --> where the 3 bit of R define the PIN (on the previous encoded port) for USB-
295 * (basically the value of USB_CFG_DMINUS_BIT)
296 * 0 <--> RRR == 0b000
297 * 1 <--> RRR == 0b001
298 * ...
299 * 7 <--> RRR == 0b111
300 *
301 *
302 * WARNING:
303 * Please note, that this 0xf------- magic does not include
304 * any information about the bootloader condition pin (and port)!
305 * This magic just ensures basic protection against different
306 * layouts. (Which only covers essential data)
307 *
308 * ?Maybe as a heuristic?:
309 * The bootloader condition pin should be the same port as USB,
310 * at the pin before USB_CFG_DMINUS_BIT.
311 *
312 * If you want to be very safe, request your own magic...
313 *
314 *
315 *
316 * Examples (USB pinning are defaults from this file):
317 *
318 * "ATmega8@16MHz powered with 5V and no watchdog fused": 0xf3078317
319 * The same just for ATmega328p: 0xf50f8317
320 *
321 * "ATmega1024p @ 20MHz powered with 3V and watchdog fused active": 0xf705fb17
322 * "ATmega2560 @ 12MHz powered with 5V and watchdog fused active": 0xf8010307
323 *
324 * ***************************************************************************
325 * ***************************************************************************
326 *
327 */
328
329 #ifndef CONFIG_NO__EEPROM_PAGED_ACCESS
330 # define HAVE_EEPROM_PAGED_ACCESS 1
331 #else
332 # define HAVE_EEPROM_PAGED_ACCESS 0
333 #endif
334 /* If HAVE_EEPROM_PAGED_ACCESS is defined to 1, page mode access to EEPROM is
335 * compiled in. Whether page mode or byte mode access is used by AVRDUDE
336 * depends on the target device. Page mode is only used if the device supports
337 * it, e.g. for the ATMega88, 168 etc. You can save quite a bit of memory by
338 * disabling page mode EEPROM access. Costs ~ 138 bytes.
339 */
340
341 #ifndef CONFIG_NO__EEPROM_BYTE_ACCESS
342 # define HAVE_EEPROM_BYTE_ACCESS 1
343 #else
344 # define HAVE_EEPROM_BYTE_ACCESS 0
345 #endif
346 /* If HAVE_EEPROM_BYTE_ACCESS is defined to 1, byte mode access to EEPROM is
347 * compiled in. Byte mode is only used if the device (as identified by its
348 * signature) does not support page mode for EEPROM. It is required for
349 * accessing the EEPROM on the ATMega8. Costs ~54 bytes.
350 */
351
352 #ifndef CONFIG_NO__BOOTLOADER_CAN_EXIT
353 # define BOOTLOADER_CAN_EXIT 1
354 #else
355 # define BOOTLOADER_CAN_EXIT 0
356 #endif
357 /* If this macro is defined to 1, the boot loader will exit shortly after the
358 * programmer closes the connection to the device. Costs extra bytes.
359 */
360
361 #ifndef CONFIG_NO__CHIP_ERASE
362 # define HAVE_CHIP_ERASE 1
363 #else
364 # define HAVE_CHIP_ERASE 0
365 #endif
366 /* If this macro is defined to 1, the boot loader implements the Chip Erase
367 * ISP command. Otherwise pages are erased on demand before they are written.
368 */
369 #ifndef CONFIG_NO__ONDEMAND_PAGEERASE
370 # define HAVE_ONDEMAND_PAGEERASE 1
371 #else
372 # define HAVE_ONDEMAND_PAGEERASE 0
373 #endif
374 /* Even if "HAVE_CHIP_ERASE" is avtivated - enabling the "HAVE_ONDEMAND_PAGEERASE"-
375 * feature the bootloader will erase pages on demand short before writing new data
376 * to it.
377 * If pages are not erase before reprogram (for example because user call avrdude -D)
378 * then data may become inconsistent since writing only allow to unset bits in the flash.
379 * This feature may prevent this...
380 */
381
382 #ifndef CONFIG_NO__NEED_WATCHDOG
383 # define NEED_WATCHDOG 1
384 #else
385 # define NEED_WATCHDOG 0
386 #endif
387 /* ATTANTION: This macro MUST BE 1, if the MCU has reset enabled watchdog (WDTON is 0).
388 * If this macro is defined to 1, the bootloader implements an additional "wdt_disable()"
389 * after its contional entry point.
390 * If the used MCU is fused not to enable watchdog after reset (WDTON is 1 - safty level 1)
391 * then "NEED_WATCHDOG" may be deactivated in order to save some memory.
392 */
393
394 #ifndef CONFIG_NO__PRECISESLEEP
395 # define HAVE_UNPRECISEWAIT 0
396 #else
397 # define HAVE_UNPRECISEWAIT 1
398 #endif
399 /* This macro enables hand-optimized assembler code
400 * instead to use _sleep_ms for delaying USB enumeration.
401 * Because normally these timings do not need to be exact,
402 * the optimized assembler code does not need to be precise.
403 * Therefore it is very small, which saves some PROGMEM bytes!
404 */
405
406 #ifndef CONFIG_NO__FLASH_BYTE_READACCESS
407 # define HAVE_FLASH_BYTE_READACCESS 1
408 #else
409 # define HAVE_FLASH_BYTE_READACCESS 0
410 #endif
411 /* If HAVE_FLASH_BYTE_READACCESS is defined to 1, byte mode access to FLASH is
412 * compiled in. Byte mode sometimes might be used by some programming softwares
413 * (avrdude in terminal mode). Without this feature the device would return "0"
414 * instead the right content of the flash memory.
415 */
416
417 #ifdef CONFIG_USE__EXCESSIVE_ASSEMBLER
418 # define USE_EXCESSIVE_ASSEMBLER 1
419 #else
420 # define USE_EXCESSIVE_ASSEMBLER 0
421 #endif
422 /* This macro enables large codeareas of hand-optimized assembler code.
423 * WARNING:
424 * It will only work properly on devices with <64k of flash memory and SRAM.
425 * Some configuration macros (when changed) may not be applied correctly
426 * (since their behaviour is raced within asm)!
427 * Nevertheless this feature saves lots of memory.
428 */
429
430 #ifdef CONFIG_USE__BOOTUP_CLEARRAM
431 # define USE_BOOTUP_CLEARRAM 1
432 #else
433 # define USE_BOOTUP_CLEARRAM 0
434 #endif
435 /* This macro enables some (init3) code, executed at bootup.
436 * This codefragment will safely overwrite the whole SRAM with "0"
437 * (except registers and IO), since RESET will NOT clear old RAM content.
438 */
439
440 #ifdef CONFIG_NO__BOOTLOADERENTRY_FROMSOFTWARE
441 # define HAVE_BOOTLOADERENTRY_FROMSOFTWARE 0
442 #else
443 # define HAVE_BOOTLOADERENTRY_FROMSOFTWARE 1
444 #endif
445 /*
446 * Enable firmware to boot the bootloader without
447 * user intervention
448 */
449
450 #ifdef CONFIG_NO__BOOTLOADER_HIDDENEXITCOMMAND
451 # define HAVE_BOOTLOADER_HIDDENEXITCOMMAND 0
452 #else
453 # define HAVE_BOOTLOADER_HIDDENEXITCOMMAND 0xff
454 #endif
455 /*
456 * When enabling "BOOTLOADER_HIDDENEXITCOMMAND", then
457 * sending the RAW-ISP command "0xff 0xXX 0xXX 0xXX"
458 * will cause the bootloader to start the firmware
459 * as soon as the programming software disconnects.
460 */
461
462 #ifdef CONFIG_BOOTLOADER_LOOPCYCLES_TIMEOUT
463 # define BOOTLOADER_LOOPCYCLES_TIMEOUT (CONFIG_BOOTLOADER_LOOPCYCLES_TIMEOUT)
464 #else
465 # define BOOTLOADER_LOOPCYCLES_TIMEOUT (0)
466 #endif
467 /*
468 * When greater than "0", "BOOTLOADER_LOOPCYCLES_TIMEOUT"
469 * defines how many 16bit loopcycles can be cycled,
470 * before bootloader times out and starts user
471 * firmware.
472 * Of course "BOOTLOADER_CAN_EXIT" must be enabled.
473 * If value is even too small, bootloader will not
474 * exit as long as bootLoaderConditionSimple stays on.
475 */
476
477 #ifdef CONFIG_HAVE__BOOTLOADER_ABORTTIMEOUTONACT
478 #endif
479 /*
480 * When defined, the bootloader will abort the timeout when
481 * it sees some activity (bootLoaderConditionSimple() or
482 * programming).
483 * After aborting timeout, the bootloader falls back to
484 * conventional exitting.
485 */
486
487 #ifdef CONFIG_HAVE__BOOTLOADER_ALWAYSENTERPROGRAMMODE
488 # if (BOOTLOADER_CAN_EXIT)
489 # define BOOTLOADER_ALWAYSENTERPROGRAMMODE 1
490 # else
491 # define BOOTLOADER_ALWAYSENTERPROGRAMMODE 0
492 # endif
493 #else
494 # define BOOTLOADER_ALWAYSENTERPROGRAMMODE 0
495 #endif
496 /*
497 * Ignore bootLoaderCondition() (BUT NOT bootLoaderConditionSimple())
498 * and always enter the program-mode.
499 * This is helpful to emulate behaviour of Arduino bootloaders
500 *
501 * However, this feature may be dangerous, since bootloader may never exit.
502 * So it is enabled, only if "CONFIG_NO__BOOTLOADER_CAN_EXIT" is disabled.
503 *
504 */
505
506 #ifdef CONFIG_HAVE__BOOTLOADER_IGNOREPROGBUTTON
507 # if ( (BOOTLOADER_ALWAYSENTERPROGRAMMODE) && (defined(BOOTLOADER_CAN_EXIT)) && (BOOTLOADER_LOOPCYCLES_TIMEOUT >= 8) )
508 # define BOOTLOADER_IGNOREPROGBUTTON 1
509 # else
510 # define BOOTLOADER_IGNOREPROGBUTTON 0
511 # endif
512 #endif
513 /*
514 * Generates an USBaspLoader without using the PROGBUTTON.
515 * It can be used to reduce the required PINcount for USBaspLoader
516 * on the MCU.
517 * However this feature is very dangerous, so it becomes only
518 * enabled, if "CONFIG_HAVE__BOOTLOADER_ALWAYSENTERPROGRAMMODE" is
519 * enabled and "CONFIG_NO__BOOTLOADER_CAN_EXIT" is disabled, too.
520 * Additionally "BOOTLOADER_LOOPCYCLES_TIMEOUT" must be greater
521 * or equal than 8 (In order to give user enough time to program).
522 *
523 * When active, "JUMPER_PORT" and "JUMPER_BIT" are ignored and
524 * can be soldered otherwise.
525 */
526
527 #ifdef CONFIG_NO__BOOTLOADER_ADDITIONALDEVICEWAIT
528 # define HAVE_BOOTLOADER_ADDITIONALMSDEVICEWAIT 0
529 #else
530 # define HAVE_BOOTLOADER_ADDITIONALMSDEVICEWAIT 50
531 #endif
532 /*
533 * When enabling "HAVE_BOOTLOADER_ADDITIONALMSDEVICEWAIT", then
534 * the bootloader will alway delay its bootup by the configured
535 * number of milliseconds.
536 * This gives the pullups additional time to charge up.
537 */
538
539 //#define SIGNATURE_BYTES 0x1e, 0x93, 0x07, 0 /* ATMega8 */
540 /* This macro defines the signature bytes returned by the emulated USBasp to
541 * the programmer software. They should match the actual device at least in
542 * memory size and features. If you don't define this, values for ATMega8,
543 * ATMega88, ATMega168 and ATMega328 are guessed correctly.
544 */
545
546
547 /* ------------------------------------------------------------------------- */
548
549 /* Example configuration: Port D bit 3 is connected to a jumper which ties
550 * this pin to GND if the boot loader is requested. Initialization allows
551 * several clock cycles for the input voltage to stabilize before
552 * bootLoaderCondition() samples the value.
553 * We use a function for bootLoaderInit() for convenience and a macro for
554 * bootLoaderCondition() for efficiency.
555 */
556
557 #ifndef __ASSEMBLER__ /* assembler cannot parse function definitions */
558
559 #ifndef MCUCSR /* compatibility between ATMega8 and ATMega88 */
560 # define MCUCSR MCUSR
561 #endif
562
563 /* WARNING:
564 * following commands and macros may not be evaluated properly when 'USE_EXCESSIVE_ASSEMBLER"
565 */
566
567 static inline void bootLoaderInit(void)
568 {
569 #if (BOOTLOADER_IGNOREPROGBUTTON)
570 #else
571 PIN_DDR(JUMPER_PORT) = 0;
572 PIN_PORT(JUMPER_PORT) = (1<< PIN(JUMPER_PORT, JUMPER_BIT)); /* activate pull-up */
573 #endif
574
575 // deactivated by Stephan - reset after each avrdude op is annoing!
576 // if(!(MCUCSR & (1 << EXTRF))) /* If this was not an external reset, ignore */
577 // leaveBootloader();
578 }
579
580 static inline void bootLoaderExit(void)
581 {
582 #if (BOOTLOADER_IGNOREPROGBUTTON)
583 #else
584 PIN_PORT(JUMPER_PORT) = 0; /* undo bootLoaderInit() changes */
585 #endif
586 }
587
588
589 #if (BOOTLOADER_IGNOREPROGBUTTON)
590 # define bootLoaderConditionSimple() (false)
591 #else
592 # define bootLoaderConditionSimple() ((PIN_PIN(JUMPER_PORT) & (1 << PIN(JUMPER_PORT, JUMPER_BIT))) == 0)
593 #endif
594
595 #if (HAVE_BOOTLOADERENTRY_FROMSOFTWARE)
596 /*
597 * How it works: The idea
598 *
599 * During normal C initialization, the stackpointer (SP) always is pointed to
600 * SRAMs end, where it grows towards RAMSTART.
601 *
602 * Check if last possible pushed address in stack is bootloaders address.
603 * Store investigation result into "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch"
604 * Result will be "0xff" in case of mismatch.
605 */
606
607 #include <stdbool.h>
608 #include <stdint.h>
609
610 #define __BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS (BOOTLOADER_ADDRESS>>1)
611 static volatile uint8_t __BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch __attribute__ ((section(".noinit")));
612 static volatile uint8_t __BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR __attribute__ ((section(".noinit")));
613
614 # if (BOOTLOADER_ALWAYSENTERPROGRAMMODE)
615 # define bootLoaderCondition() (true)
616 # else
617 static inline bool bootLoaderCondition(void)
618 {
619 if (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR & (~(_BV(WDRF)))) {
620 } else {
621 if (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch == (__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS & 0xff)) {
622 // anything else: match - the firmware is calling the bootloader
623 return true;
624 }
625 }
626 return bootLoaderConditionSimple();
627 }
628 # endif
629 #else
630 # if (BOOTLOADER_ALWAYSENTERPROGRAMMODE)
631 # define bootLoaderCondition() (true)
632 # else
633 # define bootLoaderCondition bootLoaderConditionSimple
634 # endif
635 #endif
636
637 #endif /* __ASSEMBLER__ */
638
639 /* ------------------------------------------------------------------------- */
640
641 #endif /* __bootloader_h_included__ */