fsl_wwdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #include "fsl_wwdt.h"
  31. /*******************************************************************************
  32. * Prototypes
  33. ******************************************************************************/
  34. /*!
  35. * @brief Gets the instance from the base address
  36. *
  37. * @param base WWDT peripheral base address
  38. *
  39. * @return The WWDT instance
  40. */
  41. static uint32_t WWDT_GetInstance(WWDT_Type *base);
  42. /*******************************************************************************
  43. * Variables
  44. ******************************************************************************/
  45. /*! @brief Pointers to WWDT bases for each instance. */
  46. static WWDT_Type *const s_wwdtBases[] = WWDT_BASE_PTRS;
  47. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  48. /*! @brief Pointers to WWDT clocks for each instance. */
  49. static const clock_ip_name_t s_wwdtClocks[] = WWDT_CLOCKS;
  50. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  51. /*! @brief Pointers to WWDT resets for each instance. */
  52. static const reset_ip_name_t s_wwdtResets[] = WWDT_RSTS;
  53. /*******************************************************************************
  54. * Code
  55. ******************************************************************************/
  56. static uint32_t WWDT_GetInstance(WWDT_Type *base)
  57. {
  58. uint32_t instance;
  59. uint32_t wwdtArrayCount = (sizeof(s_wwdtBases) / sizeof(s_wwdtBases[0]));
  60. /* Find the instance index from base address mappings. */
  61. for (instance = 0; instance < wwdtArrayCount; instance++)
  62. {
  63. if (s_wwdtBases[instance] == base)
  64. {
  65. break;
  66. }
  67. }
  68. assert(instance < wwdtArrayCount);
  69. return instance;
  70. }
  71. /*******************************************************************************
  72. * Code
  73. ******************************************************************************/
  74. void WWDT_GetDefaultConfig(wwdt_config_t *config)
  75. {
  76. assert(config);
  77. /* Enable the watch dog */
  78. config->enableWwdt = true;
  79. /* Disable the watchdog timeout reset */
  80. config->enableWatchdogReset = false;
  81. /* Disable the watchdog protection for updating the timeout value */
  82. config->enableWatchdogProtect = false;
  83. /* Do not lock the watchdog oscillator */
  84. config->enableLockOscillator = false;
  85. /* Windowing is not in effect */
  86. config->windowValue = 0xFFFFFFU;
  87. /* Set the timeout value to the max */
  88. config->timeoutValue = 0xFFFFFFU;
  89. /* No warning is provided */
  90. config->warningValue = 0;
  91. }
  92. void WWDT_Init(WWDT_Type *base, const wwdt_config_t *config)
  93. {
  94. assert(config);
  95. uint32_t value = 0U;
  96. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  97. /* Enable the WWDT clock */
  98. CLOCK_EnableClock(s_wwdtClocks[WWDT_GetInstance(base)]);
  99. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  100. /* Reset the WWDT module */
  101. RESET_PeripheralReset(s_wwdtResets[WWDT_GetInstance(base)]);
  102. value = WWDT_MOD_WDEN(config->enableWwdt) | WWDT_MOD_WDRESET(config->enableWatchdogReset) |
  103. WWDT_MOD_WDPROTECT(config->enableWatchdogProtect) | WWDT_MOD_LOCK(config->enableLockOscillator);
  104. /* Set configruation */
  105. base->WINDOW = WWDT_WINDOW_WINDOW(config->windowValue);
  106. base->TC = WWDT_TC_COUNT(config->timeoutValue);
  107. base->WARNINT = WWDT_WARNINT_WARNINT(config->warningValue);
  108. base->MOD = value;
  109. }
  110. void WWDT_Deinit(WWDT_Type *base)
  111. {
  112. WWDT_Disable(base);
  113. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  114. /* Disable the WWDT clock */
  115. CLOCK_DisableClock(s_wwdtClocks[WWDT_GetInstance(base)]);
  116. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  117. }
  118. void WWDT_Refresh(WWDT_Type *base)
  119. {
  120. uint32_t primaskValue = 0U;
  121. /* Disable the global interrupt to protect refresh sequence */
  122. primaskValue = DisableGlobalIRQ();
  123. base->FEED = WWDT_FIRST_WORD_OF_REFRESH;
  124. base->FEED = WWDT_SECOND_WORD_OF_REFRESH;
  125. EnableGlobalIRQ(primaskValue);
  126. }
  127. void WWDT_ClearStatusFlags(WWDT_Type *base, uint32_t mask)
  128. {
  129. /* Clear the WDINT bit so that we don't accidentally clear it */
  130. uint32_t reg = (base->MOD & (~WWDT_MOD_WDINT_MASK));
  131. /* Clear timeout by writing a zero */
  132. if (mask & kWWDT_TimeoutFlag)
  133. {
  134. reg &= ~WWDT_MOD_WDTOF_MASK;
  135. }
  136. /* Clear warning interrupt flag by writing a one */
  137. if (mask & kWWDT_WarningFlag)
  138. {
  139. reg |= WWDT_MOD_WDINT_MASK;
  140. }
  141. base->MOD = reg;
  142. }