fsl_rit.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_rit.h"
  31. /*******************************************************************************
  32. * Prototypes
  33. ******************************************************************************/
  34. /*!
  35. * @brief Gets the instance from the base address to be used to gate or ungate the module clock
  36. *
  37. * @param base RIT peripheral base address
  38. *
  39. * @return The RIT instance
  40. */
  41. static uint32_t RIT_GetInstance(RIT_Type *base);
  42. /*******************************************************************************
  43. * Variables
  44. ******************************************************************************/
  45. /*! @brief Pointers to RIT bases for each instance. */
  46. static RIT_Type *const s_ritBases[] = RIT_BASE_PTRS;
  47. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  48. /*! @brief Pointers to PIT clocks for each instance. */
  49. static const clock_ip_name_t s_ritClocks[] = RIT_CLOCKS;
  50. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  51. /*******************************************************************************
  52. * Code
  53. ******************************************************************************/
  54. static uint32_t RIT_GetInstance(RIT_Type *base)
  55. {
  56. uint32_t instance;
  57. /* Find the instance index from base address mappings. */
  58. for (instance = 0; instance < ARRAY_SIZE(s_ritBases); instance++)
  59. {
  60. if (s_ritBases[instance] == base)
  61. {
  62. break;
  63. }
  64. }
  65. assert(instance < ARRAY_SIZE(s_ritBases));
  66. return instance;
  67. }
  68. void RIT_GetDefaultConfig(rit_config_t *config)
  69. {
  70. assert(config);
  71. /* Timer operation are no effect in Debug mode */
  72. config->enableRunInDebug = false;
  73. }
  74. void RIT_Init(RIT_Type *base, const rit_config_t *config)
  75. {
  76. assert(config);
  77. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  78. /* Ungate the RIT clock*/
  79. CLOCK_EnableClock(s_ritClocks[RIT_GetInstance(base)]);
  80. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  81. /* Enable RIT timers */
  82. base->CTRL &= ~RIT_CTRL_RITEN_MASK;
  83. /* Config timer operation is no effect in debug mode */
  84. if (!config->enableRunInDebug)
  85. {
  86. base->CTRL &= ~RIT_CTRL_RITENBR_MASK;
  87. }
  88. else
  89. {
  90. base->CTRL |= RIT_CTRL_RITENBR_MASK;
  91. }
  92. }
  93. void RIT_Deinit(RIT_Type *base)
  94. {
  95. /* Disable RIT timers */
  96. base->CTRL |= ~RIT_CTRL_RITEN_MASK;
  97. #ifdef FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
  98. /* Gate the RIT clock*/
  99. CLOCK_DisableClock(s_ritClocks[RIT_GetInstance(base)]);
  100. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  101. }
  102. void RIT_SetTimerCompare(RIT_Type *base, uint64_t count)
  103. {
  104. /* Disable RIT timers */
  105. base->CTRL &= ~RIT_CTRL_RITEN_MASK;
  106. base->COMPVAL = (uint32_t)count;
  107. base->COMPVAL_H = (uint16_t)(count >> 32U);
  108. }
  109. void RIT_SetMaskBit(RIT_Type *base, uint64_t count)
  110. {
  111. base->MASK = (uint32_t)count;
  112. base->MASK_H = (uint16_t)(count >> 32U);
  113. }
  114. uint64_t RIT_GetCompareTimerCount(RIT_Type *base)
  115. {
  116. uint16_t valueH = 0U;
  117. uint32_t valueL = 0U;
  118. /* COMPVAL_H should be read before COMPVAL */
  119. valueH = base->COMPVAL_H;
  120. valueL = base->COMPVAL;
  121. return (((uint64_t)valueH << 32U) + (uint64_t)(valueL));
  122. }
  123. uint64_t RIT_GetCounterTimerCount(RIT_Type *base)
  124. {
  125. uint16_t valueH = 0U;
  126. uint32_t valueL = 0U;
  127. /* COUNTER_H should be read before COUNTER */
  128. valueH = base->COUNTER_H;
  129. valueL = base->COUNTER;
  130. return (((uint64_t)valueH << 32U) + (uint64_t)(valueL));
  131. }
  132. uint64_t RIT_GetMaskTimerCount(RIT_Type *base)
  133. {
  134. uint16_t valueH = 0U;
  135. uint32_t valueL = 0U;
  136. /* MASK_H should be read before MASK */
  137. valueH = base->MASK_H;
  138. valueL = base->MASK;
  139. return (((uint64_t)valueH << 32U) + (uint64_t)(valueL));
  140. }