Skip to content

Commit d2c510d

Browse files
bobcfacchinm
authored andcommitted
Add Due watchdog functions; modified according to feedback supplied
1 parent 4725d75 commit d2c510d

File tree

6 files changed

+112
-69
lines changed

6 files changed

+112
-69
lines changed

hardware/arduino/sam/cores/arduino/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ extern const PinDescription g_APinDescription[] ;
194194
#include "wiring_shift.h"
195195
#include "WInterrupts.h"
196196

197+
#include "watchdog.h"
198+
197199
// USB Device
198200
#define USB_VID 0x2341 // arduino LLC vid
199201
#define USB_PID_LEONARDO 0x0034

hardware/arduino/sam/cores/arduino/main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ void initVariant() { }
4141
*/
4242
int main( void )
4343
{
44+
// Initialize watchdog
45+
watchdogSetup();
46+
4447
init();
4548

4649
initVariant();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright (c) 2014 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <chip.h>
20+
21+
#include "watchdog.h"
22+
23+
24+
void watchdogEnable (uint32_t timeout)
25+
{
26+
/* this assumes the slow clock is running at 32.768 kHz
27+
watchdog frequency is therefore 32768 / 128 = 256 Hz */
28+
timeout = timeout * 256 / 1000;
29+
if (timeout == 0)
30+
timeout = 1;
31+
else if (timeout > 0xFFF)
32+
timeout = 0xFFF;
33+
timeout = WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(timeout) | WDT_MR_WDD(timeout);
34+
WDT_Enable (WDT, timeout);
35+
}
36+
37+
void watchdogDisable(void)
38+
{
39+
WDT_Disable (WDT);
40+
}
41+
42+
void watchdogReset(void)
43+
{
44+
WDT_Restart (WDT);
45+
}
46+
47+
48+
extern "C"
49+
void _watchdogDefaultSetup (void)
50+
{
51+
WDT_Disable (WDT);
52+
}
53+
void watchdogSetup (void) __attribute__ ((weak, alias("_watchdogDefaultSetup")));
54+
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright (c) 2014 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _WATCHDOG_
20+
#define _WATCHDOG_
21+
22+
#include <stdint.h>
23+
24+
// Watchdog functions
25+
26+
/*
27+
* \brief Enable the watchdog with the specified timeout. Should only be called once.
28+
*
29+
* \param timeount in milliseconds.
30+
*/
31+
void watchdogEnable (uint32_t timeout);
32+
33+
/*
34+
* \brief Disable the watchdog timer. Should only be called once.
35+
*
36+
*/
37+
void watchdogDisable (void);
38+
39+
/*
40+
* \brief Reset the watchdog counter.
41+
*
42+
*/
43+
void watchdogReset (void);
44+
45+
/*
46+
* \brief Watchdog initialize hook. This function is called from init(). If the user does not provide
47+
* this function, then the default action is to disable watchdog.
48+
*/
49+
void watchdogSetup (void);
50+
51+
#endif /* _WATCHDOG_ */
52+

hardware/arduino/sam/variants/arduino_due_x/variant.cpp

-49
Original file line numberDiff line numberDiff line change
@@ -358,52 +358,6 @@ void serialEventRun(void)
358358
if (Serial3.available()) serialEvent3();
359359
}
360360

361-
// ----------------------------------------------------------------------------
362-
extern "C" {
363-
364-
/**
365-
* Watchdog enable
366-
*
367-
* Enable the watchdog timer with the specified settings.
368-
* WDTO_xxx macros provide standard settings.
369-
* Should only be called once.
370-
*/
371-
void wdt_enable (uint32_t mode)
372-
{
373-
WDT_Enable (WDT, mode);
374-
}
375-
376-
/**
377-
* Watchdog disable
378-
*
379-
* Disable the watchdog timer.
380-
* Should only be called once.
381-
*/
382-
void wdt_disable(void)
383-
{
384-
WDT_Disable (WDT);
385-
}
386-
387-
/**
388-
* Watchdog reset
389-
*
390-
* Resets the watchdog counter
391-
*/
392-
void wdt_reset(void)
393-
{
394-
WDT_Restart (WDT);
395-
}
396-
397-
} // extern "C"
398-
399-
/**
400-
* Watchdog initialize hook
401-
*
402-
* This function is called from init().
403-
* Default action is to disable watchdog.
404-
*/
405-
void wdt_initialize(void) __attribute__ ((weak, alias("wdt_disable")));
406-
407361
// ----------------------------------------------------------------------------
408362

409363
#ifdef __cplusplus
@@ -423,9 +377,6 @@ void init( void )
423377
while (true);
424378
}
425379

426-
// Initialize watchdog
427-
wdt_initialize();
428-
429380
// Initialize C library
430381
__libc_init_array();
431382

hardware/arduino/sam/variants/arduino_due_x/variant.h

-20
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,6 @@ static const uint8_t CAN1TX = 89;
232232
#define TC_MIN_DUTY_CYCLE 0
233233
#define TC_RESOLUTION 8
234234

235-
// Watchdog routines
236-
237-
#define WDTO_15MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(4) | WDT_MR_WDD(4)
238-
#define WDTO_30MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(8) | WDT_MR_WDD(8)
239-
#define WDTO_60MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(15) | WDT_MR_WDD(15)
240-
#define WDTO_120MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(31) | WDT_MR_WDD(31)
241-
#define WDTO_250MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(64) | WDT_MR_WDD(64)
242-
#define WDTO_500MS WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(128) | WDT_MR_WDD(128)
243-
#define WDTO_1S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(256) | WDT_MR_WDD(256)
244-
#define WDTO_2S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(512) | WDT_MR_WDD(512)
245-
#define WDTO_4S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(1024) | WDT_MR_WDD(1024)
246-
#define WDTO_8S WDT_MR_WDRSTEN | WDT_MR_WDRPROC | WDT_MR_WDV(2048) | WDT_MR_WDD(2048)
247-
248-
void wdt_enable (uint32_t mode);
249-
250-
void wdt_disable(void);
251-
252-
void wdt_reset(void);
253-
254235
#ifdef __cplusplus
255236
}
256237
#endif
@@ -293,6 +274,5 @@ extern USARTClass Serial3;
293274
#define SERIAL_PORT_HARDWARE2 Serial2
294275
#define SERIAL_PORT_HARDWARE3 Serial3
295276

296-
297277
#endif /* _VARIANT_ARDUINO_DUE_X_ */
298278

0 commit comments

Comments
 (0)