fsl_ftm.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. #include "fsl_ftm.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.ftm"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Gets the instance from the base address
  44. *
  45. * @param base FTM peripheral base address
  46. *
  47. * @return The FTM instance
  48. */
  49. static uint32_t FTM_GetInstance(FTM_Type *base);
  50. /*!
  51. * @brief Sets the FTM register PWM synchronization method
  52. *
  53. * This function will set the necessary bits for the PWM synchronization mode that
  54. * user wishes to use.
  55. *
  56. * @param base FTM peripheral base address
  57. * @param syncMethod Syncronization methods to use to update buffered registers. This is a logical
  58. * OR of members of the enumeration ::ftm_pwm_sync_method_t
  59. */
  60. static void FTM_SetPwmSync(FTM_Type *base, uint32_t syncMethod);
  61. /*!
  62. * @brief Sets the reload points used as loading points for register update
  63. *
  64. * This function will set the necessary bits based on what the user wishes to use as loading
  65. * points for FTM register update. When using this it is not required to use PWM synchnronization.
  66. *
  67. * @param base FTM peripheral base address
  68. * @param reloadPoints FTM reload points. This is a logical OR of members of the
  69. * enumeration ::ftm_reload_point_t
  70. */
  71. static void FTM_SetReloadPoints(FTM_Type *base, uint32_t reloadPoints);
  72. /*******************************************************************************
  73. * Variables
  74. ******************************************************************************/
  75. /*! @brief Pointers to FTM bases for each instance. */
  76. static FTM_Type *const s_ftmBases[] = FTM_BASE_PTRS;
  77. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  78. /*! @brief Pointers to FTM clocks for each instance. */
  79. static const clock_ip_name_t s_ftmClocks[] = FTM_CLOCKS;
  80. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  81. /*******************************************************************************
  82. * Code
  83. ******************************************************************************/
  84. static uint32_t FTM_GetInstance(FTM_Type *base)
  85. {
  86. uint32_t instance;
  87. uint32_t ftmArrayCount = (sizeof(s_ftmBases) / sizeof(s_ftmBases[0]));
  88. /* Find the instance index from base address mappings. */
  89. for (instance = 0; instance < ftmArrayCount; instance++)
  90. {
  91. if (s_ftmBases[instance] == base)
  92. {
  93. break;
  94. }
  95. }
  96. assert(instance < ftmArrayCount);
  97. return instance;
  98. }
  99. static void FTM_SetPwmSync(FTM_Type *base, uint32_t syncMethod)
  100. {
  101. uint8_t chnlNumber = 0;
  102. uint32_t reg = 0, syncReg = 0;
  103. syncReg = base->SYNC;
  104. /* Enable PWM synchronization of output mask register */
  105. syncReg |= FTM_SYNC_SYNCHOM_MASK;
  106. reg = base->COMBINE;
  107. for (chnlNumber = 0; chnlNumber < (FSL_FEATURE_FTM_CHANNEL_COUNTn(base) / 2); chnlNumber++)
  108. {
  109. /* Enable PWM synchronization of registers C(n)V and C(n+1)V */
  110. reg |= (1U << (FTM_COMBINE_SYNCEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * chnlNumber)));
  111. }
  112. base->COMBINE = reg;
  113. reg = base->SYNCONF;
  114. /* Use enhanced PWM synchronization method. Use PWM sync to update register values */
  115. reg |= (FTM_SYNCONF_SYNCMODE_MASK | FTM_SYNCONF_CNTINC_MASK | FTM_SYNCONF_INVC_MASK | FTM_SYNCONF_SWOC_MASK);
  116. if (syncMethod & FTM_SYNC_SWSYNC_MASK)
  117. {
  118. /* Enable needed bits for software trigger to update registers with its buffer value */
  119. reg |= (FTM_SYNCONF_SWRSTCNT_MASK | FTM_SYNCONF_SWWRBUF_MASK | FTM_SYNCONF_SWINVC_MASK |
  120. FTM_SYNCONF_SWSOC_MASK | FTM_SYNCONF_SWOM_MASK);
  121. }
  122. if (syncMethod & (FTM_SYNC_TRIG0_MASK | FTM_SYNC_TRIG1_MASK | FTM_SYNC_TRIG2_MASK))
  123. {
  124. /* Enable needed bits for hardware trigger to update registers with its buffer value */
  125. reg |= (FTM_SYNCONF_HWRSTCNT_MASK | FTM_SYNCONF_HWWRBUF_MASK | FTM_SYNCONF_HWINVC_MASK |
  126. FTM_SYNCONF_HWSOC_MASK | FTM_SYNCONF_HWOM_MASK);
  127. /* Enable the appropriate hardware trigger that is used for PWM sync */
  128. if (syncMethod & FTM_SYNC_TRIG0_MASK)
  129. {
  130. syncReg |= FTM_SYNC_TRIG0_MASK;
  131. }
  132. if (syncMethod & FTM_SYNC_TRIG1_MASK)
  133. {
  134. syncReg |= FTM_SYNC_TRIG1_MASK;
  135. }
  136. if (syncMethod & FTM_SYNC_TRIG2_MASK)
  137. {
  138. syncReg |= FTM_SYNC_TRIG2_MASK;
  139. }
  140. }
  141. /* Write back values to the SYNC register */
  142. base->SYNC = syncReg;
  143. /* Write the PWM synch values to the SYNCONF register */
  144. base->SYNCONF = reg;
  145. }
  146. static void FTM_SetReloadPoints(FTM_Type *base, uint32_t reloadPoints)
  147. {
  148. uint32_t chnlNumber = 0;
  149. uint32_t reg = 0;
  150. /* Need CNTINC bit to be 1 for CNTIN register to update with its buffer value on reload */
  151. base->SYNCONF |= FTM_SYNCONF_CNTINC_MASK;
  152. reg = base->COMBINE;
  153. for (chnlNumber = 0; chnlNumber < (FSL_FEATURE_FTM_CHANNEL_COUNTn(base) / 2); chnlNumber++)
  154. {
  155. /* Need SYNCEN bit to be 1 for CnV reg to update with its buffer value on reload */
  156. reg |= (1U << (FTM_COMBINE_SYNCEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * chnlNumber)));
  157. }
  158. base->COMBINE = reg;
  159. /* Set the reload points */
  160. reg = base->PWMLOAD;
  161. /* Enable the selected channel match reload points */
  162. reg &= ~((1U << FSL_FEATURE_FTM_CHANNEL_COUNTn(base)) - 1);
  163. reg |= (reloadPoints & ((1U << FSL_FEATURE_FTM_CHANNEL_COUNTn(base)) - 1));
  164. #if defined(FSL_FEATURE_FTM_HAS_HALFCYCLE_RELOAD) && (FSL_FEATURE_FTM_HAS_HALFCYCLE_RELOAD)
  165. /* Enable half cycle match as a reload point */
  166. if (reloadPoints & kFTM_HalfCycMatch)
  167. {
  168. reg |= FTM_PWMLOAD_HCSEL_MASK;
  169. }
  170. else
  171. {
  172. reg &= ~FTM_PWMLOAD_HCSEL_MASK;
  173. }
  174. #endif /* FSL_FEATURE_FTM_HAS_HALFCYCLE_RELOAD */
  175. base->PWMLOAD = reg;
  176. /* These reload points are used when counter is in up-down counting mode */
  177. reg = base->SYNC;
  178. if (reloadPoints & kFTM_CntMax)
  179. {
  180. /* Reload when counter turns from up to down */
  181. reg |= FTM_SYNC_CNTMAX_MASK;
  182. }
  183. else
  184. {
  185. reg &= ~FTM_SYNC_CNTMAX_MASK;
  186. }
  187. if (reloadPoints & kFTM_CntMin)
  188. {
  189. /* Reload when counter turns from down to up */
  190. reg |= FTM_SYNC_CNTMIN_MASK;
  191. }
  192. else
  193. {
  194. reg &= ~FTM_SYNC_CNTMIN_MASK;
  195. }
  196. base->SYNC = reg;
  197. }
  198. status_t FTM_Init(FTM_Type *base, const ftm_config_t *config)
  199. {
  200. assert(config);
  201. uint32_t reg;
  202. if (!(config->pwmSyncMode &
  203. (FTM_SYNC_TRIG0_MASK | FTM_SYNC_TRIG1_MASK | FTM_SYNC_TRIG2_MASK | FTM_SYNC_SWSYNC_MASK)))
  204. {
  205. /* Invalid PWM sync mode */
  206. return kStatus_Fail;
  207. }
  208. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  209. /* Ungate the FTM clock*/
  210. CLOCK_EnableClock(s_ftmClocks[FTM_GetInstance(base)]);
  211. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  212. /* Configure the fault mode, enable FTM mode and disable write protection */
  213. base->MODE = FTM_MODE_FAULTM(config->faultMode) | FTM_MODE_FTMEN_MASK | FTM_MODE_WPDIS_MASK;
  214. /* Configure the update mechanism for buffered registers */
  215. FTM_SetPwmSync(base, config->pwmSyncMode);
  216. /* Setup intermediate register reload points */
  217. FTM_SetReloadPoints(base, config->reloadPoints);
  218. /* Set the clock prescale factor */
  219. base->SC = FTM_SC_PS(config->prescale);
  220. /* Setup the counter operation */
  221. base->CONF = (FTM_CONF_BDMMODE(config->bdmMode) | FTM_CONF_GTBEEN(config->useGlobalTimeBase));
  222. /* Initial state of channel output */
  223. base->OUTINIT = config->chnlInitState;
  224. /* Channel polarity */
  225. base->POL = config->chnlPolarity;
  226. /* Set the external trigger sources */
  227. base->EXTTRIG = config->extTriggers;
  228. #if defined(FSL_FEATURE_FTM_HAS_RELOAD_INITIALIZATION_TRIGGER) && (FSL_FEATURE_FTM_HAS_RELOAD_INITIALIZATION_TRIGGER)
  229. if (config->extTriggers & kFTM_ReloadInitTrigger)
  230. {
  231. base->CONF |= FTM_CONF_ITRIGR_MASK;
  232. }
  233. else
  234. {
  235. base->CONF &= ~FTM_CONF_ITRIGR_MASK;
  236. }
  237. #endif /* FSL_FEATURE_FTM_HAS_RELOAD_INITIALIZATION_TRIGGER */
  238. /* FTM deadtime insertion control */
  239. base->DEADTIME = (0u |
  240. #if defined(FSL_FEATURE_FTM_HAS_EXTENDED_DEADTIME_VALUE) && (FSL_FEATURE_FTM_HAS_EXTENDED_DEADTIME_VALUE)
  241. /* Has extended deadtime value register) */
  242. FTM_DEADTIME_DTVALEX(config->deadTimeValue >> 6) |
  243. #endif /* FSL_FEATURE_FTM_HAS_EXTENDED_DEADTIME_VALUE */
  244. FTM_DEADTIME_DTPS(config->deadTimePrescale) |
  245. FTM_DEADTIME_DTVAL(config->deadTimeValue));
  246. /* FTM fault filter value */
  247. reg = base->FLTCTRL;
  248. reg &= ~FTM_FLTCTRL_FFVAL_MASK;
  249. reg |= FTM_FLTCTRL_FFVAL(config->faultFilterValue);
  250. base->FLTCTRL = reg;
  251. return kStatus_Success;
  252. }
  253. void FTM_Deinit(FTM_Type *base)
  254. {
  255. /* Set clock source to none to disable counter */
  256. base->SC &= ~(FTM_SC_CLKS_MASK);
  257. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  258. /* Gate the FTM clock */
  259. CLOCK_DisableClock(s_ftmClocks[FTM_GetInstance(base)]);
  260. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  261. }
  262. void FTM_GetDefaultConfig(ftm_config_t *config)
  263. {
  264. assert(config);
  265. /* Divide FTM clock by 1 */
  266. config->prescale = kFTM_Prescale_Divide_1;
  267. /* FTM behavior in BDM mode */
  268. config->bdmMode = kFTM_BdmMode_0;
  269. /* Software trigger will be used to update registers */
  270. config->pwmSyncMode = kFTM_SoftwareTrigger;
  271. /* No intermediate register load */
  272. config->reloadPoints = 0;
  273. /* Fault control disabled for all channels */
  274. config->faultMode = kFTM_Fault_Disable;
  275. /* Disable the fault filter */
  276. config->faultFilterValue = 0;
  277. /* Divide the system clock by 1 */
  278. config->deadTimePrescale = kFTM_Deadtime_Prescale_1;
  279. /* No counts are inserted */
  280. config->deadTimeValue = 0;
  281. /* No external trigger */
  282. config->extTriggers = 0;
  283. /* Initialization value is 0 for all channels */
  284. config->chnlInitState = 0;
  285. /* Active high polarity for all channels */
  286. config->chnlPolarity = 0;
  287. /* Use internal FTM counter as timebase */
  288. config->useGlobalTimeBase = false;
  289. }
  290. status_t FTM_SetupPwm(FTM_Type *base,
  291. const ftm_chnl_pwm_signal_param_t *chnlParams,
  292. uint8_t numOfChnls,
  293. ftm_pwm_mode_t mode,
  294. uint32_t pwmFreq_Hz,
  295. uint32_t srcClock_Hz)
  296. {
  297. assert(chnlParams);
  298. assert(srcClock_Hz);
  299. assert(pwmFreq_Hz);
  300. assert(numOfChnls);
  301. uint32_t mod, reg;
  302. uint32_t ftmClock = (srcClock_Hz / (1U << (base->SC & FTM_SC_PS_MASK)));
  303. uint16_t cnv, cnvFirstEdge;
  304. uint8_t i;
  305. switch (mode)
  306. {
  307. case kFTM_EdgeAlignedPwm:
  308. case kFTM_CombinedPwm:
  309. base->SC &= ~FTM_SC_CPWMS_MASK;
  310. mod = (ftmClock / pwmFreq_Hz) - 1;
  311. break;
  312. case kFTM_CenterAlignedPwm:
  313. base->SC |= FTM_SC_CPWMS_MASK;
  314. mod = ftmClock / (pwmFreq_Hz * 2);
  315. break;
  316. default:
  317. return kStatus_Fail;
  318. }
  319. /* Return an error in case we overflow the registers, probably would require changing
  320. * clock source to get the desired frequency */
  321. if (mod > 65535U)
  322. {
  323. return kStatus_Fail;
  324. }
  325. /* Set the PWM period */
  326. base->MOD = mod;
  327. /* Setup each FTM channel */
  328. for (i = 0; i < numOfChnls; i++)
  329. {
  330. /* Return error if requested dutycycle is greater than the max allowed */
  331. if (chnlParams->dutyCyclePercent > 100)
  332. {
  333. return kStatus_Fail;
  334. }
  335. if ((mode == kFTM_EdgeAlignedPwm) || (mode == kFTM_CenterAlignedPwm))
  336. {
  337. /* Clear the current mode and edge level bits */
  338. reg = base->CONTROLS[chnlParams->chnlNumber].CnSC;
  339. reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  340. /* Setup the active level */
  341. reg |= (uint32_t)(chnlParams->level << FTM_CnSC_ELSA_SHIFT);
  342. /* Edge-aligned mode needs MSB to be 1, don't care for Center-aligned mode */
  343. reg |= FTM_CnSC_MSB(1U);
  344. /* Update the mode and edge level */
  345. base->CONTROLS[chnlParams->chnlNumber].CnSC = reg;
  346. if (chnlParams->dutyCyclePercent == 0)
  347. {
  348. /* Signal stays low */
  349. cnv = 0;
  350. }
  351. else
  352. {
  353. cnv = (mod * chnlParams->dutyCyclePercent) / 100;
  354. /* For 100% duty cycle */
  355. if (cnv >= mod)
  356. {
  357. cnv = mod + 1;
  358. }
  359. }
  360. base->CONTROLS[chnlParams->chnlNumber].CnV = cnv;
  361. #if defined(FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT) && (FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT)
  362. /* Set to output mode */
  363. FTM_SetPwmOutputEnable(base, chnlParams->chnlNumber, true);
  364. #endif
  365. }
  366. else
  367. {
  368. /* This check is added for combined mode as the channel number should be the pair number */
  369. if (chnlParams->chnlNumber >= (FSL_FEATURE_FTM_CHANNEL_COUNTn(base) / 2))
  370. {
  371. return kStatus_Fail;
  372. }
  373. /* Return error if requested value is greater than the max allowed */
  374. if (chnlParams->firstEdgeDelayPercent > 100)
  375. {
  376. return kStatus_Fail;
  377. }
  378. /* Configure delay of the first edge */
  379. if (chnlParams->firstEdgeDelayPercent == 0)
  380. {
  381. /* No delay for the first edge */
  382. cnvFirstEdge = 0;
  383. }
  384. else
  385. {
  386. cnvFirstEdge = (mod * chnlParams->firstEdgeDelayPercent) / 100;
  387. }
  388. /* Configure dutycycle */
  389. if (chnlParams->dutyCyclePercent == 0)
  390. {
  391. /* Signal stays low */
  392. cnv = 0;
  393. cnvFirstEdge = 0;
  394. }
  395. else
  396. {
  397. cnv = (mod * chnlParams->dutyCyclePercent) / 100;
  398. /* For 100% duty cycle */
  399. if (cnv >= mod)
  400. {
  401. cnv = mod + 1;
  402. }
  403. }
  404. /* Clear the current mode and edge level bits for channel n */
  405. reg = base->CONTROLS[chnlParams->chnlNumber * 2].CnSC;
  406. reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  407. /* Setup the active level for channel n */
  408. reg |= (uint32_t)(chnlParams->level << FTM_CnSC_ELSA_SHIFT);
  409. /* Update the mode and edge level for channel n */
  410. base->CONTROLS[chnlParams->chnlNumber * 2].CnSC = reg;
  411. /* Clear the current mode and edge level bits for channel n + 1 */
  412. reg = base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnSC;
  413. reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  414. /* Setup the active level for channel n + 1 */
  415. reg |= (uint32_t)(chnlParams->level << FTM_CnSC_ELSA_SHIFT);
  416. /* Update the mode and edge level for channel n + 1*/
  417. base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnSC = reg;
  418. /* Set the combine bit for the channel pair */
  419. base->COMBINE |=
  420. (1U << (FTM_COMBINE_COMBINE0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * chnlParams->chnlNumber)));
  421. /* Set the channel pair values */
  422. base->CONTROLS[chnlParams->chnlNumber * 2].CnV = cnvFirstEdge;
  423. base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnV = cnvFirstEdge + cnv;
  424. #if defined(FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT) && (FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT)
  425. /* Set to output mode */
  426. FTM_SetPwmOutputEnable(base, (ftm_chnl_t)((uint8_t)chnlParams->chnlNumber * 2), true);
  427. FTM_SetPwmOutputEnable(base, (ftm_chnl_t)((uint8_t)chnlParams->chnlNumber * 2 + 1), true);
  428. #endif
  429. }
  430. chnlParams++;
  431. }
  432. return kStatus_Success;
  433. }
  434. void FTM_UpdatePwmDutycycle(FTM_Type *base,
  435. ftm_chnl_t chnlNumber,
  436. ftm_pwm_mode_t currentPwmMode,
  437. uint8_t dutyCyclePercent)
  438. {
  439. uint16_t cnv, cnvFirstEdge = 0, mod;
  440. mod = base->MOD;
  441. if ((currentPwmMode == kFTM_EdgeAlignedPwm) || (currentPwmMode == kFTM_CenterAlignedPwm))
  442. {
  443. cnv = (mod * dutyCyclePercent) / 100;
  444. /* For 100% duty cycle */
  445. if (cnv >= mod)
  446. {
  447. cnv = mod + 1;
  448. }
  449. base->CONTROLS[chnlNumber].CnV = cnv;
  450. }
  451. else
  452. {
  453. /* This check is added for combined mode as the channel number should be the pair number */
  454. if (chnlNumber >= (FSL_FEATURE_FTM_CHANNEL_COUNTn(base) / 2))
  455. {
  456. return;
  457. }
  458. cnv = (mod * dutyCyclePercent) / 100;
  459. cnvFirstEdge = base->CONTROLS[chnlNumber * 2].CnV;
  460. /* For 100% duty cycle */
  461. if (cnv >= mod)
  462. {
  463. cnv = mod + 1;
  464. }
  465. base->CONTROLS[(chnlNumber * 2) + 1].CnV = cnvFirstEdge + cnv;
  466. }
  467. }
  468. void FTM_UpdateChnlEdgeLevelSelect(FTM_Type *base, ftm_chnl_t chnlNumber, uint8_t level)
  469. {
  470. uint32_t reg = base->CONTROLS[chnlNumber].CnSC;
  471. /* Clear the field and write the new level value */
  472. reg &= ~(FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  473. reg |= ((uint32_t)level << FTM_CnSC_ELSA_SHIFT) & (FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  474. base->CONTROLS[chnlNumber].CnSC = reg;
  475. }
  476. void FTM_SetupInputCapture(FTM_Type *base,
  477. ftm_chnl_t chnlNumber,
  478. ftm_input_capture_edge_t captureMode,
  479. uint32_t filterValue)
  480. {
  481. uint32_t reg;
  482. /* Clear the combine bit for the channel pair */
  483. base->COMBINE &= ~(1U << (FTM_COMBINE_COMBINE0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (chnlNumber >> 1))));
  484. /* Clear the dual edge capture mode because it's it's higher priority */
  485. base->COMBINE &= ~(1U << (FTM_COMBINE_DECAPEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (chnlNumber >> 1))));
  486. #if !(defined(FSL_FEATURE_FTM_HAS_NO_QDCTRL) && FSL_FEATURE_FTM_HAS_NO_QDCTRL)
  487. /* Clear the quadrature decoder mode beacause it's higher priority */
  488. base->QDCTRL &= ~FTM_QDCTRL_QUADEN_MASK;
  489. #endif
  490. reg = base->CONTROLS[chnlNumber].CnSC;
  491. reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  492. reg |= captureMode;
  493. /* Set the requested input capture mode */
  494. base->CONTROLS[chnlNumber].CnSC = reg;
  495. /* Input filter available only for channels 0, 1, 2, 3 */
  496. if (chnlNumber < kFTM_Chnl_4)
  497. {
  498. reg = base->FILTER;
  499. reg &= ~(FTM_FILTER_CH0FVAL_MASK << (FTM_FILTER_CH1FVAL_SHIFT * chnlNumber));
  500. reg |= (filterValue << (FTM_FILTER_CH1FVAL_SHIFT * chnlNumber));
  501. base->FILTER = reg;
  502. }
  503. #if defined(FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT) && (FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT)
  504. /* Set to input mode */
  505. FTM_SetPwmOutputEnable(base, chnlNumber, false);
  506. #endif
  507. }
  508. void FTM_SetupOutputCompare(FTM_Type *base,
  509. ftm_chnl_t chnlNumber,
  510. ftm_output_compare_mode_t compareMode,
  511. uint32_t compareValue)
  512. {
  513. uint32_t reg;
  514. /* Clear the combine bit for the channel pair */
  515. base->COMBINE &= ~(1U << (FTM_COMBINE_COMBINE0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (chnlNumber >> 1))));
  516. /* Clear the dual edge capture mode because it's it's higher priority */
  517. base->COMBINE &= ~(1U << (FTM_COMBINE_DECAPEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * (chnlNumber >> 1))));
  518. #if !(defined(FSL_FEATURE_FTM_HAS_NO_QDCTRL) && FSL_FEATURE_FTM_HAS_NO_QDCTRL)
  519. /* Clear the quadrature decoder mode beacause it's higher priority */
  520. base->QDCTRL &= ~FTM_QDCTRL_QUADEN_MASK;
  521. #endif
  522. reg = base->CONTROLS[chnlNumber].CnSC;
  523. reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  524. reg |= compareMode;
  525. /* Setup the channel output behaviour when a match occurs with the compare value */
  526. base->CONTROLS[chnlNumber].CnSC = reg;
  527. /* Set output on match to the requested level */
  528. base->CONTROLS[chnlNumber].CnV = compareValue;
  529. #if defined(FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT) && (FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT)
  530. /* Set to output mode */
  531. FTM_SetPwmOutputEnable(base, chnlNumber, true);
  532. #endif
  533. }
  534. void FTM_SetupDualEdgeCapture(FTM_Type *base,
  535. ftm_chnl_t chnlPairNumber,
  536. const ftm_dual_edge_capture_param_t *edgeParam,
  537. uint32_t filterValue)
  538. {
  539. assert(edgeParam);
  540. uint32_t reg;
  541. reg = base->COMBINE;
  542. /* Clear the combine bit for the channel pair */
  543. reg &= ~(1U << (FTM_COMBINE_COMBINE0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * chnlPairNumber)));
  544. /* Enable the DECAPEN bit */
  545. reg |= (1U << (FTM_COMBINE_DECAPEN0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * chnlPairNumber)));
  546. reg |= (1U << (FTM_COMBINE_DECAP0_SHIFT + (FTM_COMBINE_COMBINE1_SHIFT * chnlPairNumber)));
  547. base->COMBINE = reg;
  548. /* Setup the edge detection from channel n and n + 1 */
  549. reg = base->CONTROLS[chnlPairNumber * 2].CnSC;
  550. reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  551. reg |= ((uint32_t)edgeParam->mode | (uint32_t)edgeParam->currChanEdgeMode);
  552. base->CONTROLS[chnlPairNumber * 2].CnSC = reg;
  553. reg = base->CONTROLS[(chnlPairNumber * 2) + 1].CnSC;
  554. reg &= ~(FTM_CnSC_MSA_MASK | FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK | FTM_CnSC_ELSB_MASK);
  555. reg |= ((uint32_t)edgeParam->mode | (uint32_t)edgeParam->nextChanEdgeMode);
  556. base->CONTROLS[(chnlPairNumber * 2) + 1].CnSC = reg;
  557. /* Input filter available only for channels 0, 1, 2, 3 */
  558. if (chnlPairNumber < kFTM_Chnl_4)
  559. {
  560. reg = base->FILTER;
  561. reg &= ~(FTM_FILTER_CH0FVAL_MASK << (FTM_FILTER_CH1FVAL_SHIFT * chnlPairNumber));
  562. reg |= (filterValue << (FTM_FILTER_CH1FVAL_SHIFT * chnlPairNumber));
  563. base->FILTER = reg;
  564. }
  565. #if defined(FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT) && (FSL_FEATURE_FTM_HAS_ENABLE_PWM_OUTPUT)
  566. /* Set to input mode */
  567. FTM_SetPwmOutputEnable(base, chnlPairNumber, false);
  568. #endif
  569. }
  570. void FTM_SetupQuadDecode(FTM_Type *base,
  571. const ftm_phase_params_t *phaseAParams,
  572. const ftm_phase_params_t *phaseBParams,
  573. ftm_quad_decode_mode_t quadMode)
  574. {
  575. assert(phaseAParams);
  576. assert(phaseBParams);
  577. uint32_t reg;
  578. /* Set Phase A filter value if phase filter is enabled */
  579. if (phaseAParams->enablePhaseFilter)
  580. {
  581. reg = base->FILTER;
  582. reg &= ~(FTM_FILTER_CH0FVAL_MASK);
  583. reg |= FTM_FILTER_CH0FVAL(phaseAParams->phaseFilterVal);
  584. base->FILTER = reg;
  585. }
  586. /* Set Phase B filter value if phase filter is enabled */
  587. if (phaseBParams->enablePhaseFilter)
  588. {
  589. reg = base->FILTER;
  590. reg &= ~(FTM_FILTER_CH1FVAL_MASK);
  591. reg |= FTM_FILTER_CH1FVAL(phaseBParams->phaseFilterVal);
  592. base->FILTER = reg;
  593. }
  594. #if !(defined(FSL_FEATURE_FTM_HAS_NO_QDCTRL) && FSL_FEATURE_FTM_HAS_NO_QDCTRL)
  595. /* Set Quadrature decode properties */
  596. reg = base->QDCTRL;
  597. reg &= ~(FTM_QDCTRL_QUADMODE_MASK | FTM_QDCTRL_PHAFLTREN_MASK | FTM_QDCTRL_PHBFLTREN_MASK | FTM_QDCTRL_PHAPOL_MASK |
  598. FTM_QDCTRL_PHBPOL_MASK);
  599. reg |= (FTM_QDCTRL_QUADMODE(quadMode) | FTM_QDCTRL_PHAFLTREN(phaseAParams->enablePhaseFilter) |
  600. FTM_QDCTRL_PHBFLTREN(phaseBParams->enablePhaseFilter) | FTM_QDCTRL_PHAPOL(phaseAParams->phasePolarity) |
  601. FTM_QDCTRL_PHBPOL(phaseBParams->phasePolarity));
  602. base->QDCTRL = reg;
  603. /* Enable Quad decode */
  604. base->QDCTRL |= FTM_QDCTRL_QUADEN_MASK;
  605. #endif
  606. }
  607. void FTM_SetupFault(FTM_Type *base, ftm_fault_input_t faultNumber, const ftm_fault_param_t *faultParams)
  608. {
  609. assert(faultParams);
  610. if (faultParams->useFaultFilter)
  611. {
  612. /* Enable the fault filter */
  613. base->FLTCTRL |= (FTM_FLTCTRL_FFLTR0EN_MASK << (FTM_FLTCTRL_FFLTR0EN_SHIFT + faultNumber));
  614. }
  615. else
  616. {
  617. /* Disable the fault filter */
  618. base->FLTCTRL &= ~(FTM_FLTCTRL_FFLTR0EN_MASK << (FTM_FLTCTRL_FFLTR0EN_SHIFT + faultNumber));
  619. }
  620. if (faultParams->faultLevel)
  621. {
  622. /* Active low polarity for the fault input pin */
  623. base->FLTPOL |= (1U << faultNumber);
  624. }
  625. else
  626. {
  627. /* Active high polarity for the fault input pin */
  628. base->FLTPOL &= ~(1U << faultNumber);
  629. }
  630. if (faultParams->enableFaultInput)
  631. {
  632. /* Enable the fault input */
  633. base->FLTCTRL |= (FTM_FLTCTRL_FAULT0EN_MASK << faultNumber);
  634. }
  635. else
  636. {
  637. /* Disable the fault input */
  638. base->FLTCTRL &= ~(FTM_FLTCTRL_FAULT0EN_MASK << faultNumber);
  639. }
  640. }
  641. void FTM_EnableInterrupts(FTM_Type *base, uint32_t mask)
  642. {
  643. uint32_t chnlInts = (mask & 0xFFU);
  644. uint8_t chnlNumber = 0;
  645. /* Enable the timer overflow interrupt */
  646. if (mask & kFTM_TimeOverflowInterruptEnable)
  647. {
  648. base->SC |= FTM_SC_TOIE_MASK;
  649. }
  650. /* Enable the fault interrupt */
  651. if (mask & kFTM_FaultInterruptEnable)
  652. {
  653. base->MODE |= FTM_MODE_FAULTIE_MASK;
  654. }
  655. #if defined(FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT) && (FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT)
  656. /* Enable the reload interrupt available only on certain SoC's */
  657. if (mask & kFTM_ReloadInterruptEnable)
  658. {
  659. base->SC |= FTM_SC_RIE_MASK;
  660. }
  661. #endif
  662. /* Enable the channel interrupts */
  663. while (chnlInts)
  664. {
  665. if (chnlInts & 0x1)
  666. {
  667. base->CONTROLS[chnlNumber].CnSC |= FTM_CnSC_CHIE_MASK;
  668. }
  669. chnlNumber++;
  670. chnlInts = chnlInts >> 1U;
  671. }
  672. }
  673. void FTM_DisableInterrupts(FTM_Type *base, uint32_t mask)
  674. {
  675. uint32_t chnlInts = (mask & 0xFF);
  676. uint8_t chnlNumber = 0;
  677. /* Disable the timer overflow interrupt */
  678. if (mask & kFTM_TimeOverflowInterruptEnable)
  679. {
  680. base->SC &= ~FTM_SC_TOIE_MASK;
  681. }
  682. /* Disable the fault interrupt */
  683. if (mask & kFTM_FaultInterruptEnable)
  684. {
  685. base->MODE &= ~FTM_MODE_FAULTIE_MASK;
  686. }
  687. #if defined(FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT) && (FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT)
  688. /* Disable the reload interrupt available only on certain SoC's */
  689. if (mask & kFTM_ReloadInterruptEnable)
  690. {
  691. base->SC &= ~FTM_SC_RIE_MASK;
  692. }
  693. #endif
  694. /* Disable the channel interrupts */
  695. while (chnlInts)
  696. {
  697. if (chnlInts & 0x1)
  698. {
  699. base->CONTROLS[chnlNumber].CnSC &= ~FTM_CnSC_CHIE_MASK;
  700. }
  701. chnlNumber++;
  702. chnlInts = chnlInts >> 1U;
  703. }
  704. }
  705. uint32_t FTM_GetEnabledInterrupts(FTM_Type *base)
  706. {
  707. uint32_t enabledInterrupts = 0;
  708. int8_t chnlCount = FSL_FEATURE_FTM_CHANNEL_COUNTn(base);
  709. /* The CHANNEL_COUNT macro returns -1 if it cannot match the FTM instance */
  710. assert(chnlCount != -1);
  711. /* Check if timer overflow interrupt is enabled */
  712. if (base->SC & FTM_SC_TOIE_MASK)
  713. {
  714. enabledInterrupts |= kFTM_TimeOverflowInterruptEnable;
  715. }
  716. /* Check if fault interrupt is enabled */
  717. if (base->MODE & FTM_MODE_FAULTIE_MASK)
  718. {
  719. enabledInterrupts |= kFTM_FaultInterruptEnable;
  720. }
  721. #if defined(FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT) && (FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT)
  722. /* Check if the reload interrupt is enabled */
  723. if (base->SC & FTM_SC_RIE_MASK)
  724. {
  725. enabledInterrupts |= kFTM_ReloadInterruptEnable;
  726. }
  727. #endif
  728. /* Check if the channel interrupts are enabled */
  729. while (chnlCount > 0)
  730. {
  731. chnlCount--;
  732. if (base->CONTROLS[chnlCount].CnSC & FTM_CnSC_CHIE_MASK)
  733. {
  734. enabledInterrupts |= (1U << chnlCount);
  735. }
  736. }
  737. return enabledInterrupts;
  738. }
  739. uint32_t FTM_GetStatusFlags(FTM_Type *base)
  740. {
  741. uint32_t statusFlags = 0;
  742. /* Check the timer flag */
  743. if (base->SC & FTM_SC_TOF_MASK)
  744. {
  745. statusFlags |= kFTM_TimeOverflowFlag;
  746. }
  747. /* Check fault flag */
  748. if (base->FMS & FTM_FMS_FAULTF_MASK)
  749. {
  750. statusFlags |= kFTM_FaultFlag;
  751. }
  752. /* Check channel trigger flag */
  753. if (base->EXTTRIG & FTM_EXTTRIG_TRIGF_MASK)
  754. {
  755. statusFlags |= kFTM_ChnlTriggerFlag;
  756. }
  757. #if defined(FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT) && (FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT)
  758. /* Check reload flag */
  759. if (base->SC & FTM_SC_RF_MASK)
  760. {
  761. statusFlags |= kFTM_ReloadFlag;
  762. }
  763. #endif
  764. /* Lower 8 bits contain the channel status flags */
  765. statusFlags |= (base->STATUS & 0xFFU);
  766. return statusFlags;
  767. }
  768. void FTM_ClearStatusFlags(FTM_Type *base, uint32_t mask)
  769. {
  770. /* Clear the timer overflow flag by writing a 0 to the bit while it is set */
  771. if (mask & kFTM_TimeOverflowFlag)
  772. {
  773. base->SC &= ~FTM_SC_TOF_MASK;
  774. }
  775. /* Clear fault flag by writing a 0 to the bit while it is set */
  776. if (mask & kFTM_FaultFlag)
  777. {
  778. base->FMS &= ~FTM_FMS_FAULTF_MASK;
  779. }
  780. /* Clear channel trigger flag */
  781. if (mask & kFTM_ChnlTriggerFlag)
  782. {
  783. base->EXTTRIG &= ~FTM_EXTTRIG_TRIGF_MASK;
  784. }
  785. #if defined(FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT) && (FSL_FEATURE_FTM_HAS_RELOAD_INTERRUPT)
  786. /* Check reload flag by writing a 0 to the bit while it is set */
  787. if (mask & kFTM_ReloadFlag)
  788. {
  789. base->SC &= ~FTM_SC_RF_MASK;
  790. }
  791. #endif
  792. /* Clear the channel status flags by writing a 0 to the bit */
  793. base->STATUS &= ~(mask & 0xFFU);
  794. }