fsl_ftm.h 37 KB

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