fsl_ftm.c 30 KB

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