2017-07-18 18:29:06 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-07-18 18:29:06 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-07-18 18:29:06 -05:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ARDUINO_ARCH_SAM
|
2017-09-27 04:57:33 -05:00
|
|
|
|
2017-09-06 06:28:32 -05:00
|
|
|
#include "../../inc/MarlinConfig.h"
|
2020-01-02 19:01:38 -06:00
|
|
|
#include "../../MarlinCore.h"
|
2019-09-02 19:49:58 -05:00
|
|
|
#include "watchdog.h"
|
2017-07-18 18:29:06 -05:00
|
|
|
|
2018-03-21 19:04:45 -05:00
|
|
|
// Override Arduino runtime to either config or disable the watchdog
|
|
|
|
//
|
|
|
|
// We need to configure the watchdog as soon as possible in the boot
|
|
|
|
// process, because watchdog initialization at hardware reset on SAM3X8E
|
|
|
|
// is unreliable, and there is risk of unintended resets if we delay
|
|
|
|
// that initialization to a later time.
|
2019-09-16 20:31:08 -05:00
|
|
|
void watchdogSetup() {
|
2017-07-18 18:29:06 -05:00
|
|
|
|
2018-03-21 19:04:45 -05:00
|
|
|
#if ENABLED(USE_WATCHDOG)
|
2017-07-18 18:29:06 -05:00
|
|
|
|
2018-03-21 19:04:45 -05:00
|
|
|
// 4 seconds timeout
|
|
|
|
uint32_t timeout = 4000;
|
|
|
|
|
|
|
|
// Calculate timeout value in WDT counter ticks: This assumes
|
|
|
|
// the slow clock is running at 32.768 kHz watchdog
|
|
|
|
// frequency is therefore 32768 / 128 = 256 Hz
|
|
|
|
timeout = (timeout << 8) / 1000;
|
|
|
|
if (timeout == 0)
|
|
|
|
timeout = 1;
|
|
|
|
else if (timeout > 0xFFF)
|
|
|
|
timeout = 0xFFF;
|
|
|
|
|
|
|
|
// We want to enable the watchdog with the specified timeout
|
|
|
|
uint32_t value =
|
|
|
|
WDT_MR_WDV(timeout) | // With the specified timeout
|
|
|
|
WDT_MR_WDD(timeout) | // and no invalid write window
|
|
|
|
#if !(SAMV70 || SAMV71 || SAME70 || SAMS70)
|
|
|
|
WDT_MR_WDRPROC | // WDT fault resets processor only - We want
|
|
|
|
// to keep PIO controller state
|
|
|
|
#endif
|
|
|
|
WDT_MR_WDDBGHLT | // WDT stops in debug state.
|
|
|
|
WDT_MR_WDIDLEHLT; // WDT stops in idle state.
|
|
|
|
|
|
|
|
#if ENABLED(WATCHDOG_RESET_MANUAL)
|
|
|
|
// We enable the watchdog timer, but only for the interrupt.
|
|
|
|
|
|
|
|
// Configure WDT to only trigger an interrupt
|
|
|
|
value |= WDT_MR_WDFIEN; // Enable WDT fault interrupt.
|
|
|
|
|
|
|
|
// Disable WDT interrupt (just in case, to avoid triggering it!)
|
|
|
|
NVIC_DisableIRQ(WDT_IRQn);
|
|
|
|
|
2018-05-16 14:38:17 -05:00
|
|
|
// We NEED memory barriers to ensure Interrupts are actually disabled!
|
|
|
|
// ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
|
|
|
|
__DSB();
|
|
|
|
__ISB();
|
|
|
|
|
2018-03-21 19:04:45 -05:00
|
|
|
// Initialize WDT with the given parameters
|
|
|
|
WDT_Enable(WDT, value);
|
|
|
|
|
|
|
|
// Configure and enable WDT interrupt.
|
|
|
|
NVIC_ClearPendingIRQ(WDT_IRQn);
|
|
|
|
NVIC_SetPriority(WDT_IRQn, 0); // Use highest priority, so we detect all kinds of lockups
|
|
|
|
NVIC_EnableIRQ(WDT_IRQn);
|
2017-07-18 18:29:06 -05:00
|
|
|
|
2018-03-21 19:04:45 -05:00
|
|
|
#else
|
2017-07-18 18:29:06 -05:00
|
|
|
|
2018-03-21 19:04:45 -05:00
|
|
|
// a WDT fault triggers a reset
|
|
|
|
value |= WDT_MR_WDRSTEN;
|
|
|
|
|
|
|
|
// Initialize WDT with the given parameters
|
|
|
|
WDT_Enable(WDT, value);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Reset the watchdog
|
|
|
|
WDT_Restart(WDT);
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// Make sure to completely disable the Watchdog
|
|
|
|
WDT_Disable(WDT);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if ENABLED(USE_WATCHDOG)
|
|
|
|
// Initialize watchdog - On SAM3X, Watchdog was already configured
|
|
|
|
// and enabled or disabled at startup, so no need to reconfigure it
|
|
|
|
// here.
|
2019-09-16 20:31:08 -05:00
|
|
|
void watchdog_init() {
|
2018-03-21 19:04:45 -05:00
|
|
|
// Reset watchdog to start clean
|
|
|
|
WDT_Restart(WDT);
|
|
|
|
}
|
2017-07-18 18:29:06 -05:00
|
|
|
#endif // USE_WATCHDOG
|
|
|
|
|
|
|
|
#endif
|