25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

413 satır
13 KiB

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