fsl_wdog.h 15 KB

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