From c9c5d691e1bc8d66be28544863d365cb2549639a Mon Sep 17 00:00:00 2001 From: De-Backer Date: Thu, 17 Jun 2021 20:12:30 +0200 Subject: [PATCH] store MCUSR into R2 on bootup (#2) * 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. --- src/bootloader.cpp | 31 +++++++++++++++++++++++++++---- src/config.h | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/bootloader.cpp b/src/bootloader.cpp index 01dddfc..2b999b9 100644 --- a/src/bootloader.cpp +++ b/src/bootloader.cpp @@ -47,11 +47,21 @@ 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(); } @@ -59,6 +69,11 @@ void get_mcusr(void) { * 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(); @@ -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) { @@ -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(); } } diff --git a/src/config.h b/src/config.h index 4af5a45..30b120d 100644 --- a/src/config.h +++ b/src/config.h @@ -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,