fsl_lptmr.c 5.3 KB

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