選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

241 行
8.0 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. #include "fsl_wdog.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.wdog"
  12. #endif
  13. /*******************************************************************************
  14. * Code
  15. ******************************************************************************/
  16. /*!
  17. * brief Initializes the WDOG configuration structure.
  18. *
  19. * This function initializes the WDOG configuration structure to default values. The default
  20. * values are as follows.
  21. * code
  22. * wdogConfig->enableWdog = true;
  23. * wdogConfig->clockSource = kWDOG_LpoClockSource;
  24. * wdogConfig->prescaler = kWDOG_ClockPrescalerDivide1;
  25. * wdogConfig->workMode.enableWait = true;
  26. * wdogConfig->workMode.enableStop = false;
  27. * wdogConfig->workMode.enableDebug = false;
  28. * wdogConfig->enableUpdate = true;
  29. * wdogConfig->enableInterrupt = false;
  30. * wdogConfig->enableWindowMode = false;
  31. * wdogConfig->windowValue = 0;
  32. * wdogConfig->timeoutValue = 0xFFFFU;
  33. * endcode
  34. *
  35. * param config Pointer to the WDOG configuration structure.
  36. * see wdog_config_t
  37. */
  38. void WDOG_GetDefaultConfig(wdog_config_t *config)
  39. {
  40. assert(NULL != config);
  41. /* Initializes the configure structure to zero. */
  42. (void)memset(config, 0, sizeof(*config));
  43. config->enableWdog = true;
  44. config->clockSource = kWDOG_LpoClockSource;
  45. config->prescaler = kWDOG_ClockPrescalerDivide1;
  46. #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
  47. config->workMode.enableWait = true;
  48. #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
  49. config->workMode.enableStop = false;
  50. config->workMode.enableDebug = false;
  51. config->enableUpdate = true;
  52. config->enableInterrupt = false;
  53. config->enableWindowMode = false;
  54. config->windowValue = 0U;
  55. config->timeoutValue = 0xFFFFU;
  56. }
  57. /*!
  58. * brief Initializes the WDOG.
  59. *
  60. * This function initializes the WDOG. When called, the WDOG runs according to the configuration.
  61. * To reconfigure WDOG without forcing a reset first, enableUpdate must be set to true
  62. * in the configuration.
  63. *
  64. * This is an example.
  65. * code
  66. * wdog_config_t config;
  67. * WDOG_GetDefaultConfig(&config);
  68. * config.timeoutValue = 0x7ffU;
  69. * config.enableUpdate = true;
  70. * WDOG_Init(wdog_base,&config);
  71. * endcode
  72. *
  73. * param base WDOG peripheral base address
  74. * param config The configuration of WDOG
  75. */
  76. void WDOG_Init(WDOG_Type *base, const wdog_config_t *config)
  77. {
  78. assert(NULL != config);
  79. uint16_t value = 0U;
  80. uint32_t primaskValue = 0U;
  81. value = WDOG_STCTRLH_WDOGEN(config->enableWdog) | WDOG_STCTRLH_CLKSRC(config->clockSource) |
  82. WDOG_STCTRLH_IRQRSTEN(config->enableInterrupt) | WDOG_STCTRLH_WINEN(config->enableWindowMode) |
  83. WDOG_STCTRLH_ALLOWUPDATE(config->enableUpdate) | WDOG_STCTRLH_DBGEN(config->workMode.enableDebug) |
  84. WDOG_STCTRLH_STOPEN(config->workMode.enableStop) |
  85. #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
  86. WDOG_STCTRLH_WAITEN(config->workMode.enableWait) |
  87. #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
  88. WDOG_STCTRLH_DISTESTWDOG(1U);
  89. /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
  90. * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
  91. primaskValue = DisableGlobalIRQ();
  92. WDOG_Unlock(base);
  93. /* Wait one bus clock cycle */
  94. base->RSTCNT = 0U;
  95. /* Set configuration */
  96. base->PRESC = WDOG_PRESC_PRESCVAL(config->prescaler);
  97. base->WINH = (uint16_t)((config->windowValue >> 16U) & 0xFFFFU);
  98. base->WINL = (uint16_t)((config->windowValue) & 0xFFFFU);
  99. base->TOVALH = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
  100. base->TOVALL = (uint16_t)((config->timeoutValue) & 0xFFFFU);
  101. base->STCTRLH = value;
  102. EnableGlobalIRQ(primaskValue);
  103. }
  104. /*!
  105. * brief Shuts down the WDOG.
  106. *
  107. * This function shuts down the WDOG.
  108. * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which indicates that the register update is enabled.
  109. */
  110. void WDOG_Deinit(WDOG_Type *base)
  111. {
  112. uint32_t primaskValue = 0U;
  113. /* Disable the global interrupts */
  114. primaskValue = DisableGlobalIRQ();
  115. WDOG_Unlock(base);
  116. /* Wait one bus clock cycle */
  117. base->RSTCNT = 0U;
  118. WDOG_Disable(base);
  119. EnableGlobalIRQ(primaskValue);
  120. WDOG_ClearResetCount(base);
  121. }
  122. /*!
  123. * brief Configures the WDOG functional test.
  124. *
  125. * This function is used to configure the WDOG functional test. When called, the WDOG goes into test mode
  126. * and runs according to the configuration.
  127. * Ensure that the WDOG_STCTRLH.ALLOWUPDATE is 1 which means that the register update is enabled.
  128. *
  129. * This is an example.
  130. * code
  131. * wdog_test_config_t test_config;
  132. * test_config.testMode = kWDOG_QuickTest;
  133. * test_config.timeoutValue = 0xfffffu;
  134. * WDOG_SetTestModeConfig(wdog_base, &test_config);
  135. * endcode
  136. * param base WDOG peripheral base address
  137. * param config The functional test configuration of WDOG
  138. */
  139. void WDOG_SetTestModeConfig(WDOG_Type *base, wdog_test_config_t *config)
  140. {
  141. assert(NULL != config);
  142. uint16_t value = 0U;
  143. uint32_t primaskValue = 0U;
  144. value = WDOG_STCTRLH_DISTESTWDOG(0U) | WDOG_STCTRLH_TESTWDOG(1U) | WDOG_STCTRLH_TESTSEL(config->testMode) |
  145. WDOG_STCTRLH_BYTESEL(config->testedByte) | WDOG_STCTRLH_IRQRSTEN(0U) | WDOG_STCTRLH_WDOGEN(1U) |
  146. WDOG_STCTRLH_ALLOWUPDATE(1U);
  147. /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
  148. * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
  149. primaskValue = DisableGlobalIRQ();
  150. WDOG_Unlock(base);
  151. /* Wait one bus clock cycle */
  152. base->RSTCNT = 0U;
  153. /* Set configuration */
  154. base->TOVALH = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
  155. base->TOVALL = (uint16_t)((config->timeoutValue) & 0xFFFFU);
  156. base->STCTRLH = value;
  157. EnableGlobalIRQ(primaskValue);
  158. }
  159. /*!
  160. * brief Gets the WDOG all status flags.
  161. *
  162. * This function gets all status flags.
  163. *
  164. * This is an example for getting the Running Flag.
  165. * code
  166. * uint32_t status;
  167. * status = WDOG_GetStatusFlags (wdog_base) & kWDOG_RunningFlag;
  168. * endcode
  169. * param base WDOG peripheral base address
  170. * return State of the status flag: asserted (true) or not-asserted (false).see _wdog_status_flags_t
  171. * - true: a related status flag has been set.
  172. * - false: a related status flag is not set.
  173. */
  174. uint32_t WDOG_GetStatusFlags(WDOG_Type *base)
  175. {
  176. uint32_t status_flag = 0U;
  177. status_flag |= ((uint32_t)base->STCTRLH & (uint32_t)WDOG_STCTRLH_WDOGEN_MASK);
  178. status_flag |= ((uint32_t)base->STCTRLL & (uint32_t)WDOG_STCTRLL_INTFLG_MASK);
  179. return status_flag;
  180. }
  181. /*!
  182. * brief Clears the WDOG flag.
  183. *
  184. * This function clears the WDOG status flag.
  185. *
  186. * This is an example for clearing the timeout (interrupt) flag.
  187. * code
  188. * WDOG_ClearStatusFlags(wdog_base,kWDOG_TimeoutFlag);
  189. * endcode
  190. * param base WDOG peripheral base address
  191. * param mask The status flags to clear.
  192. * The parameter could be any combination of the following values.
  193. * kWDOG_TimeoutFlag
  194. */
  195. void WDOG_ClearStatusFlags(WDOG_Type *base, uint32_t mask)
  196. {
  197. if (0U != (mask & (uint32_t)kWDOG_TimeoutFlag))
  198. {
  199. base->STCTRLL |= WDOG_STCTRLL_INTFLG_MASK;
  200. }
  201. }
  202. /*!
  203. * brief Refreshes the WDOG timer.
  204. *
  205. * This function feeds the WDOG.
  206. * This function should be called before the WDOG timer is in timeout. Otherwise, a reset is asserted.
  207. *
  208. * param base WDOG peripheral base address
  209. */
  210. void WDOG_Refresh(WDOG_Type *base)
  211. {
  212. uint32_t primaskValue = 0U;
  213. /* Disable the global interrupt to protect refresh sequence */
  214. primaskValue = DisableGlobalIRQ();
  215. base->REFRESH = WDOG_FIRST_WORD_OF_REFRESH;
  216. base->REFRESH = WDOG_SECOND_WORD_OF_REFRESH;
  217. EnableGlobalIRQ(primaskValue);
  218. }