wdt.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include "wdt.h"
  47. #include <system.h>
  48. /**
  49. * \brief Sets up the WDT hardware module based on the configuration.
  50. *
  51. * Writes a given configuration of a WDT configuration to the
  52. * hardware module, and initializes the internal device struct.
  53. *
  54. * \param[in] config Pointer to the configuration struct
  55. *
  56. * \return Status of the configuration procedure.
  57. *
  58. * \retval STATUS_OK If the module was configured correctly
  59. * \retval STATUS_ERR_INVALID_ARG If invalid argument(s) were supplied
  60. * \retval STATUS_ERR_IO If the Watchdog module is locked to be always on
  61. */
  62. #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30)
  63. enum status_code wdt_set_config(
  64. const struct wdt_conf *const config)
  65. {
  66. /* Sanity check arguments */
  67. Assert(config);
  68. Wdt *const WDT_module = WDT;
  69. /* Turn on the digital interface clock */
  70. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBA, MCLK_APBAMASK_WDT);
  71. /* Check of the Watchdog has been locked to be always on, if so, abort */
  72. if (wdt_is_locked()) {
  73. return STATUS_ERR_IO;
  74. }
  75. /* Check for an invalid timeout period, abort if found */
  76. if (config->timeout_period == WDT_PERIOD_NONE) {
  77. return STATUS_ERR_INVALID_ARG;
  78. }
  79. /* Make sure the Window and Early Warning periods are not more than the
  80. * reset period, abort if either is invalid */
  81. if ((config->timeout_period < config->window_period) ||
  82. (config->timeout_period < config->early_warning_period)) {
  83. return STATUS_ERR_INVALID_ARG;
  84. }
  85. /* Disable the Watchdog module */
  86. WDT_module->CTRLA.reg &= ~WDT_CTRLA_ENABLE;
  87. while (wdt_is_syncing()) {
  88. /* Wait for all hardware modules to complete synchronization */
  89. }
  90. if(config->enable == false) {
  91. return STATUS_OK;
  92. }
  93. uint32_t new_config = 0;
  94. /* Update the timeout period value with the requested period */
  95. new_config |= (config->timeout_period - 1) << WDT_CONFIG_PER_Pos;
  96. /* Check if the user has requested a reset window period */
  97. if (config->window_period != WDT_PERIOD_NONE) {
  98. WDT_module->CTRLA.reg |= WDT_CTRLA_WEN;
  99. /* Update and enable the timeout period value */
  100. new_config |= (config->window_period - 1) << WDT_CONFIG_WINDOW_Pos;
  101. } else {
  102. /* Ensure the window enable control flag is cleared */
  103. WDT_module->CTRLA.reg &= ~WDT_CTRLA_WEN;
  104. }
  105. while (wdt_is_syncing()) {
  106. /* Wait for all hardware modules to complete synchronization */
  107. }
  108. /* Write the new Watchdog configuration */
  109. WDT_module->CONFIG.reg = new_config;
  110. /* Check if the user has requested an early warning period */
  111. if (config->early_warning_period != WDT_PERIOD_NONE) {
  112. /* Set the Early Warning period */
  113. WDT_module->EWCTRL.reg
  114. = (config->early_warning_period - 1) << WDT_EWCTRL_EWOFFSET_Pos;
  115. }
  116. /* Either enable or lock-enable the Watchdog timer depending on the user
  117. * settings */
  118. if (config->always_on) {
  119. WDT_module->CTRLA.reg |= WDT_CTRLA_ALWAYSON;
  120. } else {
  121. WDT_module->CTRLA.reg |= WDT_CTRLA_ENABLE;
  122. }
  123. while (wdt_is_syncing()) {
  124. /* Wait for all hardware modules to complete synchronization */
  125. }
  126. return STATUS_OK;
  127. }
  128. #else
  129. enum status_code wdt_set_config(
  130. const struct wdt_conf *const config)
  131. {
  132. /* Sanity check arguments */
  133. Assert(config);
  134. Wdt *const WDT_module = WDT;
  135. /* Turn on the digital interface clock */
  136. system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBA, PM_APBAMASK_WDT);
  137. /* Check of the Watchdog has been locked to be always on, if so, abort */
  138. if (wdt_is_locked()) {
  139. return STATUS_ERR_IO;
  140. }
  141. /* Check for an invalid timeout period, abort if found */
  142. if (config->timeout_period == WDT_PERIOD_NONE) {
  143. return STATUS_ERR_INVALID_ARG;
  144. }
  145. /* Make sure the Window and Early Warning periods are not more than the
  146. * reset period, abort if either is invalid */
  147. if ((config->timeout_period < config->window_period) ||
  148. (config->timeout_period < config->early_warning_period)) {
  149. return STATUS_ERR_INVALID_ARG;
  150. }
  151. /* Disable the Watchdog module */
  152. WDT_module->CTRL.reg &= ~WDT_CTRL_ENABLE;
  153. while (wdt_is_syncing()) {
  154. /* Wait for all hardware modules to complete synchronization */
  155. }
  156. if(config->enable == false) {
  157. return STATUS_OK;
  158. }
  159. /* Configure GCLK channel and enable clock */
  160. struct system_gclk_chan_config gclk_chan_conf;
  161. gclk_chan_conf.source_generator = config->clock_source;
  162. system_gclk_chan_set_config(WDT_GCLK_ID, &gclk_chan_conf);
  163. system_gclk_chan_enable(WDT_GCLK_ID);
  164. if (config->always_on) {
  165. system_gclk_chan_lock(WDT_GCLK_ID);
  166. }
  167. uint32_t new_config = 0;
  168. /* Update the timeout period value with the requested period */
  169. new_config |= (config->timeout_period - 1) << WDT_CONFIG_PER_Pos;
  170. /* Check if the user has requested a reset window period */
  171. if (config->window_period != WDT_PERIOD_NONE) {
  172. WDT_module->CTRL.reg |= WDT_CTRL_WEN;
  173. /* Update and enable the timeout period value */
  174. new_config |= (config->window_period - 1) << WDT_CONFIG_WINDOW_Pos;
  175. } else {
  176. /* Ensure the window enable control flag is cleared */
  177. WDT_module->CTRL.reg &= ~WDT_CTRL_WEN;
  178. }
  179. while (wdt_is_syncing()) {
  180. /* Wait for all hardware modules to complete synchronization */
  181. }
  182. /* Write the new Watchdog configuration */
  183. WDT_module->CONFIG.reg = new_config;
  184. /* Check if the user has requested an early warning period */
  185. if (config->early_warning_period != WDT_PERIOD_NONE) {
  186. while (wdt_is_syncing()) {
  187. /* Wait for all hardware modules to complete synchronization */
  188. }
  189. /* Set the Early Warning period */
  190. WDT_module->EWCTRL.reg
  191. = (config->early_warning_period - 1) << WDT_EWCTRL_EWOFFSET_Pos;
  192. }
  193. /* Either enable or lock-enable the Watchdog timer depending on the user
  194. * settings */
  195. if (config->always_on) {
  196. WDT_module->CTRL.reg |= WDT_CTRL_ALWAYSON;
  197. } else {
  198. WDT_module->CTRL.reg |= WDT_CTRL_ENABLE;
  199. }
  200. while (wdt_is_syncing()) {
  201. /* Wait for all hardware modules to complete synchronization */
  202. }
  203. return STATUS_OK;
  204. }
  205. #endif
  206. /**
  207. * \brief Resets the count of the running Watchdog Timer that was previously enabled.
  208. *
  209. * Resets the current count of the Watchdog Timer, restarting the timeout
  210. * period count elapsed. This function should be called after the window
  211. * period (if one was set in the module configuration) but before the timeout
  212. * period to prevent a reset of the system.
  213. */
  214. void wdt_reset_count(void)
  215. {
  216. Wdt *const WDT_module = WDT;
  217. /* Disable the Watchdog module */
  218. WDT_module->CLEAR.reg = WDT_CLEAR_CLEAR_KEY;
  219. while (wdt_is_syncing()) {
  220. /* Wait for all hardware modules to complete synchronization */
  221. }
  222. }