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 | |
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')
-rw-r--r-- | bootloaders/diskloader/src/DiskLoader.cpp | 18 | ||||
-rw-r--r-- | bootloaders/diskloader/src/USBCore.cpp | 4 |
2 files changed, 17 insertions, 5 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 (;;) + ; +} diff --git a/bootloaders/diskloader/src/USBCore.cpp b/bootloaders/diskloader/src/USBCore.cpp index 208121e..0467e46 100644 --- a/bootloaders/diskloader/src/USBCore.cpp +++ b/bootloaders/diskloader/src/USBCore.cpp @@ -34,7 +34,7 @@ u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */ u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */ -void Reboot(); +void Reset(); //================================================================== //================================================================== @@ -500,7 +500,7 @@ int USBGetChar() } if (!--_timeout) { - Reboot(); // USB not connected, run firmware + Reset(); } _delay_us(100); // stretch out the bootloader period to about 5 seconds after enumeration |