fsl_wwdt.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_WWDT_H_
  31. #define _FSL_WWDT_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup wwdt
  35. * @{
  36. */
  37. /*! @file */
  38. /*******************************************************************************
  39. * Definitions
  40. *******************************************************************************/
  41. /*! @name Driver version */
  42. /*@{*/
  43. /*! @brief Defines WWDT driver version 2.0.0. */
  44. #define FSL_WWDT_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  45. /*@}*/
  46. /*! @name Refresh sequence */
  47. /*@{*/
  48. #define WWDT_FIRST_WORD_OF_REFRESH (0xAAU) /*!< First word of refresh sequence */
  49. #define WWDT_SECOND_WORD_OF_REFRESH (0x55U) /*!< Second word of refresh sequence */
  50. /*@}*/
  51. /*! @brief Describes WWDT configuration structure. */
  52. typedef struct _wwdt_config
  53. {
  54. bool enableWwdt; /*!< Enables or disables WWDT */
  55. bool enableWatchdogReset; /*!< true: Watchdog timeout will cause a chip reset
  56. false: Watchdog timeout will not cause a chip reset */
  57. bool enableWatchdogProtect; /*!< true: Enable watchdog protect i.e timeout value can only be
  58. changed after counter is below warning & window values
  59. false: Disable watchdog protect; timeout value can be changed
  60. at any time */
  61. bool enableLockOscillator; /*!< true: Disabling or powering down the watchdog oscillator is prevented
  62. Once set, this bit can only be cleared by a reset
  63. false: Do not lock oscillator */
  64. uint32_t windowValue; /*!< Window value, set this to 0xFFFFFF if windowing is not in effect */
  65. uint32_t timeoutValue; /*!< Timeout value */
  66. uint32_t warningValue; /*!< Watchdog time counter value that will generate a
  67. warning interrupt. Set this to 0 for no warning */
  68. } wwdt_config_t;
  69. /*!
  70. * @brief WWDT status flags.
  71. *
  72. * This structure contains the WWDT status flags for use in the WWDT functions.
  73. */
  74. enum _wwdt_status_flags_t
  75. {
  76. kWWDT_TimeoutFlag = WWDT_MOD_WDTOF_MASK, /*!< Time-out flag, set when the timer times out */
  77. kWWDT_WarningFlag = WWDT_MOD_WDINT_MASK /*!< Warning interrupt flag, set when timer is below the value WDWARNINT */
  78. };
  79. /*******************************************************************************
  80. * API
  81. *******************************************************************************/
  82. #if defined(__cplusplus)
  83. extern "C" {
  84. #endif /* __cplusplus */
  85. /*!
  86. * @name WWDT Initialization and De-initialization
  87. * @{
  88. */
  89. /*!
  90. * @brief Initializes WWDT configure sturcture.
  91. *
  92. * This function initializes the WWDT configure structure to default value. The default
  93. * value are:
  94. * @code
  95. * config->enableWwdt = true;
  96. * config->enableWatchdogReset = false;
  97. * config->enableWatchdogProtect = false;
  98. * config->enableLockOscillator = false;
  99. * config->windowValue = 0xFFFFFFU;
  100. * config->timeoutValue = 0xFFFFFFU;
  101. * config->warningValue = 0;
  102. * @endcode
  103. *
  104. * @param config Pointer to WWDT config structure.
  105. * @see wwdt_config_t
  106. */
  107. void WWDT_GetDefaultConfig(wwdt_config_t *config);
  108. /*!
  109. * @brief Initializes the WWDT.
  110. *
  111. * This function initializes the WWDT. When called, the WWDT runs according to the configuration.
  112. *
  113. * Example:
  114. * @code
  115. * wwdt_config_t config;
  116. * WWDT_GetDefaultConfig(&config);
  117. * config.timeoutValue = 0x7ffU;
  118. * WWDT_Init(wwdt_base,&config);
  119. * @endcode
  120. *
  121. * @param base WWDT peripheral base address
  122. * @param config The configuration of WWDT
  123. */
  124. void WWDT_Init(WWDT_Type *base, const wwdt_config_t *config);
  125. /*!
  126. * @brief Shuts down the WWDT.
  127. *
  128. * This function shuts down the WWDT.
  129. *
  130. * @param base WWDT peripheral base address
  131. */
  132. void WWDT_Deinit(WWDT_Type *base);
  133. /* @} */
  134. /*!
  135. * @name WWDT Functional Operation
  136. * @{
  137. */
  138. /*!
  139. * @brief Enables the WWDT module.
  140. *
  141. * This function write value into WWDT_MOD register to enable the WWDT, it is a write-once bit;
  142. * once this bit is set to one and a watchdog feed is performed, the watchdog timer will run
  143. * permanently.
  144. *
  145. * @param base WWDT peripheral base address
  146. */
  147. static inline void WWDT_Enable(WWDT_Type *base)
  148. {
  149. base->MOD |= WWDT_MOD_WDEN_MASK;
  150. }
  151. /*!
  152. * @brief Disables the WWDT module.
  153. *
  154. * This function write value into WWDT_MOD register to disable the WWDT.
  155. *
  156. * @param base WWDT peripheral base address
  157. */
  158. static inline void WWDT_Disable(WWDT_Type *base)
  159. {
  160. base->MOD &= ~WWDT_MOD_WDEN_MASK;
  161. }
  162. /*!
  163. * @brief Gets all WWDT status flags.
  164. *
  165. * This function gets all status flags.
  166. *
  167. * Example for getting Timeout Flag:
  168. * @code
  169. * uint32_t status;
  170. * status = WWDT_GetStatusFlags(wwdt_base) & kWWDT_TimeoutFlag;
  171. * @endcode
  172. * @param base WWDT peripheral base address
  173. * @return The status flags. This is the logical OR of members of the
  174. * enumeration ::_wwdt_status_flags_t
  175. */
  176. static inline uint32_t WWDT_GetStatusFlags(WWDT_Type *base)
  177. {
  178. return (base->MOD & (WWDT_MOD_WDTOF_MASK | WWDT_MOD_WDINT_MASK));
  179. }
  180. /*!
  181. * @brief Clear WWDT flag.
  182. *
  183. * This function clears WWDT status flag.
  184. *
  185. * Example for clearing warning flag:
  186. * @code
  187. * WWDT_ClearStatusFlags(wwdt_base, kWWDT_WarningFlag);
  188. * @endcode
  189. * @param base WWDT peripheral base address
  190. * @param mask The status flags to clear. This is a logical OR of members of the
  191. * enumeration ::_wwdt_status_flags_t
  192. */
  193. void WWDT_ClearStatusFlags(WWDT_Type *base, uint32_t mask);
  194. /*!
  195. * @brief Set the WWDT warning value.
  196. *
  197. * The WDWARNINT register determines the watchdog timer counter value that will generate a watchdog
  198. * interrupt. When the watchdog timer counter is no longer greater than the value defined by
  199. * WARNINT, an interrupt will be generated after the subsequent WDCLK.
  200. *
  201. * @param base WWDT peripheral base address
  202. * @param warningValue WWDT warning value.
  203. */
  204. static inline void WWDT_SetWarningValue(WWDT_Type *base, uint32_t warningValue)
  205. {
  206. base->WARNINT = WWDT_WARNINT_WARNINT(warningValue);
  207. }
  208. /*!
  209. * @brief Set the WWDT timeout value.
  210. *
  211. * This function sets the timeout value. Every time a feed sequence occurs the value in the TC
  212. * register is loaded into the Watchdog timer. Writing a value below 0xFF will cause 0xFF to be
  213. * loaded into the TC register. Thus the minimum time-out interval is TWDCLK*256*4.
  214. * If enableWatchdogProtect flag is true in wwdt_config_t config structure, any attempt to change
  215. * the timeout value before the watchdog counter is below the warning and window values
  216. * will cause a watchdog reset and set the WDTOF flag.
  217. *
  218. * @param base WWDT peripheral base address
  219. * @param timeoutCount WWDT timeout value, count of WWDT clock tick.
  220. */
  221. static inline void WWDT_SetTimeoutValue(WWDT_Type *base, uint32_t timeoutCount)
  222. {
  223. base->TC = WWDT_TC_COUNT(timeoutCount);
  224. }
  225. /*!
  226. * @brief Sets the WWDT window value.
  227. *
  228. * The WINDOW register determines the highest TV value allowed when a watchdog feed is performed.
  229. * If a feed sequence occurs when timer value is greater than the value in WINDOW, a watchdog
  230. * event will occur. To disable windowing, set windowValue to 0xFFFFFF (maximum possible timer
  231. * value) so windowing is not in effect.
  232. *
  233. * @param base WWDT peripheral base address
  234. * @param windowValue WWDT window value.
  235. */
  236. static inline void WWDT_SetWindowValue(WWDT_Type *base, uint32_t windowValue)
  237. {
  238. base->WINDOW = WWDT_WINDOW_WINDOW(windowValue);
  239. }
  240. /*!
  241. * @brief Refreshes the WWDT timer.
  242. *
  243. * This function feeds the WWDT.
  244. * This function should be called before WWDT timer is in timeout. Otherwise, a reset is asserted.
  245. *
  246. * @param base WWDT peripheral base address
  247. */
  248. void WWDT_Refresh(WWDT_Type *base);
  249. /*@}*/
  250. #if defined(__cplusplus)
  251. }
  252. #endif /* __cplusplus */
  253. /*! @}*/
  254. #endif /* _FSL_WWDT_H_ */