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 */
85 
86  /* Set PWM to 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 {
137 }
138 
150 {
151  timer->PWMDTCTL = 0x0UL;
152 }
153 
164 {
165  timer->PWMCTL |= TIMER_PWMCTL_CNTEN_Msk;
166 }
167 
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 
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);
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 {
378  (u32BrakePinSrc << TIMER_PWMBNF_BKPINSRC_Pos) |
379  (u32DebounceCnt << TIMER_PWMBNF_BRKFCNT_Pos) |
381 }
382 
393 {
394  timer->PWMBNF &= ~TIMER_PWMBNF_BRKNFEN_Msk;
395 }
396 
397 
405 {
407 }
408 
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. ***/
#define TIMER_PWMBNF_BRKNFEN_Msk
Definition: timer_reg.h:1824
#define TPWM_UP_COUNT
Definition: timer_pwm.h:38
#define TIMER_PWMBRKCTL_BRKAEVEN_Msk
Definition: timer_reg.h:1875
void TPWM_DisableBrakePinInverse(TIMER_T *timer)
Disable brake pin inverse function.
Definition: timer_pwm.c:415
__IO uint32_t PWMBRKCTL
Definition: timer_reg.h:1630
#define TIMER_PWMEADCTS_TRGEN_Msk
Definition: timer_reg.h:1956
void TPWM_EnableTriggerADC(TIMER_T *timer, uint32_t u32Condition)
Enable Trigger ADC.
Definition: timer_pwm.c:197
#define TIMER_PWMCTL_IMMLDEN_Msk
Definition: timer_reg.h:1770
__IO uint32_t PWMEADCTS
Definition: timer_reg.h:1638
void TPWM_DisableTriggerADC(TIMER_T *timer)
Disable Trigger ADC.
Definition: timer_pwm.c:211
#define TIMER_PWMBNF_BRKFCNT_Pos
Definition: timer_reg.h:1829
__IO uint32_t PWMINTSTS1
Definition: timer_reg.h:1637
__IO uint32_t PWMBNF
Definition: timer_reg.h:1628
#define TIMER_PWMCTL_CNTTYPE_Msk
Definition: timer_reg.h:1761
#define TIMER1
Definition: M480.h:412
void TPWM_DisableDeadTime(TIMER_T *timer)
Disable Dead-Time Function.
Definition: timer_pwm.c:149
void TPWM_SetCounterClockSource(TIMER_T *timer, uint32_t u32CntClkSrc)
Set PWM Counter Clock Source.
Definition: timer_pwm.c:38
__IO uint32_t PWMCTL
Definition: timer_reg.h:1618
#define TIMER_PWMCTL_CNTEN_Msk
Definition: timer_reg.h:1758
#define TIMER_PWMBNF_BKPINSRC_Msk
Definition: timer_reg.h:1836
#define TIMER_PWMBNF_BRKNFSEL_Pos
Definition: timer_reg.h:1826
#define TIMER0
Definition: M480.h:411
NuMicro peripheral access layer header file.
#define TIMER_PWMBRKCTL_BRKAODD_Pos
Definition: timer_reg.h:1877
__IO uint32_t PWMDTCTL
Definition: timer_reg.h:1624
void TPWM_DisableBrakePinDebounce(TIMER_T *timer)
Disable brake pin noise filter function.
Definition: timer_pwm.c:392
__IO uint32_t PWMFAILBRK
Definition: timer_reg.h:1629
#define TIMER_PWMBNF_BRKNFSEL_Msk
Definition: timer_reg.h:1827
void TPWM_SetBrakePinSource(TIMER_T *timer, uint32_t u32BrakePinNum)
Set brake pin source.
Definition: timer_pwm.c:431
uint32_t CLK_GetPCLK1Freq(void)
Get PCLK1 frequency.
Definition: clk.c:204
#define TIMER_PWMCTL_CNTMODE_Msk
Definition: timer_reg.h:1764
uint32_t CLK_GetPCLK0Freq(void)
Get PCLK0 frequency.
Definition: clk.c:164
#define TIMER_PWMBNF_BKPINSRC_Pos
Definition: timer_reg.h:1835
#define TIMER_PWMCLKSRC_CLKSRC_Msk
Definition: timer_reg.h:1782
void TPWM_DisableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
Disable Fault Brake Interrupt.
Definition: timer_pwm.c:289
#define TIMER_PWMCTL_CTRLD_Msk
Definition: timer_reg.h:1767
void TPWM_EnableFaultBrake(TIMER_T *timer, uint32_t u32CH0Level, uint32_t u32CH1Level, uint32_t u32BrakeSource)
Enable Fault Brake Function.
Definition: timer_pwm.c:251
void TPWM_SetLoadMode(TIMER_T *timer, uint32_t u32LoadMode)
Enable load mode of selected channel.
Definition: timer_pwm.c:345
#define TIMER_PWMDTCTL_DTCKSEL_Msk
Definition: timer_reg.h:1803
void TPWM_EnableBrakePinInverse(TIMER_T *timer)
Enable brake pin inverse function.
Definition: timer_pwm.c:404
void TPWM_ClearFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
Clear Fault Brake Interrupt Flags.
Definition: timer_pwm.c:326
void TPWM_EnableCounter(TIMER_T *timer)
Enable PWM Counter.
Definition: timer_pwm.c:163
#define TIMER_PWMBNF_BRKFCNT_Msk
Definition: timer_reg.h:1830
#define TPWM_AUTO_RELOAD_MODE
Definition: timer_pwm.h:45
#define TIMER_PWMCTL_CNTTYPE_Pos
Definition: timer_reg.h:1760
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
uint32_t TPWM_GetFaultBrakeIntFlag(TIMER_T *timer, uint32_t u32IntSource)
Indicate Fault Brake Interrupt Flag.
Definition: timer_pwm.c:308
void TPWM_DisableCounter(TIMER_T *timer)
Disable PWM Generator.
Definition: timer_pwm.c:177
#define TPWM_SET_PRESCALER(timer, prescaler)
Set Counter Clock Prescaler.
Definition: timer_pwm.h:243
void TPWM_EnableDeadTimeWithPrescale(TIMER_T *timer, uint32_t u32DTCount)
Enable Dead-Time Function.
Definition: timer_pwm.c:134
#define TIMER_PWMBNF_BRKPINV_Msk
Definition: timer_reg.h:1833
#define TIMER_PWMBRKCTL_BRKAEVEN_Pos
Definition: timer_reg.h:1874
void TPWM_EnableDeadTime(TIMER_T *timer, uint32_t u32DTCount)
Enable Dead-Time Function.
Definition: timer_pwm.c:118
#define TIMER_PWMDTCTL_DTEN_Msk
Definition: timer_reg.h:1800
__IO uint32_t PWMINTEN1
Definition: timer_reg.h:1635
void TPWM_EnableFaultBrakeInt(TIMER_T *timer, uint32_t u32IntSource)
Enable Fault Brake Interrupt.
Definition: timer_pwm.c:271
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
#define TIMER_PWMBRKCTL_BRKAODD_Msk
Definition: timer_reg.h:1878
#define TPWM_SET_PERIOD(timer, period)
Set Counter Period.
Definition: timer_pwm.h:269
#define TPWM_SET_CMPDAT(timer, cmp)
Set Comparator Value.
Definition: timer_pwm.h:295