fsl_qtmr.h 17 KB

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