From 2282b5bbb13ad9eec2ee23040d1b204696dcede0 Mon Sep 17 00:00:00 2001 From: Skruppy Date: Thu, 4 Nov 2021 06:34:26 +0100 Subject: [PATCH 1/4] Fix potential bootloop after WD timeout on AVR --- Marlin/Configuration_adv.h | 3 +++ Marlin/src/HAL/AVR/HAL.cpp | 21 ++++++++++++++++++++- Marlin/src/HAL/AVR/HAL.h | 6 +++--- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 934ceceafd20..fc495a6ea632 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -4224,3 +4224,6 @@ */ //#define SOFT_RESET_VIA_SERIAL // 'KILL' and '^X' commands will soft-reset the controller //#define SOFT_RESET_ON_KILL // Use a digital button to soft-reset the controller after KILL + +// Report uncleaned reset reason from register r2 instead of MCUSR. Supported by Optiboot on AVR. +//#define OPTIBOOT_RESET_REASON diff --git a/Marlin/src/HAL/AVR/HAL.cpp b/Marlin/src/HAL/AVR/HAL.cpp index 708583b262a6..d7bf2a6f6fbb 100644 --- a/Marlin/src/HAL/AVR/HAL.cpp +++ b/Marlin/src/HAL/AVR/HAL.cpp @@ -35,12 +35,31 @@ // Public Variables // ------------------------ -//uint8_t MCUSR; +// Don't initialize/override variable (which would happen in .init4) +uint8_t reset_reason __attribute__((section(".noinit"))); // ------------------------ // Public functions // ------------------------ +__attribute__((naked)) // Don't output function pro- and epilogue +__attribute__((used)) // Output the function, even if "not used" +__attribute__((section(".init3"))) // Put in an early user definable section +void HAL_save_reset_reason() { + #if ENABLED(OPTIBOOT_RESET_REASON) + __asm__ __volatile__( + A("STS %0, r2") + : "=m"(reset_reason) + ); + #else + reset_reason = MCUSR; + #endif + + // Clear within 16ms since WDRF bit enables a 16ms watchdog timer -> Boot loop + MCUSR = 0; + wdt_disable(); +} + void HAL_init() { // Init Servo Pins #define INIT_SERVO(N) OUT_WRITE(SERVO##N##_PIN, LOW) diff --git a/Marlin/src/HAL/AVR/HAL.h b/Marlin/src/HAL/AVR/HAL.h index ad1f47a4e685..2217f239d64e 100644 --- a/Marlin/src/HAL/AVR/HAL.h +++ b/Marlin/src/HAL/AVR/HAL.h @@ -91,7 +91,7 @@ typedef int8_t pin_t; // Public Variables // ------------------------ -//extern uint8_t MCUSR; +extern uint8_t reset_reason; // Serial ports #ifdef USBCON @@ -152,8 +152,8 @@ void HAL_init(); //void _delay_ms(const int delay); -inline void HAL_clear_reset_source() { MCUSR = 0; } -inline uint8_t HAL_get_reset_source() { return MCUSR; } +inline void HAL_clear_reset_source() { } +inline uint8_t HAL_get_reset_source() { return reset_reason; } void HAL_reboot(); From 872ab8d9b29f3c5849f8dbf91fc24667e676c312 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 4 Nov 2021 05:22:42 -0500 Subject: [PATCH 2/4] sanity check --- Marlin/src/inc/SanityCheck.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index b4eaf242b707..fbdda2c12758 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2489,6 +2489,11 @@ static_assert(Y_MAX_LENGTH >= Y_BED_SIZE, "Movement bounds (Y_MIN_POS, Y_MAX_POS #error "An encoder button is required or SOFT_RESET_ON_KILL will reset the printer without notice!" #endif +// Reset reason for AVR +#if ENABLED(OPTIBOOT_RESET_REASON) && !defined(__AVR__) + #error "OPTIBOOT_RESET_REASON only applies to AVR." +#endif + /** * I2C bus */ From b7f7d36cfd724dc3689efdb9fef3ef7ecba4677e Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 4 Nov 2021 05:48:13 -0500 Subject: [PATCH 3/4] Enable OPTIBOOT_RESET_REASON for Optiboot targets --- ini/avr.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/ini/avr.ini b/ini/avr.ini index e4d64de712a1..d07739d6df13 100644 --- a/ini/avr.ini +++ b/ini/avr.ini @@ -142,6 +142,7 @@ extends = common_avr8 board = sanguino_atmega1284p upload_speed = 115200 board_upload.maximum_size = 130048 +build_flags = ${common_avr8.build_flags} -DOPTIBOOT_RESET_REASON # # Melzi and clones (Zonestar Melzi2 with tuned flags) From f4ac090ae58e3f67d689ce69cb826aac7e301918 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 4 Nov 2021 12:09:34 -0500 Subject: [PATCH 4/4] Update avr.ini --- ini/avr.ini | 1 - 1 file changed, 1 deletion(-) diff --git a/ini/avr.ini b/ini/avr.ini index d07739d6df13..e4d64de712a1 100644 --- a/ini/avr.ini +++ b/ini/avr.ini @@ -142,7 +142,6 @@ extends = common_avr8 board = sanguino_atmega1284p upload_speed = 115200 board_upload.maximum_size = 130048 -build_flags = ${common_avr8.build_flags} -DOPTIBOOT_RESET_REASON # # Melzi and clones (Zonestar Melzi2 with tuned flags)