fsl_rtwdog.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_RTWDOG_H_
  9. #define _FSL_RTWDOG_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup rtwdog
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. *******************************************************************************/
  18. /*! @name Unlock sequence */
  19. /*@{*/
  20. #define WDOG_FIRST_WORD_OF_UNLOCK (RTWDOG_UPDATE_KEY & 0xFFFFU) /*!< First word of unlock sequence */
  21. #define WDOG_SECOND_WORD_OF_UNLOCK ((RTWDOG_UPDATE_KEY >> 16U) & 0xFFFFU) /*!< Second word of unlock sequence */
  22. /*@}*/
  23. /*! @name Refresh sequence */
  24. /*@{*/
  25. #define WDOG_FIRST_WORD_OF_REFRESH (RTWDOG_REFRESH_KEY & 0xFFFFU) /*!< First word of refresh sequence */
  26. #define WDOG_SECOND_WORD_OF_REFRESH ((RTWDOG_REFRESH_KEY >> 16U) & 0xFFFFU) /*!< Second word of refresh sequence */
  27. /*@}*/
  28. /*! @name Driver version */
  29. /*@{*/
  30. /*! @brief RTWDOG driver version 2.1.2. */
  31. #define FSL_RTWDOG_DRIVER_VERSION (MAKE_VERSION(2, 1, 2))
  32. /*@}*/
  33. /*! @brief Describes RTWDOG clock source. */
  34. typedef enum _rtwdog_clock_source
  35. {
  36. kRTWDOG_ClockSource0 = 0U, /*!< Clock source 0 */
  37. kRTWDOG_ClockSource1 = 1U, /*!< Clock source 1 */
  38. kRTWDOG_ClockSource2 = 2U, /*!< Clock source 2 */
  39. kRTWDOG_ClockSource3 = 3U, /*!< Clock source 3 */
  40. } rtwdog_clock_source_t;
  41. /*! @brief Describes the selection of the clock prescaler. */
  42. typedef enum _rtwdog_clock_prescaler
  43. {
  44. kRTWDOG_ClockPrescalerDivide1 = 0x0U, /*!< Divided by 1 */
  45. kRTWDOG_ClockPrescalerDivide256 = 0x1U, /*!< Divided by 256 */
  46. } rtwdog_clock_prescaler_t;
  47. /*! @brief Defines RTWDOG work mode. */
  48. typedef struct _rtwdog_work_mode
  49. {
  50. bool enableWait; /*!< Enables or disables RTWDOG in wait mode */
  51. bool enableStop; /*!< Enables or disables RTWDOG in stop mode */
  52. bool enableDebug; /*!< Enables or disables RTWDOG in debug mode */
  53. } rtwdog_work_mode_t;
  54. /*! @brief Describes RTWDOG test mode. */
  55. typedef enum _rtwdog_test_mode
  56. {
  57. kRTWDOG_TestModeDisabled = 0U, /*!< Test Mode disabled */
  58. kRTWDOG_UserModeEnabled = 1U, /*!< User Mode enabled */
  59. kRTWDOG_LowByteTest = 2U, /*!< Test Mode enabled, only low byte is used */
  60. kRTWDOG_HighByteTest = 3U, /*!< Test Mode enabled, only high byte is used */
  61. } rtwdog_test_mode_t;
  62. /*! @brief Describes RTWDOG configuration structure. */
  63. typedef struct _rtwdog_config
  64. {
  65. bool enableRtwdog; /*!< Enables or disables RTWDOG */
  66. rtwdog_clock_source_t clockSource; /*!< Clock source select */
  67. rtwdog_clock_prescaler_t prescaler; /*!< Clock prescaler value */
  68. rtwdog_work_mode_t workMode; /*!< Configures RTWDOG work mode in debug stop and wait mode */
  69. rtwdog_test_mode_t testMode; /*!< Configures RTWDOG test mode */
  70. bool enableUpdate; /*!< Update write-once register enable */
  71. bool enableInterrupt; /*!< Enables or disables RTWDOG interrupt */
  72. bool enableWindowMode; /*!< Enables or disables RTWDOG window mode */
  73. uint16_t windowValue; /*!< Window value */
  74. uint16_t timeoutValue; /*!< Timeout value */
  75. } rtwdog_config_t;
  76. /*!
  77. * @brief RTWDOG interrupt configuration structure.
  78. *
  79. * This structure contains the settings for all of the RTWDOG interrupt configurations.
  80. */
  81. enum _rtwdog_interrupt_enable_t
  82. {
  83. kRTWDOG_InterruptEnable = RTWDOG_CS_INT_MASK, /*!< Interrupt is generated before forcing a reset */
  84. };
  85. /*!
  86. * @brief RTWDOG status flags.
  87. *
  88. * This structure contains the RTWDOG status flags for use in the RTWDOG functions.
  89. */
  90. enum _rtwdog_status_flags_t
  91. {
  92. kRTWDOG_RunningFlag = RTWDOG_CS_EN_MASK, /*!< Running flag, set when RTWDOG is enabled */
  93. kRTWDOG_InterruptFlag = RTWDOG_CS_FLG_MASK, /*!< Interrupt flag, set when interrupt occurs */
  94. };
  95. /*******************************************************************************
  96. * API
  97. *******************************************************************************/
  98. #if defined(__cplusplus)
  99. extern "C" {
  100. #endif /* __cplusplus */
  101. /*!
  102. * @name RTWDOG Initialization and De-initialization
  103. * @{
  104. */
  105. /*!
  106. * @brief Initializes the RTWDOG configuration structure.
  107. *
  108. * This function initializes the RTWDOG configuration structure to default values. The default
  109. * values are:
  110. * @code
  111. * rtwdogConfig->enableRtwdog = true;
  112. * rtwdogConfig->clockSource = kRTWDOG_ClockSource1;
  113. * rtwdogConfig->prescaler = kRTWDOG_ClockPrescalerDivide1;
  114. * rtwdogConfig->workMode.enableWait = true;
  115. * rtwdogConfig->workMode.enableStop = false;
  116. * rtwdogConfig->workMode.enableDebug = false;
  117. * rtwdogConfig->testMode = kRTWDOG_TestModeDisabled;
  118. * rtwdogConfig->enableUpdate = true;
  119. * rtwdogConfig->enableInterrupt = false;
  120. * rtwdogConfig->enableWindowMode = false;
  121. * rtwdogConfig->windowValue = 0U;
  122. * rtwdogConfig->timeoutValue = 0xFFFFU;
  123. * @endcode
  124. *
  125. * @param config Pointer to the RTWDOG configuration structure.
  126. * @see rtwdog_config_t
  127. */
  128. void RTWDOG_GetDefaultConfig(rtwdog_config_t *config);
  129. /*!
  130. * @brief Initializes the RTWDOG module.
  131. *
  132. * This function initializes the RTWDOG.
  133. * To reconfigure the RTWDOG without forcing a reset first, enableUpdate must be set to true
  134. * in the configuration.
  135. *
  136. * Example:
  137. * @code
  138. * rtwdog_config_t config;
  139. * RTWDOG_GetDefaultConfig(&config);
  140. * config.timeoutValue = 0x7ffU;
  141. * config.enableUpdate = true;
  142. * RTWDOG_Init(wdog_base,&config);
  143. * @endcode
  144. *
  145. * @param base RTWDOG peripheral base address.
  146. * @param config The configuration of the RTWDOG.
  147. */
  148. #if defined(DOXYGEN_OUTPUT) && DOXYGEN_OUTPUT
  149. void RTWDOG_Init(RTWDOG_Type *base, const rtwdog_config_t *config);
  150. #else
  151. AT_QUICKACCESS_SECTION_CODE(void RTWDOG_Init(RTWDOG_Type *base, const rtwdog_config_t *config));
  152. #endif
  153. /*!
  154. * @brief De-initializes the RTWDOG module.
  155. *
  156. * This function shuts down the RTWDOG.
  157. * Ensure that the WDOG_CS.UPDATE is 1, which means that the register update is enabled.
  158. *
  159. * @param base RTWDOG peripheral base address.
  160. */
  161. void RTWDOG_Deinit(RTWDOG_Type *base);
  162. /* @} */
  163. /*!
  164. * @name RTWDOG functional Operation
  165. * @{
  166. */
  167. /*!
  168. * @brief Enables the RTWDOG module.
  169. *
  170. * This function writes a value into the WDOG_CS register to enable the RTWDOG.
  171. * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
  172. * this register has not been written in this WCT while the function is called.
  173. *
  174. * @param base RTWDOG peripheral base address.
  175. */
  176. static inline void RTWDOG_Enable(RTWDOG_Type *base)
  177. {
  178. base->CS |= RTWDOG_CS_EN_MASK;
  179. }
  180. /*!
  181. * @brief Disables the RTWDOG module.
  182. *
  183. * This function writes a value into the WDOG_CS register to disable the RTWDOG.
  184. * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
  185. * this register has not been written in this WCT while the function is called.
  186. *
  187. * @param base RTWDOG peripheral base address
  188. */
  189. static inline void RTWDOG_Disable(RTWDOG_Type *base)
  190. {
  191. base->CS &= ~RTWDOG_CS_EN_MASK;
  192. }
  193. /*!
  194. * @brief Enables the RTWDOG interrupt.
  195. *
  196. * This function writes a value into the WDOG_CS register to enable the RTWDOG interrupt.
  197. * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
  198. * this register has not been written in this WCT while the function is called.
  199. *
  200. * @param base RTWDOG peripheral base address.
  201. * @param mask The interrupts to enable.
  202. * The parameter can be a combination of the following source if defined:
  203. * @arg kRTWDOG_InterruptEnable
  204. */
  205. static inline void RTWDOG_EnableInterrupts(RTWDOG_Type *base, uint32_t mask)
  206. {
  207. base->CS |= mask;
  208. }
  209. /*!
  210. * @brief Disables the RTWDOG interrupt.
  211. *
  212. * This function writes a value into the WDOG_CS register to disable the RTWDOG interrupt.
  213. * The WDOG_CS register is a write-once register. Ensure that the WCT window is still open and
  214. * this register has not been written in this WCT while the function is called.
  215. *
  216. * @param base RTWDOG peripheral base address.
  217. * @param mask The interrupts to disabled.
  218. * The parameter can be a combination of the following source if defined:
  219. * @arg kRTWDOG_InterruptEnable
  220. */
  221. static inline void RTWDOG_DisableInterrupts(RTWDOG_Type *base, uint32_t mask)
  222. {
  223. base->CS &= ~mask;
  224. }
  225. /*!
  226. * @brief Gets the RTWDOG all status flags.
  227. *
  228. * This function gets all status flags.
  229. *
  230. * Example to get the running flag:
  231. * @code
  232. * uint32_t status;
  233. * status = RTWDOG_GetStatusFlags(wdog_base) & kRTWDOG_RunningFlag;
  234. * @endcode
  235. * @param base RTWDOG peripheral base address
  236. * @return State of the status flag: asserted (true) or not-asserted (false). @see _rtwdog_status_flags_t
  237. * - true: related status flag has been set.
  238. * - false: related status flag is not set.
  239. */
  240. static inline uint32_t RTWDOG_GetStatusFlags(RTWDOG_Type *base)
  241. {
  242. return (base->CS & (RTWDOG_CS_EN_MASK | RTWDOG_CS_FLG_MASK));
  243. }
  244. /*!
  245. * @brief Enables/disables the window mode.
  246. *
  247. * @param base RTWDOG peripheral base address.
  248. * @param enable Enables(true) or disables(false) the feature.
  249. */
  250. static inline void RTWDOG_EnableWindowMode(RTWDOG_Type *base, bool enable)
  251. {
  252. if (enable)
  253. {
  254. base->CS |= RTWDOG_CS_WIN_MASK;
  255. }
  256. else
  257. {
  258. base->CS &= ~RTWDOG_CS_WIN_MASK;
  259. }
  260. }
  261. /*!
  262. * @brief Converts raw count value to millisecond.
  263. *
  264. * Note that if the clock frequency is too high the timeout period can be less than 1 ms.
  265. * In this case this api will return 0 value.
  266. *
  267. * @param base RTWDOG peripheral base address.
  268. * @param count Raw count value.
  269. * @param clockFreqInHz The frequency of the clock source RTWDOG uses.
  270. */
  271. static inline uint32_t RTWDOG_CountToMesec(RTWDOG_Type *base, uint32_t count, uint32_t clockFreqInHz)
  272. {
  273. if ((base->CS & RTWDOG_CS_PRES_MASK) != 0U)
  274. {
  275. clockFreqInHz /= 256U;
  276. }
  277. return count * 1000U / clockFreqInHz;
  278. }
  279. /*!
  280. * @brief Clears the RTWDOG flag.
  281. *
  282. * This function clears the RTWDOG status flag.
  283. *
  284. * Example to clear an interrupt flag:
  285. * @code
  286. * RTWDOG_ClearStatusFlags(wdog_base,kRTWDOG_InterruptFlag);
  287. * @endcode
  288. * @param base RTWDOG peripheral base address.
  289. * @param mask The status flags to clear.
  290. * The parameter can be any combination of the following values:
  291. * @arg kRTWDOG_InterruptFlag
  292. */
  293. void RTWDOG_ClearStatusFlags(RTWDOG_Type *base, uint32_t mask);
  294. /*!
  295. * @brief Sets the RTWDOG timeout value.
  296. *
  297. * This function writes a timeout value into the WDOG_TOVAL register.
  298. * The WDOG_TOVAL register is a write-once register. Ensure that the WCT window is still open and
  299. * this register has not been written in this WCT while the function is called.
  300. *
  301. * @param base RTWDOG peripheral base address
  302. * @param timeoutCount RTWDOG timeout value, count of RTWDOG clock ticks.
  303. */
  304. static inline void RTWDOG_SetTimeoutValue(RTWDOG_Type *base, uint16_t timeoutCount)
  305. {
  306. base->TOVAL = timeoutCount;
  307. }
  308. /*!
  309. * @brief Sets the RTWDOG window value.
  310. *
  311. * This function writes a window value into the WDOG_WIN register.
  312. * The WDOG_WIN register is a write-once register. Ensure that the WCT window is still open and
  313. * this register has not been written in this WCT while the function is called.
  314. *
  315. * @param base RTWDOG peripheral base address.
  316. * @param windowValue RTWDOG window value.
  317. */
  318. static inline void RTWDOG_SetWindowValue(RTWDOG_Type *base, uint16_t windowValue)
  319. {
  320. base->WIN = windowValue;
  321. }
  322. /*!
  323. * @brief Unlocks the RTWDOG register written.
  324. *
  325. * This function unlocks the RTWDOG register written.
  326. *
  327. * Before starting the unlock sequence and following the configuration, disable the global interrupts.
  328. * Otherwise, an interrupt could effectively invalidate the unlock sequence and the WCT may expire.
  329. * After the configuration finishes, re-enable the global interrupts.
  330. *
  331. * @param base RTWDOG peripheral base address
  332. */
  333. __STATIC_FORCEINLINE void RTWDOG_Unlock(RTWDOG_Type *base)
  334. {
  335. if (((base->CS) & RTWDOG_CS_CMD32EN_MASK) != 0U)
  336. {
  337. base->CNT = RTWDOG_UPDATE_KEY;
  338. }
  339. else
  340. {
  341. base->CNT = WDOG_FIRST_WORD_OF_UNLOCK;
  342. base->CNT = WDOG_SECOND_WORD_OF_UNLOCK;
  343. }
  344. while ((base->CS & RTWDOG_CS_ULK_MASK) == 0U)
  345. {
  346. }
  347. }
  348. /*!
  349. * @brief Refreshes the RTWDOG timer.
  350. *
  351. * This function feeds the RTWDOG.
  352. * This function should be called before the Watchdog timer is in timeout. Otherwise, a reset is asserted.
  353. *
  354. * @param base RTWDOG peripheral base address
  355. */
  356. static inline void RTWDOG_Refresh(RTWDOG_Type *base)
  357. {
  358. uint32_t primaskValue = 0U;
  359. primaskValue = DisableGlobalIRQ();
  360. if (((base->CS) & RTWDOG_CS_CMD32EN_MASK) != 0U)
  361. {
  362. base->CNT = RTWDOG_REFRESH_KEY;
  363. }
  364. else
  365. {
  366. base->CNT = WDOG_FIRST_WORD_OF_REFRESH;
  367. base->CNT = WDOG_SECOND_WORD_OF_REFRESH;
  368. }
  369. EnableGlobalIRQ(primaskValue);
  370. }
  371. /*!
  372. * @brief Gets the RTWDOG counter value.
  373. *
  374. * This function gets the RTWDOG counter value.
  375. *
  376. * @param base RTWDOG peripheral base address.
  377. * @return Current RTWDOG counter value.
  378. */
  379. static inline uint16_t RTWDOG_GetCounterValue(RTWDOG_Type *base)
  380. {
  381. return (uint16_t)base->CNT;
  382. }
  383. /*@}*/
  384. #if defined(__cplusplus)
  385. }
  386. #endif /* __cplusplus */
  387. /*! @}*/
  388. #endif /* _FSL_RTWDOG_H_ */