fsl_rtwdog.h 13 KB

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