fsl_rtwdog.c 3.7 KB

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