fsl_wdog.c 6.0 KB

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