Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store MCUSR into R2 on bootup #2

Merged
merged 7 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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