fsl_mrt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_mrt.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.mrt"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Gets the instance from the base address
  44. *
  45. * @param base Multi-Rate timer peripheral base address
  46. *
  47. * @return The MRT instance
  48. */
  49. static uint32_t MRT_GetInstance(MRT_Type *base);
  50. /*******************************************************************************
  51. * Variables
  52. ******************************************************************************/
  53. /*! @brief Pointers to MRT bases for each instance. */
  54. static MRT_Type *const s_mrtBases[] = MRT_BASE_PTRS;
  55. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  56. /*! @brief Pointers to MRT clocks for each instance. */
  57. static const clock_ip_name_t s_mrtClocks[] = MRT_CLOCKS;
  58. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  59. #if defined(FSL_FEATURE_MRT_WRITE_ZERO_ASSERT_RESET) && FSL_FEATURE_MRT_WRITE_ZERO_ASSERT_RESET
  60. /*! @brief Pointers to MRT resets for each instance, writing a zero asserts the reset */
  61. static const reset_ip_name_t s_mrtResets[] = MRT_RSTS_N;
  62. #else
  63. /*! @brief Pointers to MRT resets for each instance, writing a one asserts the reset */
  64. static const reset_ip_name_t s_mrtResets[] = MRT_RSTS;
  65. #endif
  66. /*******************************************************************************
  67. * Code
  68. ******************************************************************************/
  69. static uint32_t MRT_GetInstance(MRT_Type *base)
  70. {
  71. uint32_t instance;
  72. uint32_t mrtArrayCount = (sizeof(s_mrtBases) / sizeof(s_mrtBases[0]));
  73. /* Find the instance index from base address mappings. */
  74. for (instance = 0; instance < mrtArrayCount; instance++)
  75. {
  76. if (s_mrtBases[instance] == base)
  77. {
  78. break;
  79. }
  80. }
  81. assert(instance < mrtArrayCount);
  82. return instance;
  83. }
  84. void MRT_Init(MRT_Type *base, const mrt_config_t *config)
  85. {
  86. assert(config);
  87. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  88. /* Ungate the MRT clock */
  89. CLOCK_EnableClock(s_mrtClocks[MRT_GetInstance(base)]);
  90. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  91. /* Reset the module */
  92. RESET_PeripheralReset(s_mrtResets[MRT_GetInstance(base)]);
  93. #if !(defined(FSL_FEATURE_MRT_HAS_NO_MODCFG_MULTITASK) && FSL_FEATURE_MRT_HAS_NO_MODCFG_MULTITASK)
  94. /* Set timer operating mode */
  95. base->MODCFG = MRT_MODCFG_MULTITASK(config->enableMultiTask);
  96. #endif
  97. }
  98. void MRT_Deinit(MRT_Type *base)
  99. {
  100. /* Stop all the timers */
  101. MRT_StopTimer(base, kMRT_Channel_0);
  102. MRT_StopTimer(base, kMRT_Channel_1);
  103. #if (FSL_FEATURE_MRT_NUMBER_OF_CHANNELS > 2U)
  104. MRT_StopTimer(base, kMRT_Channel_2);
  105. #endif
  106. #if (FSL_FEATURE_MRT_NUMBER_OF_CHANNELS > 3U)
  107. MRT_StopTimer(base, kMRT_Channel_3);
  108. #endif
  109. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  110. /* Gate the MRT clock*/
  111. CLOCK_DisableClock(s_mrtClocks[MRT_GetInstance(base)]);
  112. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  113. }
  114. void MRT_UpdateTimerPeriod(MRT_Type *base, mrt_chnl_t channel, uint32_t count, bool immediateLoad)
  115. {
  116. assert(channel < FSL_FEATURE_MRT_NUMBER_OF_CHANNELS);
  117. uint32_t newValue = count;
  118. if (((base->CHANNEL[channel].CTRL & MRT_CHANNEL_CTRL_MODE_MASK) == kMRT_OneShotMode) || (immediateLoad))
  119. {
  120. /* For one-shot interrupt mode, load the new value immediately even if user forgot to enable */
  121. newValue |= MRT_CHANNEL_INTVAL_LOAD_MASK;
  122. }
  123. /* Update the timer interval value */
  124. base->CHANNEL[channel].INTVAL = newValue;
  125. }