You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

141 line
4.6 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017, 2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_ewm.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.ewm"
  12. #endif
  13. /*******************************************************************************
  14. * Code
  15. ******************************************************************************/
  16. /*!
  17. * brief Initializes the EWM peripheral.
  18. *
  19. * This function is used to initialize the EWM. After calling, the EWM
  20. * runs immediately according to the configuration.
  21. * Note that, except for the interrupt enable control bit, other control bits and registers are write once after a
  22. * CPU reset. Modifying them more than once generates a bus transfer error.
  23. *
  24. * This is an example.
  25. * code
  26. * ewm_config_t config;
  27. * EWM_GetDefaultConfig(&config);
  28. * config.compareHighValue = 0xAAU;
  29. * EWM_Init(ewm_base,&config);
  30. * endcode
  31. *
  32. * param base EWM peripheral base address
  33. * param config The configuration of the EWM
  34. */
  35. void EWM_Init(EWM_Type *base, const ewm_config_t *config)
  36. {
  37. assert(NULL != config);
  38. uint8_t value = 0U;
  39. #if !((defined(FSL_FEATURE_SOC_PCC_COUNT) && FSL_FEATURE_SOC_PCC_COUNT) && \
  40. (defined(FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE) && FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE))
  41. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  42. CLOCK_EnableClock(kCLOCK_Ewm0);
  43. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  44. #endif
  45. value = EWM_CTRL_EWMEN(config->enableEwm) | EWM_CTRL_ASSIN(config->setInputAssertLogic) |
  46. EWM_CTRL_INEN(config->enableEwmInput) | EWM_CTRL_INTEN(config->enableInterrupt);
  47. #if defined(FSL_FEATURE_EWM_HAS_PRESCALER) && FSL_FEATURE_EWM_HAS_PRESCALER
  48. base->CLKPRESCALER = config->prescaler;
  49. #endif /* FSL_FEATURE_EWM_HAS_PRESCALER */
  50. #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
  51. base->CLKCTRL = (uint8_t)config->clockSource;
  52. #endif /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT*/
  53. base->CMPL = config->compareLowValue;
  54. base->CMPH = config->compareHighValue;
  55. base->CTRL = value;
  56. }
  57. /*!
  58. * brief Deinitializes the EWM peripheral.
  59. *
  60. * This function is used to shut down the EWM.
  61. *
  62. * param base EWM peripheral base address
  63. */
  64. void EWM_Deinit(EWM_Type *base)
  65. {
  66. EWM_DisableInterrupts(base, (uint32_t)kEWM_InterruptEnable);
  67. #if !((defined(FSL_FEATURE_SOC_PCC_COUNT) && FSL_FEATURE_SOC_PCC_COUNT) && \
  68. (defined(FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE) && FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE))
  69. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  70. CLOCK_DisableClock(kCLOCK_Ewm0);
  71. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  72. #endif /* FSL_FEATURE_PCC_SUPPORT_EWM_CLOCK_REMOVE */
  73. }
  74. /*!
  75. * brief Initializes the EWM configuration structure.
  76. *
  77. * This function initializes the EWM configuration structure to default values. The default
  78. * values are as follows.
  79. * code
  80. * ewmConfig->enableEwm = true;
  81. * ewmConfig->enableEwmInput = false;
  82. * ewmConfig->setInputAssertLogic = false;
  83. * ewmConfig->enableInterrupt = false;
  84. * ewmConfig->ewm_lpo_clock_source_t = kEWM_LpoClockSource0;
  85. * ewmConfig->prescaler = 0;
  86. * ewmConfig->compareLowValue = 0;
  87. * ewmConfig->compareHighValue = 0xFEU;
  88. * endcode
  89. *
  90. * param config Pointer to the EWM configuration structure.
  91. * see ewm_config_t
  92. */
  93. void EWM_GetDefaultConfig(ewm_config_t *config)
  94. {
  95. assert(NULL != config);
  96. /* Initializes the configure structure to zero. */
  97. (void)memset(config, 0, sizeof(*config));
  98. config->enableEwm = true;
  99. config->enableEwmInput = false;
  100. config->setInputAssertLogic = false;
  101. config->enableInterrupt = false;
  102. #if defined(FSL_FEATURE_EWM_HAS_CLOCK_SELECT) && FSL_FEATURE_EWM_HAS_CLOCK_SELECT
  103. config->clockSource = kEWM_LpoClockSource0;
  104. #endif /* FSL_FEATURE_EWM_HAS_CLOCK_SELECT*/
  105. #if defined(FSL_FEATURE_EWM_HAS_PRESCALER) && FSL_FEATURE_EWM_HAS_PRESCALER
  106. config->prescaler = 0U;
  107. #endif /* FSL_FEATURE_EWM_HAS_PRESCALER */
  108. config->compareLowValue = 0U;
  109. config->compareHighValue = 0xFEU;
  110. }
  111. /*!
  112. * brief Services the EWM.
  113. *
  114. * This function resets the EWM counter to zero.
  115. *
  116. * param base EWM peripheral base address
  117. */
  118. void EWM_Refresh(EWM_Type *base)
  119. {
  120. uint32_t primaskValue = 0U;
  121. /* Disable the global interrupt to protect refresh sequence */
  122. primaskValue = DisableGlobalIRQ();
  123. base->SERV = (uint8_t)0xB4U;
  124. base->SERV = (uint8_t)0x2CU;
  125. EnableGlobalIRQ(primaskValue);
  126. }