fsl_ftm.c 31 KB

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