fsl_wdog.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2016, 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.wdog01"
  12. #endif
  13. /*******************************************************************************
  14. * Variables
  15. ******************************************************************************/
  16. static WDOG_Type *const s_wdogBases[] = WDOG_BASE_PTRS;
  17. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  18. /* Array of WDOG clock name. */
  19. static const clock_ip_name_t s_wdogClock[] = WDOG_CLOCKS;
  20. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  21. static const IRQn_Type s_wdogIRQ[] = WDOG_IRQS;
  22. /*******************************************************************************
  23. * Code
  24. ******************************************************************************/
  25. static uint32_t WDOG_GetInstance(WDOG_Type *base)
  26. {
  27. uint32_t instance;
  28. /* Find the instance index from base address mappings. */
  29. for (instance = 0; instance < ARRAY_SIZE(s_wdogBases); instance++)
  30. {
  31. if (s_wdogBases[instance] == base)
  32. {
  33. break;
  34. }
  35. }
  36. assert(instance < ARRAY_SIZE(s_wdogBases));
  37. return instance;
  38. }
  39. /*!
  40. * brief Initializes the WDOG configuration structure.
  41. *
  42. * This function initializes the WDOG configuration structure to default values. The default
  43. * values are as follows.
  44. * code
  45. * wdogConfig->enableWdog = true;
  46. * wdogConfig->workMode.enableWait = true;
  47. * wdogConfig->workMode.enableStop = false;
  48. * wdogConfig->workMode.enableDebug = false;
  49. * wdogConfig->enableInterrupt = false;
  50. * wdogConfig->enablePowerdown = false;
  51. * wdogConfig->resetExtension = flase;
  52. * wdogConfig->timeoutValue = 0xFFU;
  53. * wdogConfig->interruptTimeValue = 0x04u;
  54. * endcode
  55. *
  56. * param config Pointer to the WDOG configuration structure.
  57. * see wdog_config_t
  58. */
  59. void WDOG_GetDefaultConfig(wdog_config_t *config)
  60. {
  61. assert(NULL != config);
  62. /* Initializes the configure structure to zero. */
  63. (void)memset(config, 0, sizeof(*config));
  64. config->enableWdog = true;
  65. config->workMode.enableWait = false;
  66. config->workMode.enableStop = false;
  67. config->workMode.enableDebug = false;
  68. config->enableInterrupt = false;
  69. config->softwareResetExtension = false;
  70. config->enablePowerDown = false;
  71. config->timeoutValue = 0xffu;
  72. config->interruptTimeValue = 0x04u;
  73. config->enableTimeOutAssert = false;
  74. }
  75. /*!
  76. * brief Initializes the WDOG.
  77. *
  78. * This function initializes the WDOG. When called, the WDOG runs according to the configuration.
  79. *
  80. * This is an example.
  81. * code
  82. * wdog_config_t config;
  83. * WDOG_GetDefaultConfig(&config);
  84. * config.timeoutValue = 0xffU;
  85. * config->interruptTimeValue = 0x04u;
  86. * WDOG_Init(wdog_base,&config);
  87. * endcode
  88. *
  89. * param base WDOG peripheral base address
  90. * param config The configuration of WDOG
  91. */
  92. void WDOG_Init(WDOG_Type *base, const wdog_config_t *config)
  93. {
  94. assert(NULL != config);
  95. uint16_t value = 0u;
  96. uint32_t primaskValue = 0U;
  97. value = WDOG_WCR_WDE(config->enableWdog) | WDOG_WCR_WDW(config->workMode.enableWait) |
  98. WDOG_WCR_WDZST(config->workMode.enableStop) | WDOG_WCR_WDBG(config->workMode.enableDebug) |
  99. WDOG_WCR_SRE(config->softwareResetExtension) | WDOG_WCR_WT(config->timeoutValue) |
  100. WDOG_WCR_WDT(config->enableTimeOutAssert) | WDOG_WCR_SRS_MASK | WDOG_WCR_WDA_MASK;
  101. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  102. /* Set configuration */
  103. CLOCK_EnableClock(s_wdogClock[WDOG_GetInstance(base)]);
  104. #endif
  105. primaskValue = DisableGlobalIRQ();
  106. base->WICR = WDOG_WICR_WICT(config->interruptTimeValue) | WDOG_WICR_WIE(config->enableInterrupt);
  107. base->WMCR = WDOG_WMCR_PDE(config->enablePowerDown);
  108. base->WCR = value;
  109. EnableGlobalIRQ(primaskValue);
  110. if (config->enableInterrupt)
  111. {
  112. (void)EnableIRQ(s_wdogIRQ[WDOG_GetInstance(base)]);
  113. }
  114. }
  115. /*!
  116. * brief Shuts down the WDOG.
  117. *
  118. * This function shuts down the WDOG.
  119. * Watchdog Enable bit is a write one once only bit. It is not
  120. * possible to clear this bit by a software write, once the bit is set.
  121. * This bit(WDE) can be set/reset only in debug mode(exception).
  122. */
  123. void WDOG_Deinit(WDOG_Type *base)
  124. {
  125. if (0U != (base->WCR & WDOG_WCR_WDBG_MASK))
  126. {
  127. WDOG_Disable(base);
  128. }
  129. }
  130. /*!
  131. * brief Gets the WDOG all reset status flags.
  132. *
  133. * This function gets all reset status flags.
  134. *
  135. * code
  136. * uint16_t status;
  137. * status = WDOG_GetStatusFlags (wdog_base);
  138. * endcode
  139. * param base WDOG peripheral base address
  140. * return State of the status flag: asserted (true) or not-asserted (false).see _wdog_status_flags
  141. * - true: a related status flag has been set.
  142. * - false: a related status flag is not set.
  143. */
  144. uint16_t WDOG_GetStatusFlags(WDOG_Type *base)
  145. {
  146. uint16_t status_flag = 0U;
  147. status_flag |= (base->WCR & WDOG_WCR_WDE_MASK);
  148. status_flag |= (base->WRSR & WDOG_WRSR_POR_MASK);
  149. status_flag |= (base->WRSR & WDOG_WRSR_TOUT_MASK);
  150. status_flag |= (base->WRSR & WDOG_WRSR_SFTW_MASK);
  151. status_flag |= (base->WICR & WDOG_WICR_WTIS_MASK);
  152. return status_flag;
  153. }
  154. /*!
  155. * brief Clears the WDOG flag.
  156. *
  157. * This function clears the WDOG status flag.
  158. *
  159. * This is an example for clearing the interrupt flag.
  160. * code
  161. * WDOG_ClearStatusFlags(wdog_base,KWDOG_InterruptFlag);
  162. * endcode
  163. * param base WDOG peripheral base address
  164. * param mask The status flags to clear.
  165. * The parameter could be any combination of the following values.
  166. * kWDOG_TimeoutFlag
  167. */
  168. void WDOG_ClearInterruptStatus(WDOG_Type *base, uint16_t mask)
  169. {
  170. if (0U != (mask & (uint16_t)kWDOG_InterruptFlag))
  171. {
  172. base->WICR |= WDOG_WICR_WTIS_MASK;
  173. }
  174. }
  175. /*!
  176. * brief Refreshes the WDOG timer.
  177. *
  178. * This function feeds the WDOG.
  179. * This function should be called before the WDOG timer is in timeout. Otherwise, a reset is asserted.
  180. *
  181. * param base WDOG peripheral base address
  182. */
  183. void WDOG_Refresh(WDOG_Type *base)
  184. {
  185. uint32_t primaskValue = 0U;
  186. /* Disable the global interrupt to protect refresh sequence */
  187. primaskValue = DisableGlobalIRQ();
  188. base->WSR = WDOG_REFRESH_KEY & 0xFFFFU;
  189. base->WSR = (WDOG_REFRESH_KEY >> 16U) & 0xFFFFU;
  190. EnableGlobalIRQ(primaskValue);
  191. }