| M480 BSP V3.05.001 The Board Support Package for M480 Series

timer_pwm.c
Go to the documentation of this file.
1 /**************************************************************************/
8 #include "NuMicro.h"
9
10
38 void TPWM_SetCounterClockSource(TIMER_T *timer, uint32_t u32CntClkSrc)
39 {
40 (timer)->PWMCLKSRC = ((timer)->PWMCLKSRC & ~TIMER_PWMCLKSRC_CLKSRC_Msk) | u32CntClkSrc;
41 }
42
55 uint32_t TPWM_ConfigOutputFreqAndDuty(TIMER_T *timer, uint32_t u32Frequency, uint32_t u32DutyCycle)
56 {
57 uint32_t u32PWMClockFreq, u32TargetFreq;
58 uint32_t u32Prescaler = 0x1000UL, u32Period, u32CMP;
59
60 if((timer == TIMER0) || (timer == TIMER1))
61 {
62 u32PWMClockFreq = CLK_GetPCLK0Freq();
63 }
64 else
65 {
66 u32PWMClockFreq = CLK_GetPCLK1Freq();
67 }
68
69 /* Calculate u16PERIOD and u16PSC */
70 for(u32Prescaler = 1UL; u32Prescaler <= 0x1000UL; u32Prescaler++)
71 {
72 u32Period = (u32PWMClockFreq / u32Prescaler) / u32Frequency;
73
74 /* If target u32Period is larger than 0x10000, need to use a larger prescaler */
75 if(u32Period <= 0x10000UL)
76 {
77 break;
78 }
79 }
80 /* Store return value here 'cos we're gonna change u32Prescaler & u32Period to the real value to fill into register */
81 u32TargetFreq = (u32PWMClockFreq / u32Prescaler) / u32Period;
82
83 /* Set PWM to up count type */
84 timer->PWMCTL = (timer->PWMCTL & ~TIMER_PWMCTL_CNTTYPE_Msk) | (TPWM_UP_COUNT << TIMER_PWMCTL_CNTTYPE_Pos);
85
86 /* Set PWM to auto-reload mode */
87 timer->PWMCTL = (timer->PWMCTL & ~TIMER_PWMCTL_CNTMODE_Msk) | TPWM_AUTO_RELOAD_MODE;
88
89 /* Convert to real register value */
90 TPWM_SET_PRESCALER(timer, (u32Prescaler - 1UL));
91
92 TPWM_SET_PERIOD(timer, (u32Period - 1UL));
93 if(u32DutyCycle)
94 {
95 u32CMP = (u32DutyCycle * u32Period) / 100UL;
96 }
97 else
98 {
99 u32CMP = 0UL;
100 }
101
102 TPWM_SET_CMPDAT(timer, u32CMP);
103
104 return (u32TargetFreq);
105 }
106
118 void TPWM_EnableDeadTime(TIMER_T *timer, uint32_t u32DTCount)
119 {
120 timer->PWMDTCTL = TIMER_PWMDTCTL_DTEN_Msk | u32DTCount;
121 }
122
134 void TPWM_EnableDeadTimeWithPrescale(TIMER_T *timer, uint32_t u32DTCount)
135 {
136 timer->PWMDTCTL = TIMER_PWMDTCTL_DTCKSEL_Msk | TIMER_PWMDTCTL_DTEN_Msk | u32DTCount;
137 }
138
149 void TPWM_DisableDeadTime(TIMER_T *timer)
150 {
151 timer->PWMDTCTL = 0x0UL;
152 }
153
163 void TPWM_EnableCounter(TIMER_T *timer)
164 {
165 timer->PWMCTL |= TIMER_PWMCTL_CNTEN_Msk;
166 }
167
177 void TPWM_DisableCounter(TIMER_T *timer)
178 {
179 timer->PWMCTL &= ~TIMER_PWMCTL_CNTEN_Msk;
180 }
181
197 void TPWM_EnableTriggerADC(TIMER_T *timer, uint32_t u32Condition)
198 {
199 timer->PWMEADCTS = TIMER_PWMEADCTS_TRGEN_Msk | u32Condition;
200 }
201
211 void TPWM_DisableTriggerADC(TIMER_T *timer)
212 {
213 timer->PWMEADCTS = 0x0UL;
214 }
215
251 void TPWM_EnableFaultBrake(TIMER_T *timer, uint32_t u32CH0Level, uint32_t u32CH1Level, uint32_t u32BrakeSource)
252 {
253 timer->PWMFAILBRK |= ((u32BrakeSource >> 16) & 0xFUL);
254 timer->PWMBRKCTL = (timer->PWMBRKCTL & ~(TIMER_PWMBRKCTL_BRKAEVEN_Msk | TIMER_PWMBRKCTL_BRKAODD_Msk)) |
255 (u32BrakeSource & 0xFFFFUL) | (u32CH0Level << TIMER_PWMBRKCTL_BRKAEVEN_Pos) | (u32CH1Level << TIMER_PWMBRKCTL_BRKAODD_Pos);
256 }
257
271 void TPWM_EnableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
272 {
273 timer->PWMINTEN1 |= u32IntSource;
274 }
275
289 void TPWM_DisableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
290 {
291 timer->PWMINTEN1 &= ~u32IntSource;
292 }
293
308 uint32_t TPWM_GetFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
309 {
310 return ((timer->PWMINTSTS1 & (0x3UL << u32IntSource))? 1UL : 0UL);
311 }
312
326 void TPWM_ClearFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
327 {
328 timer->PWMINTSTS1 = (0x3UL << u32IntSource);
329 }
330
345 void TPWM_SetLoadMode(TIMER_T *timer, uint32_t u32LoadMode)
346 {
347 timer->PWMCTL = (timer->PWMCTL & ~(TIMER_PWMCTL_IMMLDEN_Msk | TIMER_PWMCTL_CTRLD_Msk)) | u32LoadMode;
348 }
349
375 void TPWM_EnableBrakePinDebounce(TIMER_T *timer, uint32_t u32BrakePinSrc, uint32_t u32DebounceCnt, uint32_t u32ClkSrcSel)
376 {
377 timer->PWMBNF = (timer->PWMBNF & ~(TIMER_PWMBNF_BKPINSRC_Msk | TIMER_PWMBNF_BRKFCNT_Msk | TIMER_PWMBNF_BRKNFSEL_Msk)) |
378 (u32BrakePinSrc << TIMER_PWMBNF_BKPINSRC_Pos) |
379 (u32DebounceCnt << TIMER_PWMBNF_BRKFCNT_Pos) |
380 (u32ClkSrcSel << TIMER_PWMBNF_BRKNFSEL_Pos) | TIMER_PWMBNF_BRKNFEN_Msk;
381 }
382
392 void TPWM_DisableBrakePinDebounce(TIMER_T *timer)
393 {
394 timer->PWMBNF &= ~TIMER_PWMBNF_BRKNFEN_Msk;
395 }
396
397
404 void TPWM_EnableBrakePinInverse(TIMER_T *timer)
405 {
406 timer->PWMBNF |= TIMER_PWMBNF_BRKPINV_Msk;
407 }
408
415 void TPWM_DisableBrakePinInverse(TIMER_T *timer)
416 {
417 timer->PWMBNF &= ~TIMER_PWMBNF_BRKPINV_Msk;
418 }
419
431 void TPWM_SetBrakePinSource(TIMER_T *timer, uint32_t u32BrakePinNum)
432 {
433 timer->PWMBNF = (((timer)->PWMBNF & ~TIMER_PWMBNF_BKPINSRC_Msk) | (u32BrakePinNum << TIMER_PWMBNF_BKPINSRC_Pos));
434 }
435
436 /* end of group TIMER_PWM_EXPORTED_FUNCTIONS */
438 /* end of group TIMER_PWM_Driver */
440 /* end of group Standard_Driver */
442
443 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/
TIMER_PWMBNF_BRKNFEN_Msk
#define TIMER_PWMBNF_BRKNFEN_Msk
Definition: timer_reg.h:1824
TPWM_UP_COUNT
#define TPWM_UP_COUNT
Definition: timer_pwm.h:38
TIMER_PWMBRKCTL_BRKAEVEN_Msk
#define TIMER_PWMBRKCTL_BRKAEVEN_Msk
Definition: timer_reg.h:1875
TPWM_DisableBrakePinInverse
void TPWM_DisableBrakePinInverse(TIMER_T *timer)
Disable brake pin inverse function.
Definition: timer_pwm.c:415
TIMER_T::PWMBRKCTL
__IO uint32_t PWMBRKCTL
Definition: timer_reg.h:1630
TIMER_PWMEADCTS_TRGEN_Msk
#define TIMER_PWMEADCTS_TRGEN_Msk
Definition: timer_reg.h:1956
TPWM_EnableTriggerADC
void TPWM_EnableTriggerADC(TIMER_T *timer, uint32_t u32Condition)
Enable Trigger ADC.
Definition: timer_pwm.c:197
TIMER_PWMCTL_IMMLDEN_Msk
#define TIMER_PWMCTL_IMMLDEN_Msk
Definition: timer_reg.h:1770
TIMER_T::PWMEADCTS
__IO uint32_t PWMEADCTS
Definition: timer_reg.h:1638
TPWM_DisableTriggerADC
void TPWM_DisableTriggerADC(TIMER_T *timer)
Disable Trigger ADC.
Definition: timer_pwm.c:211
TIMER_PWMBNF_BRKFCNT_Pos
#define TIMER_PWMBNF_BRKFCNT_Pos
Definition: timer_reg.h:1829
TIMER_T::PWMINTSTS1
__IO uint32_t PWMINTSTS1
Definition: timer_reg.h:1637
TIMER_T::PWMBNF
__IO uint32_t PWMBNF
Definition: timer_reg.h:1628
TIMER_PWMCTL_CNTTYPE_Msk
#define TIMER_PWMCTL_CNTTYPE_Msk
Definition: timer_reg.h:1761
TIMER1
#define TIMER1
Definition: M480.h:412
TPWM_DisableDeadTime
void TPWM_DisableDeadTime(TIMER_T *timer)
Disable Dead-Time Function.
Definition: timer_pwm.c:149
TPWM_SetCounterClockSource
void TPWM_SetCounterClockSource(TIMER_T *timer, uint32_t u32CntClkSrc)
Set PWM Counter Clock Source.
Definition: timer_pwm.c:38
TIMER_T::PWMCTL
__IO uint32_t PWMCTL
Definition: timer_reg.h:1618
TIMER_PWMCTL_CNTEN_Msk
#define TIMER_PWMCTL_CNTEN_Msk
Definition: timer_reg.h:1758
TIMER_PWMBNF_BKPINSRC_Msk
#define TIMER_PWMBNF_BKPINSRC_Msk
Definition: timer_reg.h:1836
TIMER_PWMBNF_BRKNFSEL_Pos
#define TIMER_PWMBNF_BRKNFSEL_Pos
Definition: timer_reg.h:1826
TIMER0
#define TIMER0
Definition: M480.h:411
NuMicro.h
NuMicro peripheral access layer header file.
TIMER_PWMBRKCTL_BRKAODD_Pos
#define TIMER_PWMBRKCTL_BRKAODD_Pos
Definition: timer_reg.h:1877
TIMER_T::PWMDTCTL
__IO uint32_t PWMDTCTL
Definition: timer_reg.h:1624
TPWM_DisableBrakePinDebounce
void TPWM_DisableBrakePinDebounce(TIMER_T *timer)
Disable brake pin noise filter function.
Definition: timer_pwm.c:392
TIMER_T::PWMFAILBRK
__IO uint32_t PWMFAILBRK
Definition: timer_reg.h:1629
TIMER_PWMBNF_BRKNFSEL_Msk
#define TIMER_PWMBNF_BRKNFSEL_Msk
Definition: timer_reg.h:1827
TPWM_SetBrakePinSource
void TPWM_SetBrakePinSource(TIMER_T *timer, uint32_t u32BrakePinNum)
Set brake pin source.
Definition: timer_pwm.c:431
CLK_GetPCLK1Freq
uint32_t CLK_GetPCLK1Freq(void)
Get PCLK1 frequency.
Definition: clk.c:204
TIMER_PWMCTL_CNTMODE_Msk
#define TIMER_PWMCTL_CNTMODE_Msk
Definition: timer_reg.h:1764
CLK_GetPCLK0Freq
uint32_t CLK_GetPCLK0Freq(void)
Get PCLK0 frequency.
Definition: clk.c:164
TIMER_PWMBNF_BKPINSRC_Pos
#define TIMER_PWMBNF_BKPINSRC_Pos
Definition: timer_reg.h:1835
TIMER_PWMCLKSRC_CLKSRC_Msk
#define TIMER_PWMCLKSRC_CLKSRC_Msk
Definition: timer_reg.h:1782
TPWM_DisableFaultBrakeInt
void TPWM_DisableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
Disable Fault Brake Interrupt.
Definition: timer_pwm.c:289
TIMER_PWMCTL_CTRLD_Msk
#define TIMER_PWMCTL_CTRLD_Msk
Definition: timer_reg.h:1767
TPWM_EnableFaultBrake
void TPWM_EnableFaultBrake(TIMER_T *timer, uint32_t u32CH0Level, uint32_t u32CH1Level, uint32_t u32BrakeSource)
Enable Fault Brake Function.
Definition: timer_pwm.c:251
TPWM_SetLoadMode
void TPWM_SetLoadMode(TIMER_T *timer, uint32_t u32LoadMode)
Enable load mode of selected channel.
Definition: timer_pwm.c:345
TIMER_PWMDTCTL_DTCKSEL_Msk
#define TIMER_PWMDTCTL_DTCKSEL_Msk
Definition: timer_reg.h:1803
TPWM_EnableBrakePinInverse
void TPWM_EnableBrakePinInverse(TIMER_T *timer)
Enable brake pin inverse function.
Definition: timer_pwm.c:404
TPWM_ClearFaultBrakeIntFlag
void TPWM_ClearFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
Clear Fault Brake Interrupt Flags.
Definition: timer_pwm.c:326
TPWM_EnableCounter
void TPWM_EnableCounter(TIMER_T *timer)
Enable PWM Counter.
Definition: timer_pwm.c:163
TIMER_PWMBNF_BRKFCNT_Msk
#define TIMER_PWMBNF_BRKFCNT_Msk
Definition: timer_reg.h:1830
TPWM_AUTO_RELOAD_MODE
#define TPWM_AUTO_RELOAD_MODE
Definition: timer_pwm.h:45
TIMER_PWMCTL_CNTTYPE_Pos
#define TIMER_PWMCTL_CNTTYPE_Pos
Definition: timer_reg.h:1760
TPWM_ConfigOutputFreqAndDuty
uint32_t TPWM_ConfigOutputFreqAndDuty(TIMER_T *timer, uint32_t u32Frequency, uint32_t u32DutyCycle)
Configure PWM Output Frequency and Duty Cycle.
Definition: timer_pwm.c:55
TPWM_GetFaultBrakeIntFlag
uint32_t TPWM_GetFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
Indicate Fault Brake Interrupt Flag.
Definition: timer_pwm.c:308
TPWM_DisableCounter
void TPWM_DisableCounter(TIMER_T *timer)
Disable PWM Generator.
Definition: timer_pwm.c:177
TPWM_SET_PRESCALER
#define TPWM_SET_PRESCALER(timer, prescaler)
Set Counter Clock Prescaler.
Definition: timer_pwm.h:243
TPWM_EnableDeadTimeWithPrescale
void TPWM_EnableDeadTimeWithPrescale(TIMER_T *timer, uint32_t u32DTCount)
Enable Dead-Time Function.
Definition: timer_pwm.c:134
TIMER_T
Definition: timer_reg.h:26
TIMER_PWMBNF_BRKPINV_Msk
#define TIMER_PWMBNF_BRKPINV_Msk
Definition: timer_reg.h:1833
TIMER_PWMBRKCTL_BRKAEVEN_Pos
#define TIMER_PWMBRKCTL_BRKAEVEN_Pos
Definition: timer_reg.h:1874
TPWM_EnableDeadTime
void TPWM_EnableDeadTime(TIMER_T *timer, uint32_t u32DTCount)
Enable Dead-Time Function.
Definition: timer_pwm.c:118
TIMER_PWMDTCTL_DTEN_Msk
#define TIMER_PWMDTCTL_DTEN_Msk
Definition: timer_reg.h:1800
TIMER_T::PWMINTEN1
__IO uint32_t PWMINTEN1
Definition: timer_reg.h:1635
TPWM_EnableFaultBrakeInt
void TPWM_EnableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
Enable Fault Brake Interrupt.
Definition: timer_pwm.c:271
TPWM_EnableBrakePinDebounce
void TPWM_EnableBrakePinDebounce(TIMER_T *timer, uint32_t u32BrakePinSrc, uint32_t u32DebounceCnt, uint32_t u32ClkSrcSel)
Enable brake pin noise filter function.
Definition: timer_pwm.c:375
TIMER_PWMBRKCTL_BRKAODD_Msk
#define TIMER_PWMBRKCTL_BRKAODD_Msk
Definition: timer_reg.h:1878
TPWM_SET_PERIOD
#define TPWM_SET_PERIOD(timer, period)
Set Counter Period.
Definition: timer_pwm.h:269
TPWM_SET_CMPDAT
#define TPWM_SET_CMPDAT(timer, cmp)
Set Comparator Value.
Definition: timer_pwm.h:295