fsl_rtwdog.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_rtwdog.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.rtwdog"
  38. #endif
  39. /*******************************************************************************
  40. * Code
  41. ******************************************************************************/
  42. void RTWDOG_ClearStatusFlags(RTWDOG_Type *base, uint32_t mask)
  43. {
  44. if (mask & kRTWDOG_InterruptFlag)
  45. {
  46. base->CS |= RTWDOG_CS_FLG_MASK;
  47. }
  48. }
  49. void RTWDOG_GetDefaultConfig(rtwdog_config_t *config)
  50. {
  51. assert(config);
  52. config->enableRtwdog = true;
  53. config->clockSource = kRTWDOG_ClockSource1;
  54. config->prescaler = kRTWDOG_ClockPrescalerDivide1;
  55. config->workMode.enableWait = true;
  56. config->workMode.enableStop = false;
  57. config->workMode.enableDebug = false;
  58. config->testMode = kRTWDOG_TestModeDisabled;
  59. config->enableUpdate = true;
  60. config->enableInterrupt = false;
  61. config->enableWindowMode = false;
  62. config->windowValue = 0U;
  63. config->timeoutValue = 0xFFFFU;
  64. }
  65. void RTWDOG_Init(RTWDOG_Type *base, const rtwdog_config_t *config)
  66. {
  67. assert(config);
  68. uint32_t value = 0U;
  69. uint32_t primaskValue = 0U;
  70. value = RTWDOG_CS_EN(config->enableRtwdog) | RTWDOG_CS_CLK(config->clockSource) | RTWDOG_CS_INT(config->enableInterrupt) |
  71. RTWDOG_CS_WIN(config->enableWindowMode) | RTWDOG_CS_UPDATE(config->enableUpdate) |
  72. RTWDOG_CS_DBG(config->workMode.enableDebug) | RTWDOG_CS_STOP(config->workMode.enableStop) |
  73. RTWDOG_CS_WAIT(config->workMode.enableWait) | RTWDOG_CS_PRES(config->prescaler) | RTWDOG_CS_CMD32EN(true) |
  74. RTWDOG_CS_TST(config->testMode);
  75. /* Disable the global interrupts. Otherwise, an interrupt could effectively invalidate the unlock sequence
  76. * and the WCT may expire. After the configuration finishes, re-enable the global interrupts. */
  77. primaskValue = DisableGlobalIRQ();
  78. RTWDOG_Unlock(base);
  79. base->WIN = config->windowValue;
  80. base->TOVAL = config->timeoutValue;
  81. base->CS = value;
  82. EnableGlobalIRQ(primaskValue);
  83. }
  84. void RTWDOG_Deinit(RTWDOG_Type *base)
  85. {
  86. uint32_t primaskValue = 0U;
  87. /* Disable the global interrupts */
  88. primaskValue = DisableGlobalIRQ();
  89. RTWDOG_Unlock(base);
  90. RTWDOG_Disable(base);
  91. EnableGlobalIRQ(primaskValue);
  92. }