@@ -72,7 +72,17 @@ void Application_Jump_Check(void)
7272{
7373 bool JumpToApplication = false;
7474
75- #if ((BOARD == BOARD_XPLAIN ) || (BOARD == BOARD_XPLAIN_REV1 ))
75+ #if (BOARD == BOARD_LEONARDO )
76+ /* Enable pull-up on the IO13 pin so we can use it to select the mode */
77+ PORTC |= (1 << 7 );
78+ Delay_MS (10 );
79+
80+ /* If IO13 is not jumpered to ground, start the user application instead */
81+ JumpToApplication |= ((PINC & (1 << 7 )) != 0 );
82+
83+ /* Disable pull-up after the check has completed */
84+ PORTC &= ~(1 << 7 );
85+ #elif ((BOARD == BOARD_XPLAIN ) || (BOARD == BOARD_XPLAIN_REV1 ))
7686 /* Disable JTAG debugging */
7787 JTAG_DISABLE ();
7888
@@ -87,19 +97,33 @@ void Application_Jump_Check(void)
8797 JTAG_ENABLE ();
8898 #endif
8999
90- /* If the reset source was the bootloader and the key is correct, clear it and jump to the application */
91- if ((MCUSR & (1 << WDRF )) && (MagicBootKey == MAGIC_BOOT_KEY ))
92- JumpToApplication |= true;
100+ /* Check if the device's BOOTRST fuse is set (bootloader is run after a device reset) */
101+ if (boot_lock_fuse_bits_get (GET_HIGH_FUSE_BITS ) & FUSE_BOOTRST )
102+ {
103+ /* If the reset source was not an external reset or the key is correct, clear it and jump to the application */
104+ if ((MCUSR & (1 << EXTRF )) || (MagicBootKey == MAGIC_BOOT_KEY ))
105+ {
106+ MCUSR &= ~(1 << EXTRF );
107+ JumpToApplication |= true;
108+ }
109+ }
110+ else
111+ {
112+ /* If the reset source was the watchdog and the key is correct, clear it and jump to the application */
113+ if ((MCUSR & (1 << WDRF )) && (MagicBootKey == MAGIC_BOOT_KEY ))
114+ {
115+ MCUSR &= ~(1 << WDRF );
116+ JumpToApplication |= true;
117+ }
118+ }
93119
94120 /* Don't run the user application if the reset vector is blank (no app loaded) */
95- if (pgm_read_word_near (0 ) == 0xFFFF )
96- JumpToApplication = false;
121+ bool ApplicationValid = (pgm_read_word_near (0 ) != 0xFFFF );
97122
98123 /* If a request has been made to jump to the user application, honor it */
99- if (JumpToApplication )
124+ if (JumpToApplication && ApplicationValid )
100125 {
101126 /* Turn off the watchdog */
102- MCUSR &= ~(1 <<WDRF );
103127 wdt_disable ();
104128
105129 /* Clear the boot key and jump to the user application */
0 commit comments