您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

174 行
5.6 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_lptmr.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.lptmr"
  12. #endif
  13. /*******************************************************************************
  14. * Prototypes
  15. ******************************************************************************/
  16. #if defined(LPTMR_CLOCKS)
  17. /*!
  18. * @brief Gets the instance from the base address to be used to gate or ungate the module clock
  19. *
  20. * @param base LPTMR peripheral base address
  21. *
  22. * @return The LPTMR instance
  23. */
  24. static uint32_t LPTMR_GetInstance(LPTMR_Type *base);
  25. #endif /* LPTMR_CLOCKS */
  26. /*******************************************************************************
  27. * Variables
  28. ******************************************************************************/
  29. #if defined(LPTMR_CLOCKS)
  30. /*! @brief Pointers to LPTMR bases for each instance. */
  31. static LPTMR_Type *const s_lptmrBases[] = LPTMR_BASE_PTRS;
  32. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  33. /*! @brief Pointers to LPTMR clocks for each instance. */
  34. static const clock_ip_name_t s_lptmrClocks[] = LPTMR_CLOCKS;
  35. #if defined(LPTMR_PERIPH_CLOCKS)
  36. /* Array of LPTMR functional clock name. */
  37. static const clock_ip_name_t s_lptmrPeriphClocks[] = LPTMR_PERIPH_CLOCKS;
  38. #endif
  39. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  40. #endif /* LPTMR_CLOCKS */
  41. /*******************************************************************************
  42. * Code
  43. ******************************************************************************/
  44. #if defined(LPTMR_CLOCKS)
  45. static uint32_t LPTMR_GetInstance(LPTMR_Type *base)
  46. {
  47. uint32_t instance;
  48. /* Find the instance index from base address mappings. */
  49. for (instance = 0; instance < ARRAY_SIZE(s_lptmrBases); instance++)
  50. {
  51. if (s_lptmrBases[instance] == base)
  52. {
  53. break;
  54. }
  55. }
  56. assert(instance < ARRAY_SIZE(s_lptmrBases));
  57. return instance;
  58. }
  59. #endif /* LPTMR_CLOCKS */
  60. /*!
  61. * brief Ungates the LPTMR clock and configures the peripheral for a basic operation.
  62. *
  63. * note This API should be called at the beginning of the application using the LPTMR driver.
  64. *
  65. * param base LPTMR peripheral base address
  66. * param config A pointer to the LPTMR configuration structure.
  67. */
  68. void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config)
  69. {
  70. assert(NULL != config);
  71. #if defined(LPTMR_CLOCKS)
  72. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  73. uint32_t instance = LPTMR_GetInstance(base);
  74. /* Ungate the LPTMR clock*/
  75. CLOCK_EnableClock(s_lptmrClocks[instance]);
  76. #if defined(LPTMR_PERIPH_CLOCKS)
  77. CLOCK_EnableClock(s_lptmrPeriphClocks[instance]);
  78. #endif
  79. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  80. #endif /* LPTMR_CLOCKS */
  81. /* Configure the timers operation mode and input pin setup */
  82. base->CSR = (LPTMR_CSR_TMS(config->timerMode) | LPTMR_CSR_TFC(config->enableFreeRunning) |
  83. LPTMR_CSR_TPP(config->pinPolarity) | LPTMR_CSR_TPS(config->pinSelect));
  84. /* Configure the prescale value and clock source */
  85. base->PSR = (LPTMR_PSR_PRESCALE(config->value) | LPTMR_PSR_PBYP(config->bypassPrescaler) |
  86. LPTMR_PSR_PCS(config->prescalerClockSource));
  87. }
  88. /*!
  89. * brief Gates the LPTMR clock.
  90. *
  91. * param base LPTMR peripheral base address
  92. */
  93. void LPTMR_Deinit(LPTMR_Type *base)
  94. {
  95. /* Disable the LPTMR and reset the internal logic */
  96. base->CSR &= ~LPTMR_CSR_TEN_MASK;
  97. #if defined(LPTMR_CLOCKS)
  98. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  99. uint32_t instance = LPTMR_GetInstance(base);
  100. /* Gate the LPTMR clock*/
  101. CLOCK_DisableClock(s_lptmrClocks[instance]);
  102. #if defined(LPTMR_PERIPH_CLOCKS)
  103. CLOCK_DisableClock(s_lptmrPeriphClocks[instance]);
  104. #endif
  105. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  106. #endif /* LPTMR_CLOCKS */
  107. }
  108. /*!
  109. * brief Fills in the LPTMR configuration structure with default settings.
  110. *
  111. * The default values are as follows.
  112. * code
  113. * config->timerMode = kLPTMR_TimerModeTimeCounter;
  114. * config->pinSelect = kLPTMR_PinSelectInput_0;
  115. * config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
  116. * config->enableFreeRunning = false;
  117. * config->bypassPrescaler = true;
  118. * config->prescalerClockSource = kLPTMR_PrescalerClock_1;
  119. * config->value = kLPTMR_Prescale_Glitch_0;
  120. * endcode
  121. * param config A pointer to the LPTMR configuration structure.
  122. */
  123. void LPTMR_GetDefaultConfig(lptmr_config_t *config)
  124. {
  125. assert(NULL != config);
  126. /* Initializes the configure structure to zero. */
  127. (void)memset(config, 0, sizeof(*config));
  128. /* Use time counter mode */
  129. config->timerMode = kLPTMR_TimerModeTimeCounter;
  130. /* Use input 0 as source in pulse counter mode */
  131. config->pinSelect = kLPTMR_PinSelectInput_0;
  132. /* Pulse input pin polarity is active-high */
  133. config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
  134. /* Counter resets whenever TCF flag is set */
  135. config->enableFreeRunning = false;
  136. /* Bypass the prescaler */
  137. config->bypassPrescaler = true;
  138. /* LPTMR clock source */
  139. #if !(defined(FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT) && \
  140. FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT)
  141. config->prescalerClockSource = kLPTMR_PrescalerClock_1;
  142. #else
  143. config->prescalerClockSource = kLPTMR_PrescalerClock_0;
  144. #endif /* FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT */
  145. /* Divide the prescaler clock by 2 */
  146. config->value = kLPTMR_Prescale_Glitch_0;
  147. }