fsl_rtwdog.h 13 KB

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