fsl_lptmr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, 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_lptmr.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.lptmr"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. #if defined(LPTMR_CLOCKS)
  43. /*!
  44. * @brief Gets the instance from the base address to be used to gate or ungate the module clock
  45. *
  46. * @param base LPTMR peripheral base address
  47. *
  48. * @return The LPTMR instance
  49. */
  50. static uint32_t LPTMR_GetInstance(LPTMR_Type *base);
  51. #endif /* LPTMR_CLOCKS */
  52. /*******************************************************************************
  53. * Variables
  54. ******************************************************************************/
  55. #if defined(LPTMR_CLOCKS)
  56. /*! @brief Pointers to LPTMR bases for each instance. */
  57. static LPTMR_Type *const s_lptmrBases[] = LPTMR_BASE_PTRS;
  58. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  59. /*! @brief Pointers to LPTMR clocks for each instance. */
  60. static const clock_ip_name_t s_lptmrClocks[] = LPTMR_CLOCKS;
  61. #if defined(LPTMR_PERIPH_CLOCKS)
  62. /* Array of LPTMR functional clock name. */
  63. static const clock_ip_name_t s_lptmrPeriphClocks[] = LPTMR_PERIPH_CLOCKS;
  64. #endif
  65. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  66. #endif /* LPTMR_CLOCKS */
  67. /*******************************************************************************
  68. * Code
  69. ******************************************************************************/
  70. #if defined(LPTMR_CLOCKS)
  71. static uint32_t LPTMR_GetInstance(LPTMR_Type *base)
  72. {
  73. uint32_t instance;
  74. /* Find the instance index from base address mappings. */
  75. for (instance = 0; instance < ARRAY_SIZE(s_lptmrBases); instance++)
  76. {
  77. if (s_lptmrBases[instance] == base)
  78. {
  79. break;
  80. }
  81. }
  82. assert(instance < ARRAY_SIZE(s_lptmrBases));
  83. return instance;
  84. }
  85. #endif /* LPTMR_CLOCKS */
  86. void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config)
  87. {
  88. assert(config);
  89. #if defined(LPTMR_CLOCKS)
  90. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  91. uint32_t instance = LPTMR_GetInstance(base);
  92. /* Ungate the LPTMR clock*/
  93. CLOCK_EnableClock(s_lptmrClocks[instance]);
  94. #if defined(LPTMR_PERIPH_CLOCKS)
  95. CLOCK_EnableClock(s_lptmrPeriphClocks[instance]);
  96. #endif
  97. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  98. #endif /* LPTMR_CLOCKS */
  99. /* Configure the timers operation mode and input pin setup */
  100. base->CSR = (LPTMR_CSR_TMS(config->timerMode) | LPTMR_CSR_TFC(config->enableFreeRunning) |
  101. LPTMR_CSR_TPP(config->pinPolarity) | LPTMR_CSR_TPS(config->pinSelect));
  102. /* Configure the prescale value and clock source */
  103. base->PSR = (LPTMR_PSR_PRESCALE(config->value) | LPTMR_PSR_PBYP(config->bypassPrescaler) |
  104. LPTMR_PSR_PCS(config->prescalerClockSource));
  105. }
  106. void LPTMR_Deinit(LPTMR_Type *base)
  107. {
  108. /* Disable the LPTMR and reset the internal logic */
  109. base->CSR &= ~LPTMR_CSR_TEN_MASK;
  110. #if defined(LPTMR_CLOCKS)
  111. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  112. uint32_t instance = LPTMR_GetInstance(base);
  113. /* Gate the LPTMR clock*/
  114. CLOCK_DisableClock(s_lptmrClocks[instance]);
  115. #if defined(LPTMR_PERIPH_CLOCKS)
  116. CLOCK_DisableClock(s_lptmrPeriphClocks[instance]);
  117. #endif
  118. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  119. #endif /* LPTMR_CLOCKS */
  120. }
  121. void LPTMR_GetDefaultConfig(lptmr_config_t *config)
  122. {
  123. assert(config);
  124. /* Use time counter mode */
  125. config->timerMode = kLPTMR_TimerModeTimeCounter;
  126. /* Use input 0 as source in pulse counter mode */
  127. config->pinSelect = kLPTMR_PinSelectInput_0;
  128. /* Pulse input pin polarity is active-high */
  129. config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
  130. /* Counter resets whenever TCF flag is set */
  131. config->enableFreeRunning = false;
  132. /* Bypass the prescaler */
  133. config->bypassPrescaler = true;
  134. /* LPTMR clock source */
  135. config->prescalerClockSource = kLPTMR_PrescalerClock_1;
  136. /* Divide the prescaler clock by 2 */
  137. config->value = kLPTMR_Prescale_Glitch_0;
  138. }