My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watchdog.cpp 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef __AVR__
  23. #include "../../inc/MarlinConfig.h"
  24. #if ENABLED(USE_WATCHDOG)
  25. #include "watchdog.h"
  26. #include "../../MarlinCore.h"
  27. // Initialize watchdog with 8s timeout, if possible. Otherwise, make it 4s.
  28. void watchdog_init() {
  29. #if ENABLED(WATCHDOG_DURATION_8S) && defined(WDTO_8S)
  30. #define WDTO_NS WDTO_8S
  31. #else
  32. #define WDTO_NS WDTO_4S
  33. #endif
  34. #if ENABLED(WATCHDOG_RESET_MANUAL)
  35. // Enable the watchdog timer, but only for the interrupt.
  36. // Take care, as this requires the correct order of operation, with interrupts disabled.
  37. // See the datasheet of any AVR chip for details.
  38. wdt_reset();
  39. cli();
  40. _WD_CONTROL_REG = _BV(_WD_CHANGE_BIT) | _BV(WDE);
  41. _WD_CONTROL_REG = _BV(WDIE) | (WDTO_NS & 0x07) | ((WDTO_NS & 0x08) << 2); // WDTO_NS directly does not work. bit 0-2 are consecutive in the register but the highest value bit is at bit 5
  42. // So worked for up to WDTO_2S
  43. sei();
  44. wdt_reset();
  45. #else
  46. wdt_enable(WDTO_NS); // The function handles the upper bit correct.
  47. #endif
  48. //delay(10000); // test it!
  49. }
  50. //===========================================================================
  51. //=================================== ISR ===================================
  52. //===========================================================================
  53. // Watchdog timer interrupt, called if main program blocks >4sec and manual reset is enabled.
  54. #if ENABLED(WATCHDOG_RESET_MANUAL)
  55. ISR(WDT_vect) {
  56. sei(); // With the interrupt driven serial we need to allow interrupts.
  57. SERIAL_ERROR_MSG(STR_WATCHDOG_FIRED);
  58. minkill(); // interrupt-safe final kill and infinite loop
  59. }
  60. #endif
  61. #endif // USE_WATCHDOG
  62. #endif // __AVR__