Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

375 řádky
12 KiB

  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_LPTMR_H_
  9. #define _FSL_LPTMR_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup lptmr
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. #define FSL_LPTMR_DRIVER_VERSION (MAKE_VERSION(2, 1, 1)) /*!< Version 2.1.1 */
  21. /*@}*/
  22. /*! @brief LPTMR pin selection used in pulse counter mode.*/
  23. typedef enum _lptmr_pin_select
  24. {
  25. kLPTMR_PinSelectInput_0 = 0x0U, /*!< Pulse counter input 0 is selected */
  26. kLPTMR_PinSelectInput_1 = 0x1U, /*!< Pulse counter input 1 is selected */
  27. kLPTMR_PinSelectInput_2 = 0x2U, /*!< Pulse counter input 2 is selected */
  28. kLPTMR_PinSelectInput_3 = 0x3U /*!< Pulse counter input 3 is selected */
  29. } lptmr_pin_select_t;
  30. /*! @brief LPTMR pin polarity used in pulse counter mode.*/
  31. typedef enum _lptmr_pin_polarity
  32. {
  33. kLPTMR_PinPolarityActiveHigh = 0x0U, /*!< Pulse Counter input source is active-high */
  34. kLPTMR_PinPolarityActiveLow = 0x1U /*!< Pulse Counter input source is active-low */
  35. } lptmr_pin_polarity_t;
  36. /*! @brief LPTMR timer mode selection.*/
  37. typedef enum _lptmr_timer_mode
  38. {
  39. kLPTMR_TimerModeTimeCounter = 0x0U, /*!< Time Counter mode */
  40. kLPTMR_TimerModePulseCounter = 0x1U /*!< Pulse Counter mode */
  41. } lptmr_timer_mode_t;
  42. /*! @brief LPTMR prescaler/glitch filter values*/
  43. typedef enum _lptmr_prescaler_glitch_value
  44. {
  45. kLPTMR_Prescale_Glitch_0 = 0x0U, /*!< Prescaler divide 2, glitch filter does not support this setting */
  46. kLPTMR_Prescale_Glitch_1 = 0x1U, /*!< Prescaler divide 4, glitch filter 2 */
  47. kLPTMR_Prescale_Glitch_2 = 0x2U, /*!< Prescaler divide 8, glitch filter 4 */
  48. kLPTMR_Prescale_Glitch_3 = 0x3U, /*!< Prescaler divide 16, glitch filter 8 */
  49. kLPTMR_Prescale_Glitch_4 = 0x4U, /*!< Prescaler divide 32, glitch filter 16 */
  50. kLPTMR_Prescale_Glitch_5 = 0x5U, /*!< Prescaler divide 64, glitch filter 32 */
  51. kLPTMR_Prescale_Glitch_6 = 0x6U, /*!< Prescaler divide 128, glitch filter 64 */
  52. kLPTMR_Prescale_Glitch_7 = 0x7U, /*!< Prescaler divide 256, glitch filter 128 */
  53. kLPTMR_Prescale_Glitch_8 = 0x8U, /*!< Prescaler divide 512, glitch filter 256 */
  54. kLPTMR_Prescale_Glitch_9 = 0x9U, /*!< Prescaler divide 1024, glitch filter 512*/
  55. kLPTMR_Prescale_Glitch_10 = 0xAU, /*!< Prescaler divide 2048 glitch filter 1024 */
  56. kLPTMR_Prescale_Glitch_11 = 0xBU, /*!< Prescaler divide 4096, glitch filter 2048 */
  57. kLPTMR_Prescale_Glitch_12 = 0xCU, /*!< Prescaler divide 8192, glitch filter 4096 */
  58. kLPTMR_Prescale_Glitch_13 = 0xDU, /*!< Prescaler divide 16384, glitch filter 8192 */
  59. kLPTMR_Prescale_Glitch_14 = 0xEU, /*!< Prescaler divide 32768, glitch filter 16384 */
  60. kLPTMR_Prescale_Glitch_15 = 0xFU /*!< Prescaler divide 65536, glitch filter 32768 */
  61. } lptmr_prescaler_glitch_value_t;
  62. /*!
  63. * @brief LPTMR prescaler/glitch filter clock select.
  64. * @note Clock connections are SoC-specific
  65. */
  66. typedef enum _lptmr_prescaler_clock_select
  67. {
  68. kLPTMR_PrescalerClock_0 = 0x0U, /*!< Prescaler/glitch filter clock 0 selected. */
  69. #if !(defined(FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT) && \
  70. FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT)
  71. kLPTMR_PrescalerClock_1 = 0x1U, /*!< Prescaler/glitch filter clock 1 selected. */
  72. #endif /* FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_1_SUPPORT */
  73. kLPTMR_PrescalerClock_2 = 0x2U, /*!< Prescaler/glitch filter clock 2 selected. */
  74. #if !(defined(FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_3_SUPPORT) && \
  75. FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_3_SUPPORT)
  76. kLPTMR_PrescalerClock_3 = 0x3U, /*!< Prescaler/glitch filter clock 3 selected. */
  77. #endif /* FSL_FEATURE_LPTMR_HAS_NO_PRESCALER_CLOCK_SOURCE_3_SUPPORT */
  78. } lptmr_prescaler_clock_select_t;
  79. /*! @brief List of the LPTMR interrupts */
  80. typedef enum _lptmr_interrupt_enable
  81. {
  82. kLPTMR_TimerInterruptEnable = LPTMR_CSR_TIE_MASK, /*!< Timer interrupt enable */
  83. } lptmr_interrupt_enable_t;
  84. /*! @brief List of the LPTMR status flags */
  85. typedef enum _lptmr_status_flags
  86. {
  87. kLPTMR_TimerCompareFlag = LPTMR_CSR_TCF_MASK, /*!< Timer compare flag */
  88. } lptmr_status_flags_t;
  89. /*!
  90. * @brief LPTMR config structure
  91. *
  92. * This structure holds the configuration settings for the LPTMR peripheral. To initialize this
  93. * structure to reasonable defaults, call the LPTMR_GetDefaultConfig() function and pass a
  94. * pointer to your configuration structure instance.
  95. *
  96. * The configuration struct can be made constant so it resides in flash.
  97. */
  98. typedef struct _lptmr_config
  99. {
  100. lptmr_timer_mode_t timerMode; /*!< Time counter mode or pulse counter mode */
  101. lptmr_pin_select_t pinSelect; /*!< LPTMR pulse input pin select; used only in pulse counter mode */
  102. lptmr_pin_polarity_t pinPolarity; /*!< LPTMR pulse input pin polarity; used only in pulse counter mode */
  103. bool enableFreeRunning; /*!< True: enable free running, counter is reset on overflow
  104. False: counter is reset when the compare flag is set */
  105. bool bypassPrescaler; /*!< True: bypass prescaler; false: use clock from prescaler */
  106. lptmr_prescaler_clock_select_t prescalerClockSource; /*!< LPTMR clock source */
  107. lptmr_prescaler_glitch_value_t value; /*!< Prescaler or glitch filter value */
  108. } lptmr_config_t;
  109. /*******************************************************************************
  110. * API
  111. ******************************************************************************/
  112. #if defined(__cplusplus)
  113. extern "C" {
  114. #endif
  115. /*!
  116. * @name Initialization and deinitialization
  117. * @{
  118. */
  119. /*!
  120. * @brief Ungates the LPTMR clock and configures the peripheral for a basic operation.
  121. *
  122. * @note This API should be called at the beginning of the application using the LPTMR driver.
  123. *
  124. * @param base LPTMR peripheral base address
  125. * @param config A pointer to the LPTMR configuration structure.
  126. */
  127. void LPTMR_Init(LPTMR_Type *base, const lptmr_config_t *config);
  128. /*!
  129. * @brief Gates the LPTMR clock.
  130. *
  131. * @param base LPTMR peripheral base address
  132. */
  133. void LPTMR_Deinit(LPTMR_Type *base);
  134. /*!
  135. * @brief Fills in the LPTMR configuration structure with default settings.
  136. *
  137. * The default values are as follows.
  138. * @code
  139. * config->timerMode = kLPTMR_TimerModeTimeCounter;
  140. * config->pinSelect = kLPTMR_PinSelectInput_0;
  141. * config->pinPolarity = kLPTMR_PinPolarityActiveHigh;
  142. * config->enableFreeRunning = false;
  143. * config->bypassPrescaler = true;
  144. * config->prescalerClockSource = kLPTMR_PrescalerClock_1;
  145. * config->value = kLPTMR_Prescale_Glitch_0;
  146. * @endcode
  147. * @param config A pointer to the LPTMR configuration structure.
  148. */
  149. void LPTMR_GetDefaultConfig(lptmr_config_t *config);
  150. /*! @}*/
  151. /*!
  152. * @name Interrupt Interface
  153. * @{
  154. */
  155. /*!
  156. * @brief Enables the selected LPTMR interrupts.
  157. *
  158. * @param base LPTMR peripheral base address
  159. * @param mask The interrupts to enable. This is a logical OR of members of the
  160. * enumeration ::lptmr_interrupt_enable_t
  161. */
  162. static inline void LPTMR_EnableInterrupts(LPTMR_Type *base, uint32_t mask)
  163. {
  164. uint32_t reg = base->CSR;
  165. /* Clear the TCF bit so that we don't clear this w1c bit when writing back */
  166. reg &= ~(LPTMR_CSR_TCF_MASK);
  167. reg |= mask;
  168. base->CSR = reg;
  169. }
  170. /*!
  171. * @brief Disables the selected LPTMR interrupts.
  172. *
  173. * @param base LPTMR peripheral base address
  174. * @param mask The interrupts to disable. This is a logical OR of members of the
  175. * enumeration ::lptmr_interrupt_enable_t.
  176. */
  177. static inline void LPTMR_DisableInterrupts(LPTMR_Type *base, uint32_t mask)
  178. {
  179. uint32_t reg = base->CSR;
  180. /* Clear the TCF bit so that we don't clear this w1c bit when writing back */
  181. reg &= ~(LPTMR_CSR_TCF_MASK);
  182. reg &= ~mask;
  183. base->CSR = reg;
  184. }
  185. /*!
  186. * @brief Gets the enabled LPTMR interrupts.
  187. *
  188. * @param base LPTMR peripheral base address
  189. *
  190. * @return The enabled interrupts. This is the logical OR of members of the
  191. * enumeration ::lptmr_interrupt_enable_t
  192. */
  193. static inline uint32_t LPTMR_GetEnabledInterrupts(LPTMR_Type *base)
  194. {
  195. return (base->CSR & LPTMR_CSR_TIE_MASK);
  196. }
  197. /*! @}*/
  198. #if defined(FSL_FEATURE_LPTMR_HAS_CSR_TDRE) && (FSL_FEATURE_LPTMR_HAS_CSR_TDRE)
  199. /*!
  200. * @brief Enable or disable timer DMA request
  201. *
  202. * @param base base LPTMR peripheral base address
  203. * @param enable Switcher of timer DMA feature. "true" means to enable, "false" means to disable.
  204. */
  205. static inline void LPTMR_EnableTimerDMA(LPTMR_Type *base, bool enable)
  206. {
  207. if (enable)
  208. {
  209. base->CSR |= LPTMR_CSR_TDRE_MASK;
  210. }
  211. else
  212. {
  213. base->CSR &= ~(LPTMR_CSR_TDRE_MASK);
  214. }
  215. }
  216. #endif /* FSL_FEATURE_LPTMR_HAS_CSR_TDRE */
  217. /*!
  218. * @name Status Interface
  219. * @{
  220. */
  221. /*!
  222. * @brief Gets the LPTMR status flags.
  223. *
  224. * @param base LPTMR peripheral base address
  225. *
  226. * @return The status flags. This is the logical OR of members of the
  227. * enumeration ::lptmr_status_flags_t
  228. */
  229. static inline uint32_t LPTMR_GetStatusFlags(LPTMR_Type *base)
  230. {
  231. return (base->CSR & LPTMR_CSR_TCF_MASK);
  232. }
  233. /*!
  234. * @brief Clears the LPTMR status flags.
  235. *
  236. * @param base LPTMR peripheral base address
  237. * @param mask The status flags to clear. This is a logical OR of members of the
  238. * enumeration ::lptmr_status_flags_t.
  239. */
  240. static inline void LPTMR_ClearStatusFlags(LPTMR_Type *base, uint32_t mask)
  241. {
  242. base->CSR |= mask;
  243. }
  244. /*! @}*/
  245. /*!
  246. * @name Read and write the timer period
  247. * @{
  248. */
  249. /*!
  250. * @brief Sets the timer period in units of count.
  251. *
  252. * Timers counts from 0 until it equals the count value set here. The count value is written to
  253. * the CMR register.
  254. *
  255. * @note
  256. * 1. The TCF flag is set with the CNR equals the count provided here and then increments.
  257. * 2. Call the utility macros provided in the fsl_common.h to convert to ticks.
  258. *
  259. * @param base LPTMR peripheral base address
  260. * @param ticks A timer period in units of ticks, which should be equal or greater than 1.
  261. */
  262. static inline void LPTMR_SetTimerPeriod(LPTMR_Type *base, uint32_t ticks)
  263. {
  264. assert(ticks > 0U);
  265. base->CMR = LPTMR_CMR_COMPARE(ticks - 1U);
  266. }
  267. /*!
  268. * @brief Reads the current timer counting value.
  269. *
  270. * This function returns the real-time timer counting value in a range from 0 to a
  271. * timer period.
  272. *
  273. * @note Call the utility macros provided in the fsl_common.h to convert ticks to usec or msec.
  274. *
  275. * @param base LPTMR peripheral base address
  276. *
  277. * @return The current counter value in ticks
  278. */
  279. static inline uint32_t LPTMR_GetCurrentTimerCount(LPTMR_Type *base)
  280. {
  281. /* Must first write any value to the CNR. This synchronizes and registers the current value
  282. * of the CNR into a temporary register which can then be read
  283. */
  284. base->CNR = 0U;
  285. return (uint32_t)((base->CNR & LPTMR_CNR_COUNTER_MASK) >> LPTMR_CNR_COUNTER_SHIFT);
  286. }
  287. /*! @}*/
  288. /*!
  289. * @name Timer Start and Stop
  290. * @{
  291. */
  292. /*!
  293. * @brief Starts the timer.
  294. *
  295. * After calling this function, the timer counts up to the CMR register value.
  296. * Each time the timer reaches the CMR value and then increments, it generates a
  297. * trigger pulse and sets the timeout interrupt flag. An interrupt is also
  298. * triggered if the timer interrupt is enabled.
  299. *
  300. * @param base LPTMR peripheral base address
  301. */
  302. static inline void LPTMR_StartTimer(LPTMR_Type *base)
  303. {
  304. uint32_t reg = base->CSR;
  305. /* Clear the TCF bit to avoid clearing the w1c bit when writing back. */
  306. reg &= ~(LPTMR_CSR_TCF_MASK);
  307. reg |= LPTMR_CSR_TEN_MASK;
  308. base->CSR = reg;
  309. }
  310. /*!
  311. * @brief Stops the timer.
  312. *
  313. * This function stops the timer and resets the timer's counter register.
  314. *
  315. * @param base LPTMR peripheral base address
  316. */
  317. static inline void LPTMR_StopTimer(LPTMR_Type *base)
  318. {
  319. uint32_t reg = base->CSR;
  320. /* Clear the TCF bit to avoid clearing the w1c bit when writing back. */
  321. reg &= ~(LPTMR_CSR_TCF_MASK);
  322. reg &= ~LPTMR_CSR_TEN_MASK;
  323. base->CSR = reg;
  324. }
  325. /*! @}*/
  326. #if defined(__cplusplus)
  327. }
  328. #endif
  329. /*! @}*/
  330. #endif /* _FSL_LPTMR_H_ */