fsl_wdog.c 4.9 KB

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