fsl_pit.h 11 KB

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