diff options
author | Zach Eveland <zeveland@blacklabel-development.com> | 2011-12-31 12:19:08 -0500 |
---|---|---|
committer | Zach Eveland <zeveland@blacklabel-development.com> | 2011-12-31 12:19:08 -0500 |
commit | 28e9e122af24e63e84d5bcd2c619b76c490970fc (patch) | |
tree | 7174e8418fc8e6658bc7e2b260d0096d607803b3 /bootloaders/diskloader/src/DiskLoader.cpp | |
parent | cbce8ed4bf330973a999220dd11444b6d1181d53 (diff) |
reset the Leonardo board when leaving the bootloader, before starting the sketch
Done so all IO and other registers are properly reinitialized when a new sketch is uploaded or when an existing sketch is started. Uses a watchdog timeout with a 15 ms period to accomplish the reset. Bootloader checks the reason for reset and only enumerates as bootloader and enters the programming loop if reset was NOT caused by WDT.
Diffstat (limited to 'bootloaders/diskloader/src/DiskLoader.cpp')
-rw-r--r-- | bootloaders/diskloader/src/DiskLoader.cpp | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/bootloaders/diskloader/src/DiskLoader.cpp b/bootloaders/diskloader/src/DiskLoader.cpp index 6580618..09f59a7 100644 --- a/bootloaders/diskloader/src/DiskLoader.cpp +++ b/bootloaders/diskloader/src/DiskLoader.cpp @@ -56,7 +56,7 @@ void Program(u8 ep, u16 page, u8 count) boot_rww_enable (); } - +void StartSketch(); int USBGetChar(); #define getch USBGetChar @@ -111,10 +111,15 @@ int main(void) __attribute__ ((naked)); // STK500v1 main loop, very similar to optiboot in protocol and implementation int main() { + uint8_t MCUSR_state = MCUSR; // store the reason for the reset + MCUSR &= ~(1 << WDRF); // must clear the watchdog reset flag before disabling and reenabling WDT wdt_disable(); TXLED0; RXLED0; - LED0; + LED0; + if (MCUSR_state & (1<<WDRF) && (pgm_read_word(0) != -1)) { + StartSketch(); // if the reset was caused by WDT and if a sketch is already present then run the sketch instead of the bootloader + } BOARD_INIT(); USBInit(); @@ -225,7 +230,7 @@ void LEDPulse() LED1; } -void Reboot() +void StartSketch() { TXLED0; // switch off the RX and TX LEDs before starting the user sketch RXLED0; @@ -237,3 +242,10 @@ void Reboot() "ijmp\n" ::); } + +void Reset() +{ + wdt_enable(WDTO_15MS); + for (;;) + ; +} |