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ů.
 
 
 

333 řádky
9.1 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. #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.2 */
  21. #define FSL_PIT_DRIVER_VERSION (MAKE_VERSION(2, 0, 2))
  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. base->CHANNEL[channel].LDVAL = count;
  219. }
  220. /*!
  221. * @brief Reads the current timer counting value.
  222. *
  223. * This function returns the real-time timer counting value, in a range from 0 to a
  224. * timer period.
  225. *
  226. * @note Users can call the utility macros provided in fsl_common.h to convert ticks to usec or msec.
  227. *
  228. * @param base PIT peripheral base address
  229. * @param channel Timer channel number
  230. *
  231. * @return Current timer counting value in ticks
  232. */
  233. static inline uint32_t PIT_GetCurrentTimerCount(PIT_Type *base, pit_chnl_t channel)
  234. {
  235. return base->CHANNEL[channel].CVAL;
  236. }
  237. /*! @}*/
  238. /*!
  239. * @name Timer Start and Stop
  240. * @{
  241. */
  242. /*!
  243. * @brief Starts the timer counting.
  244. *
  245. * After calling this function, timers load period value, count down to 0 and
  246. * then load the respective start value again. Each time a timer reaches 0,
  247. * it generates a trigger pulse and sets the timeout interrupt flag.
  248. *
  249. * @param base PIT peripheral base address
  250. * @param channel Timer channel number.
  251. */
  252. static inline void PIT_StartTimer(PIT_Type *base, pit_chnl_t channel)
  253. {
  254. base->CHANNEL[channel].TCTRL |= PIT_TCTRL_TEN_MASK;
  255. }
  256. /*!
  257. * @brief Stops the timer counting.
  258. *
  259. * This function stops every timer counting. Timers reload their periods
  260. * respectively after the next time they call the PIT_DRV_StartTimer.
  261. *
  262. * @param base PIT peripheral base address
  263. * @param channel Timer channel number.
  264. */
  265. static inline void PIT_StopTimer(PIT_Type *base, pit_chnl_t channel)
  266. {
  267. base->CHANNEL[channel].TCTRL &= ~PIT_TCTRL_TEN_MASK;
  268. }
  269. /*! @}*/
  270. #if defined(FSL_FEATURE_PIT_HAS_LIFETIME_TIMER) && FSL_FEATURE_PIT_HAS_LIFETIME_TIMER
  271. /*!
  272. * @brief Reads the current lifetime counter value.
  273. *
  274. * The lifetime timer is a 64-bit timer which chains timer 0 and timer 1 together.
  275. * Timer 0 and 1 are chained by calling the PIT_SetTimerChainMode before using this timer.
  276. * The period of lifetime timer is equal to the "period of timer 0 * period of timer 1".
  277. * For the 64-bit value, the higher 32-bit has the value of timer 1, and the lower 32-bit
  278. * has the value of timer 0.
  279. *
  280. * @param base PIT peripheral base address
  281. *
  282. * @return Current lifetime timer value
  283. */
  284. uint64_t PIT_GetLifetimeTimerCount(PIT_Type *base);
  285. #endif /* FSL_FEATURE_PIT_HAS_LIFETIME_TIMER */
  286. #if defined(__cplusplus)
  287. }
  288. #endif
  289. /*! @}*/
  290. #endif /* _FSL_PIT_H_ */