fsl_wdog.c 5.8 KB

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