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 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 ARDUINO_ARCH_SAM
  23. #include "../../inc/MarlinConfig.h"
  24. #include "../../MarlinCore.h"
  25. #include "watchdog.h"
  26. // Override Arduino runtime to either config or disable the watchdog
  27. //
  28. // We need to configure the watchdog as soon as possible in the boot
  29. // process, because watchdog initialization at hardware reset on SAM3X8E
  30. // is unreliable, and there is risk of unintended resets if we delay
  31. // that initialization to a later time.
  32. void watchdogSetup() {
  33. #if ENABLED(USE_WATCHDOG)
  34. // 4 seconds timeout
  35. uint32_t timeout = 4000;
  36. // Calculate timeout value in WDT counter ticks: This assumes
  37. // the slow clock is running at 32.768 kHz watchdog
  38. // frequency is therefore 32768 / 128 = 256 Hz
  39. timeout = (timeout << 8) / 1000;
  40. if (timeout == 0)
  41. timeout = 1;
  42. else if (timeout > 0xFFF)
  43. timeout = 0xFFF;
  44. // We want to enable the watchdog with the specified timeout
  45. uint32_t value =
  46. WDT_MR_WDV(timeout) | // With the specified timeout
  47. WDT_MR_WDD(timeout) | // and no invalid write window
  48. #if !(SAMV70 || SAMV71 || SAME70 || SAMS70)
  49. WDT_MR_WDRPROC | // WDT fault resets processor only - We want
  50. // to keep PIO controller state
  51. #endif
  52. WDT_MR_WDDBGHLT | // WDT stops in debug state.
  53. WDT_MR_WDIDLEHLT; // WDT stops in idle state.
  54. #if ENABLED(WATCHDOG_RESET_MANUAL)
  55. // We enable the watchdog timer, but only for the interrupt.
  56. // Configure WDT to only trigger an interrupt
  57. value |= WDT_MR_WDFIEN; // Enable WDT fault interrupt.
  58. // Disable WDT interrupt (just in case, to avoid triggering it!)
  59. NVIC_DisableIRQ(WDT_IRQn);
  60. // We NEED memory barriers to ensure Interrupts are actually disabled!
  61. // ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
  62. __DSB();
  63. __ISB();
  64. // Initialize WDT with the given parameters
  65. WDT_Enable(WDT, value);
  66. // Configure and enable WDT interrupt.
  67. NVIC_ClearPendingIRQ(WDT_IRQn);
  68. NVIC_SetPriority(WDT_IRQn, 0); // Use highest priority, so we detect all kinds of lockups
  69. NVIC_EnableIRQ(WDT_IRQn);
  70. #else
  71. // a WDT fault triggers a reset
  72. value |= WDT_MR_WDRSTEN;
  73. // Initialize WDT with the given parameters
  74. WDT_Enable(WDT, value);
  75. #endif
  76. // Reset the watchdog
  77. WDT_Restart(WDT);
  78. #else
  79. // Make sure to completely disable the Watchdog
  80. WDT_Disable(WDT);
  81. #endif
  82. }
  83. #if ENABLED(USE_WATCHDOG)
  84. // Initialize watchdog - On SAM3X, Watchdog was already configured
  85. // and enabled or disabled at startup, so no need to reconfigure it
  86. // here.
  87. void watchdog_init() {
  88. // Reset watchdog to start clean
  89. WDT_Restart(WDT);
  90. }
  91. #endif // USE_WATCHDOG
  92. #endif