fsl_qtmr.h 18 KB

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