fsl_pit.h 11 KB

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