fsl_tpm.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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_TPM_H_
  35. #define _FSL_TPM_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup tpm
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. #define FSL_TPM_DRIVER_VERSION (MAKE_VERSION(2, 0, 2)) /*!< Version 2.0.2 */
  47. /*@}*/
  48. /*!
  49. * @brief List of TPM channels.
  50. * @note Actual number of available channels is SoC dependent
  51. */
  52. typedef enum _tpm_chnl
  53. {
  54. kTPM_Chnl_0 = 0U, /*!< TPM channel number 0*/
  55. kTPM_Chnl_1, /*!< TPM channel number 1 */
  56. kTPM_Chnl_2, /*!< TPM channel number 2 */
  57. kTPM_Chnl_3, /*!< TPM channel number 3 */
  58. kTPM_Chnl_4, /*!< TPM channel number 4 */
  59. kTPM_Chnl_5, /*!< TPM channel number 5 */
  60. kTPM_Chnl_6, /*!< TPM channel number 6 */
  61. kTPM_Chnl_7 /*!< TPM channel number 7 */
  62. } tpm_chnl_t;
  63. /*! @brief TPM PWM operation modes */
  64. typedef enum _tpm_pwm_mode
  65. {
  66. kTPM_EdgeAlignedPwm = 0U, /*!< Edge aligned PWM */
  67. kTPM_CenterAlignedPwm, /*!< Center aligned PWM */
  68. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  69. kTPM_CombinedPwm /*!< Combined PWM */
  70. #endif
  71. } tpm_pwm_mode_t;
  72. /*! @brief TPM PWM output pulse mode: high-true, low-true or no output */
  73. typedef enum _tpm_pwm_level_select
  74. {
  75. kTPM_NoPwmSignal = 0U, /*!< No PWM output on pin */
  76. kTPM_LowTrue, /*!< Low true pulses */
  77. kTPM_HighTrue /*!< High true pulses */
  78. } tpm_pwm_level_select_t;
  79. /*! @brief Options to configure a TPM channel's PWM signal */
  80. typedef struct _tpm_chnl_pwm_signal_param
  81. {
  82. tpm_chnl_t chnlNumber; /*!< TPM channel to configure.
  83. In combined mode (available in some SoC's, this represents the
  84. channel pair number */
  85. tpm_pwm_level_select_t level; /*!< PWM output active level select */
  86. uint8_t dutyCyclePercent; /*!< PWM pulse width, value should be between 0 to 100
  87. 0=inactive signal(0% duty cycle)...
  88. 100=always active signal (100% duty cycle)*/
  89. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  90. uint8_t firstEdgeDelayPercent; /*!< Used only in combined PWM mode to generate asymmetrical PWM.
  91. Specifies the delay to the first edge in a PWM period.
  92. If unsure, leave as 0; Should be specified as
  93. percentage of the PWM period */
  94. #endif
  95. } tpm_chnl_pwm_signal_param_t;
  96. #if !(defined(FSL_FEATURE_TPM_HAS_NO_CONF) && FSL_FEATURE_TPM_HAS_NO_CONF)
  97. /*!
  98. * @brief Trigger options available.
  99. *
  100. * This is used for both internal & external trigger sources (external option available in certain SoC's)
  101. *
  102. * @note The actual trigger options available is SoC-specific.
  103. */
  104. typedef enum _tpm_trigger_select
  105. {
  106. kTPM_Trigger_Select_0 = 0U,
  107. kTPM_Trigger_Select_1,
  108. kTPM_Trigger_Select_2,
  109. kTPM_Trigger_Select_3,
  110. kTPM_Trigger_Select_4,
  111. kTPM_Trigger_Select_5,
  112. kTPM_Trigger_Select_6,
  113. kTPM_Trigger_Select_7,
  114. kTPM_Trigger_Select_8,
  115. kTPM_Trigger_Select_9,
  116. kTPM_Trigger_Select_10,
  117. kTPM_Trigger_Select_11,
  118. kTPM_Trigger_Select_12,
  119. kTPM_Trigger_Select_13,
  120. kTPM_Trigger_Select_14,
  121. kTPM_Trigger_Select_15
  122. } tpm_trigger_select_t;
  123. #if defined(FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION) && FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION
  124. /*!
  125. * @brief Trigger source options available
  126. *
  127. * @note This selection is available only on some SoC's. For SoC's without this selection, the only
  128. * trigger source available is internal triger.
  129. */
  130. typedef enum _tpm_trigger_source
  131. {
  132. kTPM_TriggerSource_External = 0U, /*!< Use external trigger input */
  133. kTPM_TriggerSource_Internal /*!< Use internal trigger */
  134. } tpm_trigger_source_t;
  135. #endif
  136. #endif
  137. /*! @brief TPM output compare modes */
  138. typedef enum _tpm_output_compare_mode
  139. {
  140. kTPM_NoOutputSignal = (1U << TPM_CnSC_MSA_SHIFT), /*!< No channel output when counter reaches CnV */
  141. kTPM_ToggleOnMatch = ((1U << TPM_CnSC_MSA_SHIFT) | (1U << TPM_CnSC_ELSA_SHIFT)), /*!< Toggle output */
  142. kTPM_ClearOnMatch = ((1U << TPM_CnSC_MSA_SHIFT) | (2U << TPM_CnSC_ELSA_SHIFT)), /*!< Clear output */
  143. kTPM_SetOnMatch = ((1U << TPM_CnSC_MSA_SHIFT) | (3U << TPM_CnSC_ELSA_SHIFT)), /*!< Set output */
  144. kTPM_HighPulseOutput = ((3U << TPM_CnSC_MSA_SHIFT) | (1U << TPM_CnSC_ELSA_SHIFT)), /*!< Pulse output high */
  145. kTPM_LowPulseOutput = ((3U << TPM_CnSC_MSA_SHIFT) | (2U << TPM_CnSC_ELSA_SHIFT)) /*!< Pulse output low */
  146. } tpm_output_compare_mode_t;
  147. /*! @brief TPM input capture edge */
  148. typedef enum _tpm_input_capture_edge
  149. {
  150. kTPM_RisingEdge = (1U << TPM_CnSC_ELSA_SHIFT), /*!< Capture on rising edge only */
  151. kTPM_FallingEdge = (2U << TPM_CnSC_ELSA_SHIFT), /*!< Capture on falling edge only */
  152. kTPM_RiseAndFallEdge = (3U << TPM_CnSC_ELSA_SHIFT) /*!< Capture on rising or falling edge */
  153. } tpm_input_capture_edge_t;
  154. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  155. /*!
  156. * @brief TPM dual edge capture parameters
  157. *
  158. * @note This mode is available only on some SoC's.
  159. */
  160. typedef struct _tpm_dual_edge_capture_param
  161. {
  162. bool enableSwap; /*!< true: Use channel n+1 input, channel n input is ignored;
  163. false: Use channel n input, channel n+1 input is ignored */
  164. tpm_input_capture_edge_t currChanEdgeMode; /*!< Input capture edge select for channel n */
  165. tpm_input_capture_edge_t nextChanEdgeMode; /*!< Input capture edge select for channel n+1 */
  166. } tpm_dual_edge_capture_param_t;
  167. #endif
  168. #if defined(FSL_FEATURE_TPM_HAS_QDCTRL) && FSL_FEATURE_TPM_HAS_QDCTRL
  169. /*!
  170. * @brief TPM quadrature decode modes
  171. *
  172. * @note This mode is available only on some SoC's.
  173. */
  174. typedef enum _tpm_quad_decode_mode
  175. {
  176. kTPM_QuadPhaseEncode = 0U, /*!< Phase A and Phase B encoding mode */
  177. kTPM_QuadCountAndDir /*!< Count and direction encoding mode */
  178. } tpm_quad_decode_mode_t;
  179. /*! @brief TPM quadrature phase polarities */
  180. typedef enum _tpm_phase_polarity
  181. {
  182. kTPM_QuadPhaseNormal = 0U, /*!< Phase input signal is not inverted */
  183. kTPM_QuadPhaseInvert /*!< Phase input signal is inverted */
  184. } tpm_phase_polarity_t;
  185. /*! @brief TPM quadrature decode phase parameters */
  186. typedef struct _tpm_phase_param
  187. {
  188. uint32_t phaseFilterVal; /*!< Filter value, filter is disabled when the value is zero */
  189. tpm_phase_polarity_t phasePolarity; /*!< Phase polarity */
  190. } tpm_phase_params_t;
  191. #endif
  192. /*! @brief TPM clock source selection*/
  193. typedef enum _tpm_clock_source
  194. {
  195. kTPM_SystemClock = 1U, /*!< System clock */
  196. #if defined(FSL_FEATURE_TPM_HAS_SC_CLKS) && FSL_FEATURE_TPM_HAS_SC_CLKS
  197. kTPM_FixedClock, /*!< Fixed frequency clock */
  198. #endif
  199. kTPM_ExternalClock /*!< External clock */
  200. } tpm_clock_source_t;
  201. /*! @brief TPM prescale value selection for the clock source*/
  202. typedef enum _tpm_clock_prescale
  203. {
  204. kTPM_Prescale_Divide_1 = 0U, /*!< Divide by 1 */
  205. kTPM_Prescale_Divide_2, /*!< Divide by 2 */
  206. kTPM_Prescale_Divide_4, /*!< Divide by 4 */
  207. kTPM_Prescale_Divide_8, /*!< Divide by 8 */
  208. kTPM_Prescale_Divide_16, /*!< Divide by 16 */
  209. kTPM_Prescale_Divide_32, /*!< Divide by 32 */
  210. kTPM_Prescale_Divide_64, /*!< Divide by 64 */
  211. kTPM_Prescale_Divide_128 /*!< Divide by 128 */
  212. } tpm_clock_prescale_t;
  213. /*!
  214. * @brief TPM config structure
  215. *
  216. * This structure holds the configuration settings for the TPM peripheral. To initialize this
  217. * structure to reasonable defaults, call the TPM_GetDefaultConfig() function and pass a
  218. * pointer to your config structure instance.
  219. *
  220. * The config struct can be made const so it resides in flash
  221. */
  222. typedef struct _tpm_config
  223. {
  224. tpm_clock_prescale_t prescale; /*!< Select TPM clock prescale value */
  225. #if !(defined(FSL_FEATURE_TPM_HAS_NO_CONF) && FSL_FEATURE_TPM_HAS_NO_CONF)
  226. bool useGlobalTimeBase; /*!< true: Use of an external global time base is enabled;
  227. false: disabled */
  228. tpm_trigger_select_t triggerSelect; /*!< Input trigger to use for controlling the counter operation */
  229. #if defined(FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION) && FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION
  230. tpm_trigger_source_t triggerSource; /*!< Decides if we use external or internal trigger. */
  231. #endif
  232. bool enableDoze; /*!< true: TPM counter is paused in doze mode;
  233. false: TPM counter continues in doze mode */
  234. bool enableDebugMode; /*!< true: TPM counter continues in debug mode;
  235. false: TPM counter is paused in debug mode */
  236. bool enableReloadOnTrigger; /*!< true: TPM counter is reloaded on trigger;
  237. false: TPM counter not reloaded */
  238. bool enableStopOnOverflow; /*!< true: TPM counter stops after overflow;
  239. false: TPM counter continues running after overflow */
  240. bool enableStartOnTrigger; /*!< true: TPM counter only starts when a trigger is detected;
  241. false: TPM counter starts immediately */
  242. #if defined(FSL_FEATURE_TPM_HAS_PAUSE_COUNTER_ON_TRIGGER) && FSL_FEATURE_TPM_HAS_PAUSE_COUNTER_ON_TRIGGER
  243. bool enablePauseOnTrigger; /*!< true: TPM counter will pause while trigger remains asserted;
  244. false: TPM counter continues running */
  245. #endif
  246. #endif
  247. } tpm_config_t;
  248. /*! @brief List of TPM interrupts */
  249. typedef enum _tpm_interrupt_enable
  250. {
  251. kTPM_Chnl0InterruptEnable = (1U << 0), /*!< Channel 0 interrupt.*/
  252. kTPM_Chnl1InterruptEnable = (1U << 1), /*!< Channel 1 interrupt.*/
  253. kTPM_Chnl2InterruptEnable = (1U << 2), /*!< Channel 2 interrupt.*/
  254. kTPM_Chnl3InterruptEnable = (1U << 3), /*!< Channel 3 interrupt.*/
  255. kTPM_Chnl4InterruptEnable = (1U << 4), /*!< Channel 4 interrupt.*/
  256. kTPM_Chnl5InterruptEnable = (1U << 5), /*!< Channel 5 interrupt.*/
  257. kTPM_Chnl6InterruptEnable = (1U << 6), /*!< Channel 6 interrupt.*/
  258. kTPM_Chnl7InterruptEnable = (1U << 7), /*!< Channel 7 interrupt.*/
  259. kTPM_TimeOverflowInterruptEnable = (1U << 8) /*!< Time overflow interrupt.*/
  260. } tpm_interrupt_enable_t;
  261. /*! @brief List of TPM flags */
  262. typedef enum _tpm_status_flags
  263. {
  264. kTPM_Chnl0Flag = (1U << 0), /*!< Channel 0 flag */
  265. kTPM_Chnl1Flag = (1U << 1), /*!< Channel 1 flag */
  266. kTPM_Chnl2Flag = (1U << 2), /*!< Channel 2 flag */
  267. kTPM_Chnl3Flag = (1U << 3), /*!< Channel 3 flag */
  268. kTPM_Chnl4Flag = (1U << 4), /*!< Channel 4 flag */
  269. kTPM_Chnl5Flag = (1U << 5), /*!< Channel 5 flag */
  270. kTPM_Chnl6Flag = (1U << 6), /*!< Channel 6 flag */
  271. kTPM_Chnl7Flag = (1U << 7), /*!< Channel 7 flag */
  272. kTPM_TimeOverflowFlag = (1U << 8) /*!< Time overflow flag */
  273. } tpm_status_flags_t;
  274. /*******************************************************************************
  275. * API
  276. ******************************************************************************/
  277. #if defined(__cplusplus)
  278. extern "C" {
  279. #endif
  280. /*!
  281. * @name Initialization and deinitialization
  282. * @{
  283. */
  284. /*!
  285. * @brief Ungates the TPM clock and configures the peripheral for basic operation.
  286. *
  287. * @note This API should be called at the beginning of the application using the TPM driver.
  288. *
  289. * @param base TPM peripheral base address
  290. * @param config Pointer to user's TPM config structure.
  291. */
  292. void TPM_Init(TPM_Type *base, const tpm_config_t *config);
  293. /*!
  294. * @brief Stops the counter and gates the TPM clock
  295. *
  296. * @param base TPM peripheral base address
  297. */
  298. void TPM_Deinit(TPM_Type *base);
  299. /*!
  300. * @brief Fill in the TPM config struct with the default settings
  301. *
  302. * The default values are:
  303. * @code
  304. * config->prescale = kTPM_Prescale_Divide_1;
  305. * config->useGlobalTimeBase = false;
  306. * config->dozeEnable = false;
  307. * config->dbgMode = false;
  308. * config->enableReloadOnTrigger = false;
  309. * config->enableStopOnOverflow = false;
  310. * config->enableStartOnTrigger = false;
  311. *#if FSL_FEATURE_TPM_HAS_PAUSE_COUNTER_ON_TRIGGER
  312. * config->enablePauseOnTrigger = false;
  313. *#endif
  314. * config->triggerSelect = kTPM_Trigger_Select_0;
  315. *#if FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION
  316. * config->triggerSource = kTPM_TriggerSource_External;
  317. *#endif
  318. * @endcode
  319. * @param config Pointer to user's TPM config structure.
  320. */
  321. void TPM_GetDefaultConfig(tpm_config_t *config);
  322. /*! @}*/
  323. /*!
  324. * @name Channel mode operations
  325. * @{
  326. */
  327. /*!
  328. * @brief Configures the PWM signal parameters
  329. *
  330. * User calls this function to configure the PWM signals period, mode, dutycycle and edge. Use this
  331. * function to configure all the TPM channels that will be used to output a PWM signal
  332. *
  333. * @param base TPM peripheral base address
  334. * @param chnlParams Array of PWM channel parameters to configure the channel(s)
  335. * @param numOfChnls Number of channels to configure, this should be the size of the array passed in
  336. * @param mode PWM operation mode, options available in enumeration ::tpm_pwm_mode_t
  337. * @param pwmFreq_Hz PWM signal frequency in Hz
  338. * @param srcClock_Hz TPM counter clock in Hz
  339. *
  340. * @return kStatus_Success if the PWM setup was successful,
  341. * kStatus_Error on failure
  342. */
  343. status_t TPM_SetupPwm(TPM_Type *base,
  344. const tpm_chnl_pwm_signal_param_t *chnlParams,
  345. uint8_t numOfChnls,
  346. tpm_pwm_mode_t mode,
  347. uint32_t pwmFreq_Hz,
  348. uint32_t srcClock_Hz);
  349. /*!
  350. * @brief Update the duty cycle of an active PWM signal
  351. *
  352. * @param base TPM peripheral base address
  353. * @param chnlNumber The channel number. In combined mode, this represents
  354. * the channel pair number
  355. * @param currentPwmMode The current PWM mode set during PWM setup
  356. * @param dutyCyclePercent New PWM pulse width, value should be between 0 to 100
  357. * 0=inactive signal(0% duty cycle)...
  358. * 100=active signal (100% duty cycle)
  359. */
  360. void TPM_UpdatePwmDutycycle(TPM_Type *base,
  361. tpm_chnl_t chnlNumber,
  362. tpm_pwm_mode_t currentPwmMode,
  363. uint8_t dutyCyclePercent);
  364. /*!
  365. * @brief Update the edge level selection for a channel
  366. *
  367. * @param base TPM peripheral base address
  368. * @param chnlNumber The channel number
  369. * @param level The level to be set to the ELSnB:ELSnA field; valid values are 00, 01, 10, 11.
  370. * See the appropriate SoC reference manual for details about this field.
  371. */
  372. void TPM_UpdateChnlEdgeLevelSelect(TPM_Type *base, tpm_chnl_t chnlNumber, uint8_t level);
  373. /*!
  374. * @brief Enables capturing an input signal on the channel using the function parameters.
  375. *
  376. * When the edge specified in the captureMode argument occurs on the channel, the TPM counter is captured into
  377. * the CnV register. The user has to read the CnV register separately to get this value.
  378. *
  379. * @param base TPM peripheral base address
  380. * @param chnlNumber The channel number
  381. * @param captureMode Specifies which edge to capture
  382. */
  383. void TPM_SetupInputCapture(TPM_Type *base, tpm_chnl_t chnlNumber, tpm_input_capture_edge_t captureMode);
  384. /*!
  385. * @brief Configures the TPM to generate timed pulses.
  386. *
  387. * When the TPM counter matches the value of compareVal argument (this is written into CnV reg), the channel
  388. * output is changed based on what is specified in the compareMode argument.
  389. *
  390. * @param base TPM peripheral base address
  391. * @param chnlNumber The channel number
  392. * @param compareMode Action to take on the channel output when the compare condition is met
  393. * @param compareValue Value to be programmed in the CnV register.
  394. */
  395. void TPM_SetupOutputCompare(TPM_Type *base,
  396. tpm_chnl_t chnlNumber,
  397. tpm_output_compare_mode_t compareMode,
  398. uint32_t compareValue);
  399. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  400. /*!
  401. * @brief Configures the dual edge capture mode of the TPM.
  402. *
  403. * This function allows to measure a pulse width of the signal on the input of channel of a
  404. * channel pair. The filter function is disabled if the filterVal argument passed is zero.
  405. *
  406. * @param base TPM peripheral base address
  407. * @param chnlPairNumber The TPM channel pair number; options are 0, 1, 2, 3
  408. * @param edgeParam Sets up the dual edge capture function
  409. * @param filterValue Filter value, specify 0 to disable filter.
  410. */
  411. void TPM_SetupDualEdgeCapture(TPM_Type *base,
  412. tpm_chnl_t chnlPairNumber,
  413. const tpm_dual_edge_capture_param_t *edgeParam,
  414. uint32_t filterValue);
  415. #endif
  416. #if defined(FSL_FEATURE_TPM_HAS_QDCTRL) && FSL_FEATURE_TPM_HAS_QDCTRL
  417. /*!
  418. * @brief Configures the parameters and activates the quadrature decode mode.
  419. *
  420. * @param base TPM peripheral base address
  421. * @param phaseAParams Phase A configuration parameters
  422. * @param phaseBParams Phase B configuration parameters
  423. * @param quadMode Selects encoding mode used in quadrature decoder mode
  424. */
  425. void TPM_SetupQuadDecode(TPM_Type *base,
  426. const tpm_phase_params_t *phaseAParams,
  427. const tpm_phase_params_t *phaseBParams,
  428. tpm_quad_decode_mode_t quadMode);
  429. #endif
  430. /*! @}*/
  431. /*!
  432. * @name Interrupt Interface
  433. * @{
  434. */
  435. /*!
  436. * @brief Enables the selected TPM interrupts.
  437. *
  438. * @param base TPM peripheral base address
  439. * @param mask The interrupts to enable. This is a logical OR of members of the
  440. * enumeration ::tpm_interrupt_enable_t
  441. */
  442. void TPM_EnableInterrupts(TPM_Type *base, uint32_t mask);
  443. /*!
  444. * @brief Disables the selected TPM interrupts.
  445. *
  446. * @param base TPM peripheral base address
  447. * @param mask The interrupts to disable. This is a logical OR of members of the
  448. * enumeration ::tpm_interrupt_enable_t
  449. */
  450. void TPM_DisableInterrupts(TPM_Type *base, uint32_t mask);
  451. /*!
  452. * @brief Gets the enabled TPM interrupts.
  453. *
  454. * @param base TPM peripheral base address
  455. *
  456. * @return The enabled interrupts. This is the logical OR of members of the
  457. * enumeration ::tpm_interrupt_enable_t
  458. */
  459. uint32_t TPM_GetEnabledInterrupts(TPM_Type *base);
  460. /*! @}*/
  461. /*!
  462. * @name Status Interface
  463. * @{
  464. */
  465. /*!
  466. * @brief Gets the TPM status flags
  467. *
  468. * @param base TPM peripheral base address
  469. *
  470. * @return The status flags. This is the logical OR of members of the
  471. * enumeration ::tpm_status_flags_t
  472. */
  473. static inline uint32_t TPM_GetStatusFlags(TPM_Type *base)
  474. {
  475. uint32_t statusFlags = 0;
  476. #if defined(FSL_FEATURE_TPM_HAS_NO_STATUS) && FSL_FEATURE_TPM_HAS_NO_STATUS
  477. uint8_t chanlNumber = 0;
  478. /* Check the timer flag */
  479. if (base->SC & TPM_SC_TOF_MASK)
  480. {
  481. statusFlags |= kTPM_TimeOverflowFlag;
  482. }
  483. for (chanlNumber = 0; chanlNumber < FSL_FEATURE_TPM_CHANNEL_COUNTn(base); chanlNumber++)
  484. {
  485. /* Check the channel flag */
  486. if (base->CONTROLS[chanlNumber].CnSC & TPM_CnSC_CHF_MASK)
  487. {
  488. statusFlags |= (1U << chanlNumber);
  489. }
  490. }
  491. #else
  492. statusFlags = base->STATUS;
  493. #endif
  494. return statusFlags;
  495. }
  496. /*!
  497. * @brief Clears the TPM status flags
  498. *
  499. * @param base TPM peripheral base address
  500. * @param mask The status flags to clear. This is a logical OR of members of the
  501. * enumeration ::tpm_status_flags_t
  502. */
  503. static inline void TPM_ClearStatusFlags(TPM_Type *base, uint32_t mask)
  504. {
  505. #if defined(FSL_FEATURE_TPM_HAS_NO_STATUS) && FSL_FEATURE_TPM_HAS_NO_STATUS
  506. uint32_t chnlStatusFlags = (mask & 0xFF);
  507. uint8_t chnlNumber = 0;
  508. /* Clear the timer overflow flag by writing a 0 to the bit while it is set */
  509. if (mask & kTPM_TimeOverflowFlag)
  510. {
  511. base->SC &= ~TPM_SC_TOF_MASK;
  512. }
  513. /* Clear the channel flag */
  514. while (chnlStatusFlags)
  515. {
  516. if (chnlStatusFlags & 0x1)
  517. {
  518. base->CONTROLS[chnlNumber].CnSC &= ~TPM_CnSC_CHF_MASK;
  519. }
  520. chnlNumber++;
  521. chnlStatusFlags = chnlStatusFlags >> 1U;
  522. }
  523. #else
  524. /* Clear the status flags */
  525. base->STATUS = mask;
  526. #endif
  527. }
  528. /*! @}*/
  529. /*!
  530. * @name Read and write the timer period
  531. * @{
  532. */
  533. /*!
  534. * @brief Sets the timer period in units of ticks.
  535. *
  536. * Timers counts from 0 until it equals the count value set here. The count value is written to
  537. * the MOD register.
  538. *
  539. * @note
  540. * 1. This API allows the user to use the TPM module as a timer. Do not mix usage
  541. * of this API with TPM's PWM setup API's.
  542. * 2. Call the utility macros provided in the fsl_common.h to convert usec or msec to ticks.
  543. *
  544. * @param base TPM peripheral base address
  545. * @param ticks A timer period in units of ticks, which should be equal or greater than 1.
  546. */
  547. static inline void TPM_SetTimerPeriod(TPM_Type *base, uint32_t ticks)
  548. {
  549. base->MOD = ticks;
  550. }
  551. /*!
  552. * @brief Reads the current timer counting value.
  553. *
  554. * This function returns the real-time timer counting value in a range from 0 to a
  555. * timer period.
  556. *
  557. * @note Call the utility macros provided in the fsl_common.h to convert ticks to usec or msec.
  558. *
  559. * @param base TPM peripheral base address
  560. *
  561. * @return The current counter value in ticks
  562. */
  563. static inline uint32_t TPM_GetCurrentTimerCount(TPM_Type *base)
  564. {
  565. return (uint32_t)((base->CNT & TPM_CNT_COUNT_MASK) >> TPM_CNT_COUNT_SHIFT);
  566. }
  567. /*!
  568. * @name Timer Start and Stop
  569. * @{
  570. */
  571. /*!
  572. * @brief Starts the TPM counter.
  573. *
  574. *
  575. * @param base TPM peripheral base address
  576. * @param clockSource TPM clock source; once clock source is set the counter will start running
  577. */
  578. static inline void TPM_StartTimer(TPM_Type *base, tpm_clock_source_t clockSource)
  579. {
  580. uint32_t reg = base->SC;
  581. #if defined(FSL_FEATURE_TPM_HAS_SC_CLKS) && FSL_FEATURE_TPM_HAS_SC_CLKS
  582. reg &= ~(TPM_SC_CLKS_MASK);
  583. reg |= TPM_SC_CLKS(clockSource);
  584. #else
  585. reg &= ~(TPM_SC_CMOD_MASK);
  586. reg |= TPM_SC_CMOD(clockSource);
  587. #endif
  588. base->SC = reg;
  589. }
  590. /*!
  591. * @brief Stops the TPM counter.
  592. *
  593. * @param base TPM peripheral base address
  594. */
  595. static inline void TPM_StopTimer(TPM_Type *base)
  596. {
  597. #if defined(FSL_FEATURE_TPM_HAS_SC_CLKS) && FSL_FEATURE_TPM_HAS_SC_CLKS
  598. /* Set clock source to none to disable counter */
  599. base->SC &= ~(TPM_SC_CLKS_MASK);
  600. /* Wait till this reads as zero acknowledging the counter is disabled */
  601. while (base->SC & TPM_SC_CLKS_MASK)
  602. {
  603. }
  604. #else
  605. /* Set clock source to none to disable counter */
  606. base->SC &= ~(TPM_SC_CMOD_MASK);
  607. /* Wait till this reads as zero acknowledging the counter is disabled */
  608. while (base->SC & TPM_SC_CMOD_MASK)
  609. {
  610. }
  611. #endif
  612. }
  613. /*! @}*/
  614. #if defined(FSL_FEATURE_TPM_HAS_GLOBAL) && FSL_FEATURE_TPM_HAS_GLOBAL
  615. /*!
  616. * @brief Performs a software reset on the TPM module.
  617. *
  618. * Reset all internal logic and registers, except the Global Register. Remains set until cleared by software.
  619. *
  620. * @note TPM software reset is available on certain SoC's only
  621. *
  622. * @param base TPM peripheral base address
  623. */
  624. static inline void TPM_Reset(TPM_Type *base)
  625. {
  626. base->GLOBAL |= TPM_GLOBAL_RST_MASK;
  627. base->GLOBAL &= ~TPM_GLOBAL_RST_MASK;
  628. }
  629. #endif
  630. #if defined(__cplusplus)
  631. }
  632. #endif
  633. /*! @}*/
  634. #endif /* _FSL_TPM_H_ */