fsl_wdog.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, 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. #include "fsl_wdog.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.wdog"
  38. #endif
  39. /*******************************************************************************
  40. * Code
  41. ******************************************************************************/
  42. void WDOG_GetDefaultConfig(wdog_config_t *config)
  43. {
  44. assert(config);
  45. config->enableWdog = true;
  46. config->clockSource = kWDOG_LpoClockSource;
  47. config->prescaler = kWDOG_ClockPrescalerDivide1;
  48. #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
  49. config->workMode.enableWait = true;
  50. #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
  51. config->workMode.enableStop = false;
  52. config->workMode.enableDebug = false;
  53. config->enableUpdate = true;
  54. config->enableInterrupt = false;
  55. config->enableWindowMode = false;
  56. config->windowValue = 0U;
  57. config->timeoutValue = 0xFFFFU;
  58. }
  59. void WDOG_Init(WDOG_Type *base, const wdog_config_t *config)
  60. {
  61. assert(config);
  62. uint32_t value = 0U;
  63. uint32_t primaskValue = 0U;
  64. value = WDOG_STCTRLH_WDOGEN(config->enableWdog) | WDOG_STCTRLH_CLKSRC(config->clockSource) |
  65. WDOG_STCTRLH_IRQRSTEN(config->enableInterrupt) | WDOG_STCTRLH_WINEN(config->enableWindowMode) |
  66. WDOG_STCTRLH_ALLOWUPDATE(config->enableUpdate) | WDOG_STCTRLH_DBGEN(config->workMode.enableDebug) |
  67. WDOG_STCTRLH_STOPEN(config->workMode.enableStop) |
  68. #if defined(FSL_FEATURE_WDOG_HAS_WAITEN) && FSL_FEATURE_WDOG_HAS_WAITEN
  69. WDOG_STCTRLH_WAITEN(config->workMode.enableWait) |
  70. #endif /* FSL_FEATURE_WDOG_HAS_WAITEN */
  71. WDOG_STCTRLH_DISTESTWDOG(1U);
  72. /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
  73. * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
  74. primaskValue = DisableGlobalIRQ();
  75. WDOG_Unlock(base);
  76. /* Wait one bus clock cycle */
  77. base->RSTCNT = 0U;
  78. /* Set configruation */
  79. base->PRESC = WDOG_PRESC_PRESCVAL(config->prescaler);
  80. base->WINH = (uint16_t)((config->windowValue >> 16U) & 0xFFFFU);
  81. base->WINL = (uint16_t)((config->windowValue) & 0xFFFFU);
  82. base->TOVALH = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
  83. base->TOVALL = (uint16_t)((config->timeoutValue) & 0xFFFFU);
  84. base->STCTRLH = value;
  85. EnableGlobalIRQ(primaskValue);
  86. }
  87. void WDOG_Deinit(WDOG_Type *base)
  88. {
  89. uint32_t primaskValue = 0U;
  90. /* Disable the global interrupts */
  91. primaskValue = DisableGlobalIRQ();
  92. WDOG_Unlock(base);
  93. /* Wait one bus clock cycle */
  94. base->RSTCNT = 0U;
  95. WDOG_Disable(base);
  96. EnableGlobalIRQ(primaskValue);
  97. WDOG_ClearResetCount(base);
  98. }
  99. void WDOG_SetTestModeConfig(WDOG_Type *base, wdog_test_config_t *config)
  100. {
  101. assert(config);
  102. uint32_t value = 0U;
  103. uint32_t primaskValue = 0U;
  104. value = WDOG_STCTRLH_DISTESTWDOG(0U) | WDOG_STCTRLH_TESTWDOG(1U) | WDOG_STCTRLH_TESTSEL(config->testMode) |
  105. WDOG_STCTRLH_BYTESEL(config->testedByte) | WDOG_STCTRLH_IRQRSTEN(0U) | WDOG_STCTRLH_WDOGEN(1U) |
  106. WDOG_STCTRLH_ALLOWUPDATE(1U);
  107. /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
  108. * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
  109. primaskValue = DisableGlobalIRQ();
  110. WDOG_Unlock(base);
  111. /* Wait one bus clock cycle */
  112. base->RSTCNT = 0U;
  113. /* Set configruation */
  114. base->TOVALH = (uint16_t)((config->timeoutValue >> 16U) & 0xFFFFU);
  115. base->TOVALL = (uint16_t)((config->timeoutValue) & 0xFFFFU);
  116. base->STCTRLH = value;
  117. EnableGlobalIRQ(primaskValue);
  118. }
  119. uint32_t WDOG_GetStatusFlags(WDOG_Type *base)
  120. {
  121. uint32_t status_flag = 0U;
  122. status_flag |= (base->STCTRLH & WDOG_STCTRLH_WDOGEN_MASK);
  123. status_flag |= (base->STCTRLL & WDOG_STCTRLL_INTFLG_MASK);
  124. return status_flag;
  125. }
  126. void WDOG_ClearStatusFlags(WDOG_Type *base, uint32_t mask)
  127. {
  128. if (mask & kWDOG_TimeoutFlag)
  129. {
  130. base->STCTRLL |= WDOG_STCTRLL_INTFLG_MASK;
  131. }
  132. }
  133. void WDOG_Refresh(WDOG_Type *base)
  134. {
  135. uint32_t primaskValue = 0U;
  136. /* Disable the global interrupt to protect refresh sequence */
  137. primaskValue = DisableGlobalIRQ();
  138. base->REFRESH = WDOG_FIRST_WORD_OF_REFRESH;
  139. base->REFRESH = WDOG_SECOND_WORD_OF_REFRESH;
  140. EnableGlobalIRQ(primaskValue);
  141. }