em_wdog.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Watchdog (WDOG) peripheral API
  4. * devices.
  5. * @author Energy Micro AS
  6. * @version 3.0.0
  7. *******************************************************************************
  8. * @section License
  9. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  10. *******************************************************************************
  11. *
  12. * Permission is granted to anyone to use this software for any purpose,
  13. * including commercial applications, and to alter it and redistribute it
  14. * freely, subject to the following restrictions:
  15. *
  16. * 1. The origin of this software must not be misrepresented; you must not
  17. * claim that you wrote the original software.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. *
  22. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  23. * obligation to support this Software. Energy Micro AS is providing the
  24. * Software "AS IS", with no express or implied warranties of any kind,
  25. * including, but not limited to, any implied warranties of merchantability
  26. * or fitness for any particular purpose or warranties against infringement
  27. * of any proprietary rights of a third party.
  28. *
  29. * Energy Micro AS will not be liable for any consequential, incidental, or
  30. * special damages, or any other relief, or for any claim by any third party,
  31. * arising from your use of this Software.
  32. *
  33. ******************************************************************************/
  34. #include "em_wdog.h"
  35. #include "em_bitband.h"
  36. /***************************************************************************//**
  37. * @addtogroup EM_Library
  38. * @{
  39. ******************************************************************************/
  40. /***************************************************************************//**
  41. * @addtogroup WDOG
  42. * @brief Watchdog (WDOG) Peripheral API
  43. * @{
  44. ******************************************************************************/
  45. /*******************************************************************************
  46. ************************** GLOBAL FUNCTIONS *******************************
  47. ******************************************************************************/
  48. /***************************************************************************//**
  49. * @brief
  50. * Enable/disable the watchdog timer.
  51. *
  52. * @note
  53. * This function modifies the WDOG CTRL register which requires
  54. * synchronization into the low frequency domain. If this register is modified
  55. * before a previous update to the same register has completed, this function
  56. * will stall until the previous synchronization has completed.
  57. *
  58. * @param[in] enable
  59. * true to enable watchdog, false to disable. Watchdog cannot be disabled if
  60. * watchdog has been locked.
  61. ******************************************************************************/
  62. void WDOG_Enable(bool enable)
  63. {
  64. if (!enable)
  65. {
  66. /* Wait for any pending previous write operation to have been completed in */
  67. /* low frequency domain */
  68. while (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CTRL)
  69. ;
  70. }
  71. BITBAND_Peripheral(&(WDOG->CTRL), _WDOG_CTRL_EN_SHIFT, (unsigned int)enable);
  72. }
  73. /***************************************************************************//**
  74. * @brief
  75. * Feed the watchdog.
  76. *
  77. * @details
  78. * When the watchdog is activated, it must be fed (ie clearing the counter)
  79. * before it reaches the defined timeout period. Otherwise, the watchdog
  80. * will generate a reset.
  81. ******************************************************************************/
  82. void WDOG_Feed(void)
  83. {
  84. /* If a previous clearing is being synchronized to LF domain, then there */
  85. /* is no point in waiting for it to complete before clearing over again. */
  86. /* This avoids stalling the core in the typical use case where some idle loop */
  87. /* keeps clearing the watchdog. */
  88. if (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CMD)
  89. return;
  90. WDOG->CMD = WDOG_CMD_CLEAR;
  91. }
  92. /***************************************************************************//**
  93. * @brief
  94. * Initialize watchdog (assuming the watchdog configuration has not been
  95. * locked).
  96. *
  97. * @note
  98. * This function modifies the WDOG CTRL register which requires
  99. * synchronization into the low frequency domain. If this register is modified
  100. * before a previous update to the same register has completed, this function
  101. * will stall until the previous synchronization has completed.
  102. *
  103. * @param[in] init
  104. * Structure holding watchdog configuration. A default setting
  105. * #WDOG_INIT_DEFAULT is available for init.
  106. ******************************************************************************/
  107. void WDOG_Init(const WDOG_Init_TypeDef *init)
  108. {
  109. uint32_t setting;
  110. if (init->enable)
  111. {
  112. setting = WDOG_CTRL_EN;
  113. }
  114. else
  115. {
  116. setting = 0;
  117. }
  118. if (init->debugRun)
  119. {
  120. setting |= WDOG_CTRL_DEBUGRUN;
  121. }
  122. if (init->em2Run)
  123. {
  124. setting |= WDOG_CTRL_EM2RUN;
  125. }
  126. if (init->em3Run)
  127. {
  128. setting |= WDOG_CTRL_EM3RUN;
  129. }
  130. if (init->em4Block)
  131. {
  132. setting |= WDOG_CTRL_EM4BLOCK;
  133. }
  134. if (init->swoscBlock)
  135. {
  136. setting |= WDOG_CTRL_SWOSCBLOCK;
  137. }
  138. setting |= ((uint32_t)(init->clkSel) << _WDOG_CTRL_CLKSEL_SHIFT) |
  139. ((uint32_t)(init->perSel) << _WDOG_CTRL_PERSEL_SHIFT);
  140. /* Wait for any pending previous write operation to have been completed in */
  141. /* low frequency domain */
  142. while (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CTRL)
  143. ;
  144. WDOG->CTRL = setting;
  145. /* Optional register locking */
  146. if (init->lock)
  147. {
  148. if (init->enable)
  149. {
  150. WDOG_Lock();
  151. }
  152. else
  153. {
  154. BITBAND_Peripheral(&(WDOG->CTRL), _WDOG_CTRL_LOCK_SHIFT, 1);
  155. }
  156. }
  157. }
  158. /***************************************************************************//**
  159. * @brief
  160. * Lock the watchdog configuration.
  161. *
  162. * @details
  163. * This prevents errors from overwriting the watchdog configuration, possibly
  164. * disabling it. Only a reset can unlock the watchdog config, once locked.
  165. *
  166. * If the LFRCO or LFXO clocks are used to clock the watchdog, one should
  167. * consider using the option of inhibiting those clocks to be disabled,
  168. * please see the WDOG_Enable() init structure.
  169. *
  170. * @note
  171. * This function modifies the WDOG CTRL register which requires
  172. * synchronization into the low frequency domain. If this register is modified
  173. * before a previous update to the same register has completed, this function
  174. * will stall until the previous synchronization has completed.
  175. ******************************************************************************/
  176. void WDOG_Lock(void)
  177. {
  178. /* Wait for any pending previous write operation to have been completed in */
  179. /* low frequency domain */
  180. while (WDOG->SYNCBUSY & WDOG_SYNCBUSY_CTRL)
  181. ;
  182. /* Disable writing to the control register */
  183. BITBAND_Peripheral(&(WDOG->CTRL), _WDOG_CTRL_LOCK_SHIFT, 1);
  184. }
  185. /** @} (end addtogroup WDOG) */
  186. /** @} (end addtogroup EM_Library) */