From 4725d750546b5bb2c6abf20fa213b06742eb9766 Mon Sep 17 00:00:00 2001 From: Bob Cousins Date: Sun, 27 Jul 2014 13:01:40 +0100 Subject: [PATCH] Add watchdog routines for Due. --- .../sam/variants/arduino_due_x/variant.cpp | 50 ++++++++++++++++++- .../sam/variants/arduino_due_x/variant.h | 20 ++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/hardware/arduino/sam/variants/arduino_due_x/variant.cpp b/hardware/arduino/sam/variants/arduino_due_x/variant.cpp index 4cbe0df80..999b5d4f7 100644 --- a/hardware/arduino/sam/variants/arduino_due_x/variant.cpp +++ b/hardware/arduino/sam/variants/arduino_due_x/variant.cpp @@ -358,6 +358,52 @@ void serialEventRun(void) if (Serial3.available()) serialEvent3(); } +// ---------------------------------------------------------------------------- +extern "C" { + +/** + * Watchdog enable + * + * Enable the watchdog timer with the specified settings. + * WDTO_xxx macros provide standard settings. + * Should only be called once. + */ +void wdt_enable (uint32_t mode) +{ + WDT_Enable (WDT, mode); +} + +/** + * Watchdog disable + * + * Disable the watchdog timer. + * Should only be called once. + */ +void wdt_disable(void) +{ + WDT_Disable (WDT); +} + +/** + * Watchdog reset + * + * Resets the watchdog counter + */ +void wdt_reset(void) +{ + WDT_Restart (WDT); +} + +} // extern "C" + +/** + * Watchdog initialize hook + * + * This function is called from init(). + * Default action is to disable watchdog. + */ +void wdt_initialize(void) __attribute__ ((weak, alias("wdt_disable"))); + // ---------------------------------------------------------------------------- #ifdef __cplusplus @@ -377,8 +423,8 @@ void init( void ) while (true); } - // Disable watchdog - WDT_Disable(WDT); + // Initialize watchdog + wdt_initialize(); // Initialize C library __libc_init_array(); diff --git a/hardware/arduino/sam/variants/arduino_due_x/variant.h b/hardware/arduino/sam/variants/arduino_due_x/variant.h index 6725a139c..d265255e4 100644 --- a/hardware/arduino/sam/variants/arduino_due_x/variant.h +++ b/hardware/arduino/sam/variants/arduino_due_x/variant.h @@ -232,6 +232,25 @@ static const uint8_t CAN1TX = 89; #define TC_MIN_DUTY_CYCLE 0 #define TC_RESOLUTION 8 +// Watchdog routines + +#define WDTO_15MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(4) | WDT_MR_WDD(4) +#define WDTO_30MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(8) | WDT_MR_WDD(8) +#define WDTO_60MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(15) | WDT_MR_WDD(15) +#define WDTO_120MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(31) | WDT_MR_WDD(31) +#define WDTO_250MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(64) | WDT_MR_WDD(64) +#define WDTO_500MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(128) | WDT_MR_WDD(128) +#define WDTO_1S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(256) | WDT_MR_WDD(256) +#define WDTO_2S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(512) | WDT_MR_WDD(512) +#define WDTO_4S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(1024) | WDT_MR_WDD(1024) +#define WDTO_8S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(2048) | WDT_MR_WDD(2048) + +void wdt_enable (uint32_t mode); + +void wdt_disable(void); + +void wdt_reset(void); + #ifdef __cplusplus } #endif @@ -274,5 +293,6 @@ extern USARTClass Serial3; #define SERIAL_PORT_HARDWARE2 Serial2 #define SERIAL_PORT_HARDWARE3 Serial3 + #endif /* _VARIANT_ARDUINO_DUE_X_ */