Skip to content

Commit

Permalink
Made CDC Bootloader Arduino compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoHood committed May 22, 2016
1 parent 36f4686 commit 123b591
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions Bootloaders/CDC/BootloaderCDC.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,37 @@ void Application_Jump_Check(void)
bool JumpToApplication = false;

#if (BOARD == BOARD_LEONARDO)
/* Enable pull-up on the IO13 pin so we can use it to select the mode */
PORTC |= (1 << 7);
Delay_MS(10);
/* First case: external reset, bootKey NOT in memory. We'll put the bootKey in memory, then spin
* our wheels for about 1000ms, then proceed to the sketch, if there is one. If, during that 1000ms,
* another external reset occurs, on the next pass through this decision tree, execution will fall
* through to the bootloader. */
if ((mcusr_state & (1 << EXTRF))) {
if (MagicBootKey != MAGIC_BOOT_KEY)
{
/* Set Bootkey and give the user a few ms to repress and enter bootloader mode */
MagicBootKey = MAGIC_BOOT_KEY;

/* Wait for a possible double tab */
_delay_ms(1000);

/* If IO13 is not jumpered to ground, start the user application instead */
JumpToApplication = ((PINC & (1 << 7)) != 0);
/* User was too slow/normal reset, start sketch now */
MagicBootKey = 0;

/* Disable pull-up after the check has completed */
PORTC &= ~(1 << 7);
/* Single rab reset, start sketch */
JumpToApplication = true;
}
}

/* On a power-on reset, we ALWAYS want to go to the sketch if there is one. */
else if ((mcusr_state & (1 << PORF))) {
JumpToApplication = true;
}

/* On a watchdog reset, if the bootKey isn't set, and there's a sketch, we should just
* go straight to the sketch. */
else if ((mcusr_state & (1 << WDRF)) && (MagicBootKey != MAGIC_BOOT_KEY)) {
JumpToApplication = true;
}
#elif ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
/* Disable JTAG debugging */
JTAG_DISABLE();
Expand Down Expand Up @@ -158,8 +180,10 @@ int main(void)
/* Disconnect from the host - USB interface will be reset later along with the AVR */
USB_Detach();

/* Unlock the forced application start mode of the bootloader if it is restarted */
MagicBootKey = MAGIC_BOOT_KEY;
#if (BOARD != BOARD_LEONARDO)
/* Unlock the forced application start mode of the bootloader if it is restarted */
MagicBootKey = MAGIC_BOOT_KEY;
#endif

/* Enable the watchdog and force a timeout to reset the AVR */
wdt_enable(WDTO_250MS);
Expand Down

0 comments on commit 123b591

Please sign in to comment.