Skip to content

Commit

Permalink
MCUSR improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoHood committed Mar 19, 2016
1 parent 0cb1ec1 commit afda49c
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions SecureLoader/BootloaderHID.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,22 @@ static bool RunBootloader = true;
uint16_t MagicBootKey ATTR_NO_INIT;


uint8_t mcusr_mirror ATTR_NO_INIT;

/** Special startup routine to check if the bootloader was started via a watchdog reset, and if the magic application
* start key has been loaded into \ref MagicBootKey. If the bootloader started via the watchdog and the key is valid,
* this will force the user application to start via a software jump.
*/
void Application_Jump_Check(void)
{
/* Turn off the watchdog */
mcusr_mirror = MCUSR;
MCUSR = 0;
wdt_disable();

/* If the reset source was the bootloader and the key is correct, clear it and jump to the application */
if ((MCUSR & (1 << WDRF)) && (MagicBootKey == MAGIC_BOOT_KEY))
if ((mcusr_mirror & (1 << WDRF)) && (MagicBootKey == MAGIC_BOOT_KEY))
{
/* Turn off the watchdog */
MCUSR &= ~(1 << WDRF);
wdt_disable();

/* Clear the boot key and jump to the user application */
MagicBootKey = 0;

Expand Down Expand Up @@ -99,10 +102,6 @@ int main(void)
/** Configures all hardware required for the bootloader. */
static void SetupHardware(void)
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();

/* Disable clock division */
clock_prescale_set(clock_div_1);

Expand Down

0 comments on commit afda49c

Please sign in to comment.