Skip to content

Commit

Permalink
store MCUSR into R2 on bootup (#2)
Browse files Browse the repository at this point in the history
* Add store MCUSR into R2 on bootup

This makes it possible to find the MCU reset caused in run code.
Because the MCUSR is being erased by the bootloader.
  • Loading branch information
De-Backer authored Jun 17, 2021
1 parent f429c48 commit c9c5d69
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/bootloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,33 @@ MCP2515 mcp2515;
*/
void get_mcusr(void) __attribute__((naked)) __attribute__((used)) __attribute__((section(".init3")));
void get_mcusr(void) {
#if defined(MCUCSR)
MCUCSR = 0;
#else
MCUSR = 0;
#if defined(MCUCSR)
#if MCUSR_TO_R2
__asm__ __volatile__(
" mov r2, %[reset_caused_by_val] ;Move Between Registers \n\t"
::[reset_caused_by_val] "r"(MCUCSR));
#endif
MCUCSR = 0;
#else
#if MCUSR_TO_R2
__asm__ __volatile__(
" mov r2, %[reset_caused_by_val] ;Move Between Registers \n\t"
::[reset_caused_by_val] "r"(MCUSR));
#endif
MCUSR = 0;
#endif
wdt_disable();
}

/**
* The main function of the bootloader.
*/
int main () {
#if MCUSR_TO_R2
uint8_t reset_caused_by=0x00;/* MCUSR from bootloader */
__asm__ __volatile__(" mov %[reset_caused_by_val],r2 ;Move Between Registers \n\t"
:[reset_caused_by_val] "=r" (reset_caused_by));
#endif
// call init from arduino framework to setup timers
init();

Expand Down Expand Up @@ -341,6 +356,10 @@ int main () {
// delay for 50ms to let the mcp send the message
delay(50);

#if MCUSR_TO_R2
__asm__ __volatile__(" mov r2,%[reset_caused_by_val] ;Move Between Registers \n\t"
::[reset_caused_by_val] "r" (reset_caused_by));
#endif
startApp();

} else if (canMsg.data[CAN_DATA_BYTE_CMD] == CMD_FLASH_DONE_VERIFY) {
Expand Down Expand Up @@ -372,6 +391,10 @@ int main () {
// delay for 50ms to let the mcp send the message
delay(50);

#if MCUSR_TO_R2
__asm__ __volatile__(" mov r2,%[reset_caused_by_val] ;Move Between Registers \n\t"
::[reset_caused_by_val] "r" (reset_caused_by));
#endif
startApp();
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,43 @@
*/
#define TIMEOUT 250

/**
* store The MCU Status Register to Register 2
* The MCU Status Register provides information on which reset source
* caused an MCU reset.
*
* paste this code into your program (not the bootloader)
* \code
* uint8_t mcusr __attribute__ ((section (".noinit")));//<= the MCU Status Register
* void getMCUSR(void) __attribute__((naked)) __attribute__((section(".init0")));
* void getMCUSR(void)
* {
* __asm__ __volatile__ ( "mov %0, r2 \n" : "=r" (mcusr) : );
* }
* \endcode
*
* to use
* \code
* mcusr;//<= the MCU Status Register (global variable)
* \endcode
*
* or
*
* \code
* void main()
* {
* uint8_t mcusr;
* __asm__ __volatile__ ( "mov %0, r2 \n" : "=r" (mcusr) : );
* }
* \endcode
*
* to use
* \code
* mcusr;//<= the MCU Status Register (local variable in function main)
* \endcode
*/
#define MCUSR_TO_R2 1

/**
* Data rate of the CAN bus.
* CAN_5KBPS, CAN_10KBPS, CAN_20KBPS, CAN_31K25BPS, CAN_33KBPS, CAN_40KBPS,
Expand Down

0 comments on commit c9c5d69

Please sign in to comment.