@@ -81,15 +81,37 @@ void Application_Jump_Check(void)
8181 bool JumpToApplication = false;
8282
8383 #if (BOARD == BOARD_LEONARDO )
84- /* Enable pull-up on the IO13 pin so we can use it to select the mode */
85- PORTC |= (1 << 7 );
86- Delay_MS (10 );
84+ /* First case: external reset, bootKey NOT in memory. We'll put the bootKey in memory, then spin
85+ * our wheels for about 1000ms, then proceed to the sketch, if there is one. If, during that 1000ms,
86+ * another external reset occurs, on the next pass through this decision tree, execution will fall
87+ * through to the bootloader. */
88+ if ((mcusr_state & (1 << EXTRF ))) {
89+ if (MagicBootKey != MAGIC_BOOT_KEY )
90+ {
91+ /* Set Bootkey and give the user a few ms to repress and enter bootloader mode */
92+ MagicBootKey = MAGIC_BOOT_KEY ;
93+
94+ /* Wait for a possible double tab */
95+ _delay_ms (1000 );
8796
88- /* If IO13 is not jumpered to ground , start the user application instead */
89- JumpToApplication = (( PINC & ( 1 << 7 )) != 0 ) ;
97+ /* User was too slow/normal reset , start sketch now */
98+ MagicBootKey = 0 ;
9099
91- /* Disable pull-up after the check has completed */
92- PORTC &= ~(1 << 7 );
100+ /* Single rab reset, start sketch */
101+ JumpToApplication = true;
102+ }
103+ }
104+
105+ /* On a power-on reset, we ALWAYS want to go to the sketch if there is one. */
106+ else if ((mcusr_state & (1 << PORF ))) {
107+ JumpToApplication = true;
108+ }
109+
110+ /* On a watchdog reset, if the bootKey isn't set, and there's a sketch, we should just
111+ * go straight to the sketch. */
112+ else if ((mcusr_state & (1 << WDRF )) && (MagicBootKey != MAGIC_BOOT_KEY )) {
113+ JumpToApplication = true;
114+ }
93115 #elif ((BOARD == BOARD_XPLAIN ) || (BOARD == BOARD_XPLAIN_REV1 ))
94116 /* Disable JTAG debugging */
95117 JTAG_DISABLE ();
@@ -158,8 +180,10 @@ int main(void)
158180 /* Disconnect from the host - USB interface will be reset later along with the AVR */
159181 USB_Detach ();
160182
161- /* Unlock the forced application start mode of the bootloader if it is restarted */
162- MagicBootKey = MAGIC_BOOT_KEY ;
183+ #if (BOARD != BOARD_LEONARDO )
184+ /* Unlock the forced application start mode of the bootloader if it is restarted */
185+ MagicBootKey = MAGIC_BOOT_KEY ;
186+ #endif
163187
164188 /* Enable the watchdog and force a timeout to reset the AVR */
165189 wdt_enable (WDTO_250MS );
0 commit comments