diff --git a/Marlin/src/HAL/HAL.h b/Marlin/src/HAL/HAL.h index f3946902c8..cecdd8b67b 100644 --- a/Marlin/src/HAL/HAL.h +++ b/Marlin/src/HAL/HAL.h @@ -24,3 +24,9 @@ #include "platforms.h" #include HAL_PATH(.,HAL.h) + +inline void watchdog_refresh() { + #if ENABLED(USE_WATCHDOG) + HAL_watchdog_refresh(); + #endif +} diff --git a/Marlin/src/HAL/HAL_AVR/watchdog.h b/Marlin/src/HAL/HAL_AVR/watchdog.h index 5190d2c55a..efd725d7d7 100644 --- a/Marlin/src/HAL/HAL_AVR/watchdog.h +++ b/Marlin/src/HAL/HAL_AVR/watchdog.h @@ -28,4 +28,4 @@ void watchdog_init(); // Reset watchdog. MUST be called at least every 4 seconds after the // first watchdog_init or AVR will go into emergency procedures. -inline void watchdog_reset() { wdt_reset(); } +inline void HAL_watchdog_refresh() { wdt_reset(); } diff --git a/Marlin/src/HAL/HAL_DUE/watchdog.h b/Marlin/src/HAL/HAL_DUE/watchdog.h index 14832a9c3c..6e70adef81 100644 --- a/Marlin/src/HAL/HAL_DUE/watchdog.h +++ b/Marlin/src/HAL/HAL_DUE/watchdog.h @@ -30,4 +30,4 @@ void watchdog_init(); // Reset watchdog. MUST be called at least every 4 seconds after the // first watchdog_init or AVR will go into emergency procedures. -inline void watchdog_reset() { watchdogReset(); } +inline void HAL_watchdog_refresh() { watchdogReset(); } diff --git a/Marlin/src/HAL/HAL_ESP32/watchdog.h b/Marlin/src/HAL/HAL_ESP32/watchdog.h index e5dd3b46d3..e1ec9fbf54 100644 --- a/Marlin/src/HAL/HAL_ESP32/watchdog.h +++ b/Marlin/src/HAL/HAL_ESP32/watchdog.h @@ -25,4 +25,4 @@ void watchdog_init(); // Reset watchdog. -inline void watchdog_reset() { } +inline void HAL_watchdog_refresh() { } diff --git a/Marlin/src/HAL/HAL_LINUX/HAL.h b/Marlin/src/HAL/HAL_LINUX/HAL.h index 39d68a7a10..8f368a1507 100644 --- a/Marlin/src/HAL/HAL_LINUX/HAL.h +++ b/Marlin/src/HAL/HAL_LINUX/HAL.h @@ -97,6 +97,10 @@ void HAL_adc_enable_channel(int pin); void HAL_adc_start_conversion(const uint8_t adc_pin); uint16_t HAL_adc_get_result(); +// Reset source +inline void HAL_clear_reset_source(void) {} +inline uint8_t HAL_get_reset_source(void) { return RST_POWER_ON; } + /* ---------------- Delay in cycles */ FORCE_INLINE static void DELAY_CYCLES(uint64_t x) { Clock::delayCycles(x); diff --git a/Marlin/src/HAL/HAL_LINUX/watchdog.cpp b/Marlin/src/HAL/HAL_LINUX/watchdog.cpp index a1ecac1cb5..6338ea3b1d 100644 --- a/Marlin/src/HAL/HAL_LINUX/watchdog.cpp +++ b/Marlin/src/HAL/HAL_LINUX/watchdog.cpp @@ -29,18 +29,8 @@ #include "watchdog.h" void watchdog_init() {} +void HAL_watchdog_refresh() {} -void HAL_clear_reset_source() {} - -uint8_t HAL_get_reset_source() { - return RST_POWER_ON; -} - -void watchdog_reset() {} - -#else - void HAL_clear_reset_source() {} - uint8_t HAL_get_reset_source() { return RST_POWER_ON; } -#endif // USE_WATCHDOG +#endif #endif // __PLAT_LINUX__ diff --git a/Marlin/src/HAL/HAL_LINUX/watchdog.h b/Marlin/src/HAL/HAL_LINUX/watchdog.h index 5bc06f04f1..51d30c8437 100644 --- a/Marlin/src/HAL/HAL_LINUX/watchdog.h +++ b/Marlin/src/HAL/HAL_LINUX/watchdog.h @@ -24,6 +24,4 @@ #define WDT_TIMEOUT 4000000 // 4 second timeout void watchdog_init(); -void watchdog_reset(); -void HAL_clear_reset_source(); -uint8_t HAL_get_reset_source(); +void HAL_watchdog_refresh(); diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL.cpp b/Marlin/src/HAL/HAL_LPC1768/HAL.cpp index b684145e99..2607ecfb46 100644 --- a/Marlin/src/HAL/HAL_LPC1768/HAL.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/HAL.cpp @@ -26,6 +26,10 @@ #include "../shared/Delay.h" #include "../../../gcode/parser.h" +#if ENABLED(USE_WATCHDOG) + #include "watchdog.h" +#endif + // U8glib required functions extern "C" void u8g_xMicroDelay(uint16_t val) { DELAY_US(val); @@ -65,4 +69,17 @@ void flashFirmware(int16_t value) { NVIC_SystemReset(); } +void HAL_clear_reset_source(void) { + #if ENABLED(USE_WATCHDOG) + watchdog_clear_timeout_flag(); + #endif +} + +uint8_t HAL_get_reset_source(void) { + #if ENABLED(USE_WATCHDOG) + if (watchdog_timed_out()) return RST_WATCHDOG; + #endif + return RST_POWER_ON; +} + #endif // TARGET_LPC1768 diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL.h b/Marlin/src/HAL/HAL_LPC1768/HAL.h index cf2576de25..7ea6d288be 100644 --- a/Marlin/src/HAL/HAL_LPC1768/HAL.h +++ b/Marlin/src/HAL/HAL_LPC1768/HAL.h @@ -164,3 +164,7 @@ void set_pwm_frequency(const pin_t pin, int f_desired); * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255] */ void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false); + +// Reset source +void HAL_clear_reset_source(void); +uint8_t HAL_get_reset_source(void); diff --git a/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp b/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp index 55e3628603..f122bf9984 100644 --- a/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/watchdog.cpp @@ -56,28 +56,16 @@ void watchdog_init() { WDT_Start(WDT_TIMEOUT); } -void HAL_clear_reset_source() { - WDT_ClrTimeOutFlag(); -} - -uint8_t HAL_get_reset_source() { - if (TEST(WDT_ReadTimeOutFlag(), 0)) return RST_WATCHDOG; - return RST_POWER_ON; -} - -void watchdog_reset() { +void HAL_watchdog_refresh() { WDT_Feed(); #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED) TOGGLE(LED_PIN); // heartbeat indicator #endif } -#else - -void watchdog_init() {} -void watchdog_reset() {} -void HAL_clear_reset_source() {} -uint8_t HAL_get_reset_source() { return RST_POWER_ON; } +// Timeout state +bool watchdog_timed_out() { return TEST(WDT_ReadTimeOutFlag(), 0); } +void watchdog_clear_timeout_flag() { WDT_ClrTimeOutFlag(); } #endif // USE_WATCHDOG diff --git a/Marlin/src/HAL/HAL_LPC1768/watchdog.h b/Marlin/src/HAL/HAL_LPC1768/watchdog.h index 5bc06f04f1..dd6617ea31 100644 --- a/Marlin/src/HAL/HAL_LPC1768/watchdog.h +++ b/Marlin/src/HAL/HAL_LPC1768/watchdog.h @@ -24,6 +24,7 @@ #define WDT_TIMEOUT 4000000 // 4 second timeout void watchdog_init(); -void watchdog_reset(); -void HAL_clear_reset_source(); -uint8_t HAL_get_reset_source(); +void HAL_watchdog_refresh(); + +bool watchdog_timed_out(); +void watchdog_clear_timeout_flag(); diff --git a/Marlin/src/HAL/HAL_SAMD51/watchdog.cpp b/Marlin/src/HAL/HAL_SAMD51/watchdog.cpp index a60bcedd8a..69a6de4ef7 100644 --- a/Marlin/src/HAL/HAL_SAMD51/watchdog.cpp +++ b/Marlin/src/HAL/HAL_SAMD51/watchdog.cpp @@ -42,7 +42,7 @@ WDT->INTENCLR.reg = WDT_INTENCLR_EW; // Disable early warning interrupt WDT->CONFIG.reg = WDT_CONFIG_PER_CYC4096; // Set at least 4s period for chip reset - watchdog_reset(); + HAL_watchdog_refresh(); WDT->CTRLA.reg = WDT_CTRLA_ENABLE; // Start watchdog now in normal mode SYNC(WDT->SYNCBUSY.bit.ENABLE); diff --git a/Marlin/src/HAL/HAL_SAMD51/watchdog.h b/Marlin/src/HAL/HAL_SAMD51/watchdog.h index 1c4e7542f2..b626b0b06a 100644 --- a/Marlin/src/HAL/HAL_SAMD51/watchdog.h +++ b/Marlin/src/HAL/HAL_SAMD51/watchdog.h @@ -25,7 +25,7 @@ void watchdog_init(); // Reset watchdog. MUST be called at least every 4 seconds after the // first watchdog_init or SAMD will go into emergency procedures. -inline void watchdog_reset() { +inline void HAL_watchdog_refresh() { SYNC(WDT->SYNCBUSY.bit.CLEAR); // Test first if previous is 'ongoing' to save time waiting for command execution WDT->CLEAR.reg = WDT_CLEAR_CLEAR_KEY; } diff --git a/Marlin/src/HAL/HAL_STM32/watchdog.cpp b/Marlin/src/HAL/HAL_STM32/watchdog.cpp index 8963d55cfc..4defadaa77 100644 --- a/Marlin/src/HAL/HAL_STM32/watchdog.cpp +++ b/Marlin/src/HAL/HAL_STM32/watchdog.cpp @@ -33,7 +33,7 @@ void watchdog_init() { IWatchdog.begin(4000000); } // 4 sec timeout - void watchdog_reset() { + void HAL_watchdog_refresh() { IWatchdog.reload(); #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED) TOGGLE(LED_PIN); // heartbeat indicator diff --git a/Marlin/src/HAL/HAL_STM32/watchdog.h b/Marlin/src/HAL/HAL_STM32/watchdog.h index f062d8a2e6..6855016737 100644 --- a/Marlin/src/HAL/HAL_STM32/watchdog.h +++ b/Marlin/src/HAL/HAL_STM32/watchdog.h @@ -22,4 +22,4 @@ #pragma once void watchdog_init(); -void watchdog_reset(); +void HAL_watchdog_refresh(); diff --git a/Marlin/src/HAL/HAL_STM32F1/watchdog.cpp b/Marlin/src/HAL/HAL_STM32F1/watchdog.cpp index 5696939f30..17f5aa9725 100644 --- a/Marlin/src/HAL/HAL_STM32F1/watchdog.cpp +++ b/Marlin/src/HAL/HAL_STM32F1/watchdog.cpp @@ -33,7 +33,7 @@ #include #include "watchdog.h" -void watchdog_reset() { +void HAL_watchdog_refresh() { #if DISABLED(PINS_DEBUGGING) && PIN_EXISTS(LED) TOGGLE(LED_PIN); // heartbeat indicator #endif diff --git a/Marlin/src/HAL/HAL_STM32F1/watchdog.h b/Marlin/src/HAL/HAL_STM32F1/watchdog.h index 4f3a8deb0d..21f97dd7a1 100644 --- a/Marlin/src/HAL/HAL_STM32F1/watchdog.h +++ b/Marlin/src/HAL/HAL_STM32F1/watchdog.h @@ -41,4 +41,4 @@ void watchdog_init(); // Reset watchdog. MUST be called at least every 4 seconds after the // first watchdog_init or STM32F1 will reset. -void watchdog_reset(); +void HAL_watchdog_refresh(); diff --git a/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.cpp b/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.cpp index e078d7a104..e51af618c9 100644 --- a/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.cpp +++ b/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.cpp @@ -44,7 +44,7 @@ } } - void watchdog_reset() { + void HAL_watchdog_refresh() { /* Refresh IWDG: reload counter */ if (HAL_IWDG_Refresh(&hiwdg) != HAL_OK) { /* Refresh Error */ diff --git a/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.h b/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.h index cc02fda24f..9e2a2dc21c 100644 --- a/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.h +++ b/Marlin/src/HAL/HAL_STM32_F4_F7/watchdog.h @@ -24,4 +24,4 @@ extern IWDG_HandleTypeDef hiwdg; void watchdog_init(); -void watchdog_reset(); +void HAL_watchdog_refresh(); diff --git a/Marlin/src/HAL/HAL_TEENSY31_32/watchdog.h b/Marlin/src/HAL/HAL_TEENSY31_32/watchdog.h index ceb041b457..861afcbc91 100644 --- a/Marlin/src/HAL/HAL_TEENSY31_32/watchdog.h +++ b/Marlin/src/HAL/HAL_TEENSY31_32/watchdog.h @@ -27,7 +27,7 @@ void watchdog_init(); -inline void watchdog_reset() { +inline void HAL_watchdog_refresh() { // Watchdog refresh sequence WDOG_REFRESH = 0xA602; WDOG_REFRESH = 0xB480; diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/watchdog.h b/Marlin/src/HAL/HAL_TEENSY35_36/watchdog.h index d2efaab372..7c27260906 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/watchdog.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/watchdog.h @@ -23,7 +23,7 @@ void watchdog_init(); -inline void watchdog_reset() { +inline void HAL_watchdog_refresh() { // Watchdog refresh sequence WDOG_REFRESH = 0xA602; WDOG_REFRESH = 0xB480; diff --git a/Marlin/src/Marlin.cpp b/Marlin/src/Marlin.cpp index d215701987..cd8ecb4325 100644 --- a/Marlin/src/Marlin.cpp +++ b/Marlin/src/Marlin.cpp @@ -801,29 +801,17 @@ void minkill(const bool steppers_off/*=false*/) { #if HAS_KILL // Wait for kill to be released - while (!READ(KILL_PIN)) { - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif - } + while (!READ(KILL_PIN)) watchdog_refresh(); // Wait for kill to be pressed - while (READ(KILL_PIN)) { - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif - } + while (READ(KILL_PIN)) watchdog_refresh(); void (*resetFunc)() = 0; // Declare resetFunc() at address 0 resetFunc(); // Jump to address 0 #else // !HAS_KILL - for (;;) { - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif - } // Wait for reset + for (;;) watchdog_refresh(); // Wait for reset #endif // !HAS_KILL } diff --git a/Marlin/src/gcode/config/M43.cpp b/Marlin/src/gcode/config/M43.cpp index e26f6d8ee2..34d42ed499 100644 --- a/Marlin/src/gcode/config/M43.cpp +++ b/Marlin/src/gcode/config/M43.cpp @@ -50,12 +50,6 @@ #define GET_PIN_MAP_PIN_M43(Q) GET_PIN_MAP_PIN(Q) #endif -inline void _watchdog_reset() { - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif -} - inline void toggle_pins() { const bool ignore_protection = parser.boolval('I'); const int repeat = parser.intval('R', 1), @@ -71,7 +65,7 @@ inline void toggle_pins() { SERIAL_EOL(); } else { - _watchdog_reset(); + watchdog_refresh(); report_pin_state_extended(pin, ignore_protection, true, "Pulsing "); #if AVR_AT90USB1286_FAMILY // Teensy IDEs don't know about these pins so must use FASTIO if (pin == TEENSY_E2) { @@ -95,10 +89,10 @@ inline void toggle_pins() { { pinMode(pin, OUTPUT); for (int16_t j = 0; j < repeat; j++) { - _watchdog_reset(); extDigitalWrite(pin, 0); safe_delay(wait); - _watchdog_reset(); extDigitalWrite(pin, 1); safe_delay(wait); - _watchdog_reset(); extDigitalWrite(pin, 0); safe_delay(wait); - _watchdog_reset(); + watchdog_refresh(); extDigitalWrite(pin, 0); safe_delay(wait); + watchdog_refresh(); extDigitalWrite(pin, 1); safe_delay(wait); + watchdog_refresh(); extDigitalWrite(pin, 0); safe_delay(wait); + watchdog_refresh(); } } } diff --git a/Marlin/src/gcode/feature/L6470/M916-918.cpp b/Marlin/src/gcode/feature/L6470/M916-918.cpp index 3b62a0f45e..2077e937b9 100644 --- a/Marlin/src/gcode/feature/L6470/M916-918.cpp +++ b/Marlin/src/gcode/feature/L6470/M916-918.cpp @@ -258,7 +258,7 @@ void GcodeSuite::M917() { } DEBUG_ECHOLNPGM("."); reset_stepper_timeout(); // reset_stepper_timeout to keep steppers powered - watchdog_reset(); // beat the dog + watchdog_refresh(); safe_delay(5000); status_composite_temp = 0; for (j = 0; j < driver_count; j++) { diff --git a/Marlin/src/module/temperature.cpp b/Marlin/src/module/temperature.cpp index c0a12c7ffa..47942441f6 100644 --- a/Marlin/src/module/temperature.cpp +++ b/Marlin/src/module/temperature.cpp @@ -1000,7 +1000,7 @@ void Temperature::manage_heater() { #if EARLY_WATCHDOG // If thermal manager is still not running, make sure to at least reset the watchdog! - if (!inited) return watchdog_reset(); + if (!inited) return watchdog_refresh(); #endif #if BOTH(PROBING_HEATERS_OFF, BED_LIMIT_SWITCHING) @@ -1518,10 +1518,8 @@ void Temperature::updateTemperaturesFromRawValues() { filwidth.update_measured_mm(); #endif - #if ENABLED(USE_WATCHDOG) - // Reset the watchdog after we know we have a temperature measurement. - watchdog_reset(); - #endif + // Reset the watchdog on good temperature measurement + watchdog_refresh(); temp_meas_ready = false; } diff --git a/Marlin/src/sd/Sd2Card.cpp b/Marlin/src/sd/Sd2Card.cpp index a6223858cb..a09de08fe5 100644 --- a/Marlin/src/sd/Sd2Card.cpp +++ b/Marlin/src/sd/Sd2Card.cpp @@ -234,11 +234,7 @@ bool Sd2Card::init(const uint8_t sckRateID, const pin_t chipSelectPin) { const millis_t init_timeout = millis() + SD_INIT_TIMEOUT; uint32_t arg; - // If init takes more than 4s it could trigger - // watchdog leading to a reboot loop. - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif + watchdog_refresh(); // In case init takes too long // Set pin modes extDigitalWrite(chipSelectPin_, HIGH); // For some CPUs pinMode can write the wrong data so init desired data value first @@ -252,10 +248,7 @@ bool Sd2Card::init(const uint8_t sckRateID, const pin_t chipSelectPin) { // Must supply min of 74 clock cycles with CS high. for (uint8_t i = 0; i < 10; i++) spiSend(0xFF); - // Initialization can cause the watchdog to timeout, so reinit it here - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif + watchdog_refresh(); // In case init takes too long // Command to go idle in SPI mode while ((status_ = cardCommand(CMD0, 0)) != R1_IDLE_STATE) { @@ -269,10 +262,7 @@ bool Sd2Card::init(const uint8_t sckRateID, const pin_t chipSelectPin) { crcSupported = (cardCommand(CMD59, 1) == R1_IDLE_STATE); #endif - // Initialization can cause the watchdog to timeout, so reinit it here - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif + watchdog_refresh(); // In case init takes too long // check SD version for (;;) { @@ -294,10 +284,7 @@ bool Sd2Card::init(const uint8_t sckRateID, const pin_t chipSelectPin) { } } - // Initialization can cause the watchdog to timeout, so reinit it here - #if ENABLED(USE_WATCHDOG) - watchdog_reset(); - #endif + watchdog_refresh(); // In case init takes too long // Initialize card and send host supports SDHC if SD2 arg = type() == SD_CARD_TYPE_SD2 ? 0x40000000 : 0; diff --git a/buildroot/share/tests/megaatmega2560-tests b/buildroot/share/tests/megaatmega2560-tests index fcab51b349..bcd3e497f8 100755 --- a/buildroot/share/tests/megaatmega2560-tests +++ b/buildroot/share/tests/megaatmega2560-tests @@ -25,6 +25,7 @@ opt_set TEMP_SENSOR_1 1 opt_set TEMP_SENSOR_BED 2 opt_set GRID_MAX_POINTS_X 16 opt_set FANMUX0_PIN 53 +opt_disable USE_WATCHDOG opt_enable REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_PROGRESS_BAR_TEST \ PIDTEMPBED FIX_MOUNTED_PROBE Z_SAFE_HOMING CODEPENDENT_XY_HOMING \ EEPROM_SETTINGS SDSUPPORT SD_REPRINT_LAST_SELECTED_FILE BINARY_FILE_TRANSFER \ diff --git a/platformio.ini b/platformio.ini index 70a0ae0e58..2597b4f7de 100644 --- a/platformio.ini +++ b/platformio.ini @@ -591,7 +591,7 @@ monitor_speed = 250000 # [env:linux_native] platform = native -build_flags = -D__PLAT_LINUX__ -std=gnu++17 -ggdb -g -lrt -lpthread -D__MARLIN_FIRMWARE__ +build_flags = -D__PLAT_LINUX__ -std=gnu++17 -ggdb -g -lrt -lpthread -D__MARLIN_FIRMWARE__ -Wno-expansion-to-defined src_build_flags = -Wall -IMarlin/src/HAL/HAL_LINUX/include build_unflags = -Wall lib_ldf_mode = off