fsl_ftm.h 36 KB

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