Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

1028 wiersze
38 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_FTM_H_
  9. #define _FSL_FTM_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup ftm
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. ******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief FTM driver version 2.2.3. */
  21. #define FSL_FTM_DRIVER_VERSION (MAKE_VERSION(2, 2, 3))
  22. /*@}*/
  23. /*!
  24. * @brief List of FTM channels
  25. * @note Actual number of available channels is SoC dependent
  26. */
  27. typedef enum _ftm_chnl
  28. {
  29. kFTM_Chnl_0 = 0U, /*!< FTM channel number 0*/
  30. kFTM_Chnl_1, /*!< FTM channel number 1 */
  31. kFTM_Chnl_2, /*!< FTM channel number 2 */
  32. kFTM_Chnl_3, /*!< FTM channel number 3 */
  33. kFTM_Chnl_4, /*!< FTM channel number 4 */
  34. kFTM_Chnl_5, /*!< FTM channel number 5 */
  35. kFTM_Chnl_6, /*!< FTM channel number 6 */
  36. kFTM_Chnl_7 /*!< FTM channel number 7 */
  37. } ftm_chnl_t;
  38. /*! @brief List of FTM faults */
  39. typedef enum _ftm_fault_input
  40. {
  41. kFTM_Fault_0 = 0U, /*!< FTM fault 0 input pin */
  42. kFTM_Fault_1, /*!< FTM fault 1 input pin */
  43. kFTM_Fault_2, /*!< FTM fault 2 input pin */
  44. kFTM_Fault_3 /*!< FTM fault 3 input pin */
  45. } ftm_fault_input_t;
  46. /*! @brief FTM PWM operation modes */
  47. typedef enum _ftm_pwm_mode
  48. {
  49. kFTM_EdgeAlignedPwm = 0U, /*!< Edge-aligned PWM */
  50. kFTM_CenterAlignedPwm, /*!< Center-aligned PWM */
  51. kFTM_CombinedPwm, /*!< Combined PWM */
  52. kFTM_ComplementaryPwm /*!< Complementary PWM */
  53. } ftm_pwm_mode_t;
  54. /*! @brief FTM PWM output pulse mode: high-true, low-true or no output */
  55. typedef enum _ftm_pwm_level_select
  56. {
  57. kFTM_NoPwmSignal = 0U, /*!< No PWM output on pin */
  58. kFTM_LowTrue, /*!< Low true pulses */
  59. kFTM_HighTrue /*!< High true pulses */
  60. } ftm_pwm_level_select_t;
  61. /*! @brief Options to configure a FTM channel's PWM signal */
  62. typedef struct _ftm_chnl_pwm_signal_param
  63. {
  64. ftm_chnl_t chnlNumber; /*!< The channel/channel pair number.
  65. In combined mode, this represents the channel pair number. */
  66. ftm_pwm_level_select_t level; /*!< PWM output active level select. */
  67. uint8_t dutyCyclePercent; /*!< PWM pulse width, value should be between 0 to 100
  68. 0 = inactive signal(0% duty cycle)...
  69. 100 = always active signal (100% duty cycle).*/
  70. uint8_t firstEdgeDelayPercent; /*!< Used only in combined PWM mode to generate an asymmetrical PWM.
  71. Specifies the delay to the first edge in a PWM period.
  72. If unsure leave as 0; Should be specified as a
  73. percentage of the PWM period */
  74. bool enableDeadtime; /*!< true: The deadtime insertion in this pair of channels is enabled;
  75. false: The deadtime insertion in this pair of channels is disabled. */
  76. } ftm_chnl_pwm_signal_param_t;
  77. /*! @brief Options to configure a FTM channel using precise setting.*/
  78. typedef struct _ftm_chnl_pwm_config_param
  79. {
  80. ftm_chnl_t chnlNumber; /*!< The channel/channel pair number.
  81. In combined mode, this represents the channel pair number. */
  82. ftm_pwm_level_select_t level; /*!< PWM output active level select. */
  83. uint16_t dutyValue; /*!< PWM pulse width, the uint of this value is timer ticks. */
  84. uint16_t firstEdgeValue; /*!< Used only in combined PWM mode to generate an asymmetrical PWM.
  85. Specifies the delay to the first edge in a PWM period.
  86. If unsure leave as 0, uint of this value is timer ticks. */
  87. } ftm_chnl_pwm_config_param_t;
  88. /*! @brief FlexTimer output compare mode */
  89. typedef enum _ftm_output_compare_mode
  90. {
  91. kFTM_NoOutputSignal = (1U << FTM_CnSC_MSA_SHIFT), /*!< No channel output when counter reaches CnV */
  92. kFTM_ToggleOnMatch = ((1U << FTM_CnSC_MSA_SHIFT) | (1U << FTM_CnSC_ELSA_SHIFT)), /*!< Toggle output */
  93. kFTM_ClearOnMatch = ((1U << FTM_CnSC_MSA_SHIFT) | (2U << FTM_CnSC_ELSA_SHIFT)), /*!< Clear output */
  94. kFTM_SetOnMatch = ((1U << FTM_CnSC_MSA_SHIFT) | (3U << FTM_CnSC_ELSA_SHIFT)) /*!< Set output */
  95. } ftm_output_compare_mode_t;
  96. /*! @brief FlexTimer input capture edge */
  97. typedef enum _ftm_input_capture_edge
  98. {
  99. kFTM_RisingEdge = (1U << FTM_CnSC_ELSA_SHIFT), /*!< Capture on rising edge only*/
  100. kFTM_FallingEdge = (2U << FTM_CnSC_ELSA_SHIFT), /*!< Capture on falling edge only*/
  101. kFTM_RiseAndFallEdge = (3U << FTM_CnSC_ELSA_SHIFT) /*!< Capture on rising or falling edge */
  102. } ftm_input_capture_edge_t;
  103. /*! @brief FlexTimer dual edge capture modes */
  104. typedef enum _ftm_dual_edge_capture_mode
  105. {
  106. kFTM_OneShot = 0U, /*!< One-shot capture mode */
  107. kFTM_Continuous = (1U << FTM_CnSC_MSA_SHIFT) /*!< Continuous capture mode */
  108. } ftm_dual_edge_capture_mode_t;
  109. /*! @brief FlexTimer dual edge capture parameters */
  110. typedef struct _ftm_dual_edge_capture_param
  111. {
  112. ftm_dual_edge_capture_mode_t mode; /*!< Dual Edge Capture mode */
  113. ftm_input_capture_edge_t currChanEdgeMode; /*!< Input capture edge select for channel n */
  114. ftm_input_capture_edge_t nextChanEdgeMode; /*!< Input capture edge select for channel n+1 */
  115. } ftm_dual_edge_capture_param_t;
  116. /*! @brief FlexTimer quadrature decode modes */
  117. typedef enum _ftm_quad_decode_mode
  118. {
  119. kFTM_QuadPhaseEncode = 0U, /*!< Phase A and Phase B encoding mode */
  120. kFTM_QuadCountAndDir /*!< Count and direction encoding mode */
  121. } ftm_quad_decode_mode_t;
  122. /*! @brief FlexTimer quadrature phase polarities */
  123. typedef enum _ftm_phase_polarity
  124. {
  125. kFTM_QuadPhaseNormal = 0U, /*!< Phase input signal is not inverted */
  126. kFTM_QuadPhaseInvert /*!< Phase input signal is inverted */
  127. } ftm_phase_polarity_t;
  128. /*! @brief FlexTimer quadrature decode phase parameters */
  129. typedef struct _ftm_phase_param
  130. {
  131. bool enablePhaseFilter; /*!< True: enable phase filter; false: disable filter */
  132. uint32_t phaseFilterVal; /*!< Filter value, used only if phase filter is enabled */
  133. ftm_phase_polarity_t phasePolarity; /*!< Phase polarity */
  134. } ftm_phase_params_t;
  135. /*! @brief Structure is used to hold the parameters to configure a FTM fault */
  136. typedef struct _ftm_fault_param
  137. {
  138. bool enableFaultInput; /*!< True: Fault input is enabled; false: Fault input is disabled */
  139. bool faultLevel; /*!< True: Fault polarity is active low; in other words, '0' indicates a fault;
  140. False: Fault polarity is active high */
  141. bool useFaultFilter; /*!< True: Use the filtered fault signal;
  142. False: Use the direct path from fault input */
  143. } ftm_fault_param_t;
  144. /*! @brief FlexTimer pre-scaler factor for the dead time insertion*/
  145. typedef enum _ftm_deadtime_prescale
  146. {
  147. kFTM_Deadtime_Prescale_1 = 1U, /*!< Divide by 1 */
  148. kFTM_Deadtime_Prescale_4, /*!< Divide by 4 */
  149. kFTM_Deadtime_Prescale_16 /*!< Divide by 16 */
  150. } ftm_deadtime_prescale_t;
  151. /*! @brief FlexTimer clock source selection*/
  152. typedef enum _ftm_clock_source
  153. {
  154. kFTM_SystemClock = 1U, /*!< System clock selected */
  155. kFTM_FixedClock, /*!< Fixed frequency clock */
  156. kFTM_ExternalClock /*!< External clock */
  157. } ftm_clock_source_t;
  158. /*! @brief FlexTimer pre-scaler factor selection for the clock source*/
  159. typedef enum _ftm_clock_prescale
  160. {
  161. kFTM_Prescale_Divide_1 = 0U, /*!< Divide by 1 */
  162. kFTM_Prescale_Divide_2, /*!< Divide by 2 */
  163. kFTM_Prescale_Divide_4, /*!< Divide by 4 */
  164. kFTM_Prescale_Divide_8, /*!< Divide by 8 */
  165. kFTM_Prescale_Divide_16, /*!< Divide by 16 */
  166. kFTM_Prescale_Divide_32, /*!< Divide by 32 */
  167. kFTM_Prescale_Divide_64, /*!< Divide by 64 */
  168. kFTM_Prescale_Divide_128 /*!< Divide by 128 */
  169. } ftm_clock_prescale_t;
  170. /*! @brief Options for the FlexTimer behaviour in BDM Mode */
  171. typedef enum _ftm_bdm_mode
  172. {
  173. kFTM_BdmMode_0 = 0U,
  174. /*!< FTM counter stopped, CH(n)F bit can be set, FTM channels in functional mode, writes to MOD,CNTIN and C(n)V
  175. registers bypass the register buffers */
  176. kFTM_BdmMode_1,
  177. /*!< FTM counter stopped, CH(n)F bit is not set, FTM channels outputs are forced to their safe value , writes to
  178. MOD,CNTIN and C(n)V registers bypass the register buffers */
  179. kFTM_BdmMode_2,
  180. /*!< FTM counter stopped, CH(n)F bit is not set, FTM channels outputs are frozen when chip enters in BDM mode,
  181. writes to MOD,CNTIN and C(n)V registers bypass the register buffers */
  182. kFTM_BdmMode_3
  183. /*!< FTM counter in functional mode, CH(n)F bit can be set, FTM channels in functional mode, writes to MOD,CNTIN and
  184. C(n)V registers is in fully functional mode */
  185. } ftm_bdm_mode_t;
  186. /*! @brief Options for the FTM fault control mode */
  187. typedef enum _ftm_fault_mode
  188. {
  189. kFTM_Fault_Disable = 0U, /*!< Fault control is disabled for all channels */
  190. kFTM_Fault_EvenChnls, /*!< Enabled for even channels only(0,2,4,6) with manual fault clearing */
  191. kFTM_Fault_AllChnlsMan, /*!< Enabled for all channels with manual fault clearing */
  192. kFTM_Fault_AllChnlsAuto /*!< Enabled for all channels with automatic fault clearing */
  193. } ftm_fault_mode_t;
  194. /*!
  195. * @brief FTM external trigger options
  196. * @note Actual available external trigger sources are SoC-specific
  197. */
  198. typedef enum _ftm_external_trigger
  199. {
  200. kFTM_Chnl0Trigger = (1U << 4), /*!< Generate trigger when counter equals chnl 0 CnV reg */
  201. kFTM_Chnl1Trigger = (1U << 5), /*!< Generate trigger when counter equals chnl 1 CnV reg */
  202. kFTM_Chnl2Trigger = (1U << 0), /*!< Generate trigger when counter equals chnl 2 CnV reg */
  203. kFTM_Chnl3Trigger = (1U << 1), /*!< Generate trigger when counter equals chnl 3 CnV reg */
  204. kFTM_Chnl4Trigger = (1U << 2), /*!< Generate trigger when counter equals chnl 4 CnV reg */
  205. kFTM_Chnl5Trigger = (1U << 3), /*!< Generate trigger when counter equals chnl 5 CnV reg */
  206. #if defined(FSL_FEATURE_FTM_HAS_CHANNEL6_TRIGGER) && (FSL_FEATURE_FTM_HAS_CHANNEL6_TRIGGER)
  207. kFTM_Chnl6Trigger =
  208. (1U << 8), /*!< Available on certain SoC's, generate trigger when counter equals chnl 6 CnV reg */
  209. #endif
  210. #if defined(FSL_FEATURE_FTM_HAS_CHANNEL7_TRIGGER) && (FSL_FEATURE_FTM_HAS_CHANNEL7_TRIGGER)
  211. kFTM_Chnl7Trigger =
  212. (1U << 9), /*!< Available on certain SoC's, generate trigger when counter equals chnl 7 CnV reg */
  213. #endif
  214. kFTM_InitTrigger = (1U << 6), /*!< Generate Trigger when counter is updated with CNTIN */
  215. #if defined(FSL_FEATURE_FTM_HAS_RELOAD_INITIALIZATION_TRIGGER) && (FSL_FEATURE_FTM_HAS_RELOAD_INITIALIZATION_TRIGGER)
  216. kFTM_ReloadInitTrigger = (1U << 7) /*!< Available on certain SoC's, trigger on reload point */
  217. #endif
  218. } ftm_external_trigger_t;
  219. /*! @brief FlexTimer PWM sync options to update registers with buffer */
  220. typedef enum _ftm_pwm_sync_method
  221. {
  222. kFTM_SoftwareTrigger = FTM_SYNC_SWSYNC_MASK, /*!< Software triggers PWM sync */
  223. kFTM_HardwareTrigger_0 = FTM_SYNC_TRIG0_MASK, /*!< Hardware trigger 0 causes PWM sync */
  224. kFTM_HardwareTrigger_1 = FTM_SYNC_TRIG1_MASK, /*!< Hardware trigger 1 causes PWM sync */
  225. kFTM_HardwareTrigger_2 = FTM_SYNC_TRIG2_MASK /*!< Hardware trigger 2 causes PWM sync */
  226. } ftm_pwm_sync_method_t;
  227. /*!
  228. * @brief FTM options available as loading point for register reload
  229. * @note Actual available reload points are SoC-specific
  230. */
  231. typedef enum _ftm_reload_point
  232. {
  233. kFTM_Chnl0Match = (1U << 0), /*!< Channel 0 match included as a reload point */
  234. kFTM_Chnl1Match = (1U << 1), /*!< Channel 1 match included as a reload point */
  235. kFTM_Chnl2Match = (1U << 2), /*!< Channel 2 match included as a reload point */
  236. kFTM_Chnl3Match = (1U << 3), /*!< Channel 3 match included as a reload point */
  237. kFTM_Chnl4Match = (1U << 4), /*!< Channel 4 match included as a reload point */
  238. kFTM_Chnl5Match = (1U << 5), /*!< Channel 5 match included as a reload point */
  239. kFTM_Chnl6Match = (1U << 6), /*!< Channel 6 match included as a reload point */
  240. kFTM_Chnl7Match = (1U << 7), /*!< Channel 7 match included as a reload point */
  241. kFTM_CntMax = (1U << 8), /*!< Use in up-down count mode only, reload when counter reaches the maximum value */
  242. kFTM_CntMin = (1U << 9), /*!< Use in up-down count mode only, reload when counter reaches the minimum value */
  243. kFTM_HalfCycMatch = (1U << 10) /*!< Available on certain SoC's, half cycle match reload point */
  244. } ftm_reload_point_t;
  245. /*!
  246. * @brief List of FTM interrupts
  247. * @note Actual available interrupts are SoC-specific
  248. */
  249. typedef enum _ftm_interrupt_enable
  250. {
  251. kFTM_Chnl0InterruptEnable = (1U << 0), /*!< Channel 0 interrupt */
  252. kFTM_Chnl1InterruptEnable = (1U << 1), /*!< Channel 1 interrupt */
  253. kFTM_Chnl2InterruptEnable = (1U << 2), /*!< Channel 2 interrupt */
  254. kFTM_Chnl3InterruptEnable = (1U << 3), /*!< Channel 3 interrupt */
  255. kFTM_Chnl4InterruptEnable = (1U << 4), /*!< Channel 4 interrupt */
  256. kFTM_Chnl5InterruptEnable = (1U << 5), /*!< Channel 5 interrupt */
  257. kFTM_Chnl6InterruptEnable = (1U << 6), /*!< Channel 6 interrupt */
  258. kFTM_Chnl7InterruptEnable = (1U << 7), /*!< Channel 7 interrupt */
  259. kFTM_FaultInterruptEnable = (1U << 8), /*!< Fault interrupt */
  260. kFTM_TimeOverflowInterruptEnable = (1U << 9), /*!< Time overflow interrupt */
  261. kFTM_ReloadInterruptEnable = (1U << 10) /*!< Reload interrupt; Available only on certain SoC's */
  262. } ftm_interrupt_enable_t;
  263. /*!
  264. * @brief List of FTM flags
  265. * @note Actual available flags are SoC-specific
  266. */
  267. typedef enum _ftm_status_flags
  268. {
  269. kFTM_Chnl0Flag = (1U << 0), /*!< Channel 0 Flag */
  270. kFTM_Chnl1Flag = (1U << 1), /*!< Channel 1 Flag */
  271. kFTM_Chnl2Flag = (1U << 2), /*!< Channel 2 Flag */
  272. kFTM_Chnl3Flag = (1U << 3), /*!< Channel 3 Flag */
  273. kFTM_Chnl4Flag = (1U << 4), /*!< Channel 4 Flag */
  274. kFTM_Chnl5Flag = (1U << 5), /*!< Channel 5 Flag */
  275. kFTM_Chnl6Flag = (1U << 6), /*!< Channel 6 Flag */
  276. kFTM_Chnl7Flag = (1U << 7), /*!< Channel 7 Flag */
  277. kFTM_FaultFlag = (1U << 8), /*!< Fault Flag */
  278. kFTM_TimeOverflowFlag = (1U << 9), /*!< Time overflow Flag */
  279. kFTM_ChnlTriggerFlag = (1U << 10), /*!< Channel trigger Flag */
  280. kFTM_ReloadFlag = (1U << 11) /*!< Reload Flag; Available only on certain SoC's */
  281. } ftm_status_flags_t;
  282. #if !(defined(FSL_FEATURE_FTM_HAS_NO_QDCTRL) && FSL_FEATURE_FTM_HAS_NO_QDCTRL)
  283. /*!
  284. * @brief List of FTM Quad Decoder flags.
  285. */
  286. enum
  287. {
  288. kFTM_QuadDecoderCountingIncreaseFlag = FTM_QDCTRL_QUADIR_MASK, /*!< Counting direction is increasing (FTM counter
  289. increment), or the direction is decreasing. */
  290. kFTM_QuadDecoderCountingOverflowOnTopFlag = FTM_QDCTRL_TOFDIR_MASK, /*!< Indicates if the TOF bit was set on the top
  291. or the bottom of counting. */
  292. };
  293. #endif
  294. /*!
  295. * @brief FTM configuration structure
  296. *
  297. * This structure holds the configuration settings for the FTM peripheral. To initialize this
  298. * structure to reasonable defaults, call the FTM_GetDefaultConfig() function and pass a
  299. * pointer to the configuration structure instance.
  300. *
  301. * The configuration structure can be made constant so as to reside in flash.
  302. */
  303. typedef struct _ftm_config
  304. {
  305. ftm_clock_prescale_t prescale; /*!< FTM clock prescale value */
  306. ftm_bdm_mode_t bdmMode; /*!< FTM behavior in BDM mode */
  307. uint32_t pwmSyncMode; /*!< Synchronization methods to use to update buffered registers; Multiple
  308. update modes can be used by providing an OR'ed list of options
  309. available in enumeration ::ftm_pwm_sync_method_t. */
  310. uint32_t reloadPoints; /*!< FTM reload points; When using this, the PWM
  311. synchronization is not required. Multiple reload points can be used by providing
  312. an OR'ed list of options available in
  313. enumeration ::ftm_reload_point_t. */
  314. ftm_fault_mode_t faultMode; /*!< FTM fault control mode */
  315. uint8_t faultFilterValue; /*!< Fault input filter value */
  316. ftm_deadtime_prescale_t deadTimePrescale; /*!< The dead time prescalar value */
  317. uint32_t deadTimeValue; /*!< The dead time value
  318. deadTimeValue's available range is 0-1023 when register has DTVALEX,
  319. otherwise its available range is 0-63. */
  320. uint32_t extTriggers; /*!< External triggers to enable. Multiple trigger sources can be
  321. enabled by providing an OR'ed list of options available in
  322. enumeration ::ftm_external_trigger_t. */
  323. uint8_t chnlInitState; /*!< Defines the initialization value of the channels in OUTINT register */
  324. uint8_t chnlPolarity; /*!< Defines the output polarity of the channels in POL register */
  325. bool useGlobalTimeBase; /*!< True: Use of an external global time base is enabled;
  326. False: disabled */
  327. } ftm_config_t;
  328. /*******************************************************************************
  329. * API
  330. ******************************************************************************/
  331. #if defined(__cplusplus)
  332. extern "C" {
  333. #endif
  334. /*!
  335. * @name Initialization and deinitialization
  336. * @{
  337. */
  338. /*!
  339. * @brief Ungates the FTM clock and configures the peripheral for basic operation.
  340. *
  341. * @note This API should be called at the beginning of the application which is using the FTM driver.
  342. * If the FTM instance has only TPM features, please use the TPM driver.
  343. *
  344. * @param base FTM peripheral base address
  345. * @param config Pointer to the user configuration structure.
  346. *
  347. * @return kStatus_Success indicates success; Else indicates failure.
  348. */
  349. status_t FTM_Init(FTM_Type *base, const ftm_config_t *config);
  350. /*!
  351. * @brief Gates the FTM clock.
  352. *
  353. * @param base FTM peripheral base address
  354. */
  355. void FTM_Deinit(FTM_Type *base);
  356. /*!
  357. * @brief Fills in the FTM configuration structure with the default settings.
  358. *
  359. * The default values are:
  360. * @code
  361. * config->prescale = kFTM_Prescale_Divide_1;
  362. * config->bdmMode = kFTM_BdmMode_0;
  363. * config->pwmSyncMode = kFTM_SoftwareTrigger;
  364. * config->reloadPoints = 0;
  365. * config->faultMode = kFTM_Fault_Disable;
  366. * config->faultFilterValue = 0;
  367. * config->deadTimePrescale = kFTM_Deadtime_Prescale_1;
  368. * config->deadTimeValue = 0;
  369. * config->extTriggers = 0;
  370. * config->chnlInitState = 0;
  371. * config->chnlPolarity = 0;
  372. * config->useGlobalTimeBase = false;
  373. * @endcode
  374. * @param config Pointer to the user configuration structure.
  375. */
  376. void FTM_GetDefaultConfig(ftm_config_t *config);
  377. /*! @}*/
  378. /*!
  379. * @name Channel mode operations
  380. * @{
  381. */
  382. /*!
  383. * @brief Configures the PWM signal parameters.
  384. *
  385. * Call this function to configure the PWM signal period, mode, duty cycle, and edge. Use this
  386. * function to configure all FTM channels that are used to output a PWM signal.
  387. *
  388. * @param base FTM peripheral base address
  389. * @param chnlParams Array of PWM channel parameters to configure the channel(s)
  390. * @param numOfChnls Number of channels to configure; This should be the size of the array passed in
  391. * @param mode PWM operation mode, options available in enumeration ::ftm_pwm_mode_t
  392. * @param pwmFreq_Hz PWM signal frequency in Hz
  393. * @param srcClock_Hz FTM counter clock in Hz
  394. *
  395. * @return kStatus_Success if the PWM setup was successful
  396. * kStatus_Error on failure
  397. */
  398. status_t FTM_SetupPwm(FTM_Type *base,
  399. const ftm_chnl_pwm_signal_param_t *chnlParams,
  400. uint8_t numOfChnls,
  401. ftm_pwm_mode_t mode,
  402. uint32_t pwmFreq_Hz,
  403. uint32_t srcClock_Hz);
  404. /*!
  405. * @brief Updates the duty cycle of an active PWM signal.
  406. *
  407. * @param base FTM peripheral base address
  408. * @param chnlNumber The channel/channel pair number. In combined mode, this represents
  409. * the channel pair number
  410. * @param currentPwmMode The current PWM mode set during PWM setup
  411. * @param dutyCyclePercent New PWM pulse width; The value should be between 0 to 100
  412. * 0=inactive signal(0% duty cycle)...
  413. * 100=active signal (100% duty cycle)
  414. */
  415. void FTM_UpdatePwmDutycycle(FTM_Type *base,
  416. ftm_chnl_t chnlNumber,
  417. ftm_pwm_mode_t currentPwmMode,
  418. uint8_t dutyCyclePercent);
  419. /*!
  420. * @brief Updates the edge level selection for a channel.
  421. *
  422. * @param base FTM peripheral base address
  423. * @param chnlNumber The channel number
  424. * @param level The level to be set to the ELSnB:ELSnA field; Valid values are 00, 01, 10, 11.
  425. * See the Kinetis SoC reference manual for details about this field.
  426. */
  427. void FTM_UpdateChnlEdgeLevelSelect(FTM_Type *base, ftm_chnl_t chnlNumber, uint8_t level);
  428. /*!
  429. * @brief Configures the PWM mode parameters.
  430. *
  431. * Call this function to configure the PWM signal mode, duty cycle in ticks, and edge. Use this
  432. * function to configure all FTM channels that are used to output a PWM signal.
  433. * Please note that: This API is similar with FTM_SetupPwm() API, but will not set the timer period,
  434. * and this API will set channel match value in timer ticks, not period percent.
  435. *
  436. * @param base FTM peripheral base address
  437. * @param chnlParams Array of PWM channel parameters to configure the channel(s)
  438. * @param numOfChnls Number of channels to configure; This should be the size of the array passed in
  439. * @param mode PWM operation mode, options available in enumeration ::ftm_pwm_mode_t
  440. *
  441. * @return kStatus_Success if the PWM setup was successful
  442. * kStatus_Error on failure
  443. */
  444. status_t FTM_SetupPwmMode(FTM_Type *base,
  445. const ftm_chnl_pwm_config_param_t *chnlParams,
  446. uint8_t numOfChnls,
  447. ftm_pwm_mode_t mode);
  448. /*!
  449. * @brief Enables capturing an input signal on the channel using the function parameters.
  450. *
  451. * When the edge specified in the captureMode argument occurs on the channel, the FTM counter is
  452. * captured into the CnV register. The user has to read the CnV register separately to get this
  453. * value. The filter function is disabled if the filterVal argument passed in is 0. The filter
  454. * function is available only for channels 0, 1, 2, 3.
  455. *
  456. * @param base FTM peripheral base address
  457. * @param chnlNumber The channel number
  458. * @param captureMode Specifies which edge to capture
  459. * @param filterValue Filter value, specify 0 to disable filter. Available only for channels 0-3.
  460. */
  461. void FTM_SetupInputCapture(FTM_Type *base,
  462. ftm_chnl_t chnlNumber,
  463. ftm_input_capture_edge_t captureMode,
  464. uint32_t filterValue);
  465. /*!
  466. * @brief Configures the FTM to generate timed pulses.
  467. *
  468. * When the FTM counter matches the value of compareVal argument (this is written into CnV reg),
  469. * the channel output is changed based on what is specified in the compareMode argument.
  470. *
  471. * @param base FTM peripheral base address
  472. * @param chnlNumber The channel number
  473. * @param compareMode Action to take on the channel output when the compare condition is met
  474. * @param compareValue Value to be programmed in the CnV register.
  475. */
  476. void FTM_SetupOutputCompare(FTM_Type *base,
  477. ftm_chnl_t chnlNumber,
  478. ftm_output_compare_mode_t compareMode,
  479. uint32_t compareValue);
  480. /*!
  481. * @brief Configures the dual edge capture mode of the FTM.
  482. *
  483. * This function sets up the dual edge capture mode on a channel pair. The capture edge for the
  484. * channel pair and the capture mode (one-shot or continuous) is specified in the parameter
  485. * argument. The filter function is disabled if the filterVal argument passed is zero. The filter
  486. * function is available only on channels 0 and 2. The user has to read the channel CnV registers
  487. * separately to get the capture values.
  488. *
  489. * @param base FTM peripheral base address
  490. * @param chnlPairNumber The FTM channel pair number; options are 0, 1, 2, 3
  491. * @param edgeParam Sets up the dual edge capture function
  492. * @param filterValue Filter value, specify 0 to disable filter. Available only for channel pair 0 and 1.
  493. */
  494. void FTM_SetupDualEdgeCapture(FTM_Type *base,
  495. ftm_chnl_t chnlPairNumber,
  496. const ftm_dual_edge_capture_param_t *edgeParam,
  497. uint32_t filterValue);
  498. /*! @}*/
  499. /*!
  500. * @brief Sets up the working of the FTM fault protection.
  501. *
  502. * FTM can have up to 4 fault inputs. This function sets up fault parameters, fault level, and a filter.
  503. *
  504. * @param base FTM peripheral base address
  505. * @param faultNumber FTM fault to configure.
  506. * @param faultParams Parameters passed in to set up the fault
  507. */
  508. void FTM_SetupFault(FTM_Type *base, ftm_fault_input_t faultNumber, const ftm_fault_param_t *faultParams);
  509. /*!
  510. * @name Interrupt Interface
  511. * @{
  512. */
  513. /*!
  514. * @brief Enables the selected FTM interrupts.
  515. *
  516. * @param base FTM peripheral base address
  517. * @param mask The interrupts to enable. This is a logical OR of members of the
  518. * enumeration ::ftm_interrupt_enable_t
  519. */
  520. void FTM_EnableInterrupts(FTM_Type *base, uint32_t mask);
  521. /*!
  522. * @brief Disables the selected FTM interrupts.
  523. *
  524. * @param base FTM peripheral base address
  525. * @param mask The interrupts to enable. This is a logical OR of members of the
  526. * enumeration ::ftm_interrupt_enable_t
  527. */
  528. void FTM_DisableInterrupts(FTM_Type *base, uint32_t mask);
  529. /*!
  530. * @brief Gets the enabled FTM interrupts.
  531. *
  532. * @param base FTM peripheral base address
  533. *
  534. * @return The enabled interrupts. This is the logical OR of members of the
  535. * enumeration ::ftm_interrupt_enable_t
  536. */
  537. uint32_t FTM_GetEnabledInterrupts(FTM_Type *base);
  538. /*! @}*/
  539. /*!
  540. * @name Status Interface
  541. * @{
  542. */
  543. /*!
  544. * @brief Gets the FTM status flags.
  545. *
  546. * @param base FTM peripheral base address
  547. *
  548. * @return The status flags. This is the logical OR of members of the
  549. * enumeration ::ftm_status_flags_t
  550. */
  551. uint32_t FTM_GetStatusFlags(FTM_Type *base);
  552. /*!
  553. * @brief Clears the FTM status flags.
  554. *
  555. * @param base FTM peripheral base address
  556. * @param mask The status flags to clear. This is a logical OR of members of the
  557. * enumeration ::ftm_status_flags_t
  558. */
  559. void FTM_ClearStatusFlags(FTM_Type *base, uint32_t mask);
  560. /*! @}*/
  561. /*!
  562. * @name Read and write the timer period
  563. * @{
  564. */
  565. /*!
  566. * @brief Sets the timer period in units of ticks.
  567. *
  568. * Timers counts from 0 until it equals the count value set here. The count value is written to
  569. * the MOD register.
  570. *
  571. * @note
  572. * 1. This API allows the user to use the FTM module as a timer. Do not mix usage
  573. * of this API with FTM's PWM setup API's.
  574. * 2. Call the utility macros provided in the fsl_common.h to convert usec or msec to ticks.
  575. *
  576. * @param base FTM peripheral base address
  577. * @param ticks A timer period in units of ticks, which should be equal or greater than 1.
  578. */
  579. static inline void FTM_SetTimerPeriod(FTM_Type *base, uint32_t ticks)
  580. {
  581. base->MOD = ticks;
  582. }
  583. /*!
  584. * @brief Reads the current timer counting value.
  585. *
  586. * This function returns the real-time timer counting value in a range from 0 to a
  587. * timer period.
  588. *
  589. * @note Call the utility macros provided in the fsl_common.h to convert ticks to usec or msec.
  590. *
  591. * @param base FTM peripheral base address
  592. *
  593. * @return The current counter value in ticks
  594. */
  595. static inline uint32_t FTM_GetCurrentTimerCount(FTM_Type *base)
  596. {
  597. return (uint32_t)((base->CNT & FTM_CNT_COUNT_MASK) >> FTM_CNT_COUNT_SHIFT);
  598. }
  599. /*! @}*/
  600. /*!
  601. * @name Timer Start and Stop
  602. * @{
  603. */
  604. /*!
  605. * @brief Starts the FTM counter.
  606. *
  607. * @param base FTM peripheral base address
  608. * @param clockSource FTM clock source; After the clock source is set, the counter starts running.
  609. */
  610. static inline void FTM_StartTimer(FTM_Type *base, ftm_clock_source_t clockSource)
  611. {
  612. uint32_t reg = base->SC;
  613. reg &= ~(FTM_SC_CLKS_MASK);
  614. reg |= FTM_SC_CLKS(clockSource);
  615. base->SC = reg;
  616. }
  617. /*!
  618. * @brief Stops the FTM counter.
  619. *
  620. * @param base FTM peripheral base address
  621. */
  622. static inline void FTM_StopTimer(FTM_Type *base)
  623. {
  624. /* Set clock source to none to disable counter */
  625. base->SC &= ~(FTM_SC_CLKS_MASK);
  626. }
  627. /*! @}*/
  628. /*!
  629. * @name Software output control
  630. * @{
  631. */
  632. /*!
  633. * @brief Enables or disables the channel software output control.
  634. *
  635. * @param base FTM peripheral base address
  636. * @param chnlNumber Channel to be enabled or disabled
  637. * @param value true: channel output is affected by software output control
  638. false: channel output is unaffected by software output control
  639. */
  640. static inline void FTM_SetSoftwareCtrlEnable(FTM_Type *base, ftm_chnl_t chnlNumber, bool value)
  641. {
  642. if (value)
  643. {
  644. base->SWOCTRL |= (1UL << (uint32_t)chnlNumber);
  645. }
  646. else
  647. {
  648. base->SWOCTRL &= ~(1UL << (uint32_t)chnlNumber);
  649. }
  650. }
  651. /*!
  652. * @brief Sets the channel software output control value.
  653. *
  654. * @param base FTM peripheral base address.
  655. * @param chnlNumber Channel to be configured
  656. * @param value true to set 1, false to set 0
  657. */
  658. static inline void FTM_SetSoftwareCtrlVal(FTM_Type *base, ftm_chnl_t chnlNumber, bool value)
  659. {
  660. if (value)
  661. {
  662. base->SWOCTRL |= (1UL << ((uint32_t)chnlNumber + FTM_SWOCTRL_CH0OCV_SHIFT));
  663. }
  664. else
  665. {
  666. base->SWOCTRL &= ~(1UL << ((uint32_t)chnlNumber + FTM_SWOCTRL_CH0OCV_SHIFT));
  667. }
  668. }
  669. /*! @}*/
  670. /*!
  671. * @brief Enables or disables the FTM global time base signal generation to other FTMs.
  672. *
  673. * @param base FTM peripheral base address
  674. * @param enable true to enable, false to disable
  675. */
  676. static inline void FTM_SetGlobalTimeBaseOutputEnable(FTM_Type *base, bool enable)
  677. {
  678. if (enable)
  679. {
  680. base->CONF |= FTM_CONF_GTBEOUT_MASK;
  681. }
  682. else
  683. {
  684. base->CONF &= ~FTM_CONF_GTBEOUT_MASK;
  685. }
  686. }
  687. /*!
  688. * @brief Sets the FTM peripheral timer channel output mask.
  689. *
  690. * @param base FTM peripheral base address
  691. * @param chnlNumber Channel to be configured
  692. * @param mask true: masked, channel is forced to its inactive state; false: unmasked
  693. */
  694. static inline void FTM_SetOutputMask(FTM_Type *base, ftm_chnl_t chnlNumber, bool mask)
  695. {
  696. if (mask)
  697. {
  698. base->OUTMASK |= (1UL << (uint32_t)chnlNumber);
  699. }
  700. else
  701. {
  702. base->OUTMASK &= ~(1UL << (uint32_t)chnlNumber);
  703. }
  704. }
  705. #if defined(FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT) && (FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT)
  706. /*!
  707. * @brief Allows users to enable an output on an FTM channel.
  708. *
  709. * To enable the PWM channel output call this function with val=true. For input mode,
  710. * call this function with val=false.
  711. *
  712. * @param base FTM peripheral base address
  713. * @param chnlNumber Channel to be configured
  714. * @param value true: enable output; false: output is disabled, used in input mode
  715. */
  716. static inline void FTM_SetPwmOutputEnable(FTM_Type *base, ftm_chnl_t chnlNumber, bool value)
  717. {
  718. if (value)
  719. {
  720. base->SC |= (1UL << ((uint32_t)chnlNumber + FTM_SC_PWMEN0_SHIFT));
  721. }
  722. else
  723. {
  724. base->SC &= ~(1UL << ((uint32_t)chnlNumber + FTM_SC_PWMEN0_SHIFT));
  725. }
  726. }
  727. #endif
  728. /*!
  729. * @name Channel pair operations
  730. * @{
  731. */
  732. /*!
  733. * @brief This function enables/disables the fault control in a channel pair.
  734. *
  735. * @param base FTM peripheral base address
  736. * @param chnlPairNumber The FTM channel pair number; options are 0, 1, 2, 3
  737. * @param value true: Enable fault control for this channel pair; false: No fault control
  738. */
  739. static inline void FTM_SetFaultControlEnable(FTM_Type *base, ftm_chnl_t chnlPairNumber, bool value)
  740. {
  741. if (value)
  742. {
  743. base->COMBINE |=
  744. (1UL << (FTM_COMBINE_FAULTEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (uint32_t)chnlPairNumber)));
  745. }
  746. else
  747. {
  748. base->COMBINE &=
  749. ~(1UL << (FTM_COMBINE_FAULTEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (uint32_t)chnlPairNumber)));
  750. }
  751. }
  752. /*!
  753. * @brief This function enables/disables the dead time insertion in a channel pair.
  754. *
  755. * @param base FTM peripheral base address
  756. * @param chnlPairNumber The FTM channel pair number; options are 0, 1, 2, 3
  757. * @param value true: Insert dead time in this channel pair; false: No dead time inserted
  758. */
  759. static inline void FTM_SetDeadTimeEnable(FTM_Type *base, ftm_chnl_t chnlPairNumber, bool value)
  760. {
  761. if (value)
  762. {
  763. base->COMBINE |= (1UL << (FTM_COMBINE_DTEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (uint32_t)chnlPairNumber)));
  764. }
  765. else
  766. {
  767. base->COMBINE &= ~(1UL << (FTM_COMBINE_DTEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (uint32_t)chnlPairNumber)));
  768. }
  769. }
  770. /*!
  771. * @brief This function enables/disables complementary mode in a channel pair.
  772. *
  773. * @param base FTM peripheral base address
  774. * @param chnlPairNumber The FTM channel pair number; options are 0, 1, 2, 3
  775. * @param value true: enable complementary mode; false: disable complementary mode
  776. */
  777. static inline void FTM_SetComplementaryEnable(FTM_Type *base, ftm_chnl_t chnlPairNumber, bool value)
  778. {
  779. if (value)
  780. {
  781. base->COMBINE |= (1UL << (FTM_COMBINE_COMP0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (uint32_t)chnlPairNumber)));
  782. }
  783. else
  784. {
  785. base->COMBINE &= ~(1UL << (FTM_COMBINE_COMP0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (uint32_t)chnlPairNumber)));
  786. }
  787. }
  788. /*!
  789. * @brief This function enables/disables inverting control in a channel pair.
  790. *
  791. * @param base FTM peripheral base address
  792. * @param chnlPairNumber The FTM channel pair number; options are 0, 1, 2, 3
  793. * @param value true: enable inverting; false: disable inverting
  794. */
  795. static inline void FTM_SetInvertEnable(FTM_Type *base, ftm_chnl_t chnlPairNumber, bool value)
  796. {
  797. if (value)
  798. {
  799. base->INVCTRL |= (1UL << (uint32_t)chnlPairNumber);
  800. }
  801. else
  802. {
  803. base->INVCTRL &= ~(1UL << (uint32_t)chnlPairNumber);
  804. }
  805. }
  806. /*! @}*/
  807. /*!
  808. * @name Quad Decoder
  809. * @{
  810. */
  811. /*!
  812. * @brief Configures the parameters and activates the quadrature decoder mode.
  813. *
  814. * @param base FTM peripheral base address
  815. * @param phaseAParams Phase A configuration parameters
  816. * @param phaseBParams Phase B configuration parameters
  817. * @param quadMode Selects encoding mode used in quadrature decoder mode
  818. */
  819. void FTM_SetupQuadDecode(FTM_Type *base,
  820. const ftm_phase_params_t *phaseAParams,
  821. const ftm_phase_params_t *phaseBParams,
  822. ftm_quad_decode_mode_t quadMode);
  823. #if !(defined(FSL_FEATURE_FTM_HAS_NO_QDCTRL) && FSL_FEATURE_FTM_HAS_NO_QDCTRL)
  824. /*!
  825. * @brief Gets the FTM Quad Decoder flags.
  826. *
  827. * @param base FTM peripheral base address.
  828. * @return Flag mask of FTM Quad Decoder, see _ftm_quad_decoder_flags.
  829. */
  830. static inline uint32_t FTM_GetQuadDecoderFlags(FTM_Type *base)
  831. {
  832. return base->QDCTRL & (FTM_QDCTRL_QUADIR_MASK | FTM_QDCTRL_TOFDIR_MASK);
  833. }
  834. #endif
  835. /*!
  836. * @brief Sets the modulo values for Quad Decoder.
  837. *
  838. * The modulo values configure the minimum and maximum values that the Quad decoder counter can reach. After the
  839. * counter goes over, the counter value goes to the other side and decrease/increase again.
  840. *
  841. * @param base FTM peripheral base address.
  842. * @param startValue The low limit value for Quad Decoder counter.
  843. * @param overValue The high limit value for Quad Decoder counter.
  844. */
  845. static inline void FTM_SetQuadDecoderModuloValue(FTM_Type *base, uint32_t startValue, uint32_t overValue)
  846. {
  847. base->CNTIN = startValue;
  848. base->MOD = overValue;
  849. }
  850. /*!
  851. * @brief Gets the current Quad Decoder counter value.
  852. *
  853. * @param base FTM peripheral base address.
  854. * @return Current quad Decoder counter value.
  855. */
  856. static inline uint32_t FTM_GetQuadDecoderCounterValue(FTM_Type *base)
  857. {
  858. return base->CNT;
  859. }
  860. /*!
  861. * @brief Clears the current Quad Decoder counter value.
  862. *
  863. * The counter is set as the initial value.
  864. *
  865. * @param base FTM peripheral base address.
  866. */
  867. static inline void FTM_ClearQuadDecoderCounterValue(FTM_Type *base)
  868. {
  869. base->CNT = base->CNTIN;
  870. }
  871. /*! @}*/
  872. /*!
  873. * @brief Enables or disables the FTM software trigger for PWM synchronization.
  874. *
  875. * @param base FTM peripheral base address
  876. * @param enable true: software trigger is selected, false: software trigger is not selected
  877. */
  878. static inline void FTM_SetSoftwareTrigger(FTM_Type *base, bool enable)
  879. {
  880. if (enable)
  881. {
  882. base->SYNC |= FTM_SYNC_SWSYNC_MASK;
  883. }
  884. else
  885. {
  886. base->SYNC &= ~FTM_SYNC_SWSYNC_MASK;
  887. }
  888. }
  889. /*!
  890. * @brief Enables or disables the FTM write protection.
  891. *
  892. * @param base FTM peripheral base address
  893. * @param enable true: Write-protection is enabled, false: Write-protection is disabled
  894. */
  895. static inline void FTM_SetWriteProtection(FTM_Type *base, bool enable)
  896. {
  897. /* Configure write protection */
  898. if (enable)
  899. {
  900. base->FMS |= FTM_FMS_WPEN_MASK;
  901. }
  902. else
  903. {
  904. base->MODE |= FTM_MODE_WPDIS_MASK;
  905. }
  906. }
  907. #if defined(FSL_FEATURE_FTM_HAS_DMA_SUPPORT) && FSL_FEATURE_FTM_HAS_DMA_SUPPORT
  908. /*!
  909. * @brief Enable DMA transfer or not.
  910. *
  911. * Note: CHnIE bit needs to be set when calling this API. The channel DMA transfer request
  912. * is generated and the channel interrupt is not generated if (CHnF = 1) when DMA and CHnIE
  913. * bits are set.
  914. *
  915. * @param base FTM peripheral base address.
  916. * @param chnlNumber Channel to be configured
  917. * @param enable true to enable, false to disable
  918. */
  919. static inline void FTM_EnableDmaTransfer(FTM_Type *base, ftm_chnl_t chnlNumber, bool enable)
  920. {
  921. if (enable)
  922. {
  923. /* Enable DMA transfer */
  924. base->CONTROLS[chnlNumber].CnSC |= FTM_CnSC_DMA_MASK;
  925. }
  926. else
  927. {
  928. /* Disable DMA transfer */
  929. base->CONTROLS[chnlNumber].CnSC &= ~FTM_CnSC_DMA_MASK;
  930. }
  931. }
  932. #endif /* FSL_FEATURE_FTM_HAS_DMA_SUPPORT */
  933. #if defined(__cplusplus)
  934. }
  935. #endif
  936. /*! @}*/
  937. #endif /* _FSL_FTM_H_*/