Skip to content

Commit

Permalink
Expose MCUSR register to applications using GPIOR0
Browse files Browse the repository at this point in the history
This allows application to handle reset reasons, e.g. watchdog, reset button, ...
  • Loading branch information
jpcornil-git committed Apr 8, 2024
1 parent 6309212 commit c727cb1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions bootloaders/caterina/Caterina.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ uint16_t Timeout = 0;
uint16_t bootKey = 0x7777;
volatile uint16_t *const bootKeyPtr = (volatile uint16_t *)0x0800;

void StartSketch(void)
void StartSketch(uint8_t mcusr_state)
{
cli();

Expand All @@ -86,6 +86,8 @@ void StartSketch(void)
TX_LED_OFF();
RX_LED_OFF();

GPIOR0 = mcusr_state;

/* jump to beginning of application space */
__asm__ volatile("jmp 0x0000");
}
Expand Down Expand Up @@ -126,10 +128,10 @@ int main(void)
} 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();
StartSketch(mcusr_state);
} 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(mcusr_state);
}

/* Setup hardware required for the bootloader */
Expand All @@ -155,7 +157,7 @@ int main(void)
USB_Detach();

/* Jump to beginning of application space to run the sketch - do not reset */
StartSketch();
StartSketch(mcusr_state);
}

/** Configures all hardware required for the bootloader. */
Expand Down
2 changes: 1 addition & 1 deletion bootloaders/caterina/Caterina.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
typedef void (*AppPtr_t)(void) ATTR_NO_RETURN;

/* Function Prototypes: */
void StartSketch(void);
void StartSketch(uint8_t mcur_state);
void LEDPulse(void);

void CDC_Task(void);
Expand Down

0 comments on commit c727cb1

Please sign in to comment.