fsl_wdog.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_wdog.h"
  31. #ifdef RT_USING_SMART
  32. #include "imx6ull.h"
  33. #endif
  34. /*******************************************************************************
  35. * Variables
  36. ******************************************************************************/
  37. static WDOG_Type *const s_wdogBases[] = WDOG_BASE_PTRS;
  38. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  39. /* Array of WDOG clock name. */
  40. static const clock_ip_name_t s_wdogClock[] = WDOG_CLOCKS;
  41. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  42. /*******************************************************************************
  43. * Code
  44. ******************************************************************************/
  45. static uint32_t WDOG_GetInstance(WDOG_Type *base)
  46. {
  47. uint32_t instance;
  48. /* Find the instance index from base address mappings. */
  49. for (instance = 0; instance < ARRAY_SIZE(s_wdogBases); instance++)
  50. {
  51. #ifdef RT_USING_SMART
  52. if (s_wdogBases[instance] == rt_kmem_v2p(base))
  53. #else
  54. if (s_wdogBases[instance] == base)
  55. #endif
  56. {
  57. break;
  58. }
  59. }
  60. assert(instance < ARRAY_SIZE(s_wdogBases));
  61. return instance;
  62. }
  63. void WDOG_GetDefaultConfig(wdog_config_t *config)
  64. {
  65. assert(config);
  66. config->enableWdog = true;
  67. config->workMode.enableWait = false;
  68. config->workMode.enableStop = false;
  69. config->workMode.enableDebug = false;
  70. config->enableInterrupt = false;
  71. config->softwareResetExtension = false;
  72. config->enablePowerDown = false;
  73. config->softwareAssertion= true;
  74. config->softwareResetSignal = true;
  75. config->timeoutValue = 0xffu;
  76. config->interruptTimeValue = 0x04u;
  77. }
  78. void WDOG_Init(WDOG_Type *base, const wdog_config_t *config)
  79. {
  80. assert(config);
  81. uint16_t value = 0u;
  82. value = WDOG_WCR_WDE(config->enableWdog) | WDOG_WCR_WDW(config->workMode.enableWait) |
  83. WDOG_WCR_WDZST(config->workMode.enableStop) | WDOG_WCR_WDBG(config->workMode.enableDebug) |
  84. WDOG_WCR_SRE(config->softwareResetExtension) | WDOG_WCR_WT(config->timeoutValue) |
  85. WDOG_WCR_WDA(config->softwareAssertion) | WDOG_WCR_SRS(config->softwareResetSignal);
  86. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  87. /* Set configruation */
  88. CLOCK_EnableClock(s_wdogClock[WDOG_GetInstance(base)]);
  89. #endif
  90. base->WICR = WDOG_WICR_WICT(config->interruptTimeValue) | WDOG_WICR_WIE(config->enableInterrupt);
  91. base->WMCR = WDOG_WMCR_PDE(config->enablePowerDown);
  92. base->WCR = value;
  93. }
  94. void WDOG_Deinit(WDOG_Type *base)
  95. {
  96. if (base->WCR & WDOG_WCR_WDBG_MASK)
  97. {
  98. WDOG_Disable(base);
  99. }
  100. }
  101. uint16_t WDOG_GetStatusFlags(WDOG_Type *base)
  102. {
  103. uint16_t status_flag = 0U;
  104. status_flag |= (base->WCR & WDOG_WCR_WDE_MASK);
  105. status_flag |= (base->WRSR & WDOG_WRSR_POR_MASK);
  106. status_flag |= (base->WRSR & WDOG_WRSR_TOUT_MASK);
  107. status_flag |= (base->WRSR & WDOG_WRSR_SFTW_MASK);
  108. status_flag |= (base->WICR & WDOG_WICR_WTIS_MASK);
  109. return status_flag;
  110. }
  111. void WDOG_ClearInterruptStatus(WDOG_Type *base, uint16_t mask)
  112. {
  113. if (mask & kWDOG_InterruptFlag)
  114. {
  115. base->WICR |= WDOG_WICR_WTIS_MASK;
  116. }
  117. }
  118. void WDOG_Refresh(WDOG_Type *base)
  119. {
  120. base->WSR = WDOG_REFRESH_KEY & 0xFFFFU;
  121. base->WSR = (WDOG_REFRESH_KEY >> 16U) & 0xFFFFU;
  122. }