From 0d8f865d9b783a74ea8238bb77a7bde239617d5e Mon Sep 17 00:00:00 2001 From: Zach Eveland Date: Sat, 21 Jan 2012 16:39:06 -0500 Subject: Leonardo bootloader checks reason for reset and jumps immediately to sketch if reset was caused by WDT --- bootloaders/caterina_LUFA/Caterina.c | 106 +++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 30 deletions(-) (limited to 'bootloaders/caterina_LUFA/Caterina.c') diff --git a/bootloaders/caterina_LUFA/Caterina.c b/bootloaders/caterina_LUFA/Caterina.c index dba00e3..8bd7403 100755 --- a/bootloaders/caterina_LUFA/Caterina.c +++ b/bootloaders/caterina_LUFA/Caterina.c @@ -56,6 +56,16 @@ static uint32_t CurrAddress; */ static bool RunBootloader = true; +void StartSketch() +{ + UDCON = 1; // Detach USB + UDIEN = 0; + __asm__ volatile ( // Reset vector to run firmware + "clr r30\n" + "clr r31\n" + "ijmp\n" + ::); +} /** Main program entry point. This routine configures the hardware required by the bootloader, then continuously * runs the bootloader processing routine until instructed to soft-exit, or hard-reset via the watchdog to start @@ -63,11 +73,21 @@ static bool RunBootloader = true; */ int main(void) { + 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(); + if (MCUSR_state & (1<> 8); WriteNextResponseByte(SPM_PAGESIZE & 0xFF); } else if ((Command == 'B') || (Command == 'g')) { - /* Delegate the block write/read to a separate function for clarity */ + // Delegate the block write/read to a separate function for clarity ReadWriteMemoryBlock(Command); } #endif #if !defined(NO_FLASH_BYTE_SUPPORT) else if (Command == 'C') { - /* Write the high byte to the current flash page */ + // Write the high byte to the current flash page boot_page_fill(CurrAddress, FetchNextCommandByte()); - /* Send confirmation byte back to the host */ + // Send confirmation byte back to the host WriteNextResponseByte('\r'); } else if (Command == 'c') { - /* Write the low byte to the current flash page */ + // Write the low byte to the current flash page boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte()); - /* Increment the address */ + // Increment the address CurrAddress += 2; - /* Send confirmation byte back to the host */ + // Send confirmation byte back to the host WriteNextResponseByte('\r'); } else if (Command == 'm') { - /* Commit the flash page to memory */ + // Commit the flash page to memory boot_page_write(CurrAddress); - /* Wait until write operation has completed */ + // Wait until write operation has completed boot_spm_busy_wait(); - /* Send confirmation byte back to the host */ + // Send confirmation byte back to the host WriteNextResponseByte('\r'); } else if (Command == 'R') @@ -517,29 +562,30 @@ void CDC_Task(void) #if !defined(NO_EEPROM_BYTE_SUPPORT) else if (Command == 'D') { - /* Read the byte from the endpoint and write it to the EEPROM */ + // Read the byte from the endpoint and write it to the EEPROM eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte()); - /* Increment the address after use */ + // Increment the address after use CurrAddress += 2; - /* Send confirmation byte back to the host */ + // Send confirmation byte back to the host WriteNextResponseByte('\r'); } else if (Command == 'd') { - /* Read the EEPROM byte and write it to the endpoint */ + // Read the EEPROM byte and write it to the endpoint WriteNextResponseByte(eeprom_read_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)))); - /* Increment the address after use */ + // Increment the address after use CurrAddress += 2; } #endif else if (Command != 27) { - /* Unknown (non-sync) command, return fail code */ + // Unknown (non-sync) command, return fail code WriteNextResponseByte('?'); } + /* Select the IN endpoint */ Endpoint_SelectEndpoint(CDC_TX_EPNUM); -- cgit v1.2.3-18-g5258