fsl_lptmr.c 5.8 KB

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