fsl_pwm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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_pwm.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.pwm"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Get the instance from the base address
  44. *
  45. * @param base PWM peripheral base address
  46. *
  47. * @return The PWM module instance
  48. */
  49. static uint32_t PWM_GetInstance(PWM_Type *base);
  50. /*******************************************************************************
  51. * Variables
  52. ******************************************************************************/
  53. /*! @brief Pointers to PWM bases for each instance. */
  54. static PWM_Type *const s_pwmBases[] = PWM_BASE_PTRS;
  55. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  56. /*! @brief Pointers to PWM clocks for each PWM submodule. */
  57. static const clock_ip_name_t s_pwmClocks[][FSL_FEATURE_PWM_SUBMODULE_COUNT] = PWM_CLOCKS;
  58. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  59. /*******************************************************************************
  60. * Code
  61. ******************************************************************************/
  62. static uint32_t PWM_GetInstance(PWM_Type *base)
  63. {
  64. uint32_t instance;
  65. /* Find the instance index from base address mappings. */
  66. for (instance = 0; instance < ARRAY_SIZE(s_pwmBases); instance++)
  67. {
  68. if (s_pwmBases[instance] == base)
  69. {
  70. break;
  71. }
  72. }
  73. assert(instance < ARRAY_SIZE(s_pwmBases));
  74. return instance;
  75. }
  76. status_t PWM_Init(PWM_Type *base, pwm_submodule_t subModule, const pwm_config_t *config)
  77. {
  78. assert(config);
  79. uint16_t reg;
  80. /* Source clock for submodule 0 cannot be itself */
  81. if ((config->clockSource == kPWM_Submodule0Clock) && (subModule == kPWM_Module_0))
  82. {
  83. return kStatus_Fail;
  84. }
  85. /* Reload source select clock for submodule 0 cannot be master reload */
  86. if ((config->reloadSelect == kPWM_MasterReload) && (subModule == kPWM_Module_0))
  87. {
  88. return kStatus_Fail;
  89. }
  90. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  91. /* Ungate the PWM submodule clock*/
  92. CLOCK_EnableClock(s_pwmClocks[PWM_GetInstance(base)][subModule]);
  93. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  94. /* Clear the fault status flags */
  95. base->FSTS |= PWM_FSTS_FFLAG_MASK;
  96. reg = base->SM[subModule].CTRL2;
  97. /* Setup the submodule clock-source, control source of the INIT signal,
  98. * source of the force output signal, operation in debug & wait modes and reload source select
  99. */
  100. reg &= ~(PWM_CTRL2_CLK_SEL_MASK | PWM_CTRL2_FORCE_SEL_MASK | PWM_CTRL2_INIT_SEL_MASK | PWM_CTRL2_INDEP_MASK |
  101. PWM_CTRL2_WAITEN_MASK | PWM_CTRL2_DBGEN_MASK | PWM_CTRL2_RELOAD_SEL_MASK);
  102. reg |= (PWM_CTRL2_CLK_SEL(config->clockSource) | PWM_CTRL2_FORCE_SEL(config->forceTrigger) |
  103. PWM_CTRL2_INIT_SEL(config->initializationControl) | PWM_CTRL2_DBGEN(config->enableDebugMode) |
  104. PWM_CTRL2_WAITEN(config->enableWait) | PWM_CTRL2_RELOAD_SEL(config->reloadSelect));
  105. /* Setup PWM A & B to be independent or a complementary-pair */
  106. switch (config->pairOperation)
  107. {
  108. case kPWM_Independent:
  109. reg |= PWM_CTRL2_INDEP_MASK;
  110. break;
  111. case kPWM_ComplementaryPwmA:
  112. base->MCTRL &= ~(1U << (PWM_MCTRL_IPOL_SHIFT + subModule));
  113. break;
  114. case kPWM_ComplementaryPwmB:
  115. base->MCTRL |= (1U << (PWM_MCTRL_IPOL_SHIFT + subModule));
  116. break;
  117. default:
  118. break;
  119. }
  120. base->SM[subModule].CTRL2 = reg;
  121. reg = base->SM[subModule].CTRL;
  122. /* Setup the clock prescale, load mode and frequency */
  123. reg &= ~(PWM_CTRL_PRSC_MASK | PWM_CTRL_LDFQ_MASK | PWM_CTRL_LDMOD_MASK);
  124. reg |= (PWM_CTRL_PRSC(config->prescale) | PWM_CTRL_LDFQ(config->reloadFrequency));
  125. /* Setup register reload logic */
  126. switch (config->reloadLogic)
  127. {
  128. case kPWM_ReloadImmediate:
  129. reg |= PWM_CTRL_LDMOD_MASK;
  130. break;
  131. case kPWM_ReloadPwmHalfCycle:
  132. reg |= PWM_CTRL_HALF_MASK;
  133. reg &= ~PWM_CTRL_FULL_MASK;
  134. break;
  135. case kPWM_ReloadPwmFullCycle:
  136. reg &= ~PWM_CTRL_HALF_MASK;
  137. reg |= PWM_CTRL_FULL_MASK;
  138. break;
  139. case kPWM_ReloadPwmHalfAndFullCycle:
  140. reg |= PWM_CTRL_HALF_MASK;
  141. reg |= PWM_CTRL_FULL_MASK;
  142. break;
  143. default:
  144. break;
  145. }
  146. base->SM[subModule].CTRL = reg;
  147. /* Setup the fault filter */
  148. if (base->FFILT & PWM_FFILT_FILT_PER_MASK)
  149. {
  150. /* When changing values for fault period from a non-zero value, first write a value of 0
  151. * to clear the filter
  152. */
  153. base->FFILT &= ~(PWM_FFILT_FILT_PER_MASK);
  154. }
  155. base->FFILT = (PWM_FFILT_FILT_CNT(config->faultFilterCount) | PWM_FFILT_FILT_PER(config->faultFilterPeriod));
  156. /* Issue a Force trigger event when configured to trigger locally */
  157. if (config->forceTrigger == kPWM_Force_Local)
  158. {
  159. base->SM[subModule].CTRL2 |= PWM_CTRL2_FORCE(1U);
  160. }
  161. return kStatus_Success;
  162. }
  163. void PWM_Deinit(PWM_Type *base, pwm_submodule_t subModule)
  164. {
  165. /* Stop the submodule */
  166. base->MCTRL &= ~(1U << (PWM_MCTRL_RUN_SHIFT + subModule));
  167. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  168. /* Gate the PWM submodule clock*/
  169. CLOCK_DisableClock(s_pwmClocks[PWM_GetInstance(base)][subModule]);
  170. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  171. }
  172. void PWM_GetDefaultConfig(pwm_config_t *config)
  173. {
  174. assert(config);
  175. /* PWM is paused in debug mode */
  176. config->enableDebugMode = false;
  177. /* PWM is paused in wait mode */
  178. config->enableWait = false;
  179. /* PWM module uses the local reload signal to reload registers */
  180. config->reloadSelect = kPWM_LocalReload;
  181. /* Fault filter count is set to 0 */
  182. config->faultFilterCount = 0;
  183. /* Fault filter period is set to 0 which disables the fault filter */
  184. config->faultFilterPeriod = 0;
  185. /* Use the IP Bus clock as source clock for the PWM submodule */
  186. config->clockSource = kPWM_BusClock;
  187. /* Clock source prescale is set to divide by 1*/
  188. config->prescale = kPWM_Prescale_Divide_1;
  189. /* Local sync causes initialization */
  190. config->initializationControl = kPWM_Initialize_LocalSync;
  191. /* The local force signal, CTRL2[FORCE], from the submodule is used to force updates */
  192. config->forceTrigger = kPWM_Force_Local;
  193. /* PWM reload frequency, reload opportunity is PWM half cycle or full cycle.
  194. * This field is not used in Immediate reload mode
  195. */
  196. config->reloadFrequency = kPWM_LoadEveryOportunity;
  197. /* Buffered-registers get loaded with new values as soon as LDOK bit is set */
  198. config->reloadLogic = kPWM_ReloadImmediate;
  199. /* PWM A & PWM B operate as 2 independent channels */
  200. config->pairOperation = kPWM_Independent;
  201. }
  202. status_t PWM_SetupPwm(PWM_Type *base,
  203. pwm_submodule_t subModule,
  204. const pwm_signal_param_t *chnlParams,
  205. uint8_t numOfChnls,
  206. pwm_mode_t mode,
  207. uint32_t pwmFreq_Hz,
  208. uint32_t srcClock_Hz)
  209. {
  210. assert(chnlParams);
  211. assert(pwmFreq_Hz);
  212. assert(numOfChnls);
  213. assert(srcClock_Hz);
  214. uint32_t pwmClock;
  215. uint16_t pulseCnt = 0, pwmHighPulse = 0;
  216. int16_t modulo = 0;
  217. uint8_t i, polarityShift = 0, outputEnableShift = 0;
  218. if (numOfChnls > 2)
  219. {
  220. /* Each submodule has 2 signals; PWM A & PWM B */
  221. return kStatus_Fail;
  222. }
  223. /* Divide the clock by the prescale value */
  224. pwmClock = (srcClock_Hz / (1U << ((base->SM[subModule].CTRL & PWM_CTRL_PRSC_MASK) >> PWM_CTRL_PRSC_SHIFT)));
  225. pulseCnt = (pwmClock / pwmFreq_Hz);
  226. /* Setup each PWM channel */
  227. for (i = 0; i < numOfChnls; i++)
  228. {
  229. /* Calculate pulse width */
  230. pwmHighPulse = (pulseCnt * chnlParams->dutyCyclePercent) / 100;
  231. /* Setup the different match registers to generate the PWM signal */
  232. switch (mode)
  233. {
  234. case kPWM_SignedCenterAligned:
  235. /* Setup the PWM period for a signed center aligned signal */
  236. modulo = pulseCnt >> 1;
  237. /* Indicates the start of the PWM period */
  238. base->SM[subModule].INIT = (-modulo);
  239. /* Indicates the center value */
  240. base->SM[subModule].VAL0 = 0;
  241. /* Indicates the end of the PWM period */
  242. base->SM[subModule].VAL1 = modulo;
  243. /* Setup the PWM dutycycle */
  244. if (chnlParams->pwmChannel == kPWM_PwmA)
  245. {
  246. base->SM[subModule].VAL2 = (-(pwmHighPulse / 2));
  247. base->SM[subModule].VAL3 = (pwmHighPulse / 2);
  248. }
  249. else
  250. {
  251. base->SM[subModule].VAL4 = (-(pwmHighPulse / 2));
  252. base->SM[subModule].VAL5 = (pwmHighPulse / 2);
  253. }
  254. break;
  255. case kPWM_CenterAligned:
  256. /* Setup the PWM period for an unsigned center aligned signal */
  257. /* Indicates the start of the PWM period */
  258. base->SM[subModule].INIT = 0;
  259. /* Indicates the center value */
  260. base->SM[subModule].VAL0 = (pulseCnt / 2);
  261. /* Indicates the end of the PWM period */
  262. base->SM[subModule].VAL1 = pulseCnt;
  263. /* Setup the PWM dutycycle */
  264. if (chnlParams->pwmChannel == kPWM_PwmA)
  265. {
  266. base->SM[subModule].VAL2 = ((pulseCnt - pwmHighPulse) / 2);
  267. base->SM[subModule].VAL3 = ((pulseCnt + pwmHighPulse) / 2);
  268. }
  269. else
  270. {
  271. base->SM[subModule].VAL4 = ((pulseCnt - pwmHighPulse) / 2);
  272. base->SM[subModule].VAL5 = ((pulseCnt + pwmHighPulse) / 2);
  273. }
  274. break;
  275. case kPWM_SignedEdgeAligned:
  276. /* Setup the PWM period for a signed edge aligned signal */
  277. modulo = pulseCnt >> 1;
  278. /* Indicates the start of the PWM period */
  279. base->SM[subModule].INIT = (-modulo);
  280. /* Indicates the center value */
  281. base->SM[subModule].VAL0 = 0;
  282. /* Indicates the end of the PWM period */
  283. base->SM[subModule].VAL1 = modulo;
  284. /* Setup the PWM dutycycle */
  285. if (chnlParams->pwmChannel == kPWM_PwmA)
  286. {
  287. base->SM[subModule].VAL2 = (-modulo);
  288. base->SM[subModule].VAL3 = (-modulo + pwmHighPulse);
  289. }
  290. else
  291. {
  292. base->SM[subModule].VAL4 = (-modulo);
  293. base->SM[subModule].VAL5 = (-modulo + pwmHighPulse);
  294. }
  295. break;
  296. case kPWM_EdgeAligned:
  297. /* Setup the PWM period for a unsigned edge aligned signal */
  298. /* Indicates the start of the PWM period */
  299. base->SM[subModule].INIT = 0;
  300. /* Indicates the center value */
  301. base->SM[subModule].VAL0 = (pulseCnt / 2);
  302. /* Indicates the end of the PWM period */
  303. base->SM[subModule].VAL1 = pulseCnt;
  304. /* Setup the PWM dutycycle */
  305. if (chnlParams->pwmChannel == kPWM_PwmA)
  306. {
  307. base->SM[subModule].VAL2 = 0;
  308. base->SM[subModule].VAL3 = pwmHighPulse;
  309. }
  310. else
  311. {
  312. base->SM[subModule].VAL4 = 0;
  313. base->SM[subModule].VAL5 = pwmHighPulse;
  314. }
  315. break;
  316. default:
  317. break;
  318. }
  319. /* Setup register shift values based on the channel being configured.
  320. * Also setup the deadtime value
  321. */
  322. if (chnlParams->pwmChannel == kPWM_PwmA)
  323. {
  324. polarityShift = PWM_OCTRL_POLA_SHIFT;
  325. outputEnableShift = PWM_OUTEN_PWMA_EN_SHIFT;
  326. base->SM[subModule].DTCNT0 = PWM_DTCNT0_DTCNT0(chnlParams->deadtimeValue);
  327. }
  328. else
  329. {
  330. polarityShift = PWM_OCTRL_POLB_SHIFT;
  331. outputEnableShift = PWM_OUTEN_PWMB_EN_SHIFT;
  332. base->SM[subModule].DTCNT1 = PWM_DTCNT1_DTCNT1(chnlParams->deadtimeValue);
  333. }
  334. /* Setup signal active level */
  335. if (chnlParams->level == kPWM_HighTrue)
  336. {
  337. base->SM[subModule].OCTRL &= ~(1U << polarityShift);
  338. }
  339. else
  340. {
  341. base->SM[subModule].OCTRL |= (1U << polarityShift);
  342. }
  343. /* Enable PWM output */
  344. base->OUTEN |= (1U << (outputEnableShift + subModule));
  345. /* Get the next channel parameters */
  346. chnlParams++;
  347. }
  348. return kStatus_Success;
  349. }
  350. void PWM_UpdatePwmDutycycle(PWM_Type *base,
  351. pwm_submodule_t subModule,
  352. pwm_channels_t pwmSignal,
  353. pwm_mode_t currPwmMode,
  354. uint8_t dutyCyclePercent)
  355. {
  356. assert(dutyCyclePercent <= 100);
  357. assert(pwmSignal < 2);
  358. uint16_t pulseCnt = 0, pwmHighPulse = 0;
  359. int16_t modulo = 0;
  360. switch (currPwmMode)
  361. {
  362. case kPWM_SignedCenterAligned:
  363. modulo = base->SM[subModule].VAL1;
  364. pulseCnt = modulo * 2;
  365. /* Calculate pulse width */
  366. pwmHighPulse = (pulseCnt * dutyCyclePercent) / 100;
  367. /* Setup the PWM dutycycle */
  368. if (pwmSignal == kPWM_PwmA)
  369. {
  370. base->SM[subModule].VAL2 = (-(pwmHighPulse / 2));
  371. base->SM[subModule].VAL3 = (pwmHighPulse / 2);
  372. }
  373. else
  374. {
  375. base->SM[subModule].VAL4 = (-(pwmHighPulse / 2));
  376. base->SM[subModule].VAL5 = (pwmHighPulse / 2);
  377. }
  378. break;
  379. case kPWM_CenterAligned:
  380. pulseCnt = base->SM[subModule].VAL1;
  381. /* Calculate pulse width */
  382. pwmHighPulse = (pulseCnt * dutyCyclePercent) / 100;
  383. /* Setup the PWM dutycycle */
  384. if (pwmSignal == kPWM_PwmA)
  385. {
  386. base->SM[subModule].VAL2 = ((pulseCnt - pwmHighPulse) / 2);
  387. base->SM[subModule].VAL3 = ((pulseCnt + pwmHighPulse) / 2);
  388. }
  389. else
  390. {
  391. base->SM[subModule].VAL4 = ((pulseCnt - pwmHighPulse) / 2);
  392. base->SM[subModule].VAL5 = ((pulseCnt + pwmHighPulse) / 2);
  393. }
  394. break;
  395. case kPWM_SignedEdgeAligned:
  396. modulo = base->SM[subModule].VAL1;
  397. pulseCnt = modulo * 2;
  398. /* Calculate pulse width */
  399. pwmHighPulse = (pulseCnt * dutyCyclePercent) / 100;
  400. /* Setup the PWM dutycycle */
  401. if (pwmSignal == kPWM_PwmA)
  402. {
  403. base->SM[subModule].VAL2 = (-modulo);
  404. base->SM[subModule].VAL3 = (-modulo + pwmHighPulse);
  405. }
  406. else
  407. {
  408. base->SM[subModule].VAL4 = (-modulo);
  409. base->SM[subModule].VAL5 = (-modulo + pwmHighPulse);
  410. }
  411. break;
  412. case kPWM_EdgeAligned:
  413. pulseCnt = base->SM[subModule].VAL1;
  414. /* Calculate pulse width */
  415. pwmHighPulse = (pulseCnt * dutyCyclePercent) / 100;
  416. /* Setup the PWM dutycycle */
  417. if (pwmSignal == kPWM_PwmA)
  418. {
  419. base->SM[subModule].VAL2 = 0;
  420. base->SM[subModule].VAL3 = pwmHighPulse;
  421. }
  422. else
  423. {
  424. base->SM[subModule].VAL4 = 0;
  425. base->SM[subModule].VAL5 = pwmHighPulse;
  426. }
  427. break;
  428. default:
  429. break;
  430. }
  431. }
  432. void PWM_SetupInputCapture(PWM_Type *base,
  433. pwm_submodule_t subModule,
  434. pwm_channels_t pwmChannel,
  435. const pwm_input_capture_param_t *inputCaptureParams)
  436. {
  437. uint32_t reg = 0;
  438. switch (pwmChannel)
  439. {
  440. case kPWM_PwmA:
  441. /* Setup the capture paramters for PWM A pin */
  442. reg = (PWM_CAPTCTRLA_INP_SELA(inputCaptureParams->captureInputSel) |
  443. PWM_CAPTCTRLA_EDGA0(inputCaptureParams->edge0) | PWM_CAPTCTRLA_EDGA1(inputCaptureParams->edge1) |
  444. PWM_CAPTCTRLA_ONESHOTA(inputCaptureParams->enableOneShotCapture) |
  445. PWM_CAPTCTRLA_CFAWM(inputCaptureParams->fifoWatermark));
  446. /* Enable the edge counter if using the output edge counter */
  447. if (inputCaptureParams->captureInputSel)
  448. {
  449. reg |= PWM_CAPTCTRLA_EDGCNTA_EN_MASK;
  450. }
  451. /* Enable input capture operation */
  452. reg |= PWM_CAPTCTRLA_ARMA_MASK;
  453. base->SM[subModule].CAPTCTRLA = reg;
  454. /* Setup the compare value when using the edge counter as source */
  455. base->SM[subModule].CAPTCOMPA = PWM_CAPTCOMPA_EDGCMPA(inputCaptureParams->edgeCompareValue);
  456. /* Setup PWM A pin for input capture */
  457. base->OUTEN &= ~(1U << (PWM_OUTEN_PWMA_EN_SHIFT + subModule));
  458. break;
  459. case kPWM_PwmB:
  460. /* Setup the capture paramters for PWM B pin */
  461. reg = (PWM_CAPTCTRLB_INP_SELB(inputCaptureParams->captureInputSel) |
  462. PWM_CAPTCTRLB_EDGB0(inputCaptureParams->edge0) | PWM_CAPTCTRLB_EDGB1(inputCaptureParams->edge1) |
  463. PWM_CAPTCTRLB_ONESHOTB(inputCaptureParams->enableOneShotCapture) |
  464. PWM_CAPTCTRLB_CFBWM(inputCaptureParams->fifoWatermark));
  465. /* Enable the edge counter if using the output edge counter */
  466. if (inputCaptureParams->captureInputSel)
  467. {
  468. reg |= PWM_CAPTCTRLB_EDGCNTB_EN_MASK;
  469. }
  470. /* Enable input capture operation */
  471. reg |= PWM_CAPTCTRLB_ARMB_MASK;
  472. base->SM[subModule].CAPTCTRLB = reg;
  473. /* Setup the compare value when using the edge counter as source */
  474. base->SM[subModule].CAPTCOMPB = PWM_CAPTCOMPB_EDGCMPB(inputCaptureParams->edgeCompareValue);
  475. /* Setup PWM B pin for input capture */
  476. base->OUTEN &= ~(1U << (PWM_OUTEN_PWMB_EN_SHIFT + subModule));
  477. break;
  478. case kPWM_PwmX:
  479. reg = (PWM_CAPTCTRLX_INP_SELX(inputCaptureParams->captureInputSel) |
  480. PWM_CAPTCTRLX_EDGX0(inputCaptureParams->edge0) | PWM_CAPTCTRLX_EDGX1(inputCaptureParams->edge1) |
  481. PWM_CAPTCTRLX_ONESHOTX(inputCaptureParams->enableOneShotCapture) |
  482. PWM_CAPTCTRLX_CFXWM(inputCaptureParams->fifoWatermark));
  483. /* Enable the edge counter if using the output edge counter */
  484. if (inputCaptureParams->captureInputSel)
  485. {
  486. reg |= PWM_CAPTCTRLX_EDGCNTX_EN_MASK;
  487. }
  488. /* Enable input capture operation */
  489. reg |= PWM_CAPTCTRLX_ARMX_MASK;
  490. base->SM[subModule].CAPTCTRLX = reg;
  491. /* Setup the compare value when using the edge counter as source */
  492. base->SM[subModule].CAPTCOMPX = PWM_CAPTCOMPX_EDGCMPX(inputCaptureParams->edgeCompareValue);
  493. /* Setup PWM X pin for input capture */
  494. base->OUTEN &= ~(1U << (PWM_OUTEN_PWMX_EN_SHIFT + subModule));
  495. break;
  496. default:
  497. break;
  498. }
  499. }
  500. void PWM_SetupFaults(PWM_Type *base, pwm_fault_input_t faultNum, const pwm_fault_param_t *faultParams)
  501. {
  502. assert(faultParams);
  503. uint16_t reg;
  504. reg = base->FCTRL;
  505. /* Set the faults level-settting */
  506. if (faultParams->faultLevel)
  507. {
  508. reg |= (1U << (PWM_FCTRL_FLVL_SHIFT + faultNum));
  509. }
  510. else
  511. {
  512. reg &= ~(1U << (PWM_FCTRL_FLVL_SHIFT + faultNum));
  513. }
  514. /* Set the fault clearing mode */
  515. if (faultParams->faultClearingMode)
  516. {
  517. /* Use manual fault clearing */
  518. reg &= ~(1U << (PWM_FCTRL_FAUTO_SHIFT + faultNum));
  519. if (faultParams->faultClearingMode == kPWM_ManualSafety)
  520. {
  521. /* Use manual fault clearing with safety mode enabled */
  522. reg |= (1U << (PWM_FCTRL_FSAFE_SHIFT + faultNum));
  523. }
  524. else
  525. {
  526. /* Use manual fault clearing with safety mode disabled */
  527. reg &= ~(1U << (PWM_FCTRL_FSAFE_SHIFT + faultNum));
  528. }
  529. }
  530. else
  531. {
  532. /* Use automatic fault clearing */
  533. reg |= (1U << (PWM_FCTRL_FAUTO_SHIFT + faultNum));
  534. }
  535. base->FCTRL = reg;
  536. /* Set the combinational path option */
  537. if (faultParams->enableCombinationalPath)
  538. {
  539. /* Combinational path from the fault input to the PWM output is available */
  540. base->FCTRL2 &= ~(1U << faultNum);
  541. }
  542. else
  543. {
  544. /* No combinational path available, only fault filter & latch signal can disable PWM output */
  545. base->FCTRL2 |= (1U << faultNum);
  546. }
  547. /* Initially clear both recovery modes */
  548. reg = base->FSTS;
  549. reg &= ~((1U << (PWM_FSTS_FFULL_SHIFT + faultNum)) | (1U << (PWM_FSTS_FHALF_SHIFT + faultNum)));
  550. /* Setup fault recovery */
  551. switch (faultParams->recoverMode)
  552. {
  553. case kPWM_NoRecovery:
  554. break;
  555. case kPWM_RecoverHalfCycle:
  556. reg |= (1U << (PWM_FSTS_FHALF_SHIFT + faultNum));
  557. break;
  558. case kPWM_RecoverFullCycle:
  559. reg |= (1U << (PWM_FSTS_FFULL_SHIFT + faultNum));
  560. break;
  561. case kPWM_RecoverHalfAndFullCycle:
  562. reg |= (1U << (PWM_FSTS_FHALF_SHIFT + faultNum));
  563. reg |= (1U << (PWM_FSTS_FFULL_SHIFT + faultNum));
  564. break;
  565. default:
  566. break;
  567. }
  568. base->FSTS = reg;
  569. }
  570. void PWM_SetupForceSignal(PWM_Type *base, pwm_submodule_t subModule, pwm_channels_t pwmChannel, pwm_force_signal_t mode)
  571. {
  572. uint16_t shift;
  573. uint16_t reg;
  574. /* DTSRCSEL register has 4 bits per submodule; 2 bits for PWM A and 2 bits for PWM B */
  575. shift = subModule * 4 + pwmChannel * 2;
  576. /* Setup the signal to be passed upon occurrence of a FORCE_OUT signal */
  577. reg = base->DTSRCSEL;
  578. reg &= ~(0x3U << shift);
  579. reg |= (uint16_t)((uint16_t)mode << shift);
  580. base->DTSRCSEL = reg;
  581. }
  582. void PWM_EnableInterrupts(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask)
  583. {
  584. /* Upper 16 bits are for related to the submodule */
  585. base->SM[subModule].INTEN |= (mask & 0xFFFFU);
  586. /* Fault related interrupts */
  587. base->FCTRL |= ((mask >> 16U) & PWM_FCTRL_FIE_MASK);
  588. }
  589. void PWM_DisableInterrupts(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask)
  590. {
  591. base->SM[subModule].INTEN &= ~(mask & 0xFFFF);
  592. base->FCTRL &= ~((mask >> 16U) & PWM_FCTRL_FIE_MASK);
  593. }
  594. uint32_t PWM_GetEnabledInterrupts(PWM_Type *base, pwm_submodule_t subModule)
  595. {
  596. uint32_t enabledInterrupts;
  597. enabledInterrupts = base->SM[subModule].INTEN;
  598. enabledInterrupts |= ((uint32_t)(base->FCTRL & PWM_FCTRL_FIE_MASK) << 16U);
  599. return enabledInterrupts;
  600. }
  601. uint32_t PWM_GetStatusFlags(PWM_Type *base, pwm_submodule_t subModule)
  602. {
  603. uint32_t statusFlags;
  604. statusFlags = base->SM[subModule].STS;
  605. statusFlags |= ((uint32_t)(base->FSTS & PWM_FSTS_FFLAG_MASK) << 16U);
  606. return statusFlags;
  607. }
  608. void PWM_ClearStatusFlags(PWM_Type *base, pwm_submodule_t subModule, uint32_t mask)
  609. {
  610. uint16_t reg;
  611. base->SM[subModule].STS = (mask & 0xFFFFU);
  612. reg = base->FSTS;
  613. /* Clear the fault flags and set only the ones we wish to clear as the fault flags are cleared
  614. * by writing a login one
  615. */
  616. reg &= ~(PWM_FSTS_FFLAG_MASK);
  617. reg |= ((mask >> 16U) & PWM_FSTS_FFLAG_MASK);
  618. base->FSTS = reg;
  619. }