nu_epwm.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. /**************************************************************************//**
  2. * @file epwm.c
  3. * @version V3.00
  4. * $Revision: 3 $
  5. * @brief M480 series EPWM driver source file
  6. *
  7. * SPDX-License-Identifier: Apache-2.0
  8. * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
  9. *****************************************************************************/
  10. #include "NuMicro.h"
  11. /** @addtogroup Standard_Driver Standard Driver
  12. @{
  13. */
  14. /** @addtogroup EPWM_Driver EPWM Driver
  15. @{
  16. */
  17. /** @addtogroup EPWM_EXPORTED_FUNCTIONS EPWM Exported Functions
  18. @{
  19. */
  20. /**
  21. * @brief Configure EPWM capture and get the nearest unit time.
  22. * @param[in] epwm The pointer of the specified EPWM module
  23. * - EPWM0 : EPWM Group 0
  24. * - EPWM1 : EPWM Group 1
  25. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  26. * @param[in] u32UnitTimeNsec The unit time of counter
  27. * @param[in] u32CaptureEdge The condition to latch the counter. This parameter is not used
  28. * @return The nearest unit time in nano second.
  29. * @details This function is used to Configure EPWM capture and get the nearest unit time.
  30. */
  31. uint32_t EPWM_ConfigCaptureChannel(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32UnitTimeNsec, uint32_t u32CaptureEdge)
  32. {
  33. uint32_t u32Src;
  34. uint32_t u32EPWMClockSrc;
  35. uint32_t u32NearestUnitTimeNsec;
  36. uint32_t u16Prescale = 1U, u16CNR = 0xFFFFU;
  37. if(epwm == EPWM0)
  38. {
  39. u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_EPWM0SEL_Msk;
  40. }
  41. else /* (epwm == EPWM1) */
  42. {
  43. u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_EPWM1SEL_Msk;
  44. }
  45. if(u32Src == 0U)
  46. {
  47. /* clock source is from PLL clock */
  48. u32EPWMClockSrc = CLK_GetPLLClockFreq();
  49. }
  50. else
  51. {
  52. /* clock source is from PCLK */
  53. SystemCoreClockUpdate();
  54. if(epwm == EPWM0)
  55. {
  56. u32EPWMClockSrc = CLK_GetPCLK0Freq();
  57. }
  58. else /* (epwm == EPWM1) */
  59. {
  60. u32EPWMClockSrc = CLK_GetPCLK1Freq();
  61. }
  62. }
  63. u32EPWMClockSrc /= 1000U;
  64. for(u16Prescale = 1U; u16Prescale <= 0x1000U; u16Prescale++)
  65. {
  66. uint32_t u32Exit = 0U;
  67. u32NearestUnitTimeNsec = (1000000U * u16Prescale) / u32EPWMClockSrc;
  68. if(u32NearestUnitTimeNsec < u32UnitTimeNsec)
  69. {
  70. if(u16Prescale == 0x1000U) /* limit to the maximum unit time(nano second) */
  71. {
  72. u32Exit = 1U;
  73. }
  74. else
  75. {
  76. u32Exit = 0U;
  77. }
  78. if(!((1000000U * (u16Prescale + 1U) > (u32NearestUnitTimeNsec * u32EPWMClockSrc))))
  79. {
  80. u32Exit = 1U;
  81. }
  82. else
  83. {
  84. u32Exit = 0U;
  85. }
  86. }
  87. else
  88. {
  89. u32Exit = 1U;
  90. }
  91. if (u32Exit == 1U)
  92. {
  93. break;
  94. }
  95. else {}
  96. }
  97. /* convert to real register value */
  98. /* every two channels share a prescaler */
  99. u16Prescale -= 1U;
  100. EPWM_SET_PRESCALER(epwm, u32ChannelNum, u16Prescale);
  101. /* set EPWM to down count type(edge aligned) */
  102. (epwm)->CTL1 = ((epwm)->CTL1 & ~(EPWM_CTL1_CNTTYPE0_Msk << (u32ChannelNum << 1U))) | (1UL << (u32ChannelNum << 1U));
  103. /* set EPWM to auto-reload mode */
  104. (epwm)->CTL1 &= ~(EPWM_CTL1_CNTMODE0_Msk << u32ChannelNum);
  105. EPWM_SET_CNR(epwm, u32ChannelNum, u16CNR);
  106. return (u32NearestUnitTimeNsec);
  107. }
  108. /**
  109. * @brief This function Configure EPWM generator and get the nearest frequency in edge aligned(up counter type) auto-reload mode
  110. * @param[in] epwm The pointer of the specified EPWM module
  111. * - EPWM0 : EPWM Group 0
  112. * - EPWM1 : EPWM Group 1
  113. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  114. * @param[in] u32Frequency Target generator frequency
  115. * @param[in] u32DutyCycle Target generator duty cycle percentage. Valid range are between 0 ~ 100. 10 means 10%, 20 means 20%...
  116. * @return Nearest frequency clock in nano second
  117. * @note Since every two channels, (0 & 1), (2 & 3), shares a prescaler. Call this API to configure EPWM frequency may affect
  118. * existing frequency of other channel.
  119. * @note This function is used for initial stage.
  120. * To change duty cycle later, it should get the configured period value and calculate the new comparator value.
  121. */
  122. uint32_t EPWM_ConfigOutputChannel(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Frequency, uint32_t u32DutyCycle)
  123. {
  124. uint32_t u32Src;
  125. uint32_t u32EPWMClockSrc;
  126. uint32_t i;
  127. uint32_t u32Prescale = 1U, u32CNR = 0xFFFFU;
  128. if(epwm == EPWM0)
  129. {
  130. u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_EPWM0SEL_Msk;
  131. }
  132. else /* (epwm == EPWM1) */
  133. {
  134. u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_EPWM1SEL_Msk;
  135. }
  136. if(u32Src == 0U)
  137. {
  138. /* clock source is from PLL clock */
  139. u32EPWMClockSrc = CLK_GetPLLClockFreq();
  140. }
  141. else
  142. {
  143. /* clock source is from PCLK */
  144. SystemCoreClockUpdate();
  145. if(epwm == EPWM0)
  146. {
  147. u32EPWMClockSrc = CLK_GetPCLK0Freq();
  148. }
  149. else /* (epwm == EPWM1) */
  150. {
  151. u32EPWMClockSrc = CLK_GetPCLK1Freq();
  152. }
  153. }
  154. for(u32Prescale = 1U; u32Prescale < 0xFFFU; u32Prescale++) /* prescale could be 0~0xFFF */
  155. {
  156. i = (u32EPWMClockSrc / u32Frequency) / u32Prescale;
  157. /* If target value is larger than CNR, need to use a larger prescaler */
  158. if(i < (0x10000U))
  159. {
  160. u32CNR = i;
  161. break;
  162. }
  163. }
  164. /* Store return value here 'cos we're gonna change u16Prescale & u16CNR to the real value to fill into register */
  165. i = u32EPWMClockSrc / (u32Prescale * u32CNR);
  166. /* convert to real register value */
  167. /* every two channels share a prescaler */
  168. u32Prescale -= 1U;
  169. EPWM_SET_PRESCALER(epwm, u32ChannelNum, u32Prescale);
  170. /* set EPWM to up counter type(edge aligned) and auto-reload mode */
  171. (epwm)->CTL1 = ((epwm)->CTL1 & ~((EPWM_CTL1_CNTTYPE0_Msk << (u32ChannelNum << 1U))|((1UL << EPWM_CTL1_CNTMODE0_Pos) << u32ChannelNum)));
  172. u32CNR -= 1U;
  173. EPWM_SET_CNR(epwm, u32ChannelNum, u32CNR);
  174. EPWM_SET_CMR(epwm, u32ChannelNum, u32DutyCycle * (u32CNR + 1U) / 100U);
  175. (epwm)->WGCTL0 = ((epwm)->WGCTL0 & ~((EPWM_WGCTL0_PRDPCTL0_Msk | EPWM_WGCTL0_ZPCTL0_Msk) << (u32ChannelNum << 1U))) | \
  176. ((uint32_t)EPWM_OUTPUT_HIGH << ((u32ChannelNum << 1U) + (uint32_t)EPWM_WGCTL0_ZPCTL0_Pos));
  177. (epwm)->WGCTL1 = ((epwm)->WGCTL1 & ~((EPWM_WGCTL1_CMPDCTL0_Msk | EPWM_WGCTL1_CMPUCTL0_Msk) << (u32ChannelNum << 1U))) | \
  178. ((uint32_t)EPWM_OUTPUT_LOW << ((u32ChannelNum << 1U) + (uint32_t)EPWM_WGCTL1_CMPUCTL0_Pos));
  179. return(i);
  180. }
  181. /**
  182. * @brief Start EPWM module
  183. * @param[in] epwm The pointer of the specified EPWM module
  184. * - EPWM0 : EPWM Group 0
  185. * - EPWM1 : EPWM Group 1
  186. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  187. * Bit 0 is channel 0, bit 1 is channel 1...
  188. * @return None
  189. * @details This function is used to start EPWM module.
  190. */
  191. void EPWM_Start(EPWM_T *epwm, uint32_t u32ChannelMask)
  192. {
  193. (epwm)->CNTEN |= u32ChannelMask;
  194. }
  195. /**
  196. * @brief Stop EPWM module
  197. * @param[in] epwm The pointer of the specified EPWM module
  198. * - EPWM0 : EPWM Group 0
  199. * - EPWM1 : EPWM Group 1
  200. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  201. * Bit 0 is channel 0, bit 1 is channel 1...
  202. * @return None
  203. * @details This function is used to stop EPWM module.
  204. */
  205. void EPWM_Stop(EPWM_T *epwm, uint32_t u32ChannelMask)
  206. {
  207. uint32_t i;
  208. for(i = 0U; i < EPWM_CHANNEL_NUM; i ++)
  209. {
  210. if(u32ChannelMask & (1UL << i))
  211. {
  212. (epwm)->PERIOD[i] = 0U;
  213. }
  214. }
  215. }
  216. /**
  217. * @brief Stop EPWM generation immediately by clear channel enable bit
  218. * @param[in] epwm The pointer of the specified EPWM module
  219. * - EPWM0 : EPWM Group 0
  220. * - EPWM1 : EPWM Group 1
  221. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  222. * Bit 0 is channel 0, bit 1 is channel 1...
  223. * @return None
  224. * @details This function is used to stop EPWM generation immediately by clear channel enable bit.
  225. */
  226. void EPWM_ForceStop(EPWM_T *epwm, uint32_t u32ChannelMask)
  227. {
  228. (epwm)->CNTEN &= ~u32ChannelMask;
  229. }
  230. /**
  231. * @brief Enable selected channel to trigger ADC
  232. * @param[in] epwm The pointer of the specified EPWM module
  233. * - EPWM0 : EPWM Group 0
  234. * - EPWM1 : EPWM Group 1
  235. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  236. * @param[in] u32Condition The condition to trigger ADC. Combination of following conditions:
  237. * - \ref EPWM_TRG_ADC_EVEN_ZERO
  238. * - \ref EPWM_TRG_ADC_EVEN_PERIOD
  239. * - \ref EPWM_TRG_ADC_EVEN_ZERO_PERIOD
  240. * - \ref EPWM_TRG_ADC_EVEN_COMPARE_UP
  241. * - \ref EPWM_TRG_ADC_EVEN_COMPARE_DOWN
  242. * - \ref EPWM_TRG_ADC_ODD_ZERO
  243. * - \ref EPWM_TRG_ADC_ODD_PERIOD
  244. * - \ref EPWM_TRG_ADC_ODD_ZERO_PERIOD
  245. * - \ref EPWM_TRG_ADC_ODD_COMPARE_UP
  246. * - \ref EPWM_TRG_ADC_ODD_COMPARE_DOWN
  247. * - \ref EPWM_TRG_ADC_CH_0_FREE_CMP_UP
  248. * - \ref EPWM_TRG_ADC_CH_0_FREE_CMP_DOWN
  249. * - \ref EPWM_TRG_ADC_CH_2_FREE_CMP_UP
  250. * - \ref EPWM_TRG_ADC_CH_2_FREE_CMP_DOWN
  251. * - \ref EPWM_TRG_ADC_CH_4_FREE_CMP_UP
  252. * - \ref EPWM_TRG_ADC_CH_4_FREE_CMP_DOWN
  253. * @return None
  254. * @details This function is used to enable selected channel to trigger ADC.
  255. */
  256. void EPWM_EnableADCTrigger(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Condition)
  257. {
  258. if(u32ChannelNum < 4U)
  259. {
  260. (epwm)->EADCTS0 &= ~((EPWM_EADCTS0_TRGSEL0_Msk) << (u32ChannelNum << 3U));
  261. (epwm)->EADCTS0 |= ((EPWM_EADCTS0_TRGEN0_Msk | u32Condition) << (u32ChannelNum << 3));
  262. }
  263. else
  264. {
  265. (epwm)->EADCTS1 &= ~((EPWM_EADCTS1_TRGSEL4_Msk) << ((u32ChannelNum - 4U) << 3U));
  266. (epwm)->EADCTS1 |= ((EPWM_EADCTS1_TRGEN4_Msk | u32Condition) << ((u32ChannelNum - 4U) << 3U));
  267. }
  268. }
  269. /**
  270. * @brief Disable selected channel to trigger ADC
  271. * @param[in] epwm The pointer of the specified EPWM module
  272. * - EPWM0 : EPWM Group 0
  273. * - EPWM1 : EPWM Group 1
  274. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  275. * @return None
  276. * @details This function is used to disable selected channel to trigger ADC.
  277. */
  278. void EPWM_DisableADCTrigger(EPWM_T *epwm, uint32_t u32ChannelNum)
  279. {
  280. if(u32ChannelNum < 4U)
  281. {
  282. (epwm)->EADCTS0 &= ~(EPWM_EADCTS0_TRGEN0_Msk << (u32ChannelNum << 3U));
  283. }
  284. else
  285. {
  286. (epwm)->EADCTS1 &= ~(EPWM_EADCTS1_TRGEN4_Msk << ((u32ChannelNum - 4U) << 3U));
  287. }
  288. }
  289. /**
  290. * @brief Enable and configure trigger ADC prescale
  291. * @param[in] epwm The pointer of the specified EPWM module
  292. * - EPWM0 : EPWM Group 0
  293. * - EPWM1 : EPWM Group 1
  294. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  295. * @param[in] u32Prescale ADC prescale. Valid values are between 0 to 0xF.
  296. * @param[in] u32PrescaleCnt ADC prescale counter. Valid values are between 0 to 0xF.
  297. * @retval 0 Success.
  298. * @retval -1 Failed.
  299. * @details This function is used to enable and configure trigger ADC prescale.
  300. * @note User can configure only when ADC trigger prescale is disabled.
  301. * @note ADC prescale counter must less than ADC prescale.
  302. */
  303. int32_t EPWM_EnableADCTriggerPrescale(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Prescale, uint32_t u32PrescaleCnt)
  304. {
  305. /* User can write only when PSCENn(n = 0 ~ 5) is 0 */
  306. if ((epwm)->EADCPSCCTL & (1UL << u32ChannelNum))
  307. return (-1);
  308. if(u32ChannelNum < 4UL)
  309. {
  310. (epwm)->EADCPSC0 = ((epwm)->EADCPSC0 & ~((EPWM_EADCPSC0_EADCPSC0_Msk) << (u32ChannelNum << 3))) | \
  311. (u32Prescale << (u32ChannelNum << 3));
  312. (epwm)->EADCPSCNT0 = ((epwm)->EADCPSCNT0 & ~((EPWM_EADCPSCNT0_PSCNT0_Msk) << (u32ChannelNum << 3))) | \
  313. (u32PrescaleCnt << (u32ChannelNum << 3));
  314. }
  315. else
  316. {
  317. (epwm)->EADCPSC1 = ((epwm)->EADCPSC1 & ~((EPWM_EADCPSC1_EADCPSC4_Msk) << ((u32ChannelNum - 4UL) << 3))) | \
  318. (u32Prescale << ((u32ChannelNum - 4UL) << 3));
  319. (epwm)->EADCPSCNT1 = ((epwm)->EADCPSCNT1 & ~((EPWM_EADCPSCNT1_PSCNT4_Msk) << ((u32ChannelNum - 4UL) << 3))) | \
  320. (u32PrescaleCnt << ((u32ChannelNum - 4UL) << 3));
  321. }
  322. (epwm)->EADCPSCCTL |= EPWM_EADCPSCCTL_PSCEN0_Msk << u32ChannelNum;
  323. return 0;
  324. }
  325. /**
  326. * @brief Disable Trigger ADC prescale function
  327. * @param[in] epwm The pointer of the specified EPWM module
  328. * - EPWM0 : EPWM Group 0
  329. * - EPWM1 : EPWM Group 1
  330. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  331. * @return None
  332. * @details This function is used to disable trigger ADC prescale.
  333. */
  334. void EPWM_DisableADCTriggerPrescale(EPWM_T *epwm, uint32_t u32ChannelNum)
  335. {
  336. (epwm)->EADCPSCCTL &= ~(EPWM_EADCPSCCTL_PSCEN0_Msk << u32ChannelNum);
  337. }
  338. /**
  339. * @brief Clear selected channel trigger ADC flag
  340. * @param[in] epwm The pointer of the specified EPWM module
  341. * - EPWM0 : EPWM Group 0
  342. * - EPWM1 : EPWM Group 1
  343. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  344. * @param[in] u32Condition This parameter is not used
  345. * @return None
  346. * @details This function is used to clear selected channel trigger ADC flag.
  347. */
  348. void EPWM_ClearADCTriggerFlag(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Condition)
  349. {
  350. (epwm)->STATUS = (EPWM_STATUS_EADCTRGF0_Msk << u32ChannelNum);
  351. }
  352. /**
  353. * @brief Get selected channel trigger ADC flag
  354. * @param[in] epwm The pointer of the specified EPWM module
  355. * - EPWM0 : EPWM Group 0
  356. * - EPWM1 : EPWM Group 1
  357. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  358. * @retval 0 The specified channel trigger ADC to start of conversion flag is not set
  359. * @retval 1 The specified channel trigger ADC to start of conversion flag is set
  360. * @details This function is used to get EPWM trigger ADC to start of conversion flag for specified channel.
  361. */
  362. uint32_t EPWM_GetADCTriggerFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  363. {
  364. return (((epwm)->STATUS & (EPWM_STATUS_EADCTRGF0_Msk << u32ChannelNum))?1UL:0UL);
  365. }
  366. /**
  367. * @brief Enable selected channel to trigger DAC
  368. * @param[in] epwm The pointer of the specified EPWM module
  369. * - EPWM0 : EPWM Group 0
  370. * - EPWM1 : EPWM Group 1
  371. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  372. * @param[in] u32Condition The condition to trigger DAC. Combination of following conditions:
  373. * - \ref EPWM_TRIGGER_DAC_ZERO
  374. * - \ref EPWM_TRIGGER_DAC_PERIOD
  375. * - \ref EPWM_TRIGGER_DAC_COMPARE_UP
  376. * - \ref EPWM_TRIGGER_DAC_COMPARE_DOWN
  377. * @return None
  378. * @details This function is used to enable selected channel to trigger DAC.
  379. */
  380. void EPWM_EnableDACTrigger(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Condition)
  381. {
  382. (epwm)->DACTRGEN |= (u32Condition << u32ChannelNum);
  383. }
  384. /**
  385. * @brief Disable selected channel to trigger DAC
  386. * @param[in] epwm The pointer of the specified EPWM module
  387. * - EPWM0 : EPWM Group 0
  388. * - EPWM1 : EPWM Group 1
  389. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  390. * @return None
  391. * @details This function is used to disable selected channel to trigger DAC.
  392. */
  393. void EPWM_DisableDACTrigger(EPWM_T *epwm, uint32_t u32ChannelNum)
  394. {
  395. (epwm)->DACTRGEN &= ~((EPWM_TRIGGER_DAC_ZERO | EPWM_TRIGGER_DAC_PERIOD | EPWM_TRIGGER_DAC_COMPARE_UP | \
  396. EPWM_TRIGGER_DAC_COMPARE_DOWN) << u32ChannelNum);
  397. }
  398. /**
  399. * @brief Clear selected channel trigger DAC flag
  400. * @param[in] epwm The pointer of the specified EPWM module
  401. * - EPWM0 : EPWM Group 0
  402. * - EPWM1 : EPWM Group 1
  403. * @param[in] u32ChannelNum EPWM channel number. This parameter is not used
  404. * @param[in] u32Condition The condition to trigger DAC. This parameter is not used
  405. * @return None
  406. * @details This function is used to clear selected channel trigger DAC flag.
  407. */
  408. void EPWM_ClearDACTriggerFlag(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Condition)
  409. {
  410. (epwm)->STATUS = EPWM_STATUS_DACTRGF_Msk;
  411. }
  412. /**
  413. * @brief Get selected channel trigger DAC flag
  414. * @param[in] epwm The pointer of the specified EPWM module
  415. * - EPWM0 : EPWM Group 0
  416. * - EPWM1 : EPWM Group 1
  417. * @param[in] u32ChannelNum EPWM channel number. This parameter is not used
  418. * @retval 0 The specified channel trigger DAC to start of conversion flag is not set
  419. * @retval 1 The specified channel trigger DAC to start of conversion flag is set
  420. * @details This function is used to get selected channel trigger DAC flag.
  421. */
  422. uint32_t EPWM_GetDACTriggerFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  423. {
  424. return (((epwm)->STATUS & EPWM_STATUS_DACTRGF_Msk)?1UL:0UL);
  425. }
  426. /**
  427. * @brief This function enable fault brake of selected channel(s)
  428. * @param[in] epwm The pointer of the specified EPWM module
  429. * - EPWM0 : EPWM Group 0
  430. * - EPWM1 : EPWM Group 1
  431. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  432. * @param[in] u32LevelMask Output high or low while fault brake occurs, each bit represent the level of a channel
  433. * while fault brake occurs. Bit 0 represents channel 0, bit 1 represents channel 1...
  434. * @param[in] u32BrakeSource Fault brake source, could be one of following source
  435. * - \ref EPWM_FB_EDGE_ADCRM
  436. * - \ref EPWM_FB_EDGE_ACMP0
  437. * - \ref EPWM_FB_EDGE_ACMP1
  438. * - \ref EPWM_FB_EDGE_BKP0
  439. * - \ref EPWM_FB_EDGE_BKP1
  440. * - \ref EPWM_FB_EDGE_SYS_CSS
  441. * - \ref EPWM_FB_EDGE_SYS_BOD
  442. * - \ref EPWM_FB_EDGE_SYS_RAM
  443. * - \ref EPWM_FB_EDGE_SYS_COR
  444. * - \ref EPWM_FB_LEVEL_ADCRM
  445. * - \ref EPWM_FB_LEVEL_ACMP0
  446. * - \ref EPWM_FB_LEVEL_ACMP1
  447. * - \ref EPWM_FB_LEVEL_BKP0
  448. * - \ref EPWM_FB_LEVEL_BKP1
  449. * - \ref EPWM_FB_LEVEL_SYS_CSS
  450. * - \ref EPWM_FB_LEVEL_SYS_BOD
  451. * - \ref EPWM_FB_LEVEL_SYS_RAM
  452. * - \ref EPWM_FB_LEVEL_SYS_COR
  453. * @return None
  454. * @details This function is used to enable fault brake of selected channel(s).
  455. * The write-protection function should be disabled before using this function.
  456. */
  457. void EPWM_EnableFaultBrake(EPWM_T *epwm, uint32_t u32ChannelMask, uint32_t u32LevelMask, uint32_t u32BrakeSource)
  458. {
  459. uint32_t i;
  460. for(i = 0U; i < EPWM_CHANNEL_NUM; i ++)
  461. {
  462. if(u32ChannelMask & (1UL << i))
  463. {
  464. if((u32BrakeSource == EPWM_FB_EDGE_SYS_CSS) || (u32BrakeSource == EPWM_FB_EDGE_SYS_BOD) || \
  465. (u32BrakeSource == EPWM_FB_EDGE_SYS_RAM) || (u32BrakeSource == EPWM_FB_EDGE_SYS_COR) || \
  466. (u32BrakeSource == EPWM_FB_LEVEL_SYS_CSS) || (u32BrakeSource == EPWM_FB_LEVEL_SYS_BOD) || \
  467. (u32BrakeSource == EPWM_FB_LEVEL_SYS_RAM) || (u32BrakeSource == EPWM_FB_LEVEL_SYS_COR))
  468. {
  469. (epwm)->BRKCTL[i >> 1U] |= (u32BrakeSource & (EPWM_BRKCTL0_1_SYSEBEN_Msk | EPWM_BRKCTL0_1_SYSLBEN_Msk));
  470. (epwm)->FAILBRK |= (u32BrakeSource & 0xFU);
  471. }
  472. else
  473. {
  474. (epwm)->BRKCTL[i >> 1U] |= u32BrakeSource;
  475. }
  476. }
  477. if(u32LevelMask & (1UL << i))
  478. {
  479. if((i & 0x1U) == 0U)
  480. {
  481. /* set brake action as high level for even channel */
  482. (epwm)->BRKCTL[i >> 1] &= ~EPWM_BRKCTL0_1_BRKAEVEN_Msk;
  483. (epwm)->BRKCTL[i >> 1] |= ((3U) << EPWM_BRKCTL0_1_BRKAEVEN_Pos);
  484. }
  485. else
  486. {
  487. /* set brake action as high level for odd channel */
  488. (epwm)->BRKCTL[i >> 1] &= ~EPWM_BRKCTL0_1_BRKAODD_Msk;
  489. (epwm)->BRKCTL[i >> 1] |= ((3U) << EPWM_BRKCTL0_1_BRKAODD_Pos);
  490. }
  491. }
  492. else
  493. {
  494. if((i & 0x1U) == 0U)
  495. {
  496. /* set brake action as low level for even channel */
  497. (epwm)->BRKCTL[i >> 1U] &= ~EPWM_BRKCTL0_1_BRKAEVEN_Msk;
  498. (epwm)->BRKCTL[i >> 1U] |= ((2U) << EPWM_BRKCTL0_1_BRKAEVEN_Pos);
  499. }
  500. else
  501. {
  502. /* set brake action as low level for odd channel */
  503. (epwm)->BRKCTL[i >> 1U] &= ~EPWM_BRKCTL0_1_BRKAODD_Msk;
  504. (epwm)->BRKCTL[i >> 1U] |= ((2U) << EPWM_BRKCTL0_1_BRKAODD_Pos);
  505. }
  506. }
  507. }
  508. }
  509. /**
  510. * @brief Enable capture of selected channel(s)
  511. * @param[in] epwm The pointer of the specified EPWM module
  512. * - EPWM0 : EPWM Group 0
  513. * - EPWM1 : EPWM Group 1
  514. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  515. * Bit 0 is channel 0, bit 1 is channel 1...
  516. * @return None
  517. * @details This function is used to enable capture of selected channel(s).
  518. */
  519. void EPWM_EnableCapture(EPWM_T *epwm, uint32_t u32ChannelMask)
  520. {
  521. (epwm)->CAPINEN |= u32ChannelMask;
  522. (epwm)->CAPCTL |= u32ChannelMask;
  523. }
  524. /**
  525. * @brief Disable capture of selected channel(s)
  526. * @param[in] epwm The pointer of the specified EPWM module
  527. * - EPWM0 : EPWM Group 0
  528. * - EPWM1 : EPWM Group 1
  529. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  530. * Bit 0 is channel 0, bit 1 is channel 1...
  531. * @return None
  532. * @details This function is used to disable capture of selected channel(s).
  533. */
  534. void EPWM_DisableCapture(EPWM_T *epwm, uint32_t u32ChannelMask)
  535. {
  536. (epwm)->CAPINEN &= ~u32ChannelMask;
  537. (epwm)->CAPCTL &= ~u32ChannelMask;
  538. }
  539. /**
  540. * @brief Enables EPWM output generation of selected channel(s)
  541. * @param[in] epwm The pointer of the specified EPWM module
  542. * - EPWM0 : EPWM Group 0
  543. * - EPWM1 : EPWM Group 1
  544. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  545. * Set bit 0 to 1 enables channel 0 output, set bit 1 to 1 enables channel 1 output...
  546. * @return None
  547. * @details This function is used to enable EPWM output generation of selected channel(s).
  548. */
  549. void EPWM_EnableOutput(EPWM_T *epwm, uint32_t u32ChannelMask)
  550. {
  551. (epwm)->POEN |= u32ChannelMask;
  552. }
  553. /**
  554. * @brief Disables EPWM output generation of selected channel(s)
  555. * @param[in] epwm The pointer of the specified EPWM module
  556. * - EPWM0 : EPWM Group 0
  557. * - EPWM1 : EPWM Group 1
  558. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
  559. * Set bit 0 to 1 disables channel 0 output, set bit 1 to 1 disables channel 1 output...
  560. * @return None
  561. * @details This function is used to disable EPWM output generation of selected channel(s).
  562. */
  563. void EPWM_DisableOutput(EPWM_T *epwm, uint32_t u32ChannelMask)
  564. {
  565. (epwm)->POEN &= ~u32ChannelMask;
  566. }
  567. /**
  568. * @brief Enables PDMA transfer of selected channel for EPWM capture
  569. * @param[in] epwm The pointer of the specified EPWM module
  570. * - EPWM0 : EPWM Group 0
  571. * - EPWM1 : EPWM Group 1
  572. * @param[in] u32ChannelNum EPWM channel number.
  573. * @param[in] u32RisingFirst The capture order is rising, falling first. Every two channels share the same setting. Valid values are TRUE and FALSE.
  574. * @param[in] u32Mode Captured data transferred by PDMA interrupt type. It could be either
  575. * - \ref EPWM_CAPTURE_PDMA_RISING_LATCH
  576. * - \ref EPWM_CAPTURE_PDMA_FALLING_LATCH
  577. * - \ref EPWM_CAPTURE_PDMA_RISING_FALLING_LATCH
  578. * @return None
  579. * @details This function is used to enable PDMA transfer of selected channel(s) for EPWM capture.
  580. * @note This function can only selects even or odd channel of pairs to do PDMA transfer.
  581. */
  582. void EPWM_EnablePDMA(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32RisingFirst, uint32_t u32Mode)
  583. {
  584. uint32_t u32IsOddCh;
  585. u32IsOddCh = u32ChannelNum & 0x1U;
  586. (epwm)->PDMACTL = ((epwm)->PDMACTL & ~((EPWM_PDMACTL_CHSEL0_1_Msk | EPWM_PDMACTL_CAPORD0_1_Msk | EPWM_PDMACTL_CAPMOD0_1_Msk) << ((u32ChannelNum >> 1U) << 3U))) | \
  587. (((u32IsOddCh << EPWM_PDMACTL_CHSEL0_1_Pos) | (u32RisingFirst << EPWM_PDMACTL_CAPORD0_1_Pos) | \
  588. u32Mode | EPWM_PDMACTL_CHEN0_1_Msk) << ((u32ChannelNum >> 1U) << 3U));
  589. }
  590. /**
  591. * @brief Disables PDMA transfer of selected channel for EPWM capture
  592. * @param[in] epwm The pointer of the specified EPWM module
  593. * - EPWM0 : EPWM Group 0
  594. * - EPWM1 : EPWM Group 1
  595. * @param[in] u32ChannelNum EPWM channel number.
  596. * @return None
  597. * @details This function is used to enable PDMA transfer of selected channel(s) for EPWM capture.
  598. */
  599. void EPWM_DisablePDMA(EPWM_T *epwm, uint32_t u32ChannelNum)
  600. {
  601. (epwm)->PDMACTL &= ~(EPWM_PDMACTL_CHEN0_1_Msk << ((u32ChannelNum >> 1U) << 3U));
  602. }
  603. /**
  604. * @brief Enable Dead zone of selected channel
  605. * @param[in] epwm The pointer of the specified EPWM module
  606. * - EPWM0 : EPWM Group 0
  607. * - EPWM1 : EPWM Group 1
  608. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  609. * @param[in] u32Duration Dead zone length in EPWM clock count, valid values are between 0~0xFFF, but 0 means there is no Dead zone.
  610. * @return None
  611. * @details This function is used to enable Dead zone of selected channel.
  612. * The write-protection function should be disabled before using this function.
  613. * @note Every two channels share the same setting.
  614. */
  615. void EPWM_EnableDeadZone(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Duration)
  616. {
  617. /* every two channels share the same setting */
  618. (epwm)->DTCTL[(u32ChannelNum) >> 1U] &= ~EPWM_DTCTL0_1_DTCNT_Msk;
  619. (epwm)->DTCTL[(u32ChannelNum) >> 1U] |= EPWM_DTCTL0_1_DTEN_Msk | u32Duration;
  620. }
  621. /**
  622. * @brief Disable Dead zone of selected channel
  623. * @param[in] epwm The pointer of the specified EPWM module
  624. * - EPWM0 : EPWM Group 0
  625. * - EPWM1 : EPWM Group 1
  626. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  627. * @return None
  628. * @details This function is used to disable Dead zone of selected channel.
  629. * The write-protection function should be disabled before using this function.
  630. */
  631. void EPWM_DisableDeadZone(EPWM_T *epwm, uint32_t u32ChannelNum)
  632. {
  633. /* every two channels shares the same setting */
  634. (epwm)->DTCTL[(u32ChannelNum) >> 1U] &= ~EPWM_DTCTL0_1_DTEN_Msk;
  635. }
  636. /**
  637. * @brief Enable capture interrupt of selected channel.
  638. * @param[in] epwm The pointer of the specified EPWM module
  639. * - EPWM0 : EPWM Group 0
  640. * - EPWM1 : EPWM Group 1
  641. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  642. * @param[in] u32Edge Rising or falling edge to latch counter.
  643. * - \ref EPWM_CAPTURE_INT_RISING_LATCH
  644. * - \ref EPWM_CAPTURE_INT_FALLING_LATCH
  645. * @return None
  646. * @details This function is used to enable capture interrupt of selected channel.
  647. */
  648. void EPWM_EnableCaptureInt(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Edge)
  649. {
  650. (epwm)->CAPIEN |= (u32Edge << u32ChannelNum);
  651. }
  652. /**
  653. * @brief Disable capture interrupt of selected channel.
  654. * @param[in] epwm The pointer of the specified EPWM module
  655. * - EPWM0 : EPWM Group 0
  656. * - EPWM1 : EPWM Group 1
  657. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  658. * @param[in] u32Edge Rising or falling edge to latch counter.
  659. * - \ref EPWM_CAPTURE_INT_RISING_LATCH
  660. * - \ref EPWM_CAPTURE_INT_FALLING_LATCH
  661. * @return None
  662. * @details This function is used to disable capture interrupt of selected channel.
  663. */
  664. void EPWM_DisableCaptureInt(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Edge)
  665. {
  666. (epwm)->CAPIEN &= ~(u32Edge << u32ChannelNum);
  667. }
  668. /**
  669. * @brief Clear capture interrupt of selected channel.
  670. * @param[in] epwm The pointer of the specified EPWM module
  671. * - EPWM0 : EPWM Group 0
  672. * - EPWM1 : EPWM Group 1
  673. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  674. * @param[in] u32Edge Rising or falling edge to latch counter.
  675. * - \ref EPWM_CAPTURE_INT_RISING_LATCH
  676. * - \ref EPWM_CAPTURE_INT_FALLING_LATCH
  677. * @return None
  678. * @details This function is used to clear capture interrupt of selected channel.
  679. */
  680. void EPWM_ClearCaptureIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32Edge)
  681. {
  682. (epwm)->CAPIF = (u32Edge << u32ChannelNum);
  683. }
  684. /**
  685. * @brief Get capture interrupt of selected channel.
  686. * @param[in] epwm The pointer of the specified EPWM module
  687. * - EPWM0 : EPWM Group 0
  688. * - EPWM1 : EPWM Group 1
  689. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  690. * @retval 0 No capture interrupt
  691. * @retval 1 Rising edge latch interrupt
  692. * @retval 2 Falling edge latch interrupt
  693. * @retval 3 Rising and falling latch interrupt
  694. * @details This function is used to get capture interrupt of selected channel.
  695. */
  696. uint32_t EPWM_GetCaptureIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  697. {
  698. return (((((epwm)->CAPIF & (EPWM_CAPIF_CFLIF0_Msk << u32ChannelNum)) ? 1UL : 0UL) << 1) | \
  699. (((epwm)->CAPIF & (EPWM_CAPIF_CRLIF0_Msk << u32ChannelNum)) ? 1UL : 0UL));
  700. }
  701. /**
  702. * @brief Enable duty interrupt of selected channel
  703. * @param[in] epwm The pointer of the specified EPWM module
  704. * - EPWM0 : EPWM Group 0
  705. * - EPWM1 : EPWM Group 1
  706. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  707. * @param[in] u32IntDutyType Duty interrupt type, could be either
  708. * - \ref EPWM_DUTY_INT_DOWN_COUNT_MATCH_CMP
  709. * - \ref EPWM_DUTY_INT_UP_COUNT_MATCH_CMP
  710. * @return None
  711. * @details This function is used to enable duty interrupt of selected channel.
  712. */
  713. void EPWM_EnableDutyInt(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType)
  714. {
  715. (epwm)->INTEN0 |= (u32IntDutyType << u32ChannelNum);
  716. }
  717. /**
  718. * @brief Disable duty interrupt of selected channel
  719. * @param[in] epwm The pointer of the specified EPWM module
  720. * - EPWM0 : EPWM Group 0
  721. * - EPWM1 : EPWM Group 1
  722. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  723. * @return None
  724. * @details This function is used to disable duty interrupt of selected channel.
  725. */
  726. void EPWM_DisableDutyInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  727. {
  728. (epwm)->INTEN0 &= ~((uint32_t)(EPWM_DUTY_INT_DOWN_COUNT_MATCH_CMP | EPWM_DUTY_INT_UP_COUNT_MATCH_CMP) << u32ChannelNum);
  729. }
  730. /**
  731. * @brief Clear duty interrupt flag of selected channel
  732. * @param[in] epwm The pointer of the specified EPWM module
  733. * - EPWM0 : EPWM Group 0
  734. * - EPWM1 : EPWM Group 1
  735. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  736. * @return None
  737. * @details This function is used to clear duty interrupt flag of selected channel.
  738. */
  739. void EPWM_ClearDutyIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  740. {
  741. (epwm)->INTSTS0 = (EPWM_INTSTS0_CMPUIF0_Msk | EPWM_INTSTS0_CMPDIF0_Msk) << u32ChannelNum;
  742. }
  743. /**
  744. * @brief Get duty interrupt flag of selected channel
  745. * @param[in] epwm The pointer of the specified EPWM module
  746. * - EPWM0 : EPWM Group 0
  747. * - EPWM1 : EPWM Group 1
  748. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  749. * @return Duty interrupt flag of specified channel
  750. * @retval 0 Duty interrupt did not occur
  751. * @retval 1 Duty interrupt occurred
  752. * @details This function is used to get duty interrupt flag of selected channel.
  753. */
  754. uint32_t EPWM_GetDutyIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  755. {
  756. return ((((epwm)->INTSTS0 & ((EPWM_INTSTS0_CMPDIF0_Msk | EPWM_INTSTS0_CMPUIF0_Msk) << u32ChannelNum))) ? 1UL : 0UL);
  757. }
  758. /**
  759. * @brief This function enable fault brake interrupt
  760. * @param[in] epwm The pointer of the specified EPWM module
  761. * @param[in] u32BrakeSource Fault brake source.
  762. * - \ref EPWM_FB_EDGE
  763. * - \ref EPWM_FB_LEVEL
  764. * @return None
  765. * @details This function is used to enable fault brake interrupt.
  766. * The write-protection function should be disabled before using this function.
  767. * @note Every two channels share the same setting.
  768. */
  769. void EPWM_EnableFaultBrakeInt(EPWM_T *epwm, uint32_t u32BrakeSource)
  770. {
  771. (epwm)->INTEN1 |= (0x7UL << u32BrakeSource);
  772. }
  773. /**
  774. * @brief This function disable fault brake interrupt
  775. * @param[in] epwm The pointer of the specified EPWM module
  776. * @param[in] u32BrakeSource Fault brake source.
  777. * - \ref EPWM_FB_EDGE
  778. * - \ref EPWM_FB_LEVEL
  779. * @return None
  780. * @details This function is used to disable fault brake interrupt.
  781. * The write-protection function should be disabled before using this function.
  782. * @note Every two channels share the same setting.
  783. */
  784. void EPWM_DisableFaultBrakeInt(EPWM_T *epwm, uint32_t u32BrakeSource)
  785. {
  786. (epwm)->INTEN1 &= ~(0x7UL << u32BrakeSource);
  787. }
  788. /**
  789. * @brief This function clear fault brake interrupt of selected source
  790. * @param[in] epwm The pointer of the specified EPWM module
  791. * @param[in] u32BrakeSource Fault brake source.
  792. * - \ref EPWM_FB_EDGE
  793. * - \ref EPWM_FB_LEVEL
  794. * @return None
  795. * @details This function is used to clear fault brake interrupt of selected source.
  796. * The write-protection function should be disabled before using this function.
  797. */
  798. void EPWM_ClearFaultBrakeIntFlag(EPWM_T *epwm, uint32_t u32BrakeSource)
  799. {
  800. (epwm)->INTSTS1 = (0x3fUL << u32BrakeSource);
  801. }
  802. /**
  803. * @brief This function get fault brake interrupt flag of selected source
  804. * @param[in] epwm The pointer of the specified EPWM module
  805. * @param[in] u32BrakeSource Fault brake source, could be either
  806. * - \ref EPWM_FB_EDGE
  807. * - \ref EPWM_FB_LEVEL
  808. * @return Fault brake interrupt flag of specified source
  809. * @retval 0 Fault brake interrupt did not occurred
  810. * @retval 1 Fault brake interrupt occurred
  811. * @details This function is used to get fault brake interrupt flag of selected source.
  812. */
  813. uint32_t EPWM_GetFaultBrakeIntFlag(EPWM_T *epwm, uint32_t u32BrakeSource)
  814. {
  815. return (((epwm)->INTSTS1 & (0x3fUL << u32BrakeSource)) ? 1UL : 0UL);
  816. }
  817. /**
  818. * @brief Enable period interrupt of selected channel
  819. * @param[in] epwm The pointer of the specified EPWM module
  820. * - EPWM0 : EPWM Group 0
  821. * - EPWM1 : EPWM Group 1
  822. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  823. * @param[in] u32IntPeriodType Period interrupt type. This parameter is not used.
  824. * @return None
  825. * @details This function is used to enable period interrupt of selected channel.
  826. */
  827. void EPWM_EnablePeriodInt(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType)
  828. {
  829. (epwm)->INTEN0 |= ((1UL << EPWM_INTEN0_PIEN0_Pos) << u32ChannelNum);
  830. }
  831. /**
  832. * @brief Disable period interrupt of selected channel
  833. * @param[in] epwm The pointer of the specified EPWM module
  834. * - EPWM0 : EPWM Group 0
  835. * - EPWM1 : EPWM Group 1
  836. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  837. * @return None
  838. * @details This function is used to disable period interrupt of selected channel.
  839. */
  840. void EPWM_DisablePeriodInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  841. {
  842. (epwm)->INTEN0 &= ~((1UL << EPWM_INTEN0_PIEN0_Pos) << u32ChannelNum);
  843. }
  844. /**
  845. * @brief Clear period interrupt of selected channel
  846. * @param[in] epwm The pointer of the specified EPWM module
  847. * - EPWM0 : EPWM Group 0
  848. * - EPWM1 : EPWM Group 1
  849. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  850. * @return None
  851. * @details This function is used to clear period interrupt of selected channel.
  852. */
  853. void EPWM_ClearPeriodIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  854. {
  855. (epwm)->INTSTS0 = ((1UL << EPWM_INTSTS0_PIF0_Pos) << u32ChannelNum);
  856. }
  857. /**
  858. * @brief Get period interrupt of selected channel
  859. * @param[in] epwm The pointer of the specified EPWM module
  860. * - EPWM0 : EPWM Group 0
  861. * - EPWM1 : EPWM Group 1
  862. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  863. * @return Period interrupt flag of specified channel
  864. * @retval 0 Period interrupt did not occur
  865. * @retval 1 Period interrupt occurred
  866. * @details This function is used to get period interrupt of selected channel.
  867. */
  868. uint32_t EPWM_GetPeriodIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  869. {
  870. return ((((epwm)->INTSTS0 & ((1UL << EPWM_INTSTS0_PIF0_Pos) << u32ChannelNum))) ? 1UL : 0UL);
  871. }
  872. /**
  873. * @brief Enable zero interrupt of selected channel
  874. * @param[in] epwm The pointer of the specified EPWM module
  875. * - EPWM0 : EPWM Group 0
  876. * - EPWM1 : EPWM Group 1
  877. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  878. * @return None
  879. * @details This function is used to enable zero interrupt of selected channel.
  880. */
  881. void EPWM_EnableZeroInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  882. {
  883. (epwm)->INTEN0 |= ((1UL << EPWM_INTEN0_ZIEN0_Pos) << u32ChannelNum);
  884. }
  885. /**
  886. * @brief Disable zero interrupt of selected channel
  887. * @param[in] epwm The pointer of the specified EPWM module
  888. * - EPWM0 : EPWM Group 0
  889. * - EPWM1 : EPWM Group 1
  890. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  891. * @return None
  892. * @details This function is used to disable zero interrupt of selected channel.
  893. */
  894. void EPWM_DisableZeroInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  895. {
  896. (epwm)->INTEN0 &= ~((1UL << EPWM_INTEN0_ZIEN0_Pos) << u32ChannelNum);
  897. }
  898. /**
  899. * @brief Clear zero interrupt of selected channel
  900. * @param[in] epwm The pointer of the specified EPWM module
  901. * - EPWM0 : EPWM Group 0
  902. * - EPWM1 : EPWM Group 1
  903. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  904. * @return None
  905. * @details This function is used to clear zero interrupt of selected channel.
  906. */
  907. void EPWM_ClearZeroIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  908. {
  909. (epwm)->INTSTS0 = ((1UL << EPWM_INTEN0_ZIEN0_Pos) << u32ChannelNum);
  910. }
  911. /**
  912. * @brief Get zero interrupt of selected channel
  913. * @param[in] epwm The pointer of the specified EPWM module
  914. * - EPWM0 : EPWM Group 0
  915. * - EPWM1 : EPWM Group 1
  916. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  917. * @return Zero interrupt flag of specified channel
  918. * @retval 0 Zero interrupt did not occur
  919. * @retval 1 Zero interrupt occurred
  920. * @details This function is used to get zero interrupt of selected channel.
  921. */
  922. uint32_t EPWM_GetZeroIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  923. {
  924. return ((((epwm)->INTSTS0 & ((1UL << EPWM_INTEN0_ZIEN0_Pos) << u32ChannelNum))) ? 1UL : 0UL);
  925. }
  926. /**
  927. * @brief Enable interrupt flag accumulator of selected channel
  928. * @param[in] epwm The pointer of the specified EPWM module
  929. * - EPWM0 : EPWM Group 0
  930. * - EPWM1 : EPWM Group 1
  931. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  932. * @param[in] u32IntFlagCnt Interrupt flag counter. Valid values are between 0~65535.
  933. * @param[in] u32IntAccSrc Interrupt flag accumulator source selection.
  934. * - \ref EPWM_IFA_ZERO_POINT
  935. * - \ref EPWM_IFA_PERIOD_POINT
  936. * - \ref EPWM_IFA_COMPARE_UP_COUNT_POINT
  937. * - \ref EPWM_IFA_COMPARE_DOWN_COUNT_POINT
  938. * @return None
  939. * @details This function is used to enable interrupt flag accumulator of selected channel.
  940. */
  941. void EPWM_EnableAcc(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32IntFlagCnt, uint32_t u32IntAccSrc)
  942. {
  943. (epwm)->IFA[u32ChannelNum] = (((epwm)->IFA[u32ChannelNum] & ~((EPWM_IFA0_IFACNT_Msk | EPWM_IFA0_IFASEL_Msk))) | \
  944. (EPWM_IFA0_IFAEN_Msk | (u32IntAccSrc << EPWM_IFA0_IFASEL_Pos) | u32IntFlagCnt) );
  945. }
  946. /**
  947. * @brief Disable interrupt flag accumulator of selected channel
  948. * @param[in] epwm The pointer of the specified EPWM module
  949. * - EPWM0 : EPWM Group 0
  950. * - EPWM1 : EPWM Group 1
  951. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  952. * @return None
  953. * @details This function is used to Disable interrupt flag accumulator of selected channel.
  954. */
  955. void EPWM_DisableAcc(EPWM_T *epwm, uint32_t u32ChannelNum)
  956. {
  957. (epwm)->IFA[u32ChannelNum] = ((epwm)->IFA[u32ChannelNum] & ~(EPWM_IFA0_IFAEN_Msk));
  958. }
  959. /**
  960. * @brief Enable interrupt flag accumulator interrupt of selected channel
  961. * @param[in] epwm The pointer of the specified EPWM module
  962. * - EPWM0 : EPWM Group 0
  963. * - EPWM1 : EPWM Group 1
  964. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  965. * @return None
  966. * @details This function is used to enable interrupt flag accumulator interrupt of selected channel.
  967. */
  968. void EPWM_EnableAccInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  969. {
  970. (epwm)->AINTEN |= (1UL << (u32ChannelNum));
  971. }
  972. /**
  973. * @brief Disable interrupt flag accumulator interrupt of selected channel
  974. * @param[in] epwm The pointer of the specified EPWM module
  975. * - EPWM0 : EPWM Group 0
  976. * - EPWM1 : EPWM Group 1
  977. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  978. * @return None
  979. * @details This function is used to disable interrupt flag accumulator interrupt of selected channel.
  980. */
  981. void EPWM_DisableAccInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  982. {
  983. (epwm)->AINTEN &= ~(1UL << (u32ChannelNum));
  984. }
  985. /**
  986. * @brief Clear interrupt flag accumulator interrupt of selected channel
  987. * @param[in] epwm The pointer of the specified EPWM module
  988. * - EPWM0 : EPWM Group 0
  989. * - EPWM1 : EPWM Group 1
  990. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  991. * @return None
  992. * @details This function is used to clear interrupt flag accumulator interrupt of selected channel.
  993. */
  994. void EPWM_ClearAccInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  995. {
  996. (epwm)->AINTSTS = (1UL << (u32ChannelNum));
  997. }
  998. /**
  999. * @brief Get interrupt flag accumulator interrupt of selected channel
  1000. * @param[in] epwm The pointer of the specified EPWM module
  1001. * - EPWM0 : EPWM Group 0
  1002. * - EPWM1 : EPWM Group 1
  1003. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1004. * @retval 0 Accumulator interrupt did not occur
  1005. * @retval 1 Accumulator interrupt occurred
  1006. * @details This function is used to Get interrupt flag accumulator interrupt of selected channel.
  1007. */
  1008. uint32_t EPWM_GetAccInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  1009. {
  1010. return (((epwm)->AINTSTS & (1UL << (u32ChannelNum))) ? 1UL : 0UL);
  1011. }
  1012. /**
  1013. * @brief Enable accumulator PDMA of selected channel
  1014. * @param[in] epwm The pointer of the specified EPWM module
  1015. * - EPWM0 : EPWM Group 0
  1016. * - EPWM1 : EPWM Group 1
  1017. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1018. * @return None
  1019. * @details This function is used to enable accumulator interrupt trigger PDMA of selected channel.
  1020. */
  1021. void EPWM_EnableAccPDMA(EPWM_T *epwm, uint32_t u32ChannelNum)
  1022. {
  1023. (epwm)->APDMACTL |= (1UL << (u32ChannelNum));
  1024. }
  1025. /**
  1026. * @brief Disable accumulator PDMA of selected channel
  1027. * @param[in] epwm The pointer of the specified EPWM module
  1028. * - EPWM0 : EPWM Group 0
  1029. * - EPWM1 : EPWM Group 1
  1030. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1031. * @return None
  1032. * @details This function is used to disable accumulator interrupt trigger PDMA of selected channel.
  1033. */
  1034. void EPWM_DisableAccPDMA(EPWM_T *epwm, uint32_t u32ChannelNum)
  1035. {
  1036. (epwm)->APDMACTL &= ~(1UL << (u32ChannelNum));
  1037. }
  1038. /**
  1039. * @brief Enable interrupt flag accumulator stop mode of selected channel
  1040. * @param[in] epwm The pointer of the specified EPWM module
  1041. * - EPWM0 : EPWM Group 0
  1042. * - EPWM1 : EPWM Group 1
  1043. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1044. * @return None
  1045. * @details This function is used to enable interrupt flag accumulator stop mode of selected channel.
  1046. */
  1047. void EPWM_EnableAccStopMode(EPWM_T *epwm, uint32_t u32ChannelNum)
  1048. {
  1049. (epwm)->IFA[u32ChannelNum] |= EPWM_IFA0_STPMOD_Msk;
  1050. }
  1051. /**
  1052. * @brief Disable interrupt flag accumulator stop mode of selected channel
  1053. * @param[in] epwm The pointer of the specified EPWM module
  1054. * - EPWM0 : EPWM Group 0
  1055. * - EPWM1 : EPWM Group 1
  1056. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1057. * @return None
  1058. * @details This function is used to disable interrupt flag accumulator stop mode of selected channel.
  1059. */
  1060. void EPWM_DisableAccStopMode(EPWM_T *epwm, uint32_t u32ChannelNum)
  1061. {
  1062. (epwm)->IFA[u32ChannelNum] &= ~EPWM_IFA0_STPMOD_Msk;
  1063. }
  1064. /**
  1065. * @brief Clear free trigger duty interrupt flag of selected channel
  1066. * @param[in] epwm The pointer of the specified EPWM module
  1067. * - EPWM0 : EPWM Group 0
  1068. * - EPWM1 : EPWM Group 1
  1069. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1070. * @return None
  1071. * @details This function is used to clear free trigger duty interrupt flag of selected channel.
  1072. */
  1073. void EPWM_ClearFTDutyIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  1074. {
  1075. (epwm)->FTCI = ((EPWM_FTCI_FTCMU0_Msk | EPWM_FTCI_FTCMD0_Msk) << (u32ChannelNum >> 1U));
  1076. }
  1077. /**
  1078. * @brief Get free trigger duty interrupt flag of selected channel
  1079. * @param[in] epwm The pointer of the specified EPWM module
  1080. * - EPWM0 : EPWM Group 0
  1081. * - EPWM1 : EPWM Group 1
  1082. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1083. * @return Duty interrupt flag of specified channel
  1084. * @retval 0 Free trigger duty interrupt did not occur
  1085. * @retval 1 Free trigger duty interrupt occurred
  1086. * @details This function is used to get free trigger duty interrupt flag of selected channel.
  1087. */
  1088. uint32_t EPWM_GetFTDutyIntFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  1089. {
  1090. return (((epwm)->FTCI & ((EPWM_FTCI_FTCMU0_Msk | EPWM_FTCI_FTCMD0_Msk) << (u32ChannelNum >> 1U))) ? 1UL : 0UL);
  1091. }
  1092. /**
  1093. * @brief Enable load mode of selected channel
  1094. * @param[in] epwm The pointer of the specified EPWM module
  1095. * - EPWM0 : EPWM Group 0
  1096. * - EPWM1 : EPWM Group 1
  1097. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1098. * @param[in] u32LoadMode EPWM counter loading mode.
  1099. * - \ref EPWM_LOAD_MODE_IMMEDIATE
  1100. * - \ref EPWM_LOAD_MODE_WINDOW
  1101. * - \ref EPWM_LOAD_MODE_CENTER
  1102. * @return None
  1103. * @details This function is used to enable load mode of selected channel.
  1104. */
  1105. void EPWM_EnableLoadMode(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32LoadMode)
  1106. {
  1107. (epwm)->CTL0 |= (u32LoadMode << u32ChannelNum);
  1108. }
  1109. /**
  1110. * @brief Disable load mode of selected channel
  1111. * @param[in] epwm The pointer of the specified EPWM module
  1112. * - EPWM0 : EPWM Group 0
  1113. * - EPWM1 : EPWM Group 1
  1114. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1115. * @param[in] u32LoadMode EPWM counter loading mode.
  1116. * - \ref EPWM_LOAD_MODE_IMMEDIATE
  1117. * - \ref EPWM_LOAD_MODE_WINDOW
  1118. * - \ref EPWM_LOAD_MODE_CENTER
  1119. * @return None
  1120. * @details This function is used to disable load mode of selected channel.
  1121. */
  1122. void EPWM_DisableLoadMode(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32LoadMode)
  1123. {
  1124. (epwm)->CTL0 &= ~(u32LoadMode << u32ChannelNum);
  1125. }
  1126. /**
  1127. * @brief Configure synchronization phase of selected channel
  1128. * @param[in] epwm The pointer of the specified EPWM module
  1129. * - EPWM0 : EPWM Group 0
  1130. * - EPWM1 : EPWM Group 1
  1131. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1132. * @param[in] u32SyncSrc EPWM synchronize source selection.
  1133. * - \ref EPWM_SYNC_OUT_FROM_SYNCIN_SWSYNC
  1134. * - \ref EPWM_SYNC_OUT_FROM_COUNT_TO_ZERO
  1135. * - \ref EPWM_SYNC_OUT_FROM_COUNT_TO_COMPARATOR
  1136. * - \ref EPWM_SYNC_OUT_DISABLE
  1137. * @param[in] u32Direction Phase direction. Control EPWM counter count decrement or increment after synchronizing.
  1138. * - \ref EPWM_PHS_DIR_DECREMENT
  1139. * - \ref EPWM_PHS_DIR_INCREMENT
  1140. * @param[in] u32StartPhase Synchronous start phase value. Valid values are between 0~65535.
  1141. * @return None
  1142. * @details This function is used to configure synchronization phase of selected channel.
  1143. * @note Every two channels share the same setting.
  1144. */
  1145. void EPWM_ConfigSyncPhase(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32SyncSrc, uint32_t u32Direction, uint32_t u32StartPhase)
  1146. {
  1147. /* every two channels shares the same setting */
  1148. u32ChannelNum >>= 1U;
  1149. (epwm)->SYNC = (((epwm)->SYNC & ~(((3UL << EPWM_SYNC_SINSRC0_Pos) << (u32ChannelNum << 1U)) | ((1UL << EPWM_SYNC_PHSDIR0_Pos) << u32ChannelNum))) | \
  1150. (u32Direction << EPWM_SYNC_PHSDIR0_Pos << u32ChannelNum) | ((u32SyncSrc << EPWM_SYNC_SINSRC0_Pos) << (u32ChannelNum << 1U)));
  1151. (epwm)->PHS[(u32ChannelNum)] = u32StartPhase;
  1152. }
  1153. /**
  1154. * @brief Enable SYNC phase of selected channel(s)
  1155. * @param[in] epwm The pointer of the specified EPWM module
  1156. * - EPWM0 : EPWM Group 0
  1157. * - EPWM1 : EPWM Group 1
  1158. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  1159. * Bit 0 is channel 0, bit 1 is channel 1...
  1160. * @return None
  1161. * @details This function is used to enable SYNC phase of selected channel(s).
  1162. * @note Every two channels share the same setting.
  1163. */
  1164. void EPWM_EnableSyncPhase(EPWM_T *epwm, uint32_t u32ChannelMask)
  1165. {
  1166. uint32_t i;
  1167. for(i = 0U; i < EPWM_CHANNEL_NUM; i ++)
  1168. {
  1169. if(u32ChannelMask & (1UL << i))
  1170. {
  1171. (epwm)->SYNC |= ((1UL << EPWM_SYNC_PHSEN0_Pos) << (i >> 1U));
  1172. }
  1173. }
  1174. }
  1175. /**
  1176. * @brief Disable SYNC phase of selected channel(s)
  1177. * @param[in] epwm The pointer of the specified EPWM module
  1178. * - EPWM0 : EPWM Group 0
  1179. * - EPWM1 : EPWM Group 1
  1180. * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
  1181. * Bit 0 is channel 0, bit 1 is channel 1...
  1182. * @return None
  1183. * @details This function is used to disable SYNC phase of selected channel(s).
  1184. * @note Every two channels share the same setting.
  1185. */
  1186. void EPWM_DisableSyncPhase(EPWM_T *epwm, uint32_t u32ChannelMask)
  1187. {
  1188. uint32_t i;
  1189. for(i = 0U; i < EPWM_CHANNEL_NUM; i ++)
  1190. {
  1191. if(u32ChannelMask & (1UL << i))
  1192. {
  1193. (epwm)->SYNC &= ~((1UL << EPWM_SYNC_PHSEN0_Pos) << (i >> 1U));
  1194. }
  1195. }
  1196. }
  1197. /**
  1198. * @brief Enable EPWM SYNC_IN noise filter function
  1199. * @param[in] epwm The pointer of the specified EPWM module
  1200. * - EPWM0 : EPWM Group 0
  1201. * - EPWM1 : EPWM Group 1
  1202. * @param[in] u32ClkCnt SYNC Edge Detector Filter Count. This controls the counter number of edge detector.
  1203. * The valid value is 0~7.
  1204. * @param[in] u32ClkDivSel SYNC Edge Detector Filter Clock Selection.
  1205. * - \ref EPWM_NF_CLK_DIV_1
  1206. * - \ref EPWM_NF_CLK_DIV_2
  1207. * - \ref EPWM_NF_CLK_DIV_4
  1208. * - \ref EPWM_NF_CLK_DIV_8
  1209. * - \ref EPWM_NF_CLK_DIV_16
  1210. * - \ref EPWM_NF_CLK_DIV_32
  1211. * - \ref EPWM_NF_CLK_DIV_64
  1212. * - \ref EPWM_NF_CLK_DIV_128
  1213. * @return None
  1214. * @details This function is used to enable EPWM SYNC_IN noise filter function.
  1215. */
  1216. void EPWM_EnableSyncNoiseFilter(EPWM_T *epwm, uint32_t u32ClkCnt, uint32_t u32ClkDivSel)
  1217. {
  1218. (epwm)->SYNC = ((epwm)->SYNC & ~(EPWM_SYNC_SFLTCNT_Msk | EPWM_SYNC_SFLTCSEL_Msk)) | \
  1219. ((u32ClkCnt << EPWM_SYNC_SFLTCNT_Pos) | (u32ClkDivSel << EPWM_SYNC_SFLTCSEL_Pos) | EPWM_SYNC_SNFLTEN_Msk);
  1220. }
  1221. /**
  1222. * @brief Disable EPWM SYNC_IN noise filter function
  1223. * @param[in] epwm The pointer of the specified EPWM module
  1224. * - EPWM0 : EPWM Group 0
  1225. * - EPWM1 : EPWM Group 1
  1226. * @return None
  1227. * @details This function is used to Disable EPWM SYNC_IN noise filter function.
  1228. */
  1229. void EPWM_DisableSyncNoiseFilter(EPWM_T *epwm)
  1230. {
  1231. (epwm)->SYNC &= ~EPWM_SYNC_SNFLTEN_Msk;
  1232. }
  1233. /**
  1234. * @brief Enable EPWM SYNC input pin inverse function
  1235. * @param[in] epwm The pointer of the specified EPWM module
  1236. * - EPWM0 : EPWM Group 0
  1237. * - EPWM1 : EPWM Group 1
  1238. * @return None
  1239. * @details This function is used to enable EPWM SYNC input pin inverse function.
  1240. */
  1241. void EPWM_EnableSyncPinInverse(EPWM_T *epwm)
  1242. {
  1243. (epwm)->SYNC |= EPWM_SYNC_SINPINV_Msk;
  1244. }
  1245. /**
  1246. * @brief Disable EPWM SYNC input pin inverse function
  1247. * @param[in] epwm The pointer of the specified EPWM module
  1248. * - EPWM0 : EPWM Group 0
  1249. * - EPWM1 : EPWM Group 1
  1250. * @return None
  1251. * @details This function is used to Disable EPWM SYNC input pin inverse function.
  1252. */
  1253. void EPWM_DisableSyncPinInverse(EPWM_T *epwm)
  1254. {
  1255. (epwm)->SYNC &= (~EPWM_SYNC_SINPINV_Msk);
  1256. }
  1257. /**
  1258. * @brief Set EPWM clock source
  1259. * @param[in] epwm The pointer of the specified EPWM module
  1260. * - EPWM0 : EPWM Group 0
  1261. * - EPWM1 : EPWM Group 1
  1262. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1263. * @param[in] u32ClkSrcSel EPWM external clock source.
  1264. * - \ref EPWM_CLKSRC_EPWM_CLK
  1265. * - \ref EPWM_CLKSRC_TIMER0
  1266. * - \ref EPWM_CLKSRC_TIMER1
  1267. * - \ref EPWM_CLKSRC_TIMER2
  1268. * - \ref EPWM_CLKSRC_TIMER3
  1269. * @return None
  1270. * @details This function is used to set EPWM clock source.
  1271. * @note Every two channels share the same setting.
  1272. * @note If the clock source of EPWM counter is selected from TIMERn interrupt events, the TRGEPWM(TIMERn_TRGCTL[1], n=0,1..3) bit must be set as 1.
  1273. */
  1274. void EPWM_SetClockSource(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32ClkSrcSel)
  1275. {
  1276. (epwm)->CLKSRC = ((epwm)->CLKSRC & ~(EPWM_CLKSRC_ECLKSRC0_Msk << ((u32ChannelNum >> 1U) << 3U))) | \
  1277. (u32ClkSrcSel << ((u32ChannelNum >> 1U) << 3U));
  1278. }
  1279. /**
  1280. * @brief Enable EPWM brake noise filter function
  1281. * @param[in] epwm The pointer of the specified EPWM module
  1282. * - EPWM0 : EPWM Group 0
  1283. * - EPWM1 : EPWM Group 1
  1284. * @param[in] u32BrakePinNum Brake pin selection. Valid values are 0 or 1.
  1285. * @param[in] u32ClkCnt SYNC Edge Detector Filter Count. This controls the counter number of edge detector
  1286. * @param[in] u32ClkDivSel SYNC Edge Detector Filter Clock Selection.
  1287. * - \ref EPWM_NF_CLK_DIV_1
  1288. * - \ref EPWM_NF_CLK_DIV_2
  1289. * - \ref EPWM_NF_CLK_DIV_4
  1290. * - \ref EPWM_NF_CLK_DIV_8
  1291. * - \ref EPWM_NF_CLK_DIV_16
  1292. * - \ref EPWM_NF_CLK_DIV_32
  1293. * - \ref EPWM_NF_CLK_DIV_64
  1294. * - \ref EPWM_NF_CLK_DIV_128
  1295. * @return None
  1296. * @details This function is used to enable EPWM brake noise filter function.
  1297. */
  1298. void EPWM_EnableBrakeNoiseFilter(EPWM_T *epwm, uint32_t u32BrakePinNum, uint32_t u32ClkCnt, uint32_t u32ClkDivSel)
  1299. {
  1300. (epwm)->BNF = ((epwm)->BNF & ~((EPWM_BNF_BRK0FCNT_Msk | EPWM_BNF_BRK0NFSEL_Msk) << (u32BrakePinNum << 3U))) | \
  1301. (((u32ClkCnt << EPWM_BNF_BRK0FCNT_Pos) | (u32ClkDivSel << EPWM_BNF_BRK0NFSEL_Pos) | EPWM_BNF_BRK0NFEN_Msk) << (u32BrakePinNum << 3U));
  1302. }
  1303. /**
  1304. * @brief Disable EPWM brake noise filter function
  1305. * @param[in] epwm The pointer of the specified EPWM module
  1306. * - EPWM0 : EPWM Group 0
  1307. * - EPWM1 : EPWM Group 1
  1308. * @param[in] u32BrakePinNum Brake pin selection. Valid values are 0 or 1.
  1309. * @return None
  1310. * @details This function is used to disable EPWM brake noise filter function.
  1311. */
  1312. void EPWM_DisableBrakeNoiseFilter(EPWM_T *epwm, uint32_t u32BrakePinNum)
  1313. {
  1314. (epwm)->BNF &= ~(EPWM_BNF_BRK0NFEN_Msk << (u32BrakePinNum << 3U));
  1315. }
  1316. /**
  1317. * @brief Enable EPWM brake pin inverse function
  1318. * @param[in] epwm The pointer of the specified EPWM module
  1319. * - EPWM0 : EPWM Group 0
  1320. * - EPWM1 : EPWM Group 1
  1321. * @param[in] u32BrakePinNum Brake pin selection. Valid values are 0 or 1.
  1322. * @return None
  1323. * @details This function is used to enable EPWM brake pin inverse function.
  1324. */
  1325. void EPWM_EnableBrakePinInverse(EPWM_T *epwm, uint32_t u32BrakePinNum)
  1326. {
  1327. (epwm)->BNF |= (EPWM_BNF_BRK0PINV_Msk << (u32BrakePinNum << 3U));
  1328. }
  1329. /**
  1330. * @brief Disable EPWM brake pin inverse function
  1331. * @param[in] epwm The pointer of the specified EPWM module
  1332. * - EPWM0 : EPWM Group 0
  1333. * - EPWM1 : EPWM Group 1
  1334. * @param[in] u32BrakePinNum Brake pin selection. Valid values are 0 or 1.
  1335. * @return None
  1336. * @details This function is used to disable EPWM brake pin inverse function.
  1337. */
  1338. void EPWM_DisableBrakePinInverse(EPWM_T *epwm, uint32_t u32BrakePinNum)
  1339. {
  1340. (epwm)->BNF &= ~(EPWM_BNF_BRK0PINV_Msk << (u32BrakePinNum * (uint32_t)EPWM_BNF_BRK1NFEN_Pos));
  1341. }
  1342. /**
  1343. * @brief Set EPWM brake pin source
  1344. * @param[in] epwm The pointer of the specified EPWM module
  1345. * - EPWM0 : EPWM Group 0
  1346. * - EPWM1 : EPWM Group 1
  1347. * @param[in] u32BrakePinNum Brake pin selection. Valid values are 0 or 1.
  1348. * @param[in] u32SelAnotherModule Select to another module. Valid values are TRUE or FALSE.
  1349. * @return None
  1350. * @details This function is used to set EPWM brake pin source.
  1351. */
  1352. void EPWM_SetBrakePinSource(EPWM_T *epwm, uint32_t u32BrakePinNum, uint32_t u32SelAnotherModule)
  1353. {
  1354. (epwm)->BNF = ((epwm)->BNF & ~(EPWM_BNF_BK0SRC_Msk << (u32BrakePinNum << 3U))) | (u32SelAnotherModule << ((uint32_t)EPWM_BNF_BK0SRC_Pos + (u32BrakePinNum << 3U)));
  1355. }
  1356. /**
  1357. * @brief Set EPWM leading edge blanking function
  1358. * @param[in] epwm The pointer of the specified EPWM module
  1359. * - EPWM0 : EPWM Group 0
  1360. * - EPWM1 : EPWM Group 1
  1361. * @param[in] u32TrigSrcSel Leading edge blanking source selection.
  1362. * - \ref EPWM_LEBCTL_SRCEN0
  1363. * - \ref EPWM_LEBCTL_SRCEN2
  1364. * - \ref EPWM_LEBCTL_SRCEN4
  1365. * - \ref EPWM_LEBCTL_SRCEN0_2
  1366. * - \ref EPWM_LEBCTL_SRCEN0_4
  1367. * - \ref EPWM_LEBCTL_SRCEN2_4
  1368. * - \ref EPWM_LEBCTL_SRCEN0_2_4
  1369. * @param[in] u32TrigType Leading edge blanking trigger type.
  1370. * - \ref EPWM_LEBCTL_TRGTYPE_RISING
  1371. * - \ref EPWM_LEBCTL_TRGTYPE_FALLING
  1372. * - \ref EPWM_LEBCTL_TRGTYPE_RISING_OR_FALLING
  1373. * @param[in] u32BlankingCnt Leading Edge Blanking Counter. Valid values are between 1~512.
  1374. This counter value decides leading edge blanking window size, and this counter clock base is ECLK.
  1375. * @param[in] u32BlankingEnable Enable EPWM leading edge blanking function. Valid values are TRUE (ENABLE) or FALSE (DISABLE).
  1376. * - \ref FALSE
  1377. * - \ref TRUE
  1378. * @return None
  1379. * @details This function is used to configure EPWM leading edge blanking function that blank the false trigger from ACMP brake source which may cause by EPWM output transition.
  1380. * @note EPWM leading edge blanking function is only used for brake source from ACMP.
  1381. */
  1382. void EPWM_SetLeadingEdgeBlanking(EPWM_T *epwm, uint32_t u32TrigSrcSel, uint32_t u32TrigType, uint32_t u32BlankingCnt, uint32_t u32BlankingEnable)
  1383. {
  1384. (epwm)->LEBCTL = (u32TrigType) | (u32TrigSrcSel) | (u32BlankingEnable);
  1385. /* Blanking window size = LEBCNT + 1, so LEBCNT = u32BlankingCnt - 1 */
  1386. (epwm)->LEBCNT = (u32BlankingCnt) - 1U;
  1387. }
  1388. /**
  1389. * @brief Get the time-base counter reached its maximum value flag of selected channel
  1390. * @param[in] epwm The pointer of the specified EPWM module
  1391. * - EPWM0 : EPWM Group 0
  1392. * - EPWM1 : EPWM Group 1
  1393. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1394. * @return Count to max interrupt flag of specified channel
  1395. * @retval 0 Count to max interrupt did not occur
  1396. * @retval 1 Count to max interrupt occurred
  1397. * @details This function is used to get the time-base counter reached its maximum value flag of selected channel.
  1398. */
  1399. uint32_t EPWM_GetWrapAroundFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  1400. {
  1401. return (((epwm)->STATUS & (EPWM_STATUS_CNTMAXF0_Msk << u32ChannelNum)) ? 1UL : 0UL);
  1402. }
  1403. /**
  1404. * @brief Clear the time-base counter reached its maximum value flag of selected channel
  1405. * @param[in] epwm The pointer of the specified EPWM module
  1406. * - EPWM0 : EPWM Group 0
  1407. * - EPWM1 : EPWM Group 1
  1408. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5
  1409. * @return None
  1410. * @details This function is used to clear the time-base counter reached its maximum value flag of selected channel.
  1411. */
  1412. void EPWM_ClearWrapAroundFlag(EPWM_T *epwm, uint32_t u32ChannelNum)
  1413. {
  1414. (epwm)->STATUS = (EPWM_STATUS_CNTMAXF0_Msk << u32ChannelNum);
  1415. }
  1416. /**
  1417. * @brief Enable fault detect of selected channel.
  1418. * @param[in] epwm The pointer of the specified EPWM module.
  1419. * - EPWM0 : EPWM Group 0
  1420. * - EPWM1 : EPWM Group 1
  1421. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1422. * @param[in] u32AfterPrescaler Fault Detect Clock Source is from prescaler output. Valid values are TRUE (after prescaler) or FALSE (before prescaler).
  1423. * @param[in] u32ClkSel Fault Detect Clock Select.
  1424. * - \ref EPWM_FDCTL_FDCKSEL_CLK_DIV_1
  1425. * - \ref EPWM_FDCTL_FDCKSEL_CLK_DIV_2
  1426. * - \ref EPWM_FDCTL_FDCKSEL_CLK_DIV_4
  1427. * - \ref EPWM_FDCTL_FDCKSEL_CLK_DIV_8
  1428. * @return None
  1429. * @details This function is used to enable fault detect of selected channel.
  1430. */
  1431. void EPWM_EnableFaultDetect(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32AfterPrescaler, uint32_t u32ClkSel)
  1432. {
  1433. (epwm)->FDEN = ((epwm)->FDEN & ~(EPWM_FDEN_FDCKS0_Msk << (u32ChannelNum))) | \
  1434. ((EPWM_FDEN_FDEN0_Msk | ((u32AfterPrescaler) << EPWM_FDEN_FDCKS0_Pos)) << (u32ChannelNum));
  1435. (epwm)->FDCTL[(u32ChannelNum)] = ((epwm)->FDCTL[(u32ChannelNum)] & ~EPWM_FDCTL0_FDCKSEL_Msk) | (u32ClkSel);
  1436. }
  1437. /**
  1438. * @brief Disable fault detect of selected channel.
  1439. * @param[in] epwm The pointer of the specified EPWM module.
  1440. * - EPWM0 : EPWM Group 0
  1441. * - EPWM1 : EPWM Group 1
  1442. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1443. * @return None
  1444. * @details This function is used to disable fault detect of selected channel.
  1445. */
  1446. void EPWM_DisableFaultDetect(EPWM_T *epwm, uint32_t u32ChannelNum)
  1447. {
  1448. (epwm)->FDEN &= ~(EPWM_FDEN_FDEN0_Msk << (u32ChannelNum));
  1449. }
  1450. /**
  1451. * @brief Enable fault detect output of selected channel.
  1452. * @param[in] epwm The pointer of the specified EPWM module.
  1453. * - EPWM0 : EPWM Group 0
  1454. * - EPWM1 : EPWM Group 1
  1455. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1456. * @return None
  1457. * @details This function is used to enable fault detect output of selected channel.
  1458. */
  1459. void EPWM_EnableFaultDetectOutput(EPWM_T *epwm, uint32_t u32ChannelNum)
  1460. {
  1461. (epwm)->FDEN &= ~(EPWM_FDEN_FDODIS0_Msk << (u32ChannelNum));
  1462. }
  1463. /**
  1464. * @brief Disable fault detect output of selected channel.
  1465. * @param[in] epwm The pointer of the specified EPWM module.
  1466. * - EPWM0 : EPWM Group 0
  1467. * - EPWM1 : EPWM Group 1
  1468. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1469. * @return None
  1470. * @details This function is used to disable fault detect output of selected channel.
  1471. */
  1472. void EPWM_DisableFaultDetectOutput(EPWM_T *epwm, uint32_t u32ChannelNum)
  1473. {
  1474. (epwm)->FDEN |= (EPWM_FDEN_FDODIS0_Msk << (u32ChannelNum));
  1475. }
  1476. /**
  1477. * @brief Enable fault detect deglitch function of selected channel.
  1478. * @param[in] epwm The pointer of the specified EPWM module.
  1479. * - EPWM0 : EPWM Group 0
  1480. * - EPWM1 : EPWM Group 1
  1481. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1482. * @param[in] u32DeglitchSmpCycle Deglitch Sampling Cycle. Valid values are between 0~7.
  1483. * @return None
  1484. * @details This function is used to enable fault detect deglitch function of selected channel.
  1485. */
  1486. void EPWM_EnableFaultDetectDeglitch(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32DeglitchSmpCycle)
  1487. {
  1488. (epwm)->FDCTL[(u32ChannelNum)] = ((epwm)->FDCTL[(u32ChannelNum)] & (~EPWM_FDCTL0_DGSMPCYC_Msk)) | \
  1489. (EPWM_FDCTL0_FDDGEN_Msk | ((u32DeglitchSmpCycle) << EPWM_FDCTL0_DGSMPCYC_Pos));
  1490. }
  1491. /**
  1492. * @brief Disable fault detect deglitch function of selected channel.
  1493. * @param[in] epwm The pointer of the specified EPWM module.
  1494. * - EPWM0 : EPWM Group 0
  1495. * - EPWM1 : EPWM Group 1
  1496. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1497. * @return None
  1498. * @details This function is used to disable fault detect deglitch function of selected channel.
  1499. */
  1500. void EPWM_DisableFaultDetectDeglitch(EPWM_T *epwm, uint32_t u32ChannelNum)
  1501. {
  1502. (epwm)->FDCTL[(u32ChannelNum)] &= ~EPWM_FDCTL0_FDDGEN_Msk;
  1503. }
  1504. /**
  1505. * @brief Enable fault detect mask function of selected channel.
  1506. * @param[in] epwm The pointer of the specified EPWM module.
  1507. * - EPWM0 : EPWM Group 0
  1508. * - EPWM1 : EPWM Group 1
  1509. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1510. * @param[in] u32MaskCnt Transition mask counter. Valid values are between 0~0x7F.
  1511. * @return None
  1512. * @details This function is used to enable fault detect mask function of selected channel.
  1513. */
  1514. void EPWM_EnableFaultDetectMask(EPWM_T *epwm, uint32_t u32ChannelNum, uint32_t u32MaskCnt)
  1515. {
  1516. (epwm)->FDCTL[(u32ChannelNum)] = ((epwm)->FDCTL[(u32ChannelNum)] & (~EPWM_FDCTL0_TRMSKCNT_Msk)) | (EPWM_FDCTL0_FDMSKEN_Msk | (u32MaskCnt));
  1517. }
  1518. /**
  1519. * @brief Disable fault detect mask function of selected channel.
  1520. * @param[in] epwm The pointer of the specified EPWM module.
  1521. * - EPWM0 : EPWM Group 0
  1522. * - EPWM1 : EPWM Group 1
  1523. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1524. * @return None
  1525. * @details This function is used to disable fault detect mask function of selected channel.
  1526. */
  1527. void EPWM_DisableFaultDetectMask(EPWM_T *epwm, uint32_t u32ChannelNum)
  1528. {
  1529. (epwm)->FDCTL[(u32ChannelNum)] &= ~EPWM_FDCTL0_FDMSKEN_Msk;
  1530. }
  1531. /**
  1532. * @brief Enable fault detect interrupt of selected channel.
  1533. * @param[in] epwm The pointer of the specified EPWM module.
  1534. * - EPWM0 : EPWM Group 0
  1535. * - EPWM1 : EPWM Group 1
  1536. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1537. * @return None
  1538. * @details This function is used to enable fault detect interrupt of selected channel.
  1539. */
  1540. void EPWM_EnableFaultDetectInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  1541. {
  1542. (epwm)->FDIEN |= (EPWM_FDIEN_FDIEN0_Msk << (u32ChannelNum));
  1543. }
  1544. /**
  1545. * @brief Disable fault detect interrupt of selected channel.
  1546. * @param[in] epwm The pointer of the specified EPWM module.
  1547. * - EPWM0 : EPWM Group 0
  1548. * - EPWM1 : EPWM Group 1
  1549. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1550. * @return None
  1551. * @details This function is used to disable fault detect interrupt of selected channel.
  1552. */
  1553. void EPWM_DisableFaultDetectInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  1554. {
  1555. (epwm)->FDIEN &= ~(EPWM_FDIEN_FDIEN0_Msk << (u32ChannelNum));
  1556. }
  1557. /**
  1558. * @brief Clear fault detect interrupt of selected channel.
  1559. * @param[in] epwm The pointer of the specified EPWM module.
  1560. * - EPWM0 : EPWM Group 0
  1561. * - EPWM1 : EPWM Group 1
  1562. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1563. * @return None
  1564. * @details This function is used to clear fault detect interrupt of selected channel.
  1565. */
  1566. void EPWM_ClearFaultDetectInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  1567. {
  1568. (epwm)->FDSTS = (EPWM_FDSTS_FDIF0_Msk << (u32ChannelNum));
  1569. }
  1570. /**
  1571. * @brief Get fault detect interrupt of selected channel.
  1572. * @param[in] epwm The pointer of the specified EPWM module.
  1573. * - EPWM0 : EPWM Group 0
  1574. * - EPWM1 : EPWM Group 1
  1575. * @param[in] u32ChannelNum EPWM channel number. Valid values are between 0~5.
  1576. * @retval 0 Fault detect interrupt did not occur.
  1577. * @retval 1 Fault detect interrupt occurred.
  1578. * @details This function is used to Get fault detect interrupt of selected channel.
  1579. */
  1580. uint32_t EPWM_GetFaultDetectInt(EPWM_T *epwm, uint32_t u32ChannelNum)
  1581. {
  1582. return (((epwm)->FDSTS & (EPWM_FDSTS_FDIF0_Msk << (u32ChannelNum))) ? 1UL : 0UL);
  1583. }
  1584. /*@}*/ /* end of group EPWM_EXPORTED_FUNCTIONS */
  1585. /*@}*/ /* end of group EPWM_Driver */
  1586. /*@}*/ /* end of group Standard_Driver */
  1587. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/