fsl_lptmr.h 12 KB

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