fsl_wdog.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * Copyright (c) 2015, 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_WDOG_H_
  31. #define _FSL_WDOG_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup wdog
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. *******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief Defines WDOG driver version 2.0.0. */
  43. #define FSL_WDOG_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. /*! @name Unlock sequence */
  46. /*@{*/
  47. #define WDOG_FIRST_WORD_OF_UNLOCK (0xC520U) /*!< First word of unlock sequence */
  48. #define WDOG_SECOND_WORD_OF_UNLOCK (0xD928U) /*!< Second word of unlock sequence */
  49. /*@}*/
  50. /*! @name Refresh sequence */
  51. /*@{*/
  52. #define WDOG_FIRST_WORD_OF_REFRESH (0xA602U) /*!< First word of refresh sequence */
  53. #define WDOG_SECOND_WORD_OF_REFRESH (0xB480U) /*!< Second word of refresh sequence */
  54. /*@}*/
  55. /*! @brief Describes WDOG clock source. */
  56. typedef enum _wdog_clock_source
  57. {
  58. kWDOG_LpoClockSource = 0U, /*!< WDOG clock sourced from LPO*/
  59. kWDOG_AlternateClockSource = 1U, /*!< WDOG clock sourced from alternate clock source*/
  60. } wdog_clock_source_t;
  61. /*! @brief Defines WDOG work mode. */
  62. typedef struct _wdog_work_mode
  63. {
  64. #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
  65. bool enableWait; /*!< Enables or disables WDOG in wait mode */
  66. #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
  67. bool enableStop; /*!< Enables or disables WDOG in stop mode */
  68. bool enableDebug; /*!< Enables or disables WDOG in debug mode */
  69. } wdog_work_mode_t;
  70. /*! @brief Describes the selection of the clock prescaler. */
  71. typedef enum _wdog_clock_prescaler
  72. {
  73. kWDOG_ClockPrescalerDivide1 = 0x0U, /*!< Divided by 1 */
  74. kWDOG_ClockPrescalerDivide2 = 0x1U, /*!< Divided by 2 */
  75. kWDOG_ClockPrescalerDivide3 = 0x2U, /*!< Divided by 3 */
  76. kWDOG_ClockPrescalerDivide4 = 0x3U, /*!< Divided by 4 */
  77. kWDOG_ClockPrescalerDivide5 = 0x4U, /*!< Divided by 5 */
  78. kWDOG_ClockPrescalerDivide6 = 0x5U, /*!< Divided by 6 */
  79. kWDOG_ClockPrescalerDivide7 = 0x6U, /*!< Divided by 7 */
  80. kWDOG_ClockPrescalerDivide8 = 0x7U, /*!< Divided by 8 */
  81. } wdog_clock_prescaler_t;
  82. /*! @brief Describes WDOG configuration structure. */
  83. typedef struct _wdog_config
  84. {
  85. bool enableWdog; /*!< Enables or disables WDOG */
  86. wdog_clock_source_t clockSource; /*!< Clock source select */
  87. wdog_clock_prescaler_t prescaler; /*!< Clock prescaler value */
  88. wdog_work_mode_t workMode; /*!< Configures WDOG work mode in debug stop and wait mode */
  89. bool enableUpdate; /*!< Update write-once register enable */
  90. bool enableInterrupt; /*!< Enables or disables WDOG interrupt */
  91. bool enableWindowMode; /*!< Enables or disables WDOG window mode */
  92. uint32_t windowValue; /*!< Window value */
  93. uint32_t timeoutValue; /*!< Timeout value */
  94. } wdog_config_t;
  95. /*! @brief Describes WDOG test mode. */
  96. typedef enum _wdog_test_mode
  97. {
  98. kWDOG_QuickTest = 0U, /*!< Selects quick test */
  99. kWDOG_ByteTest = 1U, /*!< Selects byte test */
  100. } wdog_test_mode_t;
  101. /*! @brief Describes WDOG tested byte selection in byte test mode. */
  102. typedef enum _wdog_tested_byte
  103. {
  104. kWDOG_TestByte0 = 0U, /*!< Byte 0 selected in byte test mode */
  105. kWDOG_TestByte1 = 1U, /*!< Byte 1 selected in byte test mode */
  106. kWDOG_TestByte2 = 2U, /*!< Byte 2 selected in byte test mode */
  107. kWDOG_TestByte3 = 3U, /*!< Byte 3 selected in byte test mode */
  108. } wdog_tested_byte_t;
  109. /*! @brief Describes WDOG test mode configuration structure. */
  110. typedef struct _wdog_test_config
  111. {
  112. wdog_test_mode_t testMode; /*!< Selects test mode */
  113. wdog_tested_byte_t testedByte; /*!< Selects tested byte in byte test mode */
  114. uint32_t timeoutValue; /*!< Timeout value */
  115. } wdog_test_config_t;
  116. /*!
  117. * @brief WDOG interrupt configuration structure, default settings all disabled.
  118. *
  119. * This structure contains the settings for all of the WDOG interrupt configurations.
  120. */
  121. enum _wdog_interrupt_enable_t
  122. {
  123. kWDOG_InterruptEnable = WDOG_STCTRLH_IRQRSTEN_MASK, /*!< WDOG timeout generates an interrupt before reset*/
  124. };
  125. /*!
  126. * @brief WDOG status flags.
  127. *
  128. * This structure contains the WDOG status flags for use in the WDOG functions.
  129. */
  130. enum _wdog_status_flags_t
  131. {
  132. kWDOG_RunningFlag = WDOG_STCTRLH_WDOGEN_MASK, /*!< Running flag, set when WDOG is enabled*/
  133. kWDOG_TimeoutFlag = WDOG_STCTRLL_INTFLG_MASK, /*!< Interrupt flag, set when an exception occurs*/
  134. };
  135. /*******************************************************************************
  136. * API
  137. *******************************************************************************/
  138. #if defined(__cplusplus)
  139. extern "C" {
  140. #endif /* __cplusplus */
  141. /*!
  142. * @name WDOG Initialization and De-initialization
  143. * @{
  144. */
  145. /*!
  146. * @brief Initializes the WDOG configuration sturcture.
  147. *
  148. * This function initializes the WDOG configuration structure to default values. The default
  149. * values are as follows.
  150. * @code
  151. * wdogConfig->enableWdog = true;
  152. * wdogConfig->clockSource = kWDOG_LpoClockSource;
  153. * wdogConfig->prescaler = kWDOG_ClockPrescalerDivide1;
  154. * wdogConfig->workMode.enableWait = true;
  155. * wdogConfig->workMode.enableStop = false;
  156. * wdogConfig->workMode.enableDebug = false;
  157. * wdogConfig->enableUpdate = true;
  158. * wdogConfig->enableInterrupt = false;
  159. * wdogConfig->enableWindowMode = false;
  160. * wdogConfig->windowValue = 0;
  161. * wdogConfig->timeoutValue = 0xFFFFU;
  162. * @endcode
  163. *
  164. * @param config Pointer to the WDOG configuration structure.
  165. * @see wdog_config_t
  166. */
  167. void WDOG_GetDefaultConfig(wdog_config_t *config);
  168. /*!
  169. * @brief Initializes the WDOG.
  170. *
  171. * This function initializes the WDOG. When called, the WDOG runs according to the configuration.
  172. * To reconfigure WDOG without forcing a reset first, enableUpdate must be set to true
  173. * in the configuration.
  174. *
  175. * This is an example.
  176. * @code
  177. * wdog_config_t config;
  178. * WDOG_GetDefaultConfig(&config);
  179. * config.timeoutValue = 0x7ffU;
  180. * config.enableUpdate = true;
  181. * WDOG_Init(wdog_base,&config);
  182. * @endcode
  183. *
  184. * @param base WDOG peripheral base address
  185. * @param config The configuration of WDOG
  186. */
  187. void WDOG_Init(WDOG_Type *base, const wdog_config_t *config);
  188. /*!
  189. * @brief Shuts down the WDOG.
  190. *
  191. * This function shuts down the WDOG.
  192. * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which indicates that the register update is enabled.
  193. */
  194. void WDOG_Deinit(WDOG_Type *base);
  195. /*!
  196. * @brief Configures the WDOG functional test.
  197. *
  198. * This function is used to configure the WDOG functional test. When called, the WDOG goes into test mode
  199. * and runs according to the configuration.
  200. * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which means that the register update is enabled.
  201. *
  202. * This is an example.
  203. * @code
  204. * wdog_test_config_t test_config;
  205. * test_config.testMode = kWDOG_QuickTest;
  206. * test_config.timeoutValue = 0xfffffu;
  207. * WDOG_SetTestModeConfig(wdog_base, &test_config);
  208. * @endcode
  209. * @param base WDOG peripheral base address
  210. * @param config The functional test configuration of WDOG
  211. */
  212. void WDOG_SetTestModeConfig(WDOG_Type *base, wdog_test_config_t *config);
  213. /* @} */
  214. /*!
  215. * @name WDOG Functional Operation
  216. * @{
  217. */
  218. /*!
  219. * @brief Enables the WDOG module.
  220. *
  221. * This function write value into WDOG_STCTRLH register to enable the WDOG, it is a write-once register,
  222. * make sure that the WCT window is still open and this register has not been written in this WCT
  223. * while this function is called.
  224. *
  225. * @param base WDOG peripheral base address
  226. */
  227. static inline void WDOG_Enable(WDOG_Type *base)
  228. {
  229. base->STCTRLH |= WDOG_STCTRLH_WDOGEN_MASK;
  230. }
  231. /*!
  232. * @brief Disables the WDOG module.
  233. *
  234. * This function writes a value into the WDOG_STCTRLH register to disable the WDOG. It is a write-once register.
  235. * Ensure that the WCT window is still open and that register has not been written to in this WCT
  236. * while the function is called.
  237. *
  238. * @param base WDOG peripheral base address
  239. */
  240. static inline void WDOG_Disable(WDOG_Type *base)
  241. {
  242. base->STCTRLH &= ~WDOG_STCTRLH_WDOGEN_MASK;
  243. }
  244. /*!
  245. * @brief Enables the WDOG interrupt.
  246. *
  247. * This function writes a value into the WDOG_STCTRLH register to enable the WDOG interrupt. It is a write-once register.
  248. * Ensure that the WCT window is still open and the register has not been written to in this WCT
  249. * while the function is called.
  250. *
  251. * @param base WDOG peripheral base address
  252. * @param mask The interrupts to enable
  253. * The parameter can be combination of the following source if defined.
  254. * @arg kWDOG_InterruptEnable
  255. */
  256. static inline void WDOG_EnableInterrupts(WDOG_Type *base, uint32_t mask)
  257. {
  258. base->STCTRLH |= mask;
  259. }
  260. /*!
  261. * @brief Disables the WDOG interrupt.
  262. *
  263. * This function writes a value into the WDOG_STCTRLH register to disable the WDOG interrupt. It is a write-once register.
  264. * Ensure that the WCT window is still open and the register has not been written to in this WCT
  265. * while the function is called.
  266. *
  267. * @param base WDOG peripheral base address
  268. * @param mask The interrupts to disable
  269. * The parameter can be combination of the following source if defined.
  270. * @arg kWDOG_InterruptEnable
  271. */
  272. static inline void WDOG_DisableInterrupts(WDOG_Type *base, uint32_t mask)
  273. {
  274. base->STCTRLH &= ~mask;
  275. }
  276. /*!
  277. * @brief Gets the WDOG all status flags.
  278. *
  279. * This function gets all status flags.
  280. *
  281. * This is an example for getting the Running Flag.
  282. * @code
  283. * uint32_t status;
  284. * status = WDOG_GetStatusFlags (wdog_base) & kWDOG_RunningFlag;
  285. * @endcode
  286. * @param base WDOG peripheral base address
  287. * @return State of the status flag: asserted (true) or not-asserted (false).@see _wdog_status_flags_t
  288. * - true: a related status flag has been set.
  289. * - false: a related status flag is not set.
  290. */
  291. uint32_t WDOG_GetStatusFlags(WDOG_Type *base);
  292. /*!
  293. * @brief Clears the WDOG flag.
  294. *
  295. * This function clears the WDOG status flag.
  296. *
  297. * This is an example for clearing the timeout (interrupt) flag.
  298. * @code
  299. * WDOG_ClearStatusFlags(wdog_base,kWDOG_TimeoutFlag);
  300. * @endcode
  301. * @param base WDOG peripheral base address
  302. * @param mask The status flags to clear.
  303. * The parameter could be any combination of the following values.
  304. * kWDOG_TimeoutFlag
  305. */
  306. void WDOG_ClearStatusFlags(WDOG_Type *base, uint32_t mask);
  307. /*!
  308. * @brief Sets the WDOG timeout value.
  309. *
  310. * This function sets the timeout value.
  311. * It should be ensured that the time-out value for the WDOG is always greater than
  312. * 2xWCT time + 20 bus clock cycles.
  313. * This function writes a value into WDOG_TOVALH and WDOG_TOVALL registers which are wirte-once.
  314. * Ensure the WCT window is still open and the two registers have not been written to in this WCT
  315. * while the function is called.
  316. *
  317. * @param base WDOG peripheral base address
  318. * @param timeoutCount WDOG timeout value; count of WDOG clock tick.
  319. */
  320. static inline void WDOG_SetTimeoutValue(WDOG_Type *base, uint32_t timeoutCount)
  321. {
  322. base->TOVALH = (uint16_t)((timeoutCount >> 16U) & 0xFFFFU);
  323. base->TOVALL = (uint16_t)((timeoutCount)&0xFFFFU);
  324. }
  325. /*!
  326. * @brief Sets the WDOG window value.
  327. *
  328. * This function sets the WDOG window value.
  329. * This function writes a value into WDOG_WINH and WDOG_WINL registers which are wirte-once.
  330. * Ensure the WCT window is still open and the two registers have not been written to in this WCT
  331. * while the function is called.
  332. *
  333. * @param base WDOG peripheral base address
  334. * @param windowValue WDOG window value.
  335. */
  336. static inline void WDOG_SetWindowValue(WDOG_Type *base, uint32_t windowValue)
  337. {
  338. base->WINH = (uint16_t)((windowValue >> 16U) & 0xFFFFU);
  339. base->WINL = (uint16_t)((windowValue)&0xFFFFU);
  340. }
  341. /*!
  342. * @brief Unlocks the WDOG register written.
  343. *
  344. * This function unlocks the WDOG register written.
  345. * Before starting the unlock sequence and following congfiguration, disable the global interrupts.
  346. * Otherwise, an interrupt may invalidate the unlocking sequence and the WCT may expire.
  347. * After the configuration finishes, re-enable the global interrupts.
  348. *
  349. * @param base WDOG peripheral base address
  350. */
  351. static inline void WDOG_Unlock(WDOG_Type *base)
  352. {
  353. base->UNLOCK = WDOG_FIRST_WORD_OF_UNLOCK;
  354. base->UNLOCK = WDOG_SECOND_WORD_OF_UNLOCK;
  355. }
  356. /*!
  357. * @brief Refreshes the WDOG timer.
  358. *
  359. * This function feeds the WDOG.
  360. * This function should be called before the WDOG timer is in timeout. Otherwise, a reset is asserted.
  361. *
  362. * @param base WDOG peripheral base address
  363. */
  364. void WDOG_Refresh(WDOG_Type *base);
  365. /*!
  366. * @brief Gets the WDOG reset count.
  367. *
  368. * This function gets the WDOG reset count value.
  369. *
  370. * @param base WDOG peripheral base address
  371. * @return WDOG reset count value.
  372. */
  373. static inline uint16_t WDOG_GetResetCount(WDOG_Type *base)
  374. {
  375. return base->RSTCNT;
  376. }
  377. /*!
  378. * @brief Clears the WDOG reset count.
  379. *
  380. * This function clears the WDOG reset count value.
  381. *
  382. * @param base WDOG peripheral base address
  383. */
  384. static inline void WDOG_ClearResetCount(WDOG_Type *base)
  385. {
  386. base->RSTCNT |= UINT16_MAX;
  387. }
  388. /*@}*/
  389. #if defined(__cplusplus)
  390. }
  391. #endif /* __cplusplus */
  392. /*! @}*/
  393. #endif /* _FSL_WDOG_H_ */