wdt.h 17 KB

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