Browse Source

🐛 Prevent AVR watchdogpile (#23075)

Skruppy 3 years ago
parent
commit
f53d627750
No account linked to committer's email address

+ 3
- 0
Marlin/Configuration_adv.h View File

4224
  */
4224
  */
4225
 //#define SOFT_RESET_VIA_SERIAL         // 'KILL' and '^X' commands will soft-reset the controller
4225
 //#define SOFT_RESET_VIA_SERIAL         // 'KILL' and '^X' commands will soft-reset the controller
4226
 //#define SOFT_RESET_ON_KILL            // Use a digital button to soft-reset the controller after KILL
4226
 //#define SOFT_RESET_ON_KILL            // Use a digital button to soft-reset the controller after KILL
4227
+
4228
+// Report uncleaned reset reason from register r2 instead of MCUSR. Supported by Optiboot on AVR.
4229
+//#define OPTIBOOT_RESET_REASON

+ 20
- 1
Marlin/src/HAL/AVR/HAL.cpp View File

35
 // Public Variables
35
 // Public Variables
36
 // ------------------------
36
 // ------------------------
37
 
37
 
38
-//uint8_t MCUSR;
38
+// Don't initialize/override variable (which would happen in .init4)
39
+uint8_t reset_reason __attribute__((section(".noinit")));
39
 
40
 
40
 // ------------------------
41
 // ------------------------
41
 // Public functions
42
 // Public functions
42
 // ------------------------
43
 // ------------------------
43
 
44
 
45
+__attribute__((naked))             // Don't output function pro- and epilogue
46
+__attribute__((used))              // Output the function, even if "not used"
47
+__attribute__((section(".init3"))) // Put in an early user definable section
48
+void HAL_save_reset_reason() {
49
+  #if ENABLED(OPTIBOOT_RESET_REASON)
50
+    __asm__ __volatile__(
51
+      A("STS %0, r2")
52
+      : "=m"(reset_reason)
53
+    );
54
+  #else
55
+    reset_reason = MCUSR;
56
+  #endif
57
+
58
+  // Clear within 16ms since WDRF bit enables a 16ms watchdog timer -> Boot loop
59
+  MCUSR = 0;
60
+  wdt_disable();
61
+}
62
+
44
 void HAL_init() {
63
 void HAL_init() {
45
   // Init Servo Pins
64
   // Init Servo Pins
46
   #define INIT_SERVO(N) OUT_WRITE(SERVO##N##_PIN, LOW)
65
   #define INIT_SERVO(N) OUT_WRITE(SERVO##N##_PIN, LOW)

+ 3
- 3
Marlin/src/HAL/AVR/HAL.h View File

91
 // Public Variables
91
 // Public Variables
92
 // ------------------------
92
 // ------------------------
93
 
93
 
94
-//extern uint8_t MCUSR;
94
+extern uint8_t reset_reason;
95
 
95
 
96
 // Serial ports
96
 // Serial ports
97
 #ifdef USBCON
97
 #ifdef USBCON
152
 
152
 
153
 //void _delay_ms(const int delay);
153
 //void _delay_ms(const int delay);
154
 
154
 
155
-inline void HAL_clear_reset_source() { MCUSR = 0; }
156
-inline uint8_t HAL_get_reset_source() { return MCUSR; }
155
+inline void HAL_clear_reset_source() { }
156
+inline uint8_t HAL_get_reset_source() { return reset_reason; }
157
 
157
 
158
 void HAL_reboot();
158
 void HAL_reboot();
159
 
159
 

+ 5
- 0
Marlin/src/inc/SanityCheck.h View File

2489
   #error "An encoder button is required or SOFT_RESET_ON_KILL will reset the printer without notice!"
2489
   #error "An encoder button is required or SOFT_RESET_ON_KILL will reset the printer without notice!"
2490
 #endif
2490
 #endif
2491
 
2491
 
2492
+// Reset reason for AVR
2493
+#if ENABLED(OPTIBOOT_RESET_REASON) && !defined(__AVR__)
2494
+  #error "OPTIBOOT_RESET_REASON only applies to AVR."
2495
+#endif
2496
+
2492
 /**
2497
 /**
2493
  * I2C bus
2498
  * I2C bus
2494
  */
2499
  */

Loading…
Cancel
Save