* Author: Christian Starkjohann
* Author: Stephan Baerwolf
* Creation Date: 2007-12-08
- * Modification Date: 2012-11-10
+ * Modification Date: 2013-03-31
* Tabsize: 4
* Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt)
- * This Revision: $Id: bootloaderconfig.h 729 2009-03-20 09:03:58Z cs $
*/
#ifndef __bootloaderconfig_h_included__
/* 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.
*/
+#ifndef USB_CFG_INTPORT_BIT
+ #if (defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__))
+ #define USB_CFG_INTPORT_BIT 0
+ #else
+ #define USB_CFG_INTPORT_BIT 2
+ #endif
+#endif
+/* Not all devices have their INT0 on PD2.
+ * Since "INT0" and "USB_CFG_DPLUS_BIT" should get the same signals,
+ * map them to be ideally the same:
+ * So abstract "USB_CFG_DPLUS_BIT" to this one here.
+ */
+
#ifndef USB_CFG_DMINUS_BIT
- #define USB_CFG_DMINUS_BIT 6 /* old value was 4 */
+ /* This is Revision 3 and later (where PD6 and PD7 were swapped */
+ #define USB_CFG_DMINUS_BIT 7 /* Rev.2 and previous was 6 */
#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.
*/
#ifndef USB_CFG_DPLUS_BIT
- #define USB_CFG_DPLUS_BIT 2
+ #define USB_CFG_DPLUS_BIT USB_CFG_INTPORT_BIT
#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
* jumper is connected to this port
*/
#ifndef JUMPER_BIT
- #define JUMPER_BIT 7 /* old value was 0 */
+ /* This is Revision 3 and later (where PD6 and PD7 were swapped */
+ #define JUMPER_BIT 6 /* Rev.2 and previous was 7 */
#endif
/*
* jumper is connected to this bit in port "JUMPER_PORT", active low
* WITH REQUESTING A MAGIC YOU AGREE TO PUBLISHED YOUR DATA SEND WITHIN THE REQUEST
*/
-#define HAVE_EEPROM_PAGED_ACCESS 1
+#ifndef CONFIG_NO__EEPROM_PAGED_ACCESS
+# define HAVE_EEPROM_PAGED_ACCESS 1
+#else
+# define HAVE_EEPROM_PAGED_ACCESS 0
+#endif
/* If HAVE_EEPROM_PAGED_ACCESS is defined to 1, page mode access to EEPROM is
* compiled in. Whether page mode or byte mode access is used by AVRDUDE
* depends on the target device. Page mode is only used if the device supports
* it, e.g. for the ATMega88, 168 etc. You can save quite a bit of memory by
* disabling page mode EEPROM access. Costs ~ 138 bytes.
*/
-#define HAVE_EEPROM_BYTE_ACCESS 1
+
+#ifndef CONFIG_NO__EEPROM_BYTE_ACCESS
+# define HAVE_EEPROM_BYTE_ACCESS 1
+#else
+# define HAVE_EEPROM_BYTE_ACCESS 0
+#endif
/* If HAVE_EEPROM_BYTE_ACCESS is defined to 1, byte mode access to EEPROM is
* compiled in. Byte mode is only used if the device (as identified by its
* signature) does not support page mode for EEPROM. It is required for
*/
#ifndef CONFIG_NO__BOOTLOADER_CAN_EXIT
- #define BOOTLOADER_CAN_EXIT 1
+# define BOOTLOADER_CAN_EXIT 1
#else
- #define BOOTLOADER_CAN_EXIT 0
+# define BOOTLOADER_CAN_EXIT 0
#endif
/* If this macro is defined to 1, the boot loader will exit shortly after the
* programmer closes the connection to the device. Costs extra bytes.
*/
-#define HAVE_CHIP_ERASE 0
+#ifndef CONFIG_NO__CHIP_ERASE
+# define HAVE_CHIP_ERASE 1
+#else
+# define HAVE_CHIP_ERASE 0
+#endif
/* 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__ONDEMAND_PAGEERASE
+# define HAVE_ONDEMAND_PAGEERASE 1
+#else
+# define HAVE_ONDEMAND_PAGEERASE 0
+#endif
+/* Even if "HAVE_CHIP_ERASE" is avtivated - enabling the "HAVE_ONDEMAND_PAGEERASE"-
+ * feature the bootloader will erase pages on demand short before writing new data
+ * to it.
+ * If pages are not erase before reprogram (for example because user call avrdude -D)
+ * then data may become inconsistent since writing only allow to unset bits in the flash.
+ * This feature may prevent this...
+ */
#ifndef CONFIG_NO__NEED_WATCHDOG
- #define NEED_WATCHDOG 1
+# define NEED_WATCHDOG 1
#else
- #define NEED_WATCHDOG 0
+# 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()"
*/
#ifndef CONFIG_NO__PRECISESLEEP
- #define HAVE_UNPRECISEWAIT 0
+# define HAVE_UNPRECISEWAIT 0
#else
- #define HAVE_UNPRECISEWAIT 1
+# define HAVE_UNPRECISEWAIT 1
#endif
/* This macro enables hand-optimized assembler code
* instead to use _sleep_ms for delaying USB enumeration.
* Therefore it is very small, which saves some PROGMEM bytes!
*/
-#define HAVE_FLASH_BYTE_READACCESS 1
+#ifndef CONFIG_NO__FLASH_BYTE_READACCESS
+# define HAVE_FLASH_BYTE_READACCESS 1
+#else
+# define HAVE_FLASH_BYTE_READACCESS 0
+#endif
/* If HAVE_FLASH_BYTE_READACCESS is defined to 1, byte mode access to FLASH is
* compiled in. Byte mode sometimes might be used by some programming softwares
* (avrdude in terminal mode). Without this feature the device would return "0"
* instead the right content of the flash memory.
*/
+#ifdef CONFIG_USE__EXCESSIVE_ASSEMBLER
+# define USE_EXCESSIVE_ASSEMBLER 1
+#else
+# define USE_EXCESSIVE_ASSEMBLER 0
+#endif
+/* This macro enables large codeareas of hand-optimized assembler code.
+ * WARNING:
+ * It will only work properly on devices with <64k of flash memory and SRAM.
+ * Some configuration macros (when changed) may not be applied correctly
+ * (since their behaviour is raced within asm)!
+ * Nevertheless this feature saves lots of memory.
+ */
+
+#ifdef CONFIG_USE__BOOTUP_CLEARRAM
+# define USE_BOOTUP_CLEARRAM 1
+#else
+# define USE_BOOTUP_CLEARRAM 0
+#endif
+/* This macro enables some (init3) code, executed at bootup.
+ * This codefragment will safely overwrite the whole SRAM with "0"
+ * (except registers and IO), since RESET will NOT clear old RAM content.
+ */
+
+#ifdef CONFIG_NO__BOOTLOADERENTRY_FROMSOFTWARE
+# define HAVE_BOOTLOADERENTRY_FROMSOFTWARE 0
+#else
+# define HAVE_BOOTLOADERENTRY_FROMSOFTWARE 1
+#endif
+/*
+ * Enable firmware to boot the bootloader without
+ * user intervention
+ */
+
+#ifdef CONFIG_NO__BOOTLOADER_HIDDENEXITCOMMAND
+# define HAVE_BOOTLOADER_HIDDENEXITCOMMAND 0
+#else
+# define HAVE_BOOTLOADER_HIDDENEXITCOMMAND 0xff
+#endif
+/*
+ * When enabling "BOOTLOADER_HIDDENEXITCOMMAND", then
+ * sending the RAW-ISP command "0xff 0xXX 0xXX 0xXX"
+ * will cause the bootloader to start the firmware
+ * as soon as the programming software disconnects.
+ */
+
+#ifndef BOOTLOADER_LOOPCYCLES_TIMEOUT
+# define BOOTLOADER_LOOPCYCLES_TIMEOUT 0
+#endif
+/*
+ * When greater than "0", "BOOTLOADER_LOOPCYCLES_TIMEOUT"
+ * defines how many 16bit loopcycles can be cycled,
+ * before bootloader times out and starts user
+ * firmware.
+ * Of course "BOOTLOADER_CAN_EXIT" must be enabled.
+ * If value is even too small, bootloader will not
+ * exit as long as bootLoaderConditionSimple stays on.
+ */
+
//#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 MCUCSR MCUSR
#endif
+/* WARNING:
+ * following commands and macros may not be evaluated properly when 'USE_EXCESSIVE_ASSEMBLER"
+ */
+
static inline void bootLoaderInit(void)
{
PIN_DDR(JUMPER_PORT) = 0;
// 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)
PIN_PORT(JUMPER_PORT) = 0; /* undo bootLoaderInit() changes */
}
-#define bootLoaderCondition() ((PIN_PIN(JUMPER_PORT) & (1 << PIN(JUMPER_PORT, JUMPER_BIT))) == 0)
+
+#define bootLoaderConditionSimple() ((PIN_PIN(JUMPER_PORT) & (1 << PIN(JUMPER_PORT, JUMPER_BIT))) == 0)
+
+#if (HAVE_BOOTLOADERENTRY_FROMSOFTWARE)
+/*
+ * How it works: The idea
+ *
+ * During normal C initialization, the stackpointer (SP) always is pointed to
+ * SRAMs end, where it grows towards RAMSTART.
+ *
+ * Check if last possible pushed address in stack is bootloaders address.
+ * Store investigation result into "__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch"
+ * Result will be "0xff" in case of mismatch.
+ */
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#define __BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS (BOOTLOADER_ADDRESS>>1)
+static volatile uint8_t __BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch __attribute__ ((section(".noinit")));
+static volatile uint8_t __BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR __attribute__ ((section(".noinit")));
+
+static inline bool bootLoaderCondition(void)
+{
+ if (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_MCUCSR & (~(_BV(WDRF)))) {
+ } else {
+ if (__BOOTLOADERENTRY_FROMSOFTWARE__bootup_RAMEND_doesmatch == (__BOOTLOADERENTRY_FROMSOFTWARE__EXPECTEDADDRESS & 0xff)) {
+ // anything else: match - the firmware is calling the bootloader
+ return true;
+ }
+ }
+ return bootLoaderConditionSimple();
+}
+#else
+#define bootLoaderCondition bootLoaderConditionSimple
+#endif
#endif /* __ASSEMBLER__ */