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