fsl_pit.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_PIT_H_
  9. #define _FSL_PIT_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup pit
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief PIT Driver Version 2.0.4 */
  21. #define FSL_PIT_DRIVER_VERSION (MAKE_VERSION(2, 0, 4))
  22. /*@}*/
  23. /*!
  24. * @brief List of PIT channels
  25. * @note Actual number of available channels is SoC dependent
  26. */
  27. typedef enum _pit_chnl
  28. {
  29. kPIT_Chnl_0 = 0U, /*!< PIT channel number 0*/
  30. kPIT_Chnl_1, /*!< PIT channel number 1 */
  31. kPIT_Chnl_2, /*!< PIT channel number 2 */
  32. kPIT_Chnl_3, /*!< PIT channel number 3 */
  33. } pit_chnl_t;
  34. /*! @brief List of PIT interrupts */
  35. typedef enum _pit_interrupt_enable
  36. {
  37. kPIT_TimerInterruptEnable = PIT_TCTRL_TIE_MASK, /*!< Timer interrupt enable*/
  38. } pit_interrupt_enable_t;
  39. /*! @brief List of PIT status flags */
  40. typedef enum _pit_status_flags
  41. {
  42. kPIT_TimerFlag = PIT_TFLG_TIF_MASK, /*!< Timer flag */
  43. } pit_status_flags_t;
  44. /*!
  45. * @brief PIT configuration structure
  46. *
  47. * This structure holds the configuration settings for the PIT peripheral. To initialize this
  48. * structure to reasonable defaults, call the PIT_GetDefaultConfig() function and pass a
  49. * pointer to your config structure instance.
  50. *
  51. * The configuration structure can be made constant so it resides in flash.
  52. */
  53. typedef struct _pit_config
  54. {
  55. bool enableRunInDebug; /*!< true: Timers run in debug mode; false: Timers stop in debug mode */
  56. } pit_config_t;
  57. /*******************************************************************************
  58. * API
  59. ******************************************************************************/
  60. #if defined(__cplusplus)
  61. extern "C" {
  62. #endif
  63. /*!
  64. * @name Initialization and deinitialization
  65. * @{
  66. */
  67. /*!
  68. * @brief Ungates the PIT clock, enables the PIT module, and configures the peripheral for basic operations.
  69. *
  70. * @note This API should be called at the beginning of the application using the PIT driver.
  71. *
  72. * @param base PIT peripheral base address
  73. * @param config Pointer to the user's PIT config structure
  74. */
  75. void PIT_Init(PIT_Type *base, const pit_config_t *config);
  76. /*!
  77. * @brief Gates the PIT clock and disables the PIT module.
  78. *
  79. * @param base PIT peripheral base address
  80. */
  81. void PIT_Deinit(PIT_Type *base);
  82. /*!
  83. * @brief Fills in the PIT configuration structure with the default settings.
  84. *
  85. * The default values are as follows.
  86. * @code
  87. * config->enableRunInDebug = false;
  88. * @endcode
  89. * @param config Pointer to the configuration structure.
  90. */
  91. static inline void PIT_GetDefaultConfig(pit_config_t *config)
  92. {
  93. assert(NULL != config);
  94. /* Timers are stopped in Debug mode */
  95. config->enableRunInDebug = false;
  96. }
  97. #if defined(FSL_FEATURE_PIT_HAS_CHAIN_MODE) && FSL_FEATURE_PIT_HAS_CHAIN_MODE
  98. /*!
  99. * @brief Enables or disables chaining a timer with the previous timer.
  100. *
  101. * When a timer has a chain mode enabled, it only counts after the previous
  102. * timer has expired. If the timer n-1 has counted down to 0, counter n
  103. * decrements the value by one. Each timer is 32-bits, which allows the developers
  104. * to chain timers together and form a longer timer (64-bits and larger). The first timer
  105. * (timer 0) can't be chained to any other timer.
  106. *
  107. * @param base PIT peripheral base address
  108. * @param channel Timer channel number which is chained with the previous timer
  109. * @param enable Enable or disable chain.
  110. * true: Current timer is chained with the previous timer.
  111. * false: Timer doesn't chain with other timers.
  112. */
  113. static inline void PIT_SetTimerChainMode(PIT_Type *base, pit_chnl_t channel, bool enable)
  114. {
  115. if (enable)
  116. {
  117. base->CHANNEL[channel].TCTRL |= PIT_TCTRL_CHN_MASK;
  118. }
  119. else
  120. {
  121. base->CHANNEL[channel].TCTRL &= ~PIT_TCTRL_CHN_MASK;
  122. }
  123. }
  124. #endif /* FSL_FEATURE_PIT_HAS_CHAIN_MODE */
  125. /*! @}*/
  126. /*!
  127. * @name Interrupt Interface
  128. * @{
  129. */
  130. /*!
  131. * @brief Enables the selected PIT interrupts.
  132. *
  133. * @param base PIT peripheral base address
  134. * @param channel Timer channel number
  135. * @param mask The interrupts to enable. This is a logical OR of members of the
  136. * enumeration ::pit_interrupt_enable_t
  137. */
  138. static inline void PIT_EnableInterrupts(PIT_Type *base, pit_chnl_t channel, uint32_t mask)
  139. {
  140. base->CHANNEL[channel].TCTRL |= mask;
  141. }
  142. /*!
  143. * @brief Disables the selected PIT interrupts.
  144. *
  145. * @param base PIT peripheral base address
  146. * @param channel Timer channel number
  147. * @param mask The interrupts to disable. This is a logical OR of members of the
  148. * enumeration ::pit_interrupt_enable_t
  149. */
  150. static inline void PIT_DisableInterrupts(PIT_Type *base, pit_chnl_t channel, uint32_t mask)
  151. {
  152. base->CHANNEL[channel].TCTRL &= ~mask;
  153. }
  154. /*!
  155. * @brief Gets the enabled PIT interrupts.
  156. *
  157. * @param base PIT peripheral base address
  158. * @param channel Timer channel number
  159. *
  160. * @return The enabled interrupts. This is the logical OR of members of the
  161. * enumeration ::pit_interrupt_enable_t
  162. */
  163. static inline uint32_t PIT_GetEnabledInterrupts(PIT_Type *base, pit_chnl_t channel)
  164. {
  165. return (base->CHANNEL[channel].TCTRL & PIT_TCTRL_TIE_MASK);
  166. }
  167. /*! @}*/
  168. /*!
  169. * @name Status Interface
  170. * @{
  171. */
  172. /*!
  173. * @brief Gets the PIT status flags.
  174. *
  175. * @param base PIT peripheral base address
  176. * @param channel Timer channel number
  177. *
  178. * @return The status flags. This is the logical OR of members of the
  179. * enumeration ::pit_status_flags_t
  180. */
  181. static inline uint32_t PIT_GetStatusFlags(PIT_Type *base, pit_chnl_t channel)
  182. {
  183. return (base->CHANNEL[channel].TFLG & PIT_TFLG_TIF_MASK);
  184. }
  185. /*!
  186. * @brief Clears the PIT status flags.
  187. *
  188. * @param base PIT peripheral base address
  189. * @param channel Timer channel number
  190. * @param mask The status flags to clear. This is a logical OR of members of the
  191. * enumeration ::pit_status_flags_t
  192. */
  193. static inline void PIT_ClearStatusFlags(PIT_Type *base, pit_chnl_t channel, uint32_t mask)
  194. {
  195. base->CHANNEL[channel].TFLG = mask;
  196. }
  197. /*! @}*/
  198. /*!
  199. * @name Read and Write the timer period
  200. * @{
  201. */
  202. /*!
  203. * @brief Sets the timer period in units of count.
  204. *
  205. * Timers begin counting from the value set by this function until it reaches 0,
  206. * then it generates an interrupt and load this register value again.
  207. * Writing a new value to this register does not restart the timer. Instead, the value
  208. * is loaded after the timer expires.
  209. *
  210. * @note Users can call the utility macros provided in fsl_common.h to convert to ticks.
  211. *
  212. * @param base PIT peripheral base address
  213. * @param channel Timer channel number
  214. * @param count Timer period in units of ticks
  215. */
  216. static inline void PIT_SetTimerPeriod(PIT_Type *base, pit_chnl_t channel, uint32_t count)
  217. {
  218. assert(count != 0U);
  219. /* According to RM, the LDVAL trigger = clock ticks -1 */
  220. base->CHANNEL[channel].LDVAL = count - 1U;
  221. }
  222. /*!
  223. * @brief Reads the current timer counting value.
  224. *
  225. * This function returns the real-time timer counting value, in a range from 0 to a
  226. * timer period.
  227. *
  228. * @note Users can call the utility macros provided in fsl_common.h to convert ticks to usec or msec.
  229. *
  230. * @param base PIT peripheral base address
  231. * @param channel Timer channel number
  232. *
  233. * @return Current timer counting value in ticks
  234. */
  235. static inline uint32_t PIT_GetCurrentTimerCount(PIT_Type *base, pit_chnl_t channel)
  236. {
  237. return base->CHANNEL[channel].CVAL;
  238. }
  239. /*! @}*/
  240. /*!
  241. * @name Timer Start and Stop
  242. * @{
  243. */
  244. /*!
  245. * @brief Starts the timer counting.
  246. *
  247. * After calling this function, timers load period value, count down to 0 and
  248. * then load the respective start value again. Each time a timer reaches 0,
  249. * it generates a trigger pulse and sets the timeout interrupt flag.
  250. *
  251. * @param base PIT peripheral base address
  252. * @param channel Timer channel number.
  253. */
  254. static inline void PIT_StartTimer(PIT_Type *base, pit_chnl_t channel)
  255. {
  256. base->CHANNEL[channel].TCTRL |= PIT_TCTRL_TEN_MASK;
  257. }
  258. /*!
  259. * @brief Stops the timer counting.
  260. *
  261. * This function stops every timer counting. Timers reload their periods
  262. * respectively after the next time they call the PIT_DRV_StartTimer.
  263. *
  264. * @param base PIT peripheral base address
  265. * @param channel Timer channel number.
  266. */
  267. static inline void PIT_StopTimer(PIT_Type *base, pit_chnl_t channel)
  268. {
  269. base->CHANNEL[channel].TCTRL &= ~PIT_TCTRL_TEN_MASK;
  270. }
  271. /*! @}*/
  272. #if defined(FSL_FEATURE_PIT_HAS_LIFETIME_TIMER) && FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
  273. /*!
  274. * @brief Reads the current lifetime counter value.
  275. *
  276. * The lifetime timer is a 64-bit timer which chains timer 0 and timer 1 together.
  277. * Timer 0 and 1 are chained by calling the PIT_SetTimerChainMode before using this timer.
  278. * The period of lifetime timer is equal to the "period of timer 0 * period of timer 1".
  279. * For the 64-bit value, the higher 32-bit has the value of timer 1, and the lower 32-bit
  280. * has the value of timer 0.
  281. *
  282. * @param base PIT peripheral base address
  283. *
  284. * @return Current lifetime timer value
  285. */
  286. uint64_t PIT_GetLifetimeTimerCount(PIT_Type *base);
  287. #endif /* FSL_FEATURE_PIT_HAS_LIFETIME_TIMER */
  288. #if defined(__cplusplus)
  289. }
  290. #endif
  291. /*! @}*/
  292. #endif /* _FSL_PIT_H_ */