1 /**
2 * \file
3 *
4 * \brief SAM Watchdog Driver
5 *
6 * Copyright (c) 2012-2016 Atmel Corporation. All rights reserved.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 *
22 * 3. The name of Atmel may not be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * 4. This software may only be redistributed and used in connection with an
26 * Atmel microcontroller product.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 *
40 * \asf_license_stop
41 *
42 */
43 /*
44 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45 */
46 #ifndef WDT_H_INCLUDED
47 #define WDT_H_INCLUDED
48
49 /**
50 * \defgroup asfdoc_sam0_wdt_group SAM Watchdog (WDT) Driver
51 *
52 * This driver for Atmel® | SMART ARM®-based microcontrollers provides
53 * an interface for the configuration and management of the device's Watchdog
54 * Timer module, including the enabling, disabling, and kicking within the device.
55 * The following driver API modes are covered by this manual:
56 *
57 * - Polled APIs
58 * \if WDT_CALLBACK_MODE
59 * - Callback APIs
60 * \endif
61 *
62 * The following peripherals are used by this module:
63 * - WDT (Watchdog Timer)
64 *
65 * The following devices can use this module:
66 * - Atmel | SMART SAM D20/D21
67 * - Atmel | SMART SAM R21
68 * - Atmel | SMART SAM D09/D10/D11
69 * - Atmel | SMART SAM L21/L22
70 * - Atmel | SMART SAM DA1
71 * - Atmel | SMART SAM C20/C21
72 * - Atmel | SMART SAM HA1
73 * - Atmel | SMART SAM R30
74 *
75 * The outline of this documentation is as follows:
76 * - \ref asfdoc_sam0_wdt_prerequisites
77 * - \ref asfdoc_sam0_wdt_module_overview
78 * - \ref asfdoc_sam0_wdt_special_considerations
79 * - \ref asfdoc_sam0_wdt_extra_info
80 * - \ref asfdoc_sam0_wdt_examples
81 * - \ref asfdoc_sam0_wdt_api_overview
82 *
83 *
84 * \section asfdoc_sam0_wdt_prerequisites Prerequisites
85 *
86 * There are no prerequisites for this module.
87 *
88 *
89 * \section asfdoc_sam0_wdt_module_overview Module Overview
90 *
91 * The Watchdog module (WDT) is designed to give an added level of safety in
92 * critical systems, to ensure a system reset is triggered in the case of a
93 * deadlock or other software malfunction that prevents normal device operation.
94 *
95 * At a basic level, the Watchdog is a system timer with a fixed period; once
96 * enabled, it will continue to count ticks of its asynchronous clock until
97 * it is periodically reset, or the timeout period is reached. In the event of a
98 * Watchdog timeout, the module will trigger a system reset identical to a pulse
99 * of the device's reset pin, resetting all peripherals to their power-on
100 * default states and restarting the application software from the reset vector.
101 *
102 * In many systems, there is an obvious upper bound to the amount of time each
103 * iteration of the main application loop can be expected to run, before a
104 * malfunction can be assumed (either due to a deadlock waiting on hardware or
105 * software, or due to other means). When the Watchdog is configured with a
106 * timeout period equal to this upper bound, a malfunction in the system will
107 * force a full system reset to allow for a graceful recovery.
108 *
109 * \subsection asfdoc_sam0_wdt_module_locked_mode Locked Mode
110 * The Watchdog configuration can be set in the device fuses and locked in
111 * hardware, so that no software changes can be made to the Watchdog
112 * configuration. Additionally, the Watchdog can be locked on in software if it
113 * is not already locked, so that the module configuration cannot be modified
114 * until a power on reset of the device.
115 *
116 * The locked configuration can be used to ensure that faulty software does not
117 * cause the Watchdog configuration to be changed, preserving the level of
118 * safety given by the module.
119 *
120 * \subsection asfdoc_sam0_wdt_module_window_mode Window Mode
121 * Just as there is a reasonable upper bound to the time the main program loop
122 * should take for each iteration, there is also in many applications a lower
123 * bound, i.e. a \a minimum time for which each loop iteration should run for
124 * under normal circumstances. To guard against a system failure resetting the
125 * Watchdog in a tight loop (or a failure in the system application causing the
126 * main loop to run faster than expected) a "Window" mode can be enabled to
127 * disallow resetting of the Watchdog counter before a certain period of time.
128 * If the Watchdog is not reset \a after the window opens but not \a before the
129 * Watchdog expires, the system will reset.
130 *
131 * \subsection asfdoc_sam0_wdt_module_early_warning Early Warning
132 * In some cases it is desirable to receive an early warning that the Watchdog is
133 * about to expire, so that some system action (such as saving any system
134 * configuration data for failure analysis purposes) can be performed before the
135 * system reset occurs. The Early Warning feature of the Watchdog module allows
136 * such a notification to be requested; after the configured early warning time
137 * (but before the expiry of the Watchdog counter) the Early Warning flag will
138 * become set, so that the user application can take an appropriate action.
139 *
140 * \note It is important to note that the purpose of the Early Warning feature
141 * is \a not to allow the user application to reset the Watchdog; doing
142 * so will defeat the safety the module gives to the user application.
143 * Instead, this feature should be used purely to perform any tasks that
144 * need to be undertaken before the system reset occurs.
145 *
146 * \subsection asfdoc_sam0_wdt_module_overview_physical Physical Connection
147 *
148 * \ref asfdoc_sam0_wdt_module_int_connections "The figure below" shows how
149 * this module is interconnected within the device.
150 *
151 * \anchor asfdoc_sam0_wdt_module_int_connections
152 * \dot
153 * digraph overview {
154 * rankdir=LR;
155 * node [label="GCLK*\nGeneric Clock" shape=square] wdt_clock;
156 *
157 * subgraph driver {
158 * node [label="<f0> WDT | <f1> Watchdog Counter" shape=record] wdt_module;
159 * node [label="System Reset Logic" shape=ellipse style=filled fillcolor=lightgray] sys_reset;
160 * }
161 *
162 * wdt_clock -> wdt_module:f1;
163 * wdt_module:f1 -> sys_reset;
164 * }
165 * \enddot
166 *
167 * \note Watchdog Counter of SAM L21/L22/R30 is \a not provided by GCLK, but it uses an
168 * internal 1KHz OSCULP32K output clock.
169 *
170 * \section asfdoc_sam0_wdt_special_considerations Special Considerations
171 *
172 * On some devices the Watchdog configuration can be fused to be always on in
173 * a particular configuration; if this mode is enabled the Watchdog is not
174 * software configurable and can have its count reset and early warning state
175 * checked/cleared only.
176 *
177 * \section asfdoc_sam0_wdt_extra_info Extra Information
178 *
179 * For extra information, see \ref asfdoc_sam0_wdt_extra. This includes:
180 * - \ref asfdoc_sam0_wdt_extra_acronyms
181 * - \ref asfdoc_sam0_wdt_extra_dependencies
182 * - \ref asfdoc_sam0_wdt_extra_errata
183 * - \ref asfdoc_sam0_wdt_extra_history
184 *
185 *
186 * \section asfdoc_sam0_wdt_examples Examples
187 *
188 * For a list of examples related to this driver, see
189 * \ref asfdoc_sam0_wdt_exqsg.
190 *
191 * \section asfdoc_sam0_wdt_api_overview API Overview
192 * @{
193 */
194
195 #include <compiler.h>
196 #include <clock.h>
197 #include <gclk.h>
198
199 #if WDT_CALLBACK_MODE == true
200 # include "wdt_callback.h"
201 #endif
202
203 #ifdef __cplusplus
204 extern "C" {
205 #endif
206
207 /**
208 * \brief Watchdog Timer period configuration enum.
209 *
210 * Enum for the possible period settings of the Watchdog timer module, for
211 * values requiring a period as a number of Watchdog timer clock ticks.
212 */
213 enum wdt_period {
214 /** No Watchdog period. This value can only be used when setting the
215 * Window and Early Warning periods; its use as the Watchdog Reset
216 * Period is invalid. */
217 WDT_PERIOD_NONE = 0,
218 /** Watchdog period of 8 clocks of the Watchdog Timer Generic Clock */
219 WDT_PERIOD_8CLK = 1,
220 /** Watchdog period of 16 clocks of the Watchdog Timer Generic Clock */
221 WDT_PERIOD_16CLK = 2,
222 /** Watchdog period of 32 clocks of the Watchdog Timer Generic Clock */
223 WDT_PERIOD_32CLK = 3,
224 /** Watchdog period of 64 clocks of the Watchdog Timer Generic Clock */
225 WDT_PERIOD_64CLK = 4,
226 /** Watchdog period of 128 clocks of the Watchdog Timer Generic Clock */
227 WDT_PERIOD_128CLK = 5,
228 /** Watchdog period of 256 clocks of the Watchdog Timer Generic Clock */
229 WDT_PERIOD_256CLK = 6,
230 /** Watchdog period of 512 clocks of the Watchdog Timer Generic Clock */
231 WDT_PERIOD_512CLK = 7,
232 /** Watchdog period of 1024 clocks of the Watchdog Timer Generic Clock */
233 WDT_PERIOD_1024CLK = 8,
234 /** Watchdog period of 2048 clocks of the Watchdog Timer Generic Clock */
235 WDT_PERIOD_2048CLK = 9,
236 /** Watchdog period of 4096 clocks of the Watchdog Timer Generic Clock */
237 WDT_PERIOD_4096CLK = 10,
238 /** Watchdog period of 8192 clocks of the Watchdog Timer Generic Clock */
239 WDT_PERIOD_8192CLK = 11,
240 /** Watchdog period of 16384 clocks of the Watchdog Timer Generic Clock */
241 WDT_PERIOD_16384CLK = 12,
242 };
243
244 /**
245 * \brief Watchdog Timer configuration structure.
246 *
247 * Configuration structure for a Watchdog Timer instance. This
248 * structure should be initialized by the \ref wdt_get_config_defaults()
249 * function before being modified by the user application.
250 */
251 struct wdt_conf {
252 /** If \c true, the Watchdog will be locked to the current configuration
253 * settings when the Watchdog is enabled */
254 bool always_on;
255 /** Enable/Disable the Watchdog Timer */
256 bool enable;
257 #if !(SAML21) && !(SAML22) && !(SAMC20) && !(SAMC21) && !(SAMR30)
258 /** GCLK generator used to clock the peripheral except SAM L21/L22/C21/C20/R30*/
259 enum gclk_generator clock_source;
260 #endif
261 /** Number of Watchdog timer clock ticks until the Watchdog expires */
262 enum wdt_period timeout_period;
263 /** Number of Watchdog timer clock ticks until the reset window opens */
264 enum wdt_period window_period;
265 /** Number of Watchdog timer clock ticks until the early warning flag is
266 * set */
267 enum wdt_period early_warning_period;
268 };
269
270 /** \name Configuration and Initialization
271 * @{
272 */
273
274 /**
275 * \brief Determines if the hardware module(s) are currently synchronizing to the bus.
276 *
277 * Checks to see if the underlying hardware peripheral module(s) are currently
278 * synchronizing across multiple clock domains to the hardware bus. This
279 * function can be used to delay further operations on a module until such time
280 * that it is ready, to prevent blocking delays for synchronization in the
281 * user application.
282 *
283 * \return Synchronization status of the underlying hardware module(s).
284 *
285 * \retval false If the module has completed synchronization
286 * \retval true If the module synchronization is ongoing
287 */
wdt_is_syncing(void)288 static inline bool wdt_is_syncing(void)
289 {
290 Wdt *const WDT_module = WDT;
291
292 #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
293 if (WDT_module->SYNCBUSY.reg) {
294 #else
295 if (WDT_module->STATUS.reg & WDT_STATUS_SYNCBUSY) {
296 #endif
297 return true;
298 }
299
300 return false;
301 }
302
303 /**
304 * \brief Initializes a Watchdog Timer configuration structure to defaults.
305 *
306 * Initializes a given Watchdog Timer configuration structure to a set of
307 * known default values. This function should be called on all new
308 * instances of these configuration structures before being modified by the
309 * user application.
310 *
311 * The default configuration is as follows:
312 * \li Not locked, to allow for further (re-)configuration
313 * \li Enable WDT
314 * \li Watchdog timer sourced from Generic Clock Channel 4
315 * \li A timeout period of 16384 clocks of the Watchdog module clock
316 * \li No window period, so that the Watchdog count can be reset at any time
317 * \li No early warning period to indicate the Watchdog will soon expire
318 *
319 * \param[out] config Configuration structure to initialize to default values
320 */
321 static inline void wdt_get_config_defaults(
322 struct wdt_conf *const config)
323 {
324 /* Sanity check arguments */
325 Assert(config);
326
327 /* Default configuration values */
328 config->always_on = false;
329 config->enable = true;
330 #if !(SAML21) && !(SAML22) && !(SAMC20) && !(SAMC21) && !(SAMR30)
331 config->clock_source = GCLK_GENERATOR_4;
332 #endif
333 config->timeout_period = WDT_PERIOD_16384CLK;
334 config->window_period = WDT_PERIOD_NONE;
335 config->early_warning_period = WDT_PERIOD_NONE;
336 }
337
338 enum status_code wdt_set_config(
339 const struct wdt_conf *const config);
340
341 /** \brief Determines if the Watchdog timer is currently locked in an enabled state.
342 *
343 * Determines if the Watchdog timer is currently enabled and locked, so that
344 * it cannot be disabled or otherwise reconfigured.
345 *
346 * \return Current Watchdog lock state.
347 */
348 static inline bool wdt_is_locked(void)
349 {
350 Wdt *const WDT_module = WDT;
351
352 #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
353 return (WDT_module->CTRLA.reg & WDT_CTRLA_ALWAYSON);
354 #else
355 return (WDT_module->CTRL.reg & WDT_CTRL_ALWAYSON);
356 #endif
357 }
358
359 /** @} */
360
361 /** \name Timeout and Early Warning Management
362 * @{
363 */
364
365 /** \brief Clears the Watchdog timer early warning period elapsed flag.
366 *
367 * Clears the Watchdog timer early warning period elapsed flag, so that a new
368 * early warning period can be detected.
369 */
370 static inline void wdt_clear_early_warning(void)
371 {
372 Wdt *const WDT_module = WDT;
373
374 WDT_module->INTFLAG.reg = WDT_INTFLAG_EW;
375 }
376
377 /** \brief Determines if the Watchdog timer early warning period has elapsed.
378 *
379 * Determines if the Watchdog timer early warning period has elapsed.
380 *
381 * \note If no early warning period was configured, the value returned by this
382 * function is invalid.
383 *
384 * \return Current Watchdog Early Warning state.
385 */
386 static inline bool wdt_is_early_warning(void)
387 {
388 Wdt *const WDT_module = WDT;
389
390 return (WDT_module->INTFLAG.reg & WDT_INTFLAG_EW);
391 }
392
393 void wdt_reset_count(void);
394
395 /** @} */
396
397 #ifdef __cplusplus
398 }
399 #endif
400
401 /** @} */
402
403 /**
404 * \page asfdoc_sam0_wdt_extra Extra Information for WDT Driver
405 *
406 * \section asfdoc_sam0_wdt_extra_acronyms Acronyms
407 * The table below presents the acronyms used in this module:
408 *
409 * <table>
410 * <tr>
411 * <th>Acronym</th>
412 * <th>Description</th>
413 * </tr>
414 * <tr>
415 * <td>WDT</td>
416 * <td>Watchdog Timer</td>
417 * </tr>
418 * </table>
419 *
420 *
421 * \section asfdoc_sam0_wdt_extra_dependencies Dependencies
422 * This driver has the following dependencies:
423 *
424 * - \ref asfdoc_sam0_system_clock_group "System Clock Driver"
425 *
426 *
427 * \section asfdoc_sam0_wdt_extra_errata Errata
428 * There are no errata related to this driver.
429 *
430 *
431 * \section asfdoc_sam0_wdt_extra_history Module History
432 * An overview of the module history is presented in the table below, with
433 * details on the enhancements and fixes made to the module since its first
434 * release. The current version of this corresponds to the newest version in
435 * the table.
436 *
437 * <table>
438 * <tr>
439 * <th>Changelog</th>
440 * </tr>
441 * <tr>
442 * <td>Driver updated to follow driver type convention:
443 * \li wdt_init, wdt_enable, wdt_disable functions removed
444 * \li wdt_set_config function added
445 * \li WDT module enable state moved inside the configuration struct </td>
446 * </tr>
447 * <tr>
448 * <td>Initial Release</td>
449 * </tr>
450 * </table>
451 */
452
453 /**
454 * \page asfdoc_sam0_wdt_exqsg Examples for WDT Driver
455 *
456 * This is a list of the available Quick Start guides (QSGs) and example
457 * applications for \ref asfdoc_sam0_wdt_group. QSGs are simple examples with
458 * step-by-step instructions to configure and use this driver in a selection of
459 * use cases. Note that a QSG can be compiled as a standalone application or be
460 * added to the user application.
461 *
462 * - \subpage asfdoc_sam0_wdt_basic_use_case
463 * \if WDT_CALLBACK_MODE
464 * - \subpage asfdoc_sam0_wdt_callback_use_case
465 * \endif
466 *
467 * \page asfdoc_sam0_wdt_document_revision_history Document Revision History
468 *
469 * <table>
470 * <tr>
471 * <th>Doc. Rev.</th>
472 * <th>Date</th>
473 * <th>Comments</th>
474 * </tr>
475 * <tr>
476 * <td>42124E</td>
477 * <td>12/2015</td>
478 * <td>Added support for SAM L21/L22, SAM DA1, SAM D09, SAM R30, and SAM C20/C21</td>
479 * </tr>
480 * <tr>
481 * <td>42124D</td>
482 * <td>12/2014</td>
483 * <td>Added SAM R21 and SAM D10/D11 support</td>
484 * </tr>
485 * <tr>
486 * <td>42124C</td>
487 * <td>01/2014</td>
488 * <td>Add SAM D21 support</td>
489 * </tr>
490 * <tr>
491 * <td>42124B</td>
492 * <td>06/2013</td>
493 * <td>Corrected documentation typos</td>
494 * </tr>
495 * <tr>
496 * <td>42124A</td>
497 * <td>06/2013</td>
498 * <td>Initial release</td>
499 * </tr>
500 * </table>
501 */
502
503 #endif /* WDT_H_INCLUDED */
504