fsl_wdog.c 4.6 KB

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