fsl_qtmr.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * Copyright 2017-2020 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef _FSL_QTMR_H_
  8. #define _FSL_QTMR_H_
  9. #include "fsl_common.h"
  10. /*!
  11. * @addtogroup qtmr
  12. * @{
  13. */
  14. /*******************************************************************************
  15. * Definitions
  16. ******************************************************************************/
  17. /*! @name Driver version */
  18. /*@{*/
  19. #define FSL_QTMR_DRIVER_VERSION (MAKE_VERSION(2, 0, 2)) /*!< Version */
  20. /*@}*/
  21. /*! @brief Quad Timer primary clock source selection*/
  22. typedef enum _qtmr_primary_count_source
  23. {
  24. kQTMR_ClockCounter0InputPin = 0, /*!< Use counter 0 input pin */
  25. kQTMR_ClockCounter1InputPin, /*!< Use counter 1 input pin */
  26. kQTMR_ClockCounter2InputPin, /*!< Use counter 2 input pin */
  27. kQTMR_ClockCounter3InputPin, /*!< Use counter 3 input pin */
  28. kQTMR_ClockCounter0Output, /*!< Use counter 0 output */
  29. kQTMR_ClockCounter1Output, /*!< Use counter 1 output */
  30. kQTMR_ClockCounter2Output, /*!< Use counter 2 output */
  31. kQTMR_ClockCounter3Output, /*!< Use counter 3 output */
  32. kQTMR_ClockDivide_1, /*!< IP bus clock divide by 1 prescaler */
  33. kQTMR_ClockDivide_2, /*!< IP bus clock divide by 2 prescaler */
  34. kQTMR_ClockDivide_4, /*!< IP bus clock divide by 4 prescaler */
  35. kQTMR_ClockDivide_8, /*!< IP bus clock divide by 8 prescaler */
  36. kQTMR_ClockDivide_16, /*!< IP bus clock divide by 16 prescaler */
  37. kQTMR_ClockDivide_32, /*!< IP bus clock divide by 32 prescaler */
  38. kQTMR_ClockDivide_64, /*!< IP bus clock divide by 64 prescaler */
  39. kQTMR_ClockDivide_128 /*!< IP bus clock divide by 128 prescaler */
  40. } qtmr_primary_count_source_t;
  41. /*! @brief Quad Timer input sources selection*/
  42. typedef enum _qtmr_input_source
  43. {
  44. kQTMR_Counter0InputPin = 0, /*!< Use counter 0 input pin */
  45. kQTMR_Counter1InputPin, /*!< Use counter 1 input pin */
  46. kQTMR_Counter2InputPin, /*!< Use counter 2 input pin */
  47. kQTMR_Counter3InputPin /*!< Use counter 3 input pin */
  48. } qtmr_input_source_t;
  49. /*! @brief Quad Timer counting mode selection */
  50. typedef enum _qtmr_counting_mode
  51. {
  52. kQTMR_NoOperation = 0, /*!< No operation */
  53. kQTMR_PriSrcRiseEdge, /*!< Count rising edges of primary source */
  54. kQTMR_PriSrcRiseAndFallEdge, /*!< Count rising and falling edges of primary source */
  55. kQTMR_PriSrcRiseEdgeSecInpHigh, /*!< Count rise edges of pri SRC while sec inp high active */
  56. kQTMR_QuadCountMode, /*!< Quadrature count mode, uses pri and sec sources */
  57. kQTMR_PriSrcRiseEdgeSecDir, /*!< Count rising edges of pri SRC; sec SRC specifies dir */
  58. kQTMR_SecSrcTrigPriCnt, /*!< Edge of sec SRC trigger primary count until compare*/
  59. kQTMR_CascadeCount /*!< Cascaded count mode (up/down) */
  60. } qtmr_counting_mode_t;
  61. /*! @brief Quad Timer output mode selection*/
  62. typedef enum _qtmr_output_mode
  63. {
  64. kQTMR_AssertWhenCountActive = 0, /*!< Assert OFLAG while counter is active*/
  65. kQTMR_ClearOnCompare, /*!< Clear OFLAG on successful compare */
  66. kQTMR_SetOnCompare, /*!< Set OFLAG on successful compare */
  67. kQTMR_ToggleOnCompare, /*!< Toggle OFLAG on successful compare */
  68. kQTMR_ToggleOnAltCompareReg, /*!< Toggle OFLAG using alternating compare registers */
  69. kQTMR_SetOnCompareClearOnSecSrcInp, /*!< Set OFLAG on compare, clear on sec SRC input edge */
  70. kQTMR_SetOnCompareClearOnCountRoll, /*!< Set OFLAG on compare, clear on counter rollover */
  71. kQTMR_EnableGateClock /*!< Enable gated clock output while count is active */
  72. } qtmr_output_mode_t;
  73. /*! @brief Quad Timer input capture edge mode, rising edge, or falling edge */
  74. typedef enum _qtmr_input_capture_edge
  75. {
  76. kQTMR_NoCapture = 0, /*!< Capture is disabled */
  77. kQTMR_RisingEdge, /*!< Capture on rising edge (IPS=0) or falling edge (IPS=1)*/
  78. kQTMR_FallingEdge, /*!< Capture on falling edge (IPS=0) or rising edge (IPS=1)*/
  79. kQTMR_RisingAndFallingEdge /*!< Capture on both edges */
  80. } qtmr_input_capture_edge_t;
  81. /*! @brief Quad Timer input capture edge mode, rising edge, or falling edge */
  82. typedef enum _qtmr_preload_control
  83. {
  84. kQTMR_NoPreload = 0, /*!< Never preload */
  85. kQTMR_LoadOnComp1, /*!< Load upon successful compare with value in COMP1 */
  86. kQTMR_LoadOnComp2 /*!< Load upon successful compare with value in COMP2*/
  87. } qtmr_preload_control_t;
  88. /*! @brief List of Quad Timer run options when in Debug mode */
  89. typedef enum _qtmr_debug_action
  90. {
  91. kQTMR_RunNormalInDebug = 0U, /*!< Continue with normal operation */
  92. kQTMR_HaltCounter, /*!< Halt counter */
  93. kQTMR_ForceOutToZero, /*!< Force output to logic 0 */
  94. kQTMR_HaltCountForceOutZero /*!< Halt counter and force output to logic 0 */
  95. } qtmr_debug_action_t;
  96. /*! @brief List of Quad Timer interrupts */
  97. // typedef enum _qtmr_interrupt_enable
  98. typedef enum _qtmr_interrupt_enable
  99. {
  100. kQTMR_CompareInterruptEnable = (1U << 0), /*!< Compare interrupt.*/
  101. kQTMR_Compare1InterruptEnable = (1U << 1), /*!< Compare 1 interrupt.*/
  102. kQTMR_Compare2InterruptEnable = (1U << 2), /*!< Compare 2 interrupt.*/
  103. kQTMR_OverflowInterruptEnable = (1U << 3), /*!< Timer overflow interrupt.*/
  104. kQTMR_EdgeInterruptEnable = (1U << 4) /*!< Input edge interrupt.*/
  105. } qtmr_interrupt_enable_t;
  106. /*! @brief List of Quad Timer flags */
  107. typedef enum _qtmr_status_flags
  108. {
  109. kQTMR_CompareFlag = (1U << 0), /*!< Compare flag */
  110. kQTMR_Compare1Flag = (1U << 1), /*!< Compare 1 flag */
  111. kQTMR_Compare2Flag = (1U << 2), /*!< Compare 2 flag */
  112. kQTMR_OverflowFlag = (1U << 3), /*!< Timer overflow flag */
  113. kQTMR_EdgeFlag = (1U << 4) /*!< Input edge flag */
  114. } qtmr_status_flags_t;
  115. /*! @brief List of channel selection */
  116. typedef enum _qtmr_channel_selection
  117. {
  118. kQTMR_Channel_0 = 0U, /*!< TMR Channel 0 */
  119. kQTMR_Channel_1, /*!< TMR Channel 1 */
  120. kQTMR_Channel_2, /*!< TMR Channel 2 */
  121. kQTMR_Channel_3, /*!< TMR Channel 3 */
  122. } qtmr_channel_selection_t;
  123. /*! @brief List of Quad Timer DMA enable */
  124. typedef enum _qtmr_dma_enable
  125. {
  126. kQTMR_InputEdgeFlagDmaEnable = (1U << 0), /*!< Input Edge Flag DMA Enable.*/
  127. kQTMR_ComparatorPreload1DmaEnable = (1U << 1), /*!< Comparator Preload Register 1 DMA Enable.*/
  128. kQTMR_ComparatorPreload2DmaEnable = (1U << 2), /*!< Comparator Preload Register 2 DMA Enable.*/
  129. } qtmr_dma_enable_t;
  130. /*!
  131. * @brief Quad Timer config structure
  132. *
  133. * This structure holds the configuration settings for the Quad Timer peripheral. To initialize this
  134. * structure to reasonable defaults, call the QTMR_GetDefaultConfig() function and pass a
  135. * pointer to your config structure instance.
  136. *
  137. * The config struct can be made const so it resides in flash
  138. */
  139. typedef struct _qtmr_config
  140. {
  141. qtmr_primary_count_source_t primarySource; /*!< Specify the primary count source */
  142. qtmr_input_source_t secondarySource; /*!< Specify the secondary count source */
  143. bool enableMasterMode; /*!< true: Broadcast compare function output to other counters;
  144. false no broadcast */
  145. bool enableExternalForce; /*!< true: Compare from another counter force state of OFLAG signal
  146. false: OFLAG controlled by local counter */
  147. uint8_t faultFilterCount; /*!< Fault filter count */
  148. uint8_t faultFilterPeriod; /*!< Fault filter period;value of 0 will bypass the filter */
  149. qtmr_debug_action_t debugMode; /*!< Operation in Debug mode */
  150. } qtmr_config_t;
  151. /*******************************************************************************
  152. * API
  153. ******************************************************************************/
  154. #if defined(__cplusplus)
  155. extern "C" {
  156. #endif
  157. /*!
  158. * @name Initialization and deinitialization
  159. * @{
  160. */
  161. /*!
  162. * @brief Ungates the Quad Timer clock and configures the peripheral for basic operation.
  163. *
  164. * @note This API should be called at the beginning of the application using the Quad Timer driver.
  165. *
  166. * @param base Quad Timer peripheral base address
  167. * @param channel Quad Timer channel number
  168. * @param config Pointer to user's Quad Timer config structure
  169. */
  170. void QTMR_Init(TMR_Type *base, qtmr_channel_selection_t channel, const qtmr_config_t *config);
  171. /*!
  172. * @brief Stops the counter and gates the Quad Timer clock
  173. *
  174. * @param base Quad Timer peripheral base address
  175. * @param channel Quad Timer channel number
  176. */
  177. void QTMR_Deinit(TMR_Type *base, qtmr_channel_selection_t channel);
  178. /*!
  179. * @brief Fill in the Quad Timer config struct with the default settings
  180. *
  181. * The default values are:
  182. * @code
  183. * config->debugMode = kQTMR_RunNormalInDebug;
  184. * config->enableExternalForce = false;
  185. * config->enableMasterMode = false;
  186. * config->faultFilterCount = 0;
  187. * config->faultFilterPeriod = 0;
  188. * config->primarySource = kQTMR_ClockDivide_2;
  189. * config->secondarySource = kQTMR_Counter0InputPin;
  190. * @endcode
  191. * @param config Pointer to user's Quad Timer config structure.
  192. */
  193. void QTMR_GetDefaultConfig(qtmr_config_t *config);
  194. /*! @}*/
  195. /*!
  196. * @brief Sets up Quad timer module for PWM signal output.
  197. *
  198. * The function initializes the timer module according to the parameters passed in by the user. The
  199. * function also sets up the value compare registers to match the PWM signal requirements.
  200. *
  201. * @param base Quad Timer peripheral base address
  202. * @param channel Quad Timer channel number
  203. * @param pwmFreqHz PWM signal frequency in Hz
  204. * @param dutyCyclePercent PWM pulse width, value should be between 0 to 100
  205. * 0=inactive signal(0% duty cycle)...
  206. * 100=active signal (100% duty cycle)
  207. * @param outputPolarity true: invert polarity of the output signal, false: no inversion
  208. * @param srcClock_Hz Main counter clock in Hz.
  209. *
  210. * @return Returns an error if there was error setting up the signal.
  211. */
  212. status_t QTMR_SetupPwm(TMR_Type *base,
  213. qtmr_channel_selection_t channel,
  214. uint32_t pwmFreqHz,
  215. uint8_t dutyCyclePercent,
  216. bool outputPolarity,
  217. uint32_t srcClock_Hz);
  218. /*!
  219. * @brief Allows the user to count the source clock cycles until a capture event arrives.
  220. *
  221. * The count is stored in the capture register.
  222. *
  223. * @param base Quad Timer peripheral base address
  224. * @param channel Quad Timer channel number
  225. * @param capturePin Pin through which we receive the input signal to trigger the capture
  226. * @param inputPolarity true: invert polarity of the input signal, false: no inversion
  227. * @param reloadOnCapture true: reload the counter when an input capture occurs, false: no reload
  228. * @param captureMode Specifies which edge of the input signal triggers a capture
  229. */
  230. void QTMR_SetupInputCapture(TMR_Type *base,
  231. qtmr_channel_selection_t channel,
  232. qtmr_input_source_t capturePin,
  233. bool inputPolarity,
  234. bool reloadOnCapture,
  235. qtmr_input_capture_edge_t captureMode);
  236. /*!
  237. * @name Interrupt Interface
  238. * @{
  239. */
  240. /*!
  241. * @brief Enables the selected Quad Timer interrupts
  242. *
  243. * @param base Quad Timer peripheral base address
  244. * @param channel Quad Timer channel number
  245. * @param mask The interrupts to enable. This is a logical OR of members of the
  246. * enumeration ::qtmr_interrupt_enable_t
  247. */
  248. void QTMR_EnableInterrupts(TMR_Type *base, qtmr_channel_selection_t channel, uint32_t mask);
  249. /*!
  250. * @brief Disables the selected Quad Timer interrupts
  251. *
  252. * @param base Quad Timer peripheral base addres
  253. * @param channel Quad Timer channel number
  254. * @param mask The interrupts to enable. This is a logical OR of members of the
  255. * enumeration ::qtmr_interrupt_enable_t
  256. */
  257. void QTMR_DisableInterrupts(TMR_Type *base, qtmr_channel_selection_t channel, uint32_t mask);
  258. /*!
  259. * @brief Gets the enabled Quad Timer interrupts
  260. *
  261. * @param base Quad Timer peripheral base address
  262. * @param channel Quad Timer channel number
  263. *
  264. * @return The enabled interrupts. This is the logical OR of members of the
  265. * enumeration ::qtmr_interrupt_enable_t
  266. */
  267. uint32_t QTMR_GetEnabledInterrupts(TMR_Type *base, qtmr_channel_selection_t channel);
  268. /*! @}*/
  269. /*!
  270. * @name Status Interface
  271. * @{
  272. */
  273. /*!
  274. * @brief Gets the Quad Timer status flags
  275. *
  276. * @param base Quad Timer peripheral base address
  277. * @param channel Quad Timer channel number
  278. *
  279. * @return The status flags. This is the logical OR of members of the
  280. * enumeration ::qtmr_status_flags_t
  281. */
  282. uint32_t QTMR_GetStatus(TMR_Type *base, qtmr_channel_selection_t channel);
  283. /*!
  284. * @brief Clears the Quad Timer status flags.
  285. *
  286. * @param base Quad Timer peripheral base address
  287. * @param channel Quad Timer channel number
  288. * @param mask The status flags to clear. This is a logical OR of members of the
  289. * enumeration ::qtmr_status_flags_t
  290. */
  291. void QTMR_ClearStatusFlags(TMR_Type *base, qtmr_channel_selection_t channel, uint32_t mask);
  292. /*! @}*/
  293. /*!
  294. * @name Read and Write the timer period
  295. * @{
  296. */
  297. /*!
  298. * @brief Sets the timer period in ticks.
  299. *
  300. * Timers counts from initial value till it equals the count value set here. The counter
  301. * will then reinitialize to the value specified in the Load register.
  302. *
  303. * @note
  304. * 1. This function will write the time period in ticks to COMP1 or COMP2 register
  305. * depending on the count direction
  306. * 2. User can call the utility macros provided in fsl_common.h to convert to ticks
  307. * 3. This function supports cases, providing only primary source clock without secondary source clock.
  308. *
  309. * @param base Quad Timer peripheral base address
  310. * @param channel Quad Timer channel number
  311. * @param ticks Timer period in units of ticks
  312. */
  313. void QTMR_SetTimerPeriod(TMR_Type *base, qtmr_channel_selection_t channel, uint16_t ticks);
  314. /*!
  315. * @brief Reads the current timer counting value.
  316. *
  317. * This function returns the real-time timer counting value, in a range from 0 to a
  318. * timer period.
  319. *
  320. * @note User can call the utility macros provided in fsl_common.h to convert ticks to usec or msec
  321. *
  322. * @param base Quad Timer peripheral base address
  323. * @param channel Quad Timer channel number
  324. *
  325. * @return Current counter value in ticks
  326. */
  327. static inline uint16_t QTMR_GetCurrentTimerCount(TMR_Type *base, qtmr_channel_selection_t channel)
  328. {
  329. return base->CHANNEL[channel].CNTR;
  330. }
  331. /*! @}*/
  332. /*!
  333. * @name Timer Start and Stop
  334. * @{
  335. */
  336. /*!
  337. * @brief Starts the Quad Timer counter.
  338. *
  339. *
  340. * @param base Quad Timer peripheral base address
  341. * @param channel Quad Timer channel number
  342. * @param clockSource Quad Timer clock source
  343. */
  344. static inline void QTMR_StartTimer(TMR_Type *base, qtmr_channel_selection_t channel, qtmr_counting_mode_t clockSource)
  345. {
  346. uint16_t reg = base->CHANNEL[channel].CTRL;
  347. reg &= (uint16_t)(~(TMR_CTRL_CM_MASK));
  348. reg |= TMR_CTRL_CM(clockSource);
  349. base->CHANNEL[channel].CTRL = reg;
  350. }
  351. /*!
  352. * @brief Stops the Quad Timer counter.
  353. *
  354. * @param base Quad Timer peripheral base address
  355. * @param channel Quad Timer channel number
  356. */
  357. static inline void QTMR_StopTimer(TMR_Type *base, qtmr_channel_selection_t channel)
  358. {
  359. base->CHANNEL[channel].CTRL &= (uint16_t)(~TMR_CTRL_CM_MASK);
  360. }
  361. /*! @}*/
  362. /*!
  363. * @name Enable and Disable the Quad Timer DMA
  364. * @{
  365. */
  366. /*!
  367. * @brief Enable the Quad Timer DMA.
  368. *
  369. * @param base Quad Timer peripheral base address
  370. * @param channel Quad Timer channel number
  371. * @param mask The DMA to enable. This is a logical OR of members of the
  372. * enumeration ::qtmr_dma_enable_t
  373. */
  374. void QTMR_EnableDma(TMR_Type *base, qtmr_channel_selection_t channel, uint32_t mask);
  375. /*!
  376. * @brief Disable the Quad Timer DMA.
  377. *
  378. * @param base Quad Timer peripheral base address
  379. * @param channel Quad Timer channel number
  380. * @param mask The DMA to enable. This is a logical OR of members of the
  381. * enumeration ::qtmr_dma_enable_t
  382. */
  383. void QTMR_DisableDma(TMR_Type *base, qtmr_channel_selection_t channel, uint32_t mask);
  384. /*! @}*/
  385. #if defined(__cplusplus)
  386. }
  387. #endif
  388. /*! @}*/
  389. #endif /* _FSL_QTMR_H_ */