1 /**************************************************************************//**
2  * @file     timer_pwm.c
3  * @version  V3.00
4  * @brief    Timer PWM Controller(Timer PWM) driver source file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #include "NuMicro.h"
10 
11 
12 /** @addtogroup Standard_Driver Standard Driver
13   @{
14 */
15 
16 /** @addtogroup TIMER_PWM_Driver TIMER PWM Driver
17   @{
18 */
19 
20 /** @addtogroup TIMER_PWM_EXPORTED_FUNCTIONS TIMER PWM Exported Functions
21   @{
22 */
23 
24 /**
25   * @brief      Set PWM Counter Clock Source
26   *
27   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
28   * @param[in]  u32CntClkSrc    PWM counter clock source, could be one of following source
29   *                                 - \ref TPWM_CNTR_CLKSRC_TMR_CLK
30   *                                 - \ref TPWM_CNTR_CLKSRC_TIMER0_INT
31   *                                 - \ref TPWM_CNTR_CLKSRC_TIMER1_INT
32   *                                 - \ref TPWM_CNTR_CLKSRC_TIMER2_INT
33   *                                 - \ref TPWM_CNTR_CLKSRC_TIMER3_INT
34   *
35   * @return     None
36   *
37   * @details    This function is used to set PWM counter clock source.
38   */
TPWM_SetCounterClockSource(TIMER_T * timer,uint32_t u32CntClkSrc)39 void TPWM_SetCounterClockSource(TIMER_T *timer, uint32_t u32CntClkSrc)
40 {
41     (timer)->PWMCLKSRC = ((timer)->PWMCLKSRC & ~TIMER_PWMCLKSRC_CLKSRC_Msk) | u32CntClkSrc;
42 }
43 
44 /**
45   * @brief      Configure PWM Output Frequency and Duty Cycle
46   *
47   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
48   * @param[in]  u32Frequency    Target generator frequency.
49   * @param[in]  u32DutyCycle    Target generator duty cycle percentage. Valid range are between 0~100. 10 means 10%, 20 means 20%...
50   *
51   * @return     Nearest frequency clock in nano second
52   *
53   * @details    This API is used to configure PWM output frequency and duty cycle in up count type and auto-reload operation mode.
54   * @note       This API is only available if Timer PWM counter clock source is from TMRx_CLK.
55   */
TPWM_ConfigOutputFreqAndDuty(TIMER_T * timer,uint32_t u32Frequency,uint32_t u32DutyCycle)56 uint32_t TPWM_ConfigOutputFreqAndDuty(TIMER_T *timer, uint32_t u32Frequency, uint32_t u32DutyCycle)
57 {
58     uint32_t u32PWMClockFreq, u32TargetFreq;
59     uint32_t u32Prescaler = 0x1000UL, u32Period, u32CMP;
60 
61     if((timer == TIMER0) || (timer == TIMER1))
62     {
63         u32PWMClockFreq = CLK_GetPCLK0Freq();
64     }
65     else
66     {
67         u32PWMClockFreq = CLK_GetPCLK1Freq();
68     }
69 
70     /* Calculate u16PERIOD and u16PSC */
71     for(u32Prescaler = 1UL; u32Prescaler <= 0x1000UL; u32Prescaler++)
72     {
73         u32Period = (u32PWMClockFreq / u32Prescaler) / u32Frequency;
74 
75         /* If target u32Period is larger than 0x10000, need to use a larger prescaler */
76         if(u32Period <= 0x10000UL)
77         {
78             break;
79         }
80     }
81     /* Store return value here 'cos we're gonna change u32Prescaler & u32Period to the real value to fill into register */
82     u32TargetFreq = (u32PWMClockFreq / u32Prescaler) / u32Period;
83 
84     /* Set PWM to up count type */
85     timer->PWMCTL = (timer->PWMCTL & ~TIMER_PWMCTL_CNTTYPE_Msk) | (TPWM_UP_COUNT << TIMER_PWMCTL_CNTTYPE_Pos);
86 
87     /* Set PWM to auto-reload mode */
88     timer->PWMCTL = (timer->PWMCTL & ~TIMER_PWMCTL_CNTMODE_Msk) | TPWM_AUTO_RELOAD_MODE;
89 
90     /* Convert to real register value */
91     TPWM_SET_PRESCALER(timer, (u32Prescaler - 1UL));
92 
93     TPWM_SET_PERIOD(timer, (u32Period - 1UL));
94     if(u32DutyCycle)
95     {
96         u32CMP = (u32DutyCycle * u32Period) / 100UL;
97     }
98     else
99     {
100         u32CMP = 0UL;
101     }
102 
103     TPWM_SET_CMPDAT(timer, u32CMP);
104 
105     return (u32TargetFreq);
106 }
107 
108 /**
109   * @brief      Enable Dead-Time Function
110   *
111   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
112   * @param[in]  u32DTCount  Dead-Time duration in PWM clock count, valid values are between 0x0~0xFFF, but 0x0 means there is no Dead-Time insertion.
113   *
114   * @return     None
115   *
116   * @details    This function is used to enable Dead-Time function and counter source is the same as Timer PWM clock source.
117   * @note       The register write-protection function should be disabled before using this function.
118   */
TPWM_EnableDeadTime(TIMER_T * timer,uint32_t u32DTCount)119 void TPWM_EnableDeadTime(TIMER_T *timer, uint32_t u32DTCount)
120 {
121     timer->PWMDTCTL = TIMER_PWMDTCTL_DTEN_Msk | u32DTCount;
122 }
123 
124 /**
125   * @brief      Enable Dead-Time Function
126   *
127   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
128   * @param[in]  u32DTCount  Dead-Time duration in PWM clock count, valid values are between 0x0~0xFFF, but 0x0 means there is no Dead-Time insertion.
129   *
130   * @return     None
131   *
132   * @details    This function is used to enable Dead-Time function and counter source is the Timer PWM clock source with prescale.
133   * @note       The register write-protection function should be disabled before using this function.
134   */
TPWM_EnableDeadTimeWithPrescale(TIMER_T * timer,uint32_t u32DTCount)135 void TPWM_EnableDeadTimeWithPrescale(TIMER_T *timer, uint32_t u32DTCount)
136 {
137     timer->PWMDTCTL = TIMER_PWMDTCTL_DTCKSEL_Msk | TIMER_PWMDTCTL_DTEN_Msk | u32DTCount;
138 }
139 
140 /**
141   * @brief      Disable Dead-Time Function
142   *
143   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
144   *
145   * @return     None
146   *
147   * @details    This function is used to disable Dead-time of selected channel.
148   * @note       The register write-protection function should be disabled before using this function.
149   */
TPWM_DisableDeadTime(TIMER_T * timer)150 void TPWM_DisableDeadTime(TIMER_T *timer)
151 {
152     timer->PWMDTCTL = 0x0UL;
153 }
154 
155 /**
156   * @brief      Enable PWM Counter
157   *
158   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
159   *
160   * @return     None
161   *
162   * @details    This function is used to enable PWM generator and start counter counting.
163   */
TPWM_EnableCounter(TIMER_T * timer)164 void TPWM_EnableCounter(TIMER_T *timer)
165 {
166     timer->PWMCTL |= TIMER_PWMCTL_CNTEN_Msk;
167 }
168 
169 /**
170   * @brief      Disable PWM Generator
171   *
172   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
173   *
174   * @return     None
175   *
176   * @details This function is used to disable PWM counter immediately by clear CNTEN (TIMERx_PWMCTL[0]) bit.
177   */
TPWM_DisableCounter(TIMER_T * timer)178 void TPWM_DisableCounter(TIMER_T *timer)
179 {
180     timer->PWMCTL &= ~TIMER_PWMCTL_CNTEN_Msk;
181 }
182 
183 /**
184   * @brief      Enable Trigger ADC
185   *
186   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
187   * @param[in]  u32Condition    The condition to trigger EADC. It could be one of following conditions:
188   *                                 - \ref TPWM_TRIGGER_EADC_AT_ZERO_POINT
189   *                                 - \ref TPWM_TRIGGER_EADC_AT_PERIOD_POINT
190   *                                 - \ref TPWM_TRIGGER_EADC_AT_ZERO_OR_PERIOD_POINT
191   *                                 - \ref TPWM_TRIGGER_EADC_AT_COMPARE_UP_COUNT_POINT
192   *                                 - \ref TPWM_TRIGGER_EADC_AT_COMPARE_DOWN_COUNT_POINT
193   *
194   * @return     None
195   *
196   * @details    This function is used to enable specified counter compare event to trigger EADC.
197   */
TPWM_EnableTriggerADC(TIMER_T * timer,uint32_t u32Condition)198 void TPWM_EnableTriggerADC(TIMER_T *timer, uint32_t u32Condition)
199 {
200     timer->PWMTRGCTL = TIMER_PWMTRGCTL_TRGEADC_Msk | u32Condition;
201 }
202 
203 /**
204   * @brief      Disable Trigger ADC
205   *
206   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
207   *
208   * @return     None
209   *
210   * @details    This function is used to disable counter compare event to trigger ADC.
211   */
TPWM_DisableTriggerADC(TIMER_T * timer)212 void TPWM_DisableTriggerADC(TIMER_T *timer)
213 {
214     timer->PWMTRGCTL = 0x0UL;
215 }
216 
217 /**
218   * @brief      Enable Fault Brake Function
219   *
220   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
221   * @param[in]  u32CH0Level     PWMx_CH0 output level while fault brake event occurs. Valid value is one of following setting
222   *                                 - \ref TPWM_OUTPUT_TOGGLE
223   *                                 - \ref TPWM_OUTPUT_NOTHING
224   *                                 - \ref TPWM_OUTPUT_LOW
225   *                                 - \ref TPWM_OUTPUT_HIGH
226   * @param[in]  u32CH1Level     PWMx_CH1 output level while fault brake event occurs. Valid value is one of following setting
227   *                                 - \ref TPWM_OUTPUT_TOGGLE
228   *                                 - \ref TPWM_OUTPUT_NOTHING
229   *                                 - \ref TPWM_OUTPUT_LOW
230   *                                 - \ref TPWM_OUTPUT_HIGH
231   * @param[in]  u32BrakeSource  Fault brake source, combination of following source
232   *                                 - \ref TPWM_BRAKE_SOURCE_EDGE_ACMP0
233   *                                 - \ref TPWM_BRAKE_SOURCE_EDGE_ACMP1
234   *                                 - \ref TPWM_BRAKE_SOURCE_EDGE_BKPIN
235   *                                 - \ref TPWM_BRAKE_SOURCE_EDGE_SYS_CSS
236   *                                 - \ref TPWM_BRAKE_SOURCE_EDGE_SYS_BOD
237   *                                 - \ref TPWM_BRAKE_SOURCE_EDGE_SYS_COR
238   *                                 - \ref TPWM_BRAKE_SOURCE_EDGE_SYS_RAM
239   *                                 - \ref TPWM_BRAKE_SOURCE_LEVEL_ACMP0
240   *                                 - \ref TPWM_BRAKE_SOURCE_LEVEL_ACMP1
241   *                                 - \ref TPWM_BRAKE_SOURCE_LEVEL_BKPIN
242   *                                 - \ref TPWM_BRAKE_SOURCE_LEVEL_SYS_CSS
243   *                                 - \ref TPWM_BRAKE_SOURCE_LEVEL_SYS_BOD
244   *                                 - \ref TPWM_BRAKE_SOURCE_LEVEL_SYS_COR
245   *                                 - \ref TPWM_BRAKE_SOURCE_LEVEL_SYS_RAM
246   *
247   * @return     None
248   *
249   * @details    This function is used to enable fault brake function.
250   * @note       The register write-protection function should be disabled before using this function.
251   */
TPWM_EnableFaultBrake(TIMER_T * timer,uint32_t u32CH0Level,uint32_t u32CH1Level,uint32_t u32BrakeSource)252 void TPWM_EnableFaultBrake(TIMER_T *timer, uint32_t u32CH0Level, uint32_t u32CH1Level, uint32_t u32BrakeSource)
253 {
254     timer->PWMFAILBRK |= ((u32BrakeSource >> 16) & 0xFUL);
255     timer->PWMBRKCTL = (timer->PWMBRKCTL & ~(TIMER_PWMBRKCTL_BRKAEVEN_Msk | TIMER_PWMBRKCTL_BRKAODD_Msk)) |
256                        (u32BrakeSource & 0xFFFFUL) | (u32CH0Level << TIMER_PWMBRKCTL_BRKAEVEN_Pos) | (u32CH1Level << TIMER_PWMBRKCTL_BRKAODD_Pos);
257 }
258 
259 /**
260   * @brief      Enable Fault Brake Interrupt
261   *
262   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
263   * @param[in]  u32IntSource    Interrupt source, could be one of following source
264   *                                 - \ref TPWM_BRAKE_EDGE
265   *                                 - \ref TPWM_BRAKE_LEVEL
266   *
267   * @return     None
268   *
269   * @details    This function is used to enable fault brake interrupt.
270   * @note       The register write-protection function should be disabled before using this function.
271   */
TPWM_EnableFaultBrakeInt(TIMER_T * timer,uint32_t u32IntSource)272 void TPWM_EnableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
273 {
274     timer->PWMINTEN1 |= u32IntSource;
275 }
276 
277 /**
278   * @brief      Disable Fault Brake Interrupt
279   *
280   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
281   * @param[in]  u32IntSource    Interrupt source, could be one of following source
282   *                                 - \ref TPWM_BRAKE_EDGE
283   *                                 - \ref TPWM_BRAKE_LEVEL
284   *
285   * @return     None
286   *
287   * @details    This function is used to disable fault brake interrupt.
288   * @note       The register write-protection function should be disabled before using this function.
289   */
TPWM_DisableFaultBrakeInt(TIMER_T * timer,uint32_t u32IntSource)290 void TPWM_DisableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
291 {
292     timer->PWMINTEN1 &= ~u32IntSource;
293 }
294 
295 /**
296   * @brief      Indicate Fault Brake Interrupt Flag
297   *
298   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
299   * @param[in]  u32IntSource    Interrupt source, could be one of following source
300   *                                 - \ref TPWM_BRAKE_EDGE
301   *                                 - \ref TPWM_BRAKE_LEVEL
302   *
303   * @return     Fault brake interrupt flag of specified source
304   * @retval     0       Fault brake interrupt did not occurred
305   * @retval     1       Fault brake interrupt occurred
306   *
307   * @details    This function is used to indicate fault brake interrupt flag occurred or not of selected source.
308   */
TPWM_GetFaultBrakeIntFlag(TIMER_T * timer,uint32_t u32IntSource)309 uint32_t TPWM_GetFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
310 {
311     return ((timer->PWMINTSTS1 & (0x3UL << u32IntSource))? 1UL : 0UL);
312 }
313 
314 /**
315   * @brief      Clear Fault Brake Interrupt Flags
316   *
317   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
318   * @param[in]  u32IntSource    Interrupt source, could be one of following source
319   *                                 - \ref TPWM_BRAKE_EDGE
320   *                                 - \ref TPWM_BRAKE_LEVEL
321   *
322   * @return     None
323   *
324   * @details    This function is used to clear fault brake interrupt flags of selected source.
325   * @note       The register write-protection function should be disabled before using this function.
326   */
TPWM_ClearFaultBrakeIntFlag(TIMER_T * timer,uint32_t u32IntSource)327 void TPWM_ClearFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
328 {
329     timer->PWMINTSTS1 = (0x3UL << u32IntSource);
330 }
331 
332 /**
333   * @brief      Enable Load Mode of Selected Channel
334   *
335   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
336   * @param[in]  u32LoadMode  Timer PWM counter loading mode, could be one of following mode
337   *                             - \ref TPWM_LOAD_MODE_PERIOD
338   *                             - \ref TPWM_LOAD_MODE_IMMEDIATE
339   *                             - \ref TPWM_LOAD_MODE_CENTER
340   *
341   * @return     None
342   *
343   * @details    This function is used to enable load mode of selected channel.
344   * @note       The default loading mode is period loading mode.
345   */
TPWM_SetLoadMode(TIMER_T * timer,uint32_t u32LoadMode)346 void TPWM_SetLoadMode(TIMER_T *timer, uint32_t u32LoadMode)
347 {
348     timer->PWMCTL = (timer->PWMCTL & ~(TIMER_PWMCTL_IMMLDEN_Msk | TIMER_PWMCTL_CTRLD_Msk)) | u32LoadMode;
349 }
350 
351 /**
352   * @brief      Enable Brake Pin Noise Filter Function
353   *
354   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
355   * @param[in]  u32BrakePinSrc  The external brake pin source, could be one of following source
356   *                                 - \ref TPWM_TM_BRAKE0
357   *                                 - \ref TPWM_TM_BRAKE1
358   *                                 - \ref TPWM_TM_BRAKE2
359   *                                 - \ref TPWM_TM_BRAKE3
360   * @param[in]  u32DebounceCnt  This value controls the real debounce sample time.
361   *                             The target debounce sample time is (debounce sample clock period) * (u32DebounceCnt).
362   * @param[in]  u32ClkSrcSel    Brake pin detector debounce clock source, could be one of following source
363   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_1
364   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_2
365   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_4
366   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_8
367   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_16
368   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_32
369   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_64
370   *                                 - \ref TPWM_BKP_DBCLK_PCLK_DIV_128
371   *
372   * @return     None
373   *
374   * @details    This function is used to enable external brake pin detector noise filter function.
375   */
TPWM_EnableBrakePinDebounce(TIMER_T * timer,uint32_t u32BrakePinSrc,uint32_t u32DebounceCnt,uint32_t u32ClkSrcSel)376 void TPWM_EnableBrakePinDebounce(TIMER_T *timer, uint32_t u32BrakePinSrc, uint32_t u32DebounceCnt, uint32_t u32ClkSrcSel)
377 {
378     timer->PWMBNF = (timer->PWMBNF & ~(TIMER_PWMBNF_BKPINSRC_Msk | TIMER_PWMBNF_BRKFCNT_Msk | TIMER_PWMBNF_BRKNFSEL_Msk)) |
379                     (u32BrakePinSrc << TIMER_PWMBNF_BKPINSRC_Pos) |
380                     (u32DebounceCnt << TIMER_PWMBNF_BRKFCNT_Pos) |
381                     (u32ClkSrcSel << TIMER_PWMBNF_BRKNFSEL_Pos) | TIMER_PWMBNF_BRKNFEN_Msk;
382 }
383 
384 /**
385   * @brief      Disable Brake Pin Noise Filter Function
386   *
387   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
388   *
389   * @return     None
390   *
391   * @details    This function is used to disable external brake pin detector noise filter function.
392   */
TPWM_DisableBrakePinDebounce(TIMER_T * timer)393 void TPWM_DisableBrakePinDebounce(TIMER_T *timer)
394 {
395     timer->PWMBNF &= ~TIMER_PWMBNF_BRKNFEN_Msk;
396 }
397 
398 
399 /**
400   * @brief      Enable Brake Pin Inverse Function
401   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
402   * @return     None
403   * @details    This function is used to enable PWM brake pin inverse function.
404   */
TPWM_EnableBrakePinInverse(TIMER_T * timer)405 void TPWM_EnableBrakePinInverse(TIMER_T *timer)
406 {
407     timer->PWMBNF |= TIMER_PWMBNF_BRKPINV_Msk;
408 }
409 
410 /**
411   * @brief      Disable Brake Pin Inverse Function
412   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
413   * @return     None
414   * @details    This function is used to disable PWM brake pin inverse function.
415   */
TPWM_DisableBrakePinInverse(TIMER_T * timer)416 void TPWM_DisableBrakePinInverse(TIMER_T *timer)
417 {
418     timer->PWMBNF &= ~TIMER_PWMBNF_BRKPINV_Msk;
419 }
420 
421 /**
422   * @brief      Set Brake Pin Source
423   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
424   * @param[in]  u32BrakePinNum  Brake pin selection. One of the following:
425   *                                 - \ref TPWM_TM_BRAKE0
426   *                                 - \ref TPWM_TM_BRAKE1
427   *                                 - \ref TPWM_TM_BRAKE2
428   *                                 - \ref TPWM_TM_BRAKE3
429   * @return None
430   * @details This function is used to set PWM brake pin source.
431   */
TPWM_SetBrakePinSource(TIMER_T * timer,uint32_t u32BrakePinNum)432 void TPWM_SetBrakePinSource(TIMER_T *timer, uint32_t u32BrakePinNum)
433 {
434     timer->PWMBNF = (((timer)->PWMBNF & ~TIMER_PWMBNF_BKPINSRC_Msk) | (u32BrakePinNum << TIMER_PWMBNF_BKPINSRC_Pos));
435 }
436 
437 /**
438   * @brief      Enable Interrupt Flag Accumulator
439   * @param[in]  timer           The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
440   * @param[in]  u32IntFlagCnt   Interrupt flag counter. Valid values are between 0~65535.
441   * @param[in]  u32IntAccSrc    Interrupt flag accumulator source selection.
442   *                                 - \ref TPWM_IFA_ZERO_POINT
443   *                                 - \ref TPWM_IFA_PERIOD_POINT
444   *                                 - \ref TPWM_IFA_COMPARE_UP_COUNT_POINT
445   *                                 - \ref TPWM_IFA_COMPARE_DOWN_COUNT_POINT
446   * @return     None
447   * @details    This function is used to enable interrupt flag accumulator.
448   */
TPWM_EnableAcc(TIMER_T * timer,uint32_t u32IntFlagCnt,uint32_t u32IntAccSrc)449 void TPWM_EnableAcc(TIMER_T *timer, uint32_t u32IntFlagCnt, uint32_t u32IntAccSrc)
450 {
451     timer->PWMIFA = (((timer)->PWMIFA & ~(TIMER_PWMIFA_IFACNT_Msk | TIMER_PWMIFA_IFASEL_Msk | TIMER_PWMIFA_STPMOD_Msk))
452                         | (TIMER_PWMIFA_IFAEN_Msk | (u32IntFlagCnt << TIMER_PWMIFA_IFACNT_Pos) | (u32IntAccSrc << TIMER_PWMIFA_IFASEL_Pos)));
453 }
454 
455 /**
456   * @brief      Disable Interrupt Flag Accumulator
457   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
458   * @return     None
459   * @details    This function is used to disable interrupt flag accumulator.
460   */
TPWM_DisableAcc(TIMER_T * timer)461 void TPWM_DisableAcc(TIMER_T *timer)
462 {
463     timer->PWMIFA &= ~TIMER_PWMIFA_IFAEN_Msk;
464 }
465 
466 /**
467   * @brief      Enable Interrupt Flag Accumulator Interrupt Function
468   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
469   * @return     None
470   * @details    This function is used to enable interrupt flag accumulator interrupt.
471   */
TPWM_EnableAccInt(TIMER_T * timer)472 void TPWM_EnableAccInt(TIMER_T *timer)
473 {
474     timer->PWMAINTEN |= TIMER_PWMAINTEN_IFAIEN_Msk;
475 }
476 
477 /**
478   * @brief      Disable Interrupt Flag Accumulator Interrupt Function
479   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
480   * @return     None
481   * @details    This function is used to disable interrupt flag accumulator interrupt.
482   */
TPWM_DisableAccInt(TIMER_T * timer)483 void TPWM_DisableAccInt(TIMER_T *timer)
484 {
485     timer->PWMAINTEN &= ~TIMER_PWMAINTEN_IFAIEN_Msk;
486 }
487 
488 /**
489   * @brief      Clear Interrupt Flag Accumulator Interrupt Flag
490   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
491   * @return     None
492   * @details    This function is used to clear interrupt flag accumulator interrupt.
493   */
TPWM_ClearAccInt(TIMER_T * timer)494 void TPWM_ClearAccInt(TIMER_T *timer)
495 {
496     timer->PWMAINTSTS = TIMER_PWMAINTSTS_IFAIF_Msk;
497 }
498 
499 /**
500   * @brief      Get Interrupt Flag Accumulator Interrupt Flag
501   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
502   * @retval     0   Accumulator interrupt did not occur
503   * @retval     1   Accumulator interrupt occurred
504   * @details    This function is used to get interrupt flag accumulator interrupt.
505   */
TPWM_GetAccInt(TIMER_T * timer)506 uint32_t TPWM_GetAccInt(TIMER_T *timer)
507 {
508     return (((timer)->PWMAINTSTS & TIMER_PWMAINTSTS_IFAIF_Msk)? 1UL : 0UL);
509 }
510 
511 /**
512   * @brief      Enable Accumulator Interrupt Trigger PDMA
513   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
514   * @return     None
515   * @details    This function is used to enable accumulator interrupt trigger PDMA transfer.
516   */
TPWM_EnableAccPDMA(TIMER_T * timer)517 void TPWM_EnableAccPDMA(TIMER_T *timer)
518 {
519     timer->PWMAPDMACTL |= TIMER_PWMAPDMACTL_APDMAEN_Msk;
520 }
521 
522 /**
523   * @brief      Disable Accumulator Interrupt Trigger PDMA
524   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
525   * @return     None
526   * @details    This function is used to disable accumulator interrupt trigger PDMA transfer.
527   */
TPWM_DisableAccPDMA(TIMER_T * timer)528 void TPWM_DisableAccPDMA(TIMER_T *timer)
529 {
530     timer->PWMAPDMACTL &= ~TIMER_PWMAPDMACTL_APDMAEN_Msk;
531 }
532 
533 /**
534   * @brief      Enable Interrupt Flag Accumulator Stop Mode
535   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
536   * @return     None
537   * @details    This function is used to enable interrupt flag accumulator event to stop PWM counting.
538   */
TPWM_EnableAccStopMode(TIMER_T * timer)539 void TPWM_EnableAccStopMode(TIMER_T *timer)
540 {
541     timer->PWMIFA |= TIMER_PWMIFA_STPMOD_Msk;
542 }
543 
544 /**
545   * @brief      Disable Interrupt Flag Accumulator Stop Mode
546   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
547   * @return     None
548   * @details    This function is used to disable interrupt flag accumulator event to stop PWM counting.
549   */
TPWM_DisableAccStopMode(TIMER_T * timer)550 void TPWM_DisableAccStopMode(TIMER_T *timer)
551 {
552     timer->PWMIFA &= ~TIMER_PWMIFA_STPMOD_Msk;
553 }
554 
555 /**
556   * @brief      Enable External Event Trigger Counter Action
557   * @param[in]  timer               The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
558   * @param[in]  u32ExtEventSrc      External event source selection.
559   *                                 - \ref TPWM_EXT_TGR_PIN_INT0
560   *                                 - \ref TPWM_EXT_TGR_PIN_INT1
561   *                                 - \ref TPWM_EXT_TGR_PIN_INT2
562   *                                 - \ref TPWM_EXT_TGR_PIN_INT3
563   *                                 - \ref TPWM_EXT_TGR_PIN_INT4
564   *                                 - \ref TPWM_EXT_TGR_PIN_INT5
565   *                                 - \ref TPWM_EXT_TGR_PIN_INT6
566   *                                 - \ref TPWM_EXT_TGR_PIN_INT7
567   * @param[in]  u32CounterAction    Counter action selection.
568   *                                 - \ref TPWM_EXT_TGR_COUNTER_RESET
569   *                                 - \ref TPWM_EXT_TGR_COUNTER_START
570   *                                 - \ref TPWM_EXT_TGR_COUNTER_RESET_AND_START
571   * @return     None
572   * @details    This function is used to enable external event to trigger the counter specified action.
573   */
TPWM_EnableExtEventTrigger(TIMER_T * timer,uint32_t u32ExtEventSrc,uint32_t u32CounterAction)574 void TPWM_EnableExtEventTrigger(TIMER_T *timer, uint32_t u32ExtEventSrc, uint32_t u32CounterAction)
575 {
576     timer->PWMEXTETCTL = (((timer)->PWMEXTETCTL & ~(TIMER_PWMEXTETCTL_EXTTRGS_Msk | TIMER_PWMEXTETCTL_CNTACTS_Msk))
577                         | (TIMER_PWMEXTETCTL_EXTETEN_Msk | (u32ExtEventSrc << TIMER_PWMEXTETCTL_EXTTRGS_Pos) | (u32CounterAction << TIMER_PWMEXTETCTL_CNTACTS_Pos)));
578 }
579 
580 /**
581   * @brief      Disable External Event Trigger Counter Action
582   * @param[in]  timer       The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3.
583   * @return     None
584   * @details    This function is used to disable external event to trigger counter action.
585   */
TPWM_DisableExtEventTrigger(TIMER_T * timer)586 void TPWM_DisableExtEventTrigger(TIMER_T *timer)
587 {
588     timer->PWMEXTETCTL &= ~TIMER_PWMEXTETCTL_EXTETEN_Msk;
589 }
590 
591 /*@}*/ /* end of group TIMER_PWM_EXPORTED_FUNCTIONS */
592 
593 /*@}*/ /* end of group TIMER_PWM_Driver */
594 
595 /*@}*/ /* end of group Standard_Driver */
596