fsl_tpm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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_tpm.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.tpm"
  41. #endif
  42. #define TPM_COMBINE_SHIFT (8U)
  43. /*******************************************************************************
  44. * Prototypes
  45. ******************************************************************************/
  46. /*!
  47. * @brief Gets the instance from the base address
  48. *
  49. * @param base TPM peripheral base address
  50. *
  51. * @return The TPM instance
  52. */
  53. static uint32_t TPM_GetInstance(TPM_Type *base);
  54. /*******************************************************************************
  55. * Variables
  56. ******************************************************************************/
  57. /*! @brief Pointers to TPM bases for each instance. */
  58. static TPM_Type *const s_tpmBases[] = TPM_BASE_PTRS;
  59. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  60. /*! @brief Pointers to TPM clocks for each instance. */
  61. static const clock_ip_name_t s_tpmClocks[] = TPM_CLOCKS;
  62. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  63. /*******************************************************************************
  64. * Code
  65. ******************************************************************************/
  66. static uint32_t TPM_GetInstance(TPM_Type *base)
  67. {
  68. uint32_t instance;
  69. uint32_t tpmArrayCount = (sizeof(s_tpmBases) / sizeof(s_tpmBases[0]));
  70. /* Find the instance index from base address mappings. */
  71. for (instance = 0; instance < tpmArrayCount; instance++)
  72. {
  73. if (s_tpmBases[instance] == base)
  74. {
  75. break;
  76. }
  77. }
  78. assert(instance < tpmArrayCount);
  79. return instance;
  80. }
  81. void TPM_Init(TPM_Type *base, const tpm_config_t *config)
  82. {
  83. assert(config);
  84. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  85. /* Enable the module clock */
  86. CLOCK_EnableClock(s_tpmClocks[TPM_GetInstance(base)]);
  87. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  88. #if defined(FSL_FEATURE_TPM_HAS_GLOBAL) && FSL_FEATURE_TPM_HAS_GLOBAL
  89. /* TPM reset is available on certain SoC's */
  90. TPM_Reset(base);
  91. #endif
  92. /* Set the clock prescale factor */
  93. base->SC = TPM_SC_PS(config->prescale);
  94. #if !(defined(FSL_FEATURE_TPM_HAS_NO_CONF) && FSL_FEATURE_TPM_HAS_NO_CONF)
  95. /* Setup the counter operation */
  96. base->CONF = TPM_CONF_DOZEEN(config->enableDoze) | TPM_CONF_GTBEEN(config->useGlobalTimeBase) |
  97. TPM_CONF_CROT(config->enableReloadOnTrigger) | TPM_CONF_CSOT(config->enableStartOnTrigger) |
  98. TPM_CONF_CSOO(config->enableStopOnOverflow) |
  99. #if defined(FSL_FEATURE_TPM_HAS_PAUSE_COUNTER_ON_TRIGGER) && FSL_FEATURE_TPM_HAS_PAUSE_COUNTER_ON_TRIGGER
  100. TPM_CONF_CPOT(config->enablePauseOnTrigger) |
  101. #endif
  102. #if defined(FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION) && FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION
  103. TPM_CONF_TRGSRC(config->triggerSource) |
  104. #endif
  105. TPM_CONF_TRGSEL(config->triggerSelect);
  106. if (config->enableDebugMode)
  107. {
  108. base->CONF |= TPM_CONF_DBGMODE_MASK;
  109. }
  110. else
  111. {
  112. base->CONF &= ~TPM_CONF_DBGMODE_MASK;
  113. }
  114. #endif
  115. }
  116. void TPM_Deinit(TPM_Type *base)
  117. {
  118. #if defined(FSL_FEATURE_TPM_HAS_SC_CLKS) && FSL_FEATURE_TPM_HAS_SC_CLKS
  119. /* Stop the counter */
  120. base->SC &= ~TPM_SC_CLKS_MASK;
  121. #else
  122. /* Stop the counter */
  123. base->SC &= ~TPM_SC_CMOD_MASK;
  124. #endif
  125. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  126. /* Gate the TPM clock */
  127. CLOCK_DisableClock(s_tpmClocks[TPM_GetInstance(base)]);
  128. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  129. }
  130. void TPM_GetDefaultConfig(tpm_config_t *config)
  131. {
  132. assert(config);
  133. /* TPM clock divide by 1 */
  134. config->prescale = kTPM_Prescale_Divide_1;
  135. #if !(defined(FSL_FEATURE_TPM_HAS_NO_CONF) && FSL_FEATURE_TPM_HAS_NO_CONF)
  136. /* Use internal TPM counter as timebase */
  137. config->useGlobalTimeBase = false;
  138. /* TPM counter continues in doze mode */
  139. config->enableDoze = false;
  140. /* TPM counter pauses when in debug mode */
  141. config->enableDebugMode = false;
  142. /* TPM counter will not be reloaded on input trigger */
  143. config->enableReloadOnTrigger = false;
  144. /* TPM counter continues running after overflow */
  145. config->enableStopOnOverflow = false;
  146. /* TPM counter starts immediately once it is enabled */
  147. config->enableStartOnTrigger = false;
  148. #if defined(FSL_FEATURE_TPM_HAS_PAUSE_COUNTER_ON_TRIGGER) && FSL_FEATURE_TPM_HAS_PAUSE_COUNTER_ON_TRIGGER
  149. config->enablePauseOnTrigger = false;
  150. #endif
  151. /* Choose trigger select 0 as input trigger for controlling counter operation */
  152. config->triggerSelect = kTPM_Trigger_Select_0;
  153. #if defined(FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION) && FSL_FEATURE_TPM_HAS_EXTERNAL_TRIGGER_SELECTION
  154. /* Choose external trigger source to control counter operation */
  155. config->triggerSource = kTPM_TriggerSource_External;
  156. #endif
  157. #endif
  158. }
  159. status_t TPM_SetupPwm(TPM_Type *base,
  160. const tpm_chnl_pwm_signal_param_t *chnlParams,
  161. uint8_t numOfChnls,
  162. tpm_pwm_mode_t mode,
  163. uint32_t pwmFreq_Hz,
  164. uint32_t srcClock_Hz)
  165. {
  166. assert(chnlParams);
  167. assert(pwmFreq_Hz);
  168. assert(numOfChnls);
  169. assert(srcClock_Hz);
  170. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  171. if(mode == kTPM_CombinedPwm)
  172. {
  173. assert(FSL_FEATURE_TPM_COMBINE_HAS_EFFECTn(base));
  174. }
  175. #endif
  176. uint32_t mod;
  177. uint32_t tpmClock = (srcClock_Hz / (1U << (base->SC & TPM_SC_PS_MASK)));
  178. uint16_t cnv;
  179. uint8_t i;
  180. #if defined(FSL_FEATURE_TPM_HAS_QDCTRL) && FSL_FEATURE_TPM_HAS_QDCTRL
  181. /* The TPM's QDCTRL register required to be effective */
  182. if( FSL_FEATURE_TPM_QDCTRL_HAS_EFFECTn(base) )
  183. {
  184. /* Clear quadrature Decoder mode because in quadrature Decoder mode PWM doesn't operate*/
  185. base->QDCTRL &= ~TPM_QDCTRL_QUADEN_MASK;
  186. }
  187. #endif
  188. switch (mode)
  189. {
  190. case kTPM_EdgeAlignedPwm:
  191. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  192. case kTPM_CombinedPwm:
  193. #endif
  194. base->SC &= ~TPM_SC_CPWMS_MASK;
  195. mod = (tpmClock / pwmFreq_Hz) - 1;
  196. break;
  197. case kTPM_CenterAlignedPwm:
  198. base->SC |= TPM_SC_CPWMS_MASK;
  199. mod = tpmClock / (pwmFreq_Hz * 2);
  200. break;
  201. default:
  202. return kStatus_Fail;
  203. }
  204. /* Return an error in case we overflow the registers, probably would require changing
  205. * clock source to get the desired frequency */
  206. if (mod > 65535U)
  207. {
  208. return kStatus_Fail;
  209. }
  210. /* Set the PWM period */
  211. base->MOD = mod;
  212. /* Setup each TPM channel */
  213. for (i = 0; i < numOfChnls; i++)
  214. {
  215. /* Return error if requested dutycycle is greater than the max allowed */
  216. if (chnlParams->dutyCyclePercent > 100)
  217. {
  218. return kStatus_Fail;
  219. }
  220. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  221. if (mode == kTPM_CombinedPwm)
  222. {
  223. uint16_t cnvFirstEdge;
  224. /* This check is added for combined mode as the channel number should be the pair number */
  225. if (chnlParams->chnlNumber >= (FSL_FEATURE_TPM_CHANNEL_COUNTn(base) / 2))
  226. {
  227. return kStatus_Fail;
  228. }
  229. /* Return error if requested value is greater than the max allowed */
  230. if (chnlParams->firstEdgeDelayPercent > 100)
  231. {
  232. return kStatus_Fail;
  233. }
  234. /* Configure delay of the first edge */
  235. if (chnlParams->firstEdgeDelayPercent == 0)
  236. {
  237. /* No delay for the first edge */
  238. cnvFirstEdge = 0;
  239. }
  240. else
  241. {
  242. cnvFirstEdge = (mod * chnlParams->firstEdgeDelayPercent) / 100;
  243. }
  244. /* Configure dutycycle */
  245. if (chnlParams->dutyCyclePercent == 0)
  246. {
  247. /* Signal stays low */
  248. cnv = 0;
  249. cnvFirstEdge = 0;
  250. }
  251. else
  252. {
  253. cnv = (mod * chnlParams->dutyCyclePercent) / 100;
  254. /* For 100% duty cycle */
  255. if (cnv >= mod)
  256. {
  257. cnv = mod + 1;
  258. }
  259. }
  260. /* Set the combine bit for the channel pair */
  261. base->COMBINE |= (1U << (TPM_COMBINE_COMBINE0_SHIFT + (TPM_COMBINE_SHIFT * chnlParams->chnlNumber)));
  262. /* When switching mode, disable channel n first */
  263. base->CONTROLS[chnlParams->chnlNumber * 2].CnSC &=
  264. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  265. /* Wait till mode change to disable channel is acknowledged */
  266. while ((base->CONTROLS[chnlParams->chnlNumber * 2].CnSC &
  267. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  268. {
  269. }
  270. /* Set the requested PWM mode for channel n, PWM output requires mode select to be set to 2 */
  271. base->CONTROLS[chnlParams->chnlNumber * 2].CnSC |=
  272. ((chnlParams->level << TPM_CnSC_ELSA_SHIFT) | (2U << TPM_CnSC_MSA_SHIFT));
  273. /* Wait till mode change is acknowledged */
  274. while (!(base->CONTROLS[chnlParams->chnlNumber * 2].CnSC &
  275. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  276. {
  277. }
  278. /* Set the channel pair values */
  279. base->CONTROLS[chnlParams->chnlNumber * 2].CnV = cnvFirstEdge;
  280. /* When switching mode, disable channel n + 1 first */
  281. base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnSC &=
  282. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  283. /* Wait till mode change to disable channel is acknowledged */
  284. while ((base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnSC &
  285. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  286. {
  287. }
  288. /* Set the requested PWM mode for channel n + 1, PWM output requires mode select to be set to 2 */
  289. base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnSC |=
  290. ((chnlParams->level << TPM_CnSC_ELSA_SHIFT) | (2U << TPM_CnSC_MSA_SHIFT));
  291. /* Wait till mode change is acknowledged */
  292. while (!(base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnSC &
  293. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  294. {
  295. }
  296. /* Set the channel pair values */
  297. base->CONTROLS[(chnlParams->chnlNumber * 2) + 1].CnV = cnvFirstEdge + cnv;
  298. }
  299. else
  300. {
  301. #endif
  302. if (chnlParams->dutyCyclePercent == 0)
  303. {
  304. /* Signal stays low */
  305. cnv = 0;
  306. }
  307. else
  308. {
  309. cnv = (mod * chnlParams->dutyCyclePercent) / 100;
  310. /* For 100% duty cycle */
  311. if (cnv >= mod)
  312. {
  313. cnv = mod + 1;
  314. }
  315. }
  316. /* When switching mode, disable channel first */
  317. base->CONTROLS[chnlParams->chnlNumber].CnSC &=
  318. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  319. /* Wait till mode change to disable channel is acknowledged */
  320. while ((base->CONTROLS[chnlParams->chnlNumber].CnSC &
  321. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  322. {
  323. }
  324. /* Set the requested PWM mode, PWM output requires mode select to be set to 2 */
  325. base->CONTROLS[chnlParams->chnlNumber].CnSC |=
  326. ((chnlParams->level << TPM_CnSC_ELSA_SHIFT) | (2U << TPM_CnSC_MSA_SHIFT));
  327. /* Wait till mode change is acknowledged */
  328. while (!(base->CONTROLS[chnlParams->chnlNumber].CnSC &
  329. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  330. {
  331. }
  332. base->CONTROLS[chnlParams->chnlNumber].CnV = cnv;
  333. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  334. }
  335. #endif
  336. chnlParams++;
  337. }
  338. return kStatus_Success;
  339. }
  340. void TPM_UpdatePwmDutycycle(TPM_Type *base,
  341. tpm_chnl_t chnlNumber,
  342. tpm_pwm_mode_t currentPwmMode,
  343. uint8_t dutyCyclePercent)
  344. {
  345. assert(chnlNumber < FSL_FEATURE_TPM_CHANNEL_COUNTn(base));
  346. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  347. if(currentPwmMode == kTPM_CombinedPwm)
  348. {
  349. assert(FSL_FEATURE_TPM_COMBINE_HAS_EFFECTn(base));
  350. }
  351. #endif
  352. uint16_t cnv, mod;
  353. mod = base->MOD;
  354. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  355. if (currentPwmMode == kTPM_CombinedPwm)
  356. {
  357. uint16_t cnvFirstEdge;
  358. /* This check is added for combined mode as the channel number should be the pair number */
  359. if (chnlNumber >= (FSL_FEATURE_TPM_CHANNEL_COUNTn(base) / 2))
  360. {
  361. return;
  362. }
  363. cnv = (mod * dutyCyclePercent) / 100;
  364. cnvFirstEdge = base->CONTROLS[chnlNumber * 2].CnV;
  365. /* For 100% duty cycle */
  366. if (cnv >= mod)
  367. {
  368. cnv = mod + 1;
  369. }
  370. base->CONTROLS[(chnlNumber * 2) + 1].CnV = cnvFirstEdge + cnv;
  371. }
  372. else
  373. {
  374. #endif
  375. cnv = (mod * dutyCyclePercent) / 100;
  376. /* For 100% duty cycle */
  377. if (cnv >= mod)
  378. {
  379. cnv = mod + 1;
  380. }
  381. base->CONTROLS[chnlNumber].CnV = cnv;
  382. #if defined(FSL_FEATURE_TPM_WAIT_CnV_REGISTER_UPDATE) && FSL_FEATURE_TPM_WAIT_CnV_REGISTER_UPDATE
  383. while(!(cnv == base->CONTROLS[chnlNumber].CnV))
  384. {
  385. }
  386. #endif
  387. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  388. }
  389. #endif
  390. }
  391. void TPM_UpdateChnlEdgeLevelSelect(TPM_Type *base, tpm_chnl_t chnlNumber, uint8_t level)
  392. {
  393. assert(chnlNumber < FSL_FEATURE_TPM_CHANNEL_COUNTn(base));
  394. uint32_t reg = base->CONTROLS[chnlNumber].CnSC
  395. #if !(defined(FSL_FEATURE_TPM_CnSC_CHF_WRITE_0_CLEAR) && FSL_FEATURE_TPM_CnSC_CHF_WRITE_0_CLEAR)
  396. & ~(TPM_CnSC_CHF_MASK)
  397. #endif
  398. ;
  399. /* When switching mode, disable channel first */
  400. base->CONTROLS[chnlNumber].CnSC &=
  401. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  402. /* Wait till mode change to disable channel is acknowledged */
  403. while ((base->CONTROLS[chnlNumber].CnSC &
  404. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  405. {
  406. }
  407. /* Clear the field and write the new level value */
  408. reg &= ~(TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  409. reg |= ((uint32_t)level << TPM_CnSC_ELSA_SHIFT) & (TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  410. base->CONTROLS[chnlNumber].CnSC = reg;
  411. /* Wait till mode change is acknowledged */
  412. reg &= (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  413. while (reg != (base->CONTROLS[chnlNumber].CnSC &
  414. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  415. {
  416. }
  417. }
  418. void TPM_SetupInputCapture(TPM_Type *base, tpm_chnl_t chnlNumber, tpm_input_capture_edge_t captureMode)
  419. {
  420. assert(chnlNumber < FSL_FEATURE_TPM_CHANNEL_COUNTn(base));
  421. #if defined(FSL_FEATURE_TPM_HAS_QDCTRL) && FSL_FEATURE_TPM_HAS_QDCTRL
  422. /* The TPM's QDCTRL register required to be effective */
  423. if( FSL_FEATURE_TPM_QDCTRL_HAS_EFFECTn(base) )
  424. {
  425. /* Clear quadrature Decoder mode for channel 0 or 1*/
  426. if ((chnlNumber == 0) || (chnlNumber == 1))
  427. {
  428. base->QDCTRL &= ~TPM_QDCTRL_QUADEN_MASK;
  429. }
  430. }
  431. #endif
  432. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  433. /* The TPM's COMBINE register required to be effective */
  434. if( FSL_FEATURE_TPM_COMBINE_HAS_EFFECTn(base) )
  435. {
  436. /* Clear the combine bit for chnlNumber */
  437. base->COMBINE &= ~(1U << TPM_COMBINE_SHIFT * (chnlNumber / 2));
  438. }
  439. #endif
  440. /* When switching mode, disable channel first */
  441. base->CONTROLS[chnlNumber].CnSC &=
  442. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  443. /* Wait till mode change to disable channel is acknowledged */
  444. while ((base->CONTROLS[chnlNumber].CnSC &
  445. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  446. {
  447. }
  448. /* Set the requested input capture mode */
  449. base->CONTROLS[chnlNumber].CnSC |= captureMode;
  450. /* Wait till mode change is acknowledged */
  451. while (!(base->CONTROLS[chnlNumber].CnSC &
  452. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  453. {
  454. }
  455. }
  456. void TPM_SetupOutputCompare(TPM_Type *base,
  457. tpm_chnl_t chnlNumber,
  458. tpm_output_compare_mode_t compareMode,
  459. uint32_t compareValue)
  460. {
  461. assert(chnlNumber < FSL_FEATURE_TPM_CHANNEL_COUNTn(base));
  462. #if defined(FSL_FEATURE_TPM_HAS_QDCTRL) && FSL_FEATURE_TPM_HAS_QDCTRL
  463. /* The TPM's QDCTRL register required to be effective */
  464. if( FSL_FEATURE_TPM_QDCTRL_HAS_EFFECTn(base) )
  465. {
  466. /* Clear quadrature Decoder mode for channel 0 or 1 */
  467. if ((chnlNumber == 0) || (chnlNumber == 1))
  468. {
  469. base->QDCTRL &= ~TPM_QDCTRL_QUADEN_MASK;
  470. }
  471. }
  472. #endif
  473. /* When switching mode, disable channel first */
  474. base->CONTROLS[chnlNumber].CnSC &=
  475. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  476. /* Wait till mode change to disable channel is acknowledged */
  477. while ((base->CONTROLS[chnlNumber].CnSC &
  478. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  479. {
  480. }
  481. /* Setup the channel output behaviour when a match occurs with the compare value */
  482. base->CONTROLS[chnlNumber].CnSC |= compareMode;
  483. /* Setup the compare value */
  484. base->CONTROLS[chnlNumber].CnV = compareValue;
  485. /* Wait till mode change is acknowledged */
  486. while (!(base->CONTROLS[chnlNumber].CnSC &
  487. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  488. {
  489. }
  490. }
  491. #if defined(FSL_FEATURE_TPM_HAS_COMBINE) && FSL_FEATURE_TPM_HAS_COMBINE
  492. void TPM_SetupDualEdgeCapture(TPM_Type *base,
  493. tpm_chnl_t chnlPairNumber,
  494. const tpm_dual_edge_capture_param_t *edgeParam,
  495. uint32_t filterValue)
  496. {
  497. assert(edgeParam);
  498. assert(chnlPairNumber < FSL_FEATURE_TPM_CHANNEL_COUNTn(base) / 2);
  499. assert(FSL_FEATURE_TPM_COMBINE_HAS_EFFECTn(base));
  500. uint32_t reg;
  501. #if defined(FSL_FEATURE_TPM_HAS_QDCTRL) && FSL_FEATURE_TPM_HAS_QDCTRL
  502. /* The TPM's QDCTRL register required to be effective */
  503. if( FSL_FEATURE_TPM_QDCTRL_HAS_EFFECTn(base) )
  504. {
  505. /* Clear quadrature Decoder mode for channel 0 or 1*/
  506. if (chnlPairNumber == 0)
  507. {
  508. base->QDCTRL &= ~TPM_QDCTRL_QUADEN_MASK;
  509. }
  510. }
  511. #endif
  512. /* Unlock: When switching mode, disable channel first */
  513. base->CONTROLS[chnlPairNumber * 2].CnSC &=
  514. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  515. /* Wait till mode change to disable channel is acknowledged */
  516. while ((base->CONTROLS[chnlPairNumber * 2].CnSC &
  517. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  518. {
  519. }
  520. base->CONTROLS[chnlPairNumber * 2 + 1].CnSC &=
  521. ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  522. /* Wait till mode change to disable channel is acknowledged */
  523. while ((base->CONTROLS[chnlPairNumber * 2 + 1].CnSC &
  524. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  525. {
  526. }
  527. /* Now, the registers for input mode can be operated. */
  528. if (edgeParam->enableSwap)
  529. {
  530. /* Set the combine and swap bits for the channel pair */
  531. base->COMBINE |= (TPM_COMBINE_COMBINE0_MASK | TPM_COMBINE_COMSWAP0_MASK)
  532. << (TPM_COMBINE_SHIFT * chnlPairNumber);
  533. /* Input filter setup for channel n+1 input */
  534. reg = base->FILTER;
  535. reg &= ~(TPM_FILTER_CH0FVAL_MASK << (TPM_FILTER_CH1FVAL_SHIFT * (chnlPairNumber + 1)));
  536. reg |= (filterValue << (TPM_FILTER_CH1FVAL_SHIFT * (chnlPairNumber + 1)));
  537. base->FILTER = reg;
  538. }
  539. else
  540. {
  541. reg = base->COMBINE;
  542. /* Clear the swap bit for the channel pair */
  543. reg &= ~(TPM_COMBINE_COMSWAP0_MASK << (TPM_COMBINE_COMSWAP0_SHIFT * chnlPairNumber));
  544. /* Set the combine bit for the channel pair */
  545. reg |= TPM_COMBINE_COMBINE0_MASK << (TPM_COMBINE_SHIFT * chnlPairNumber);
  546. base->COMBINE = reg;
  547. /* Input filter setup for channel n input */
  548. reg = base->FILTER;
  549. reg &= ~(TPM_FILTER_CH0FVAL_MASK << (TPM_FILTER_CH1FVAL_SHIFT * chnlPairNumber));
  550. reg |= (filterValue << (TPM_FILTER_CH1FVAL_SHIFT * chnlPairNumber));
  551. base->FILTER = reg;
  552. }
  553. /* Setup the edge detection from channel n */
  554. base->CONTROLS[chnlPairNumber * 2].CnSC |= edgeParam->currChanEdgeMode;
  555. /* Wait till mode change is acknowledged */
  556. while (!(base->CONTROLS[chnlPairNumber * 2].CnSC &
  557. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  558. {
  559. }
  560. /* Setup the edge detection from channel n+1 */
  561. base->CONTROLS[(chnlPairNumber * 2) + 1].CnSC |= edgeParam->nextChanEdgeMode;
  562. /* Wait till mode change is acknowledged */
  563. while (!(base->CONTROLS[(chnlPairNumber * 2) + 1].CnSC &
  564. (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  565. {
  566. }
  567. }
  568. #endif
  569. #if defined(FSL_FEATURE_TPM_HAS_QDCTRL) && FSL_FEATURE_TPM_HAS_QDCTRL
  570. void TPM_SetupQuadDecode(TPM_Type *base,
  571. const tpm_phase_params_t *phaseAParams,
  572. const tpm_phase_params_t *phaseBParams,
  573. tpm_quad_decode_mode_t quadMode)
  574. {
  575. assert(phaseAParams);
  576. assert(phaseBParams);
  577. assert(FSL_FEATURE_TPM_QDCTRL_HAS_EFFECTn(base));
  578. base->CONTROLS[0].CnSC &= ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  579. /* Wait till mode change to disable channel is acknowledged */
  580. while ((base->CONTROLS[0].CnSC & (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  581. {
  582. }
  583. uint32_t reg;
  584. /* Set Phase A filter value */
  585. reg = base->FILTER;
  586. reg &= ~(TPM_FILTER_CH0FVAL_MASK);
  587. reg |= TPM_FILTER_CH0FVAL(phaseAParams->phaseFilterVal);
  588. base->FILTER = reg;
  589. #if defined(FSL_FEATURE_TPM_HAS_POL) && FSL_FEATURE_TPM_HAS_POL
  590. /* Set Phase A polarity */
  591. if (phaseAParams->phasePolarity)
  592. {
  593. base->POL |= TPM_POL_POL0_MASK;
  594. }
  595. else
  596. {
  597. base->POL &= ~TPM_POL_POL0_MASK;
  598. }
  599. #endif
  600. base->CONTROLS[1].CnSC &= ~(TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK);
  601. /* Wait till mode change to disable channel is acknowledged */
  602. while ((base->CONTROLS[1].CnSC & (TPM_CnSC_MSA_MASK | TPM_CnSC_MSB_MASK | TPM_CnSC_ELSA_MASK | TPM_CnSC_ELSB_MASK)))
  603. {
  604. }
  605. /* Set Phase B filter value */
  606. reg = base->FILTER;
  607. reg &= ~(TPM_FILTER_CH1FVAL_MASK);
  608. reg |= TPM_FILTER_CH1FVAL(phaseBParams->phaseFilterVal);
  609. base->FILTER = reg;
  610. #if defined(FSL_FEATURE_TPM_HAS_POL) && FSL_FEATURE_TPM_HAS_POL
  611. /* Set Phase B polarity */
  612. if (phaseBParams->phasePolarity)
  613. {
  614. base->POL |= TPM_POL_POL1_MASK;
  615. }
  616. else
  617. {
  618. base->POL &= ~TPM_POL_POL1_MASK;
  619. }
  620. #endif
  621. /* Set Quadrature mode */
  622. reg = base->QDCTRL;
  623. reg &= ~(TPM_QDCTRL_QUADMODE_MASK);
  624. reg |= TPM_QDCTRL_QUADMODE(quadMode);
  625. base->QDCTRL = reg;
  626. /* Enable Quad decode */
  627. base->QDCTRL |= TPM_QDCTRL_QUADEN_MASK;
  628. }
  629. #endif
  630. void TPM_EnableInterrupts(TPM_Type *base, uint32_t mask)
  631. {
  632. uint32_t chnlInterrupts = (mask & 0xFF);
  633. uint8_t chnlNumber = 0;
  634. /* Enable the timer overflow interrupt */
  635. if (mask & kTPM_TimeOverflowInterruptEnable)
  636. {
  637. base->SC |= TPM_SC_TOIE_MASK;
  638. }
  639. /* Enable the channel interrupts */
  640. while (chnlInterrupts)
  641. {
  642. if (chnlInterrupts & 0x1)
  643. {
  644. base->CONTROLS[chnlNumber].CnSC |= TPM_CnSC_CHIE_MASK;
  645. }
  646. chnlNumber++;
  647. chnlInterrupts = chnlInterrupts >> 1U;
  648. }
  649. }
  650. void TPM_DisableInterrupts(TPM_Type *base, uint32_t mask)
  651. {
  652. uint32_t chnlInterrupts = (mask & 0xFF);
  653. uint8_t chnlNumber = 0;
  654. /* Disable the timer overflow interrupt */
  655. if (mask & kTPM_TimeOverflowInterruptEnable)
  656. {
  657. base->SC &= ~TPM_SC_TOIE_MASK;
  658. }
  659. /* Disable the channel interrupts */
  660. while (chnlInterrupts)
  661. {
  662. if (chnlInterrupts & 0x1)
  663. {
  664. base->CONTROLS[chnlNumber].CnSC &= ~TPM_CnSC_CHIE_MASK;
  665. }
  666. chnlNumber++;
  667. chnlInterrupts = chnlInterrupts >> 1U;
  668. }
  669. }
  670. uint32_t TPM_GetEnabledInterrupts(TPM_Type *base)
  671. {
  672. uint32_t enabledInterrupts = 0;
  673. int8_t chnlCount = FSL_FEATURE_TPM_CHANNEL_COUNTn(base);
  674. /* The CHANNEL_COUNT macro returns -1 if it cannot match the TPM instance */
  675. assert(chnlCount != -1);
  676. /* Check if timer overflow interrupt is enabled */
  677. if (base->SC & TPM_SC_TOIE_MASK)
  678. {
  679. enabledInterrupts |= kTPM_TimeOverflowInterruptEnable;
  680. }
  681. /* Check if the channel interrupts are enabled */
  682. while (chnlCount > 0)
  683. {
  684. chnlCount--;
  685. if (base->CONTROLS[chnlCount].CnSC & TPM_CnSC_CHIE_MASK)
  686. {
  687. enabledInterrupts |= (1U << chnlCount);
  688. }
  689. }
  690. return enabledInterrupts;
  691. }