fixed logic bug in Caterina that could stop the bootloader from entering self-programming mode

This commit is contained in:
Zach Eveland 2012-04-11 23:19:05 -04:00
parent e2e0260094
commit 49f7fb00fd
3 changed files with 277 additions and 1042 deletions

File diff suppressed because it is too large Load Diff

View File

@ -66,7 +66,7 @@ uint16_t RxLEDPulse = 0; // time remaining for Rx LED pulse
uint16_t Timeout = 0; uint16_t Timeout = 0;
uint16_t bootKey = 0x7777; uint16_t bootKey = 0x7777;
volatile uint16_t *const bootKeyPtr = (volatile uint16_t *)0x0A00; volatile uint16_t *const bootKeyPtr = (volatile uint16_t *)0x0800;
void StartSketch(void) void StartSketch(void)
{ {
@ -106,25 +106,29 @@ void LEDPulse(void)
} }
/** Main program entry point. This routine configures the hardware required by the bootloader, then continuously /** 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 * runs the bootloader processing routine until it times out or is instructed to exit.
* the loaded application code.
*/ */
int main(void) int main(void)
{ {
/* Watchdog may be configured with a 15 ms period so must disable it before doing anything else */ /* Save the value of the boot key memory before it is overwritten */
wdt_disable();
/* Check the reason for the reset and act accordingly */
uint8_t mcusr_state = MCUSR; // store the initial state of the Status register
MCUSR = 0; // clear all reset flags
// After a power-on reset skip the bootloader and jump straight to sketch
// if one exists.
if (mcusr_state & (1<<PORF) && pgm_read_word(0) != 0xFFFF) {
StartSketch();
}
uint16_t bootKeyPtrVal = *bootKeyPtr; uint16_t bootKeyPtrVal = *bootKeyPtr;
*bootKeyPtr = 0; *bootKeyPtr = 0;
if ((mcusr_state & (1<<WDRF)) && (bootKeyPtrVal != bootKey) && (pgm_read_word(0) != 0xFFFF)) {
/* Check the reason for the reset so we can act accordingly */
uint8_t mcusr_state = MCUSR; // store the initial state of the Status register
MCUSR = 0; // clear all reset flags
/* Watchdog may be configured with a 15 ms period so must disable it before going any further */
wdt_disable();
if (mcusr_state & (1<<EXTRF)) {
// External reset - we should continue to self-programming mode.
} else if (mcusr_state == (1<<PORF) && pgm_read_word(0) != 0xFFFF) {
// After a power-on reset skip the bootloader and jump straight to sketch
// if one exists.
StartSketch();
} else if ((mcusr_state == (1<<WDRF)) && (bootKeyPtrVal != bootKey) && (pgm_read_word(0) != 0xFFFF)) {
// If it looks like an "accidental" watchdog reset then start the sketch.
StartSketch(); StartSketch();
} }

View File

@ -108,9 +108,8 @@ bool WEAK CDC_Setup(Setup& setup)
// like servicing endpoints before the sketch ends // like servicing endpoints before the sketch ends
if (1200 == _usbLineInfo.dwDTERate) { if (1200 == _usbLineInfo.dwDTERate) {
// We check DTR state to determine if host port is open (bit 0 of lineState). // We check DTR state to determine if host port is open (bit 0 of lineState).
// Serial1.print(">"); Serial1.println(_usbLineInfo.lineState, HEX);
if ((_usbLineInfo.lineState & 0x01) == 0) { if ((_usbLineInfo.lineState & 0x01) == 0) {
*(uint16_t *)0x0A00 = 0x7777; *(uint16_t *)0x0800 = 0x7777;
wdt_enable(WDTO_120MS); wdt_enable(WDTO_120MS);
} else { } else {
// Most OSs do some intermediate steps when configuring ports and DTR can // Most OSs do some intermediate steps when configuring ports and DTR can
@ -120,7 +119,7 @@ bool WEAK CDC_Setup(Setup& setup)
wdt_disable(); wdt_disable();
wdt_reset(); wdt_reset();
*(uint16_t *)0x0A00 = 0x0; *(uint16_t *)0x0800 = 0x0;
} }
} }
return true; return true;