diff --git a/hardware/arduino/sam/cores/arduino/Arduino.h b/hardware/arduino/sam/cores/arduino/Arduino.h index ba63d6c713a..f074d5b27bc 100644 --- a/hardware/arduino/sam/cores/arduino/Arduino.h +++ b/hardware/arduino/sam/cores/arduino/Arduino.h @@ -194,6 +194,8 @@ extern const PinDescription g_APinDescription[] ; #include "wiring_shift.h" #include "WInterrupts.h" +#include "watchdog.h" + // USB Device #define USB_VID 0x2341 // arduino LLC vid #define USB_PID_LEONARDO 0x0034 diff --git a/hardware/arduino/sam/cores/arduino/main.cpp b/hardware/arduino/sam/cores/arduino/main.cpp index ab125365de3..9b0250bfbc7 100644 --- a/hardware/arduino/sam/cores/arduino/main.cpp +++ b/hardware/arduino/sam/cores/arduino/main.cpp @@ -41,6 +41,9 @@ void initVariant() { } */ int main( void ) { + // Initialize watchdog + watchdogSetup(); + init(); initVariant(); diff --git a/hardware/arduino/sam/cores/arduino/watchdog.cpp b/hardware/arduino/sam/cores/arduino/watchdog.cpp new file mode 100644 index 00000000000..942ba13726c --- /dev/null +++ b/hardware/arduino/sam/cores/arduino/watchdog.cpp @@ -0,0 +1,55 @@ +/* + Copyright (c) 2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include + +#include "watchdog.h" + + +void watchdogEnable (uint32_t timeout) +{ + /* this assumes the slow clock is running at 32.768 kHz + watchdog frequency is therefore 32768 / 128 = 256 Hz */ + timeout = timeout * 256 / 1000; + if (timeout == 0) + timeout = 1; + else if (timeout > 0xFFF) + timeout = 0xFFF; + timeout = WDT_MR_WDRSTEN | WDT_MR_WDV(timeout) | WDT_MR_WDD(timeout); + WDT_Enable (WDT, timeout); +} + +void watchdogDisable(void) +{ + WDT_Disable (WDT); +} + +void watchdogReset(void) +{ + WDT_Restart (WDT); +} + + +extern "C" +void _watchdogDefaultSetup (void) +{ + WDT_Disable (WDT); +} +void watchdogSetup (void) __attribute__ ((weak, alias("_watchdogDefaultSetup"))); + + diff --git a/hardware/arduino/sam/cores/arduino/watchdog.h b/hardware/arduino/sam/cores/arduino/watchdog.h new file mode 100644 index 00000000000..eecdb897401 --- /dev/null +++ b/hardware/arduino/sam/cores/arduino/watchdog.h @@ -0,0 +1,52 @@ +/* + Copyright (c) 2014 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef _WATCHDOG_ +#define _WATCHDOG_ + +#include + +// Watchdog functions + +/* + * \brief Enable the watchdog with the specified timeout. Should only be called once. + * + * \param timeount in milliseconds. + */ +void watchdogEnable (uint32_t timeout); + +/* + * \brief Disable the watchdog timer. Should only be called once. + * + */ +void watchdogDisable (void); + +/* + * \brief Reset the watchdog counter. + * + */ +void watchdogReset (void); + +/* + * \brief Watchdog initialize hook. This function is called from init(). If the user does not provide + * this function, then the default action is to disable watchdog. + */ +void watchdogSetup (void); + +#endif /* _WATCHDOG_ */ + diff --git a/hardware/arduino/sam/variants/arduino_due_x/variant.cpp b/hardware/arduino/sam/variants/arduino_due_x/variant.cpp index 4cbe0df80dc..f95d8501745 100644 --- a/hardware/arduino/sam/variants/arduino_due_x/variant.cpp +++ b/hardware/arduino/sam/variants/arduino_due_x/variant.cpp @@ -377,9 +377,6 @@ void init( void ) while (true); } - // Disable watchdog - WDT_Disable(WDT); - // Initialize C library __libc_init_array();