fsl_lptmr.h 13 KB

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