1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
10 Sets up the watchdog timer (WDT). The WDT allows you to monitor
11 whether the device is still executing properly. If the watchdog
12 isn't 'fed' within a pre-determined delay, it will raise an interrupt
13 allowing you to decide if you want to reset the device, etc.
16 #include "core/cpu/cpu.h"
17 #include "core/wdt/wdt.h"
21 // Initialise wdt with no reset on timeout
24 // Pat the watchdog (to start the timer)
29 // Keep the watchdog happy by regularly feeding it
36 Software License Agreement (BSD License)
38 Copyright (c) 2010, microBuilder SARL
41 Redistribution and use in source and binary forms, with or without
42 modification, are permitted provided that the following conditions are met:
43 1. Redistributions of source code must retain the above copyright
44 notice, this list of conditions and the following disclaimer.
45 2. Redistributions in binary form must reproduce the above copyright
46 notice, this list of conditions and the following disclaimer in the
47 documentation and/or other materials provided with the distribution.
48 3. Neither the name of the copyright holders nor the
49 names of its contributors may be used to endorse or promote products
50 derived from this software without specific prior written permission.
52 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
53 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
56 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
58 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
59 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 /**************************************************************************/
67 #define WDT_FEED_VALUE (0x003FFFFF)
69 volatile uint32_t wdt_counter
;
71 /**************************************************************************/
73 IRQ Handler when the watchdog times out. Any actions that you wish
74 to take when a timeout occurs should be called from here.
76 /**************************************************************************/
77 void WDT_IRQHandler(void)
79 /* Clear the time-out interrupt flag */
80 WDT_WDMOD
&= ~WDT_WDMOD_WDTOF
;
84 /**************************************************************************/
86 Setup the clock for the watchdog timer. The default setting is 250kHz.
88 /**************************************************************************/
89 static void wdtClockSetup (void)
91 /* Watchdog Configuration */
92 /* Freq. = 0.5MHz, div = 2: WDT_OSC = 250kHz */
93 SCB_WDTOSCCTRL
= SCB_WDTOSCCTRL_FREQSEL_0_5MHZ
|
94 SCB_WDTOSCCTRL_DIVSEL_DIV2
;
96 /* Set clock source (use WDT oscillator) */
97 SCB_WDTCLKSEL
= SCB_WDTCLKSEL_SOURCE_WATCHDOGOSC
;
98 SCB_WDTCLKUEN
= SCB_WDTCLKUEN_UPDATE
;
99 SCB_WDTCLKUEN
= SCB_WDTCLKUEN_DISABLE
;
100 SCB_WDTCLKUEN
= SCB_WDTCLKUEN_UPDATE
;
102 /* Wait until updated */
103 while (!(SCB_WDTCLKUEN
& SCB_WDTCLKUEN_UPDATE
));
106 SCB_WDTCLKDIV
= SCB_WDTCLKDIV_DIV1
;
108 /* Enable WDT clock */
109 SCB_PDRUNCFG
&= ~(SCB_PDRUNCFG_WDTOSC
);
112 /**************************************************************************/
114 Initialises the watchdog timer and sets up the interrupt.
116 /**************************************************************************/
117 void wdtInit (bool reset
)
119 /* Setup the WDT clock */
122 /* Enable AHB clock to the WDT domain. */
123 SCB_SYSAHBCLKCTRL
|= SCB_SYSAHBCLKCTRL_WDT
;
127 /* Enable the WDT interrupt */
128 NVIC_EnableIRQ(WDT_IRQn
);
130 /* Set timeout value (must be at least 0x000000FF) */
131 WDT_WDTC
= WDT_FEED_VALUE
;
133 /* Enable the watchdog timer (without system reset) */
134 WDT_WDMOD
= WDT_WDMOD_WDEN_ENABLED
|
135 reset
? WDT_WDMOD_WDRESET_ENABLED
: WDT_WDMOD_WDRESET_DISABLED
;
138 /**************************************************************************/
140 Feeds the watchdog to keep it from timing out. Interrupts will be
141 disabled while feeding the watchdog.
143 /**************************************************************************/
146 /* Pet the watchdog */
148 WDT_WDFEED
= WDT_WDFEED_FEED1
;
149 WDT_WDFEED
= WDT_WDFEED_FEED2
;