1 /**************************************************************************//**
2  * @file     bpwm.c
3  * @version  V1.00
4  * @brief    M480 series BPWM driver source file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8 *****************************************************************************/
9 #include "NuMicro.h"
10 
11 /** @addtogroup Standard_Driver Standard Driver
12   @{
13 */
14 
15 /** @addtogroup BPWM_Driver BPWM Driver
16   @{
17 */
18 
19 
20 /** @addtogroup BPWM_EXPORTED_FUNCTIONS BPWM Exported Functions
21   @{
22 */
23 
24 /**
25  * @brief Configure BPWM capture and get the nearest unit time.
26  * @param[in] bpwm The pointer of the specified BPWM module
27  *                - BPWM0 : BPWM Group 0
28  *                - BPWM1 : BPWM Group 1
29  * @param[in] u32ChannelNum BPWM 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 BPWM capture and get the nearest unit time.
34  */
BPWM_ConfigCaptureChannel(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32UnitTimeNsec,uint32_t u32CaptureEdge)35 uint32_t BPWM_ConfigCaptureChannel(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32UnitTimeNsec, uint32_t u32CaptureEdge)
36 {
37     uint32_t u32Src;
38     uint32_t u32PWMClockSrc;
39     uint32_t u32NearestUnitTimeNsec;
40     uint16_t u16Prescale = 1U, u16CNR = 0xFFFFU;
41 
42     if(bpwm == BPWM0)
43     {
44         u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_BPWM0SEL_Msk;
45     }
46     else     /* (bpwm == BPWM1) */
47     {
48         u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_BPWM1SEL_Msk;
49     }
50 
51     if(u32Src == 0U)
52     {
53         /* clock source is from PLL clock */
54         u32PWMClockSrc = CLK_GetPLLClockFreq();
55     }
56     else
57     {
58         /* clock source is from PCLK */
59         SystemCoreClockUpdate();
60         if(bpwm == BPWM0)
61         {
62             u32PWMClockSrc = CLK_GetPCLK0Freq();
63         }
64         else    /* (bpwm == BPWM1) */
65         {
66             u32PWMClockSrc = CLK_GetPCLK1Freq();
67         }
68     }
69 
70     u32PWMClockSrc /= 1000UL;
71     for(u16Prescale = 1U; u16Prescale <= 0x1000U; u16Prescale++)
72     {
73         uint32_t u32Exit = 0U;
74         u32NearestUnitTimeNsec = (1000000UL * u16Prescale) / u32PWMClockSrc;
75         if(u32NearestUnitTimeNsec < u32UnitTimeNsec)
76         {
77             if (u16Prescale == 0x1000U)   /* limit to the maximum unit time(nano second) */
78             {
79                 u32Exit = 1U;
80             }
81             else
82             {
83                 u32Exit = 0U;
84             }
85             if (!(1000000UL * (u16Prescale + 1UL) > (u32NearestUnitTimeNsec * u32PWMClockSrc)))
86             {
87                 u32Exit = 1U;
88             }
89             else
90             {
91                 u32Exit = 0U;
92             }
93         }
94         else
95         {
96             u32Exit = 1U;
97         }
98         if (u32Exit == 1U)
99         {
100             break;
101         }
102         else {}
103     }
104 
105     /* convert to real register value */
106     /* all channels share a prescaler */
107     u16Prescale -= 1U;
108     BPWM_SET_PRESCALER(bpwm, u32ChannelNum, u16Prescale);
109 
110     /* set BPWM to down count type(edge aligned) */
111     (bpwm)->CTL1 = (1UL);
112 
113     BPWM_SET_CNR(bpwm, u32ChannelNum, u16CNR);
114 
115     return (u32NearestUnitTimeNsec);
116 }
117 
118 /**
119  * @brief This function Configure BPWM generator and get the nearest frequency in edge aligned auto-reload mode
120  * @param[in] bpwm The pointer of the specified BPWM module
121  *                - BPWM0 : BPWM Group 0
122  *                - BPWM1 : BPWM Group 1
123  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
124  * @param[in] u32Frequency Target generator frequency
125  * @param[in] u32DutyCycle Target generator duty cycle percentage. Valid range are between 0 ~ 100. 10 means 10%, 20 means 20%...
126  * @return Nearest frequency clock in nano second
127  * @note Since all channels shares a prescaler. Call this API to configure BPWM frequency may affect
128  *       existing frequency of other channel.
129  */
BPWM_ConfigOutputChannel(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32Frequency,uint32_t u32DutyCycle)130 uint32_t BPWM_ConfigOutputChannel(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32Frequency, uint32_t u32DutyCycle)
131 {
132     uint32_t u32Src;
133     uint32_t u32PWMClockSrc;
134     uint32_t i;
135     uint32_t u32Prescale = 1U, u32CNR = 0xFFFFU;
136 
137     if(bpwm == BPWM0)
138     {
139         u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_BPWM0SEL_Msk;
140     }
141     else     /* (bpwm == BPWM1) */
142     {
143         u32Src = CLK->CLKSEL2 & CLK_CLKSEL2_BPWM1SEL_Msk;
144     }
145 
146     if(u32Src == 0U)
147     {
148         /* clock source is from PLL clock */
149         u32PWMClockSrc = CLK_GetPLLClockFreq();
150     }
151     else
152     {
153         /* clock source is from PCLK */
154         SystemCoreClockUpdate();
155         if(bpwm == BPWM0)
156         {
157             u32PWMClockSrc = CLK_GetPCLK0Freq();
158         }
159         else     /* (bpwm == BPWM1) */
160         {
161             u32PWMClockSrc = CLK_GetPCLK1Freq();
162         }
163     }
164 
165     for(u32Prescale = 1U; u32Prescale < 0xFFFU; u32Prescale++)   /* prescale could be 0~0xFFF */
166     {
167         i = (u32PWMClockSrc / u32Frequency) / u32Prescale;
168         /* If target value is larger than CNR, need to use a larger prescaler */
169         if(i < (0x10000U))
170         {
171             u32CNR = i;
172             break;
173         }
174     }
175     /* Store return value here 'cos we're gonna change u16Prescale & u16CNR to the real value to fill into register */
176     i = u32PWMClockSrc / (u32Prescale * u32CNR);
177 
178     /* convert to real register value */
179     /* all channels share a prescaler */
180     u32Prescale -= 1U;
181     BPWM_SET_PRESCALER(bpwm, u32ChannelNum, u32Prescale);
182     /* set BPWM to down count type(edge aligned) */
183     (bpwm)->CTL1 = (1UL);
184 
185     u32CNR -= 1U;
186     BPWM_SET_CNR(bpwm, u32ChannelNum, u32CNR);
187     if(u32DutyCycle)
188     {
189         BPWM_SET_CMR(bpwm, u32ChannelNum, u32DutyCycle * (u32CNR + 1UL) / 100UL - 1UL);
190         (bpwm)->WGCTL0 &= ~((BPWM_WGCTL0_PRDPCTL0_Msk | BPWM_WGCTL0_ZPCTL0_Msk) << (u32ChannelNum * 2U));
191         (bpwm)->WGCTL0 |= (BPWM_OUTPUT_LOW << ((u32ChannelNum * (2U)) + (uint32_t)BPWM_WGCTL0_PRDPCTL0_Pos));
192         (bpwm)->WGCTL1 &= ~((BPWM_WGCTL1_CMPDCTL0_Msk | BPWM_WGCTL1_CMPUCTL0_Msk) << (u32ChannelNum * 2U));
193         (bpwm)->WGCTL1 |= (BPWM_OUTPUT_HIGH << (u32ChannelNum * (2U) + (uint32_t)BPWM_WGCTL1_CMPDCTL0_Pos));
194     }
195     else
196     {
197         BPWM_SET_CMR(bpwm, u32ChannelNum, 0U);
198         (bpwm)->WGCTL0 &= ~((BPWM_WGCTL0_PRDPCTL0_Msk | BPWM_WGCTL0_ZPCTL0_Msk) << (u32ChannelNum * 2U));
199         (bpwm)->WGCTL0 |= (BPWM_OUTPUT_LOW << (u32ChannelNum * 2U + (uint32_t)BPWM_WGCTL0_ZPCTL0_Pos));
200         (bpwm)->WGCTL1 &= ~((BPWM_WGCTL1_CMPDCTL0_Msk | BPWM_WGCTL1_CMPUCTL0_Msk) << (u32ChannelNum * 2U));
201         (bpwm)->WGCTL1 |= (BPWM_OUTPUT_HIGH << (u32ChannelNum * 2U + (uint32_t)BPWM_WGCTL1_CMPDCTL0_Pos));
202     }
203 
204     return(i);
205 }
206 
207 /**
208  * @brief Start BPWM module
209  * @param[in] bpwm The pointer of the specified BPWM module
210  *                - BPWM0 : BPWM Group 0
211  *                - BPWM1 : BPWM Group 1
212  * @param[in] u32ChannelMask Combination of enabled channels. This parameter is not used.
213  * @return None
214  * @details This function is used to start BPWM module.
215  * @note All channels share one counter.
216  */
BPWM_Start(BPWM_T * bpwm,uint32_t u32ChannelMask)217 void BPWM_Start(BPWM_T *bpwm, uint32_t u32ChannelMask)
218 {
219     (bpwm)->CNTEN = BPWM_CNTEN_CNTEN0_Msk;
220 }
221 
222 /**
223  * @brief Stop BPWM module
224  * @param[in] bpwm The pointer of the specified BPWM module
225  *                - BPWM0 : BPWM Group 0
226  *                - BPWM1 : BPWM Group 1
227  * @param[in] u32ChannelMask Combination of enabled channels. This parameter is not used.
228  * @return None
229  * @details This function is used to stop BPWM module.
230  * @note All channels share one period.
231  */
BPWM_Stop(BPWM_T * bpwm,uint32_t u32ChannelMask)232 void BPWM_Stop(BPWM_T *bpwm, uint32_t u32ChannelMask)
233 {
234     (bpwm)->PERIOD = 0U;
235 }
236 
237 /**
238  * @brief Stop BPWM generation immediately by clear channel enable bit
239  * @param[in] bpwm The pointer of the specified BPWM module
240  *                - BPWM0 : BPWM Group 0
241  *                - BPWM1 : BPWM Group 1
242  * @param[in] u32ChannelMask Combination of enabled channels. This parameter is not used.
243  * @return None
244  * @details This function is used to stop BPWM generation immediately by clear channel enable bit.
245  * @note All channels share one counter.
246  */
BPWM_ForceStop(BPWM_T * bpwm,uint32_t u32ChannelMask)247 void BPWM_ForceStop(BPWM_T *bpwm, uint32_t u32ChannelMask)
248 {
249     (bpwm)->CNTEN &= ~BPWM_CNTEN_CNTEN0_Msk;
250 }
251 
252 /**
253  * @brief Enable selected channel to trigger ADC
254  * @param[in] bpwm The pointer of the specified BPWM module
255  *                - BPWM0 : BPWM Group 0
256  *                - BPWM1 : BPWM Group 1
257  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
258  * @param[in] u32Condition The condition to trigger ADC. Combination of following conditions:
259  *                  - \ref BPWM_TRIGGER_ADC_EVEN_ZERO_POINT
260  *                  - \ref BPWM_TRIGGER_ADC_EVEN_PERIOD_POINT
261  *                  - \ref BPWM_TRIGGER_ADC_EVEN_ZERO_OR_PERIOD_POINT
262  *                  - \ref BPWM_TRIGGER_ADC_EVEN_CMP_UP_COUNT_POINT
263  *                  - \ref BPWM_TRIGGER_ADC_EVEN_CMP_DOWN_COUNT_POINT
264  *                  - \ref BPWM_TRIGGER_ADC_ODD_CMP_UP_COUNT_POINT
265  *                  - \ref BPWM_TRIGGER_ADC_ODD_CMP_DOWN_COUNT_POINT
266  * @return None
267  * @details This function is used to enable selected channel to trigger ADC
268  */
BPWM_EnableADCTrigger(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32Condition)269 void BPWM_EnableADCTrigger(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32Condition)
270 {
271     if(u32ChannelNum < 4U)
272     {
273         (bpwm)->EADCTS0 &= ~((BPWM_EADCTS0_TRGSEL0_Msk) << (u32ChannelNum * 8U));
274         (bpwm)->EADCTS0 |= ((BPWM_EADCTS0_TRGEN0_Msk | u32Condition) << (u32ChannelNum * 8U));
275     }
276     else
277     {
278         (bpwm)->EADCTS1 &= ~((BPWM_EADCTS1_TRGSEL4_Msk) << ((u32ChannelNum - 4U) * 8U));
279         (bpwm)->EADCTS1 |= ((BPWM_EADCTS1_TRGEN4_Msk | u32Condition) << ((u32ChannelNum - 4U) * 8U));
280     }
281 }
282 
283 /**
284  * @brief Disable selected channel to trigger ADC
285  * @param[in] bpwm The pointer of the specified BPWM module
286  *                - BPWM0 : BPWM Group 0
287  *                - BPWM1 : BPWM Group 1
288  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~3
289  * @return None
290  * @details This function is used to disable selected channel to trigger ADC
291  */
BPWM_DisableADCTrigger(BPWM_T * bpwm,uint32_t u32ChannelNum)292 void BPWM_DisableADCTrigger(BPWM_T *bpwm, uint32_t u32ChannelNum)
293 {
294     if(u32ChannelNum < 4U)
295     {
296         (bpwm)->EADCTS0 &= ~(BPWM_EADCTS0_TRGEN0_Msk << (u32ChannelNum * 8U));
297     }
298     else
299     {
300         (bpwm)->EADCTS1 &= ~(BPWM_EADCTS1_TRGEN4_Msk << ((u32ChannelNum - 4U) * 8U));
301     }
302 }
303 
304 /**
305  * @brief Clear selected channel trigger ADC flag
306  * @param[in] bpwm The pointer of the specified BPWM module
307  *                - BPWM0 : BPWM Group 0
308  *                - BPWM1 : BPWM Group 1
309  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
310  * @param[in] u32Condition This parameter is not used
311  * @return None
312  * @details This function is used to clear selected channel trigger ADC flag
313  */
BPWM_ClearADCTriggerFlag(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32Condition)314 void BPWM_ClearADCTriggerFlag(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32Condition)
315 {
316     (bpwm)->STATUS = (BPWM_STATUS_EADCTRGn_Msk << u32ChannelNum);
317 }
318 
319 /**
320  * @brief Get selected channel trigger ADC flag
321  * @param[in] bpwm The pointer of the specified BPWM module
322  *                - BPWM0 : BPWM Group 0
323  *                - BPWM1 : BPWM Group 1
324  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
325  * @retval 0 The specified channel trigger ADC to start of conversion flag is not set
326  * @retval 1 The specified channel trigger ADC to start of conversion flag is set
327  * @details This function is used to get BPWM trigger ADC to start of conversion flag for specified channel
328  */
BPWM_GetADCTriggerFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)329 uint32_t BPWM_GetADCTriggerFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
330 {
331     return (((bpwm)->STATUS & (BPWM_STATUS_EADCTRGn_Msk << u32ChannelNum)) ? 1UL : 0UL);
332 }
333 
334 /**
335  * @brief Enable capture of selected channel(s)
336  * @param[in] bpwm The pointer of the specified BPWM module
337  *                - BPWM0 : BPWM Group 0
338  *                - BPWM1 : BPWM Group 1
339  * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
340  *                           Bit 0 is channel 0, bit 1 is channel 1...
341  * @return None
342  * @details This function is used to enable capture of selected channel(s)
343  */
BPWM_EnableCapture(BPWM_T * bpwm,uint32_t u32ChannelMask)344 void BPWM_EnableCapture(BPWM_T *bpwm, uint32_t u32ChannelMask)
345 {
346     (bpwm)->CAPINEN |= u32ChannelMask;
347     (bpwm)->CAPCTL |= u32ChannelMask;
348 }
349 
350 /**
351  * @brief Disable capture of selected channel(s)
352  * @param[in] bpwm The pointer of the specified BPWM module
353  *                - BPWM0 : BPWM Group 0
354  *                - BPWM1 : BPWM Group 1
355  * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
356  *                           Bit 0 is channel 0, bit 1 is channel 1...
357  * @return None
358  * @details This function is used to disable capture of selected channel(s)
359  */
BPWM_DisableCapture(BPWM_T * bpwm,uint32_t u32ChannelMask)360 void BPWM_DisableCapture(BPWM_T *bpwm, uint32_t u32ChannelMask)
361 {
362     (bpwm)->CAPINEN &= ~u32ChannelMask;
363     (bpwm)->CAPCTL &= ~u32ChannelMask;
364 }
365 
366 /**
367  * @brief Enables BPWM output generation of selected channel(s)
368  * @param[in] bpwm The pointer of the specified BPWM module
369  *                - BPWM0 : BPWM Group 0
370  *                - BPWM1 : BPWM Group 1
371  * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel.
372  *                           Set bit 0 to 1 enables channel 0 output, set bit 1 to 1 enables channel 1 output...
373  * @return None
374  * @details This function is used to enables BPWM output generation of selected channel(s)
375  */
BPWM_EnableOutput(BPWM_T * bpwm,uint32_t u32ChannelMask)376 void BPWM_EnableOutput(BPWM_T *bpwm, uint32_t u32ChannelMask)
377 {
378     (bpwm)->POEN |= u32ChannelMask;
379 }
380 
381 /**
382  * @brief Disables BPWM output generation of selected channel(s)
383  * @param[in] bpwm The pointer of the specified BPWM module
384  *                - BPWM0 : BPWM Group 0
385  *                - BPWM1 : BPWM Group 1
386  * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel
387  *                           Set bit 0 to 1 disables channel 0 output, set bit 1 to 1 disables channel 1 output...
388  * @return None
389  * @details This function is used to disables BPWM output generation of selected channel(s)
390  */
BPWM_DisableOutput(BPWM_T * bpwm,uint32_t u32ChannelMask)391 void BPWM_DisableOutput(BPWM_T *bpwm, uint32_t u32ChannelMask)
392 {
393     (bpwm)->POEN &= ~u32ChannelMask;
394 }
395 
396 /**
397  * @brief Enable capture interrupt of selected channel.
398  * @param[in] bpwm The pointer of the specified BPWM module
399  *                - BPWM0 : BPWM Group 0
400  *                - BPWM1 : BPWM Group 1
401  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
402  * @param[in] u32Edge Rising or falling edge to latch counter.
403  *              - \ref BPWM_CAPTURE_INT_RISING_LATCH
404  *              - \ref BPWM_CAPTURE_INT_FALLING_LATCH
405  * @return None
406  * @details This function is used to enable capture interrupt of selected channel.
407  */
BPWM_EnableCaptureInt(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32Edge)408 void BPWM_EnableCaptureInt(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32Edge)
409 {
410     (bpwm)->CAPIEN |= (u32Edge << u32ChannelNum);
411 }
412 
413 /**
414  * @brief Disable capture interrupt of selected channel.
415  * @param[in] bpwm The pointer of the specified BPWM module
416  *                - BPWM0 : BPWM Group 0
417  *                - BPWM1 : BPWM Group 1
418  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
419  * @param[in] u32Edge Rising or falling edge to latch counter.
420  *              - \ref BPWM_CAPTURE_INT_RISING_LATCH
421  *              - \ref BPWM_CAPTURE_INT_FALLING_LATCH
422  * @return None
423  * @details This function is used to disable capture interrupt of selected channel.
424  */
BPWM_DisableCaptureInt(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32Edge)425 void BPWM_DisableCaptureInt(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32Edge)
426 {
427     (bpwm)->CAPIEN &= ~(u32Edge << u32ChannelNum);
428 }
429 
430 /**
431  * @brief Clear capture interrupt of selected channel.
432  * @param[in] bpwm The pointer of the specified BPWM module
433  *                - BPWM0 : BPWM Group 0
434  *                - BPWM1 : BPWM Group 1
435  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
436  * @param[in] u32Edge Rising or falling edge to latch counter.
437  *              - \ref BPWM_CAPTURE_INT_RISING_LATCH
438  *              - \ref BPWM_CAPTURE_INT_FALLING_LATCH
439  * @return None
440  * @details This function is used to clear capture interrupt of selected channel.
441  */
BPWM_ClearCaptureIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32Edge)442 void BPWM_ClearCaptureIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32Edge)
443 {
444     (bpwm)->CAPIF = (u32Edge << u32ChannelNum);
445 }
446 
447 /**
448  * @brief Get capture interrupt of selected channel.
449  * @param[in] bpwm The pointer of the specified BPWM module
450  *                - BPWM0 : BPWM Group 0
451  *                - BPWM1 : BPWM Group 1
452  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
453  * @retval 0 No capture interrupt
454  * @retval 1 Rising edge latch interrupt
455  * @retval 2 Falling edge latch interrupt
456  * @retval 3 Rising and falling latch interrupt
457  * @details This function is used to get capture interrupt of selected channel.
458  */
BPWM_GetCaptureIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)459 uint32_t BPWM_GetCaptureIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
460 {
461     return (((((bpwm)->CAPIF & (BPWM_CAPIF_CAPFIFn_Msk << u32ChannelNum)) ? 1UL : 0UL) << 1) | \
462             (((bpwm)->CAPIF & (BPWM_CAPIF_CAPRIFn_Msk << u32ChannelNum)) ? 1UL : 0UL));
463 }
464 /**
465  * @brief Enable duty interrupt of selected channel
466  * @param[in] bpwm The pointer of the specified BPWM module
467  *                - BPWM0 : BPWM Group 0
468  *                - BPWM1 : BPWM Group 1
469  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
470  * @param[in] u32IntDutyType Duty interrupt type, could be either
471  *              - \ref BPWM_DUTY_INT_DOWN_COUNT_MATCH_CMP
472  *              - \ref BPWM_DUTY_INT_UP_COUNT_MATCH_CMP
473  * @return None
474  * @details This function is used to enable duty interrupt of selected channel.
475  */
BPWM_EnableDutyInt(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32IntDutyType)476 void BPWM_EnableDutyInt(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType)
477 {
478     (bpwm)->INTEN |= (u32IntDutyType << u32ChannelNum);
479 }
480 
481 /**
482  * @brief Disable duty interrupt of selected channel
483  * @param[in] bpwm The pointer of the specified BPWM module
484  *                - BPWM0 : BPWM Group 0
485  *                - BPWM1 : BPWM Group 1
486  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
487  * @return None
488  * @details This function is used to disable duty interrupt of selected channel
489  */
BPWM_DisableDutyInt(BPWM_T * bpwm,uint32_t u32ChannelNum)490 void BPWM_DisableDutyInt(BPWM_T *bpwm, uint32_t u32ChannelNum)
491 {
492 
493     (bpwm)->INTEN &= ~((uint32_t)(BPWM_DUTY_INT_DOWN_COUNT_MATCH_CMP | BPWM_DUTY_INT_UP_COUNT_MATCH_CMP) << u32ChannelNum);
494 }
495 
496 /**
497  * @brief Clear duty interrupt flag of selected channel
498  * @param[in] bpwm The pointer of the specified BPWM module
499  *                - BPWM0 : BPWM Group 0
500  *                - BPWM1 : BPWM Group 1
501  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
502  * @return None
503  * @details This function is used to clear duty interrupt flag of selected channel
504  */
BPWM_ClearDutyIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)505 void BPWM_ClearDutyIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
506 {
507     (bpwm)->INTSTS = (BPWM_INTSTS_CMPUIFn_Msk | BPWM_INTSTS_CMPDIFn_Msk) << u32ChannelNum;
508 }
509 
510 /**
511  * @brief Get duty interrupt flag of selected channel
512  * @param[in] bpwm The pointer of the specified BPWM module
513  *                - BPWM0 : BPWM Group 0
514  *                - BPWM1 : BPWM Group 1
515  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
516  * @return Duty interrupt flag of specified channel
517  * @retval 0 Duty interrupt did not occur
518  * @retval 1 Duty interrupt occurred
519  * @details This function is used to get duty interrupt flag of selected channel
520  */
BPWM_GetDutyIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)521 uint32_t BPWM_GetDutyIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
522 {
523     return ((((bpwm)->INTSTS & ((BPWM_INTSTS_CMPDIFn_Msk | BPWM_INTSTS_CMPUIFn_Msk) << u32ChannelNum))) ? 1UL : 0UL);
524 }
525 
526 /**
527  * @brief Enable period interrupt of selected channel
528  * @param[in] bpwm The pointer of the specified BPWM module
529  *                - BPWM0 : BPWM Group 0
530  *                - BPWM1 : BPWM Group 1
531  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
532  * @param[in] u32IntPeriodType Period interrupt type. This parameter is not used.
533  * @return None
534  * @details This function is used to enable period interrupt of selected channel.
535  * @note All channels share channel 0's setting.
536  */
BPWM_EnablePeriodInt(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32IntPeriodType)537 void BPWM_EnablePeriodInt(BPWM_T *bpwm, uint32_t u32ChannelNum,  uint32_t u32IntPeriodType)
538 {
539     (bpwm)->INTEN |= BPWM_INTEN_PIEN0_Msk;
540 }
541 
542 /**
543  * @brief Disable period interrupt of selected channel
544  * @param[in] bpwm The pointer of the specified BPWM module
545  *                - BPWM0 : BPWM Group 0
546  *                - BPWM1 : BPWM Group 1
547  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
548  * @return None
549  * @details This function is used to disable period interrupt of selected channel.
550  * @note All channels share channel 0's setting.
551  */
BPWM_DisablePeriodInt(BPWM_T * bpwm,uint32_t u32ChannelNum)552 void BPWM_DisablePeriodInt(BPWM_T *bpwm, uint32_t u32ChannelNum)
553 {
554     (bpwm)->INTEN &= ~BPWM_INTEN_PIEN0_Msk;
555 }
556 
557 /**
558  * @brief Clear period interrupt of selected channel
559  * @param[in] bpwm The pointer of the specified BPWM module
560  *                - BPWM0 : BPWM Group 0
561  *                - BPWM1 : BPWM Group 1
562  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
563  * @return None
564  * @details This function is used to clear period interrupt of selected channel
565  * @note All channels share channel 0's setting.
566  */
BPWM_ClearPeriodIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)567 void BPWM_ClearPeriodIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
568 {
569     (bpwm)->INTSTS = BPWM_INTSTS_PIF0_Msk;
570 }
571 
572 /**
573  * @brief Get period interrupt of selected channel
574  * @param[in] bpwm The pointer of the specified BPWM module
575  *                - BPWM0 : BPWM Group 0
576  *                - BPWM1 : BPWM Group 1
577  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
578  * @return Period interrupt flag of specified channel
579  * @retval 0 Period interrupt did not occur
580  * @retval 1 Period interrupt occurred
581  * @details This function is used to get period interrupt of selected channel
582  * @note All channels share channel 0's setting.
583  */
BPWM_GetPeriodIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)584 uint32_t BPWM_GetPeriodIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
585 {
586     return (((bpwm)->INTSTS & BPWM_INTSTS_PIF0_Msk) ? 1UL : 0UL);
587 }
588 
589 /**
590  * @brief Enable zero interrupt of selected channel
591  * @param[in] bpwm The pointer of the specified BPWM module
592  *                - BPWM0 : BPWM Group 0
593  *                - BPWM1 : BPWM Group 1
594  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
595  * @return None
596  * @details This function is used to enable zero interrupt of selected channel.
597  * @note All channels share channel 0's setting.
598  */
BPWM_EnableZeroInt(BPWM_T * bpwm,uint32_t u32ChannelNum)599 void BPWM_EnableZeroInt(BPWM_T *bpwm, uint32_t u32ChannelNum)
600 {
601     (bpwm)->INTEN |= BPWM_INTEN_ZIEN0_Msk;
602 }
603 
604 /**
605  * @brief Disable zero interrupt of selected channel
606  * @param[in] bpwm The pointer of the specified BPWM module
607  *                - BPWM0 : BPWM Group 0
608  *                - BPWM1 : BPWM Group 1
609  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
610  * @return None
611  * @details This function is used to disable zero interrupt of selected channel.
612  * @note All channels share channel 0's setting.
613  */
BPWM_DisableZeroInt(BPWM_T * bpwm,uint32_t u32ChannelNum)614 void BPWM_DisableZeroInt(BPWM_T *bpwm, uint32_t u32ChannelNum)
615 {
616     (bpwm)->INTEN &= ~BPWM_INTEN_ZIEN0_Msk;
617 }
618 
619 /**
620  * @brief Clear zero interrupt of selected channel
621  * @param[in] bpwm The pointer of the specified BPWM module
622  *                - BPWM0 : BPWM Group 0
623  *                - BPWM1 : BPWM Group 1
624  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
625  * @return None
626  * @details This function is used to clear zero interrupt of selected channel.
627  * @note All channels share channel 0's setting.
628  */
BPWM_ClearZeroIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)629 void BPWM_ClearZeroIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
630 {
631     (bpwm)->INTSTS = BPWM_INTSTS_ZIF0_Msk;
632 }
633 
634 /**
635  * @brief Get zero interrupt of selected channel
636  * @param[in] bpwm The pointer of the specified BPWM module
637  *                - BPWM0 : BPWM Group 0
638  *                - BPWM1 : BPWM Group 1
639  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
640  * @return zero interrupt flag of specified channel
641  * @retval 0 zero interrupt did not occur
642  * @retval 1 zero interrupt occurred
643  * @details This function is used to get zero interrupt of selected channel.
644  * @note All channels share channel 0's setting.
645  */
BPWM_GetZeroIntFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)646 uint32_t BPWM_GetZeroIntFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
647 {
648     return (((bpwm)->INTSTS & BPWM_INTSTS_ZIF0_Msk) ? 1UL : 0UL);
649 }
650 
651 /**
652  * @brief Enable load mode of selected channel
653  * @param[in] bpwm The pointer of the specified BPWM module
654  *                - BPWM0 : BPWM Group 0
655  *                - BPWM1 : BPWM Group 1
656  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
657  * @param[in] u32LoadMode BPWM counter loading mode.
658  *              - \ref BPWM_LOAD_MODE_IMMEDIATE
659  *              - \ref BPWM_LOAD_MODE_CENTER
660  * @return None
661  * @details This function is used to enable load mode of selected channel.
662  */
BPWM_EnableLoadMode(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32LoadMode)663 void BPWM_EnableLoadMode(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32LoadMode)
664 {
665     (bpwm)->CTL0 |= (u32LoadMode << u32ChannelNum);
666 }
667 
668 /**
669  * @brief Disable load mode of selected channel
670  * @param[in] bpwm The pointer of the specified BPWM module
671  *                - BPWM0 : BPWM Group 0
672  *                - BPWM1 : BPWM Group 1
673  * @param[in] u32ChannelNum BPWM channel number. Valid values are between 0~5
674  * @param[in] u32LoadMode PWM counter loading mode.
675  *              - \ref BPWM_LOAD_MODE_IMMEDIATE
676  *              - \ref BPWM_LOAD_MODE_CENTER
677  * @return None
678  * @details This function is used to disable load mode of selected channel.
679  */
BPWM_DisableLoadMode(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32LoadMode)680 void BPWM_DisableLoadMode(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32LoadMode)
681 {
682     (bpwm)->CTL0 &= ~(u32LoadMode << u32ChannelNum);
683 }
684 
685 /**
686  * @brief Set BPWM clock source
687  * @param[in] bpwm The pointer of the specified BPWM module
688  *                - BPWM0 : BPWM Group 0
689  *                - BPWM1 : BPWM Group 1
690  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
691  * @param[in] u32ClkSrcSel BPWM external clock source.
692  *              - \ref BPWM_CLKSRC_BPWM_CLK
693  *              - \ref BPWM_CLKSRC_TIMER0
694  *              - \ref BPWM_CLKSRC_TIMER1
695  *              - \ref BPWM_CLKSRC_TIMER2
696  *              - \ref BPWM_CLKSRC_TIMER3
697  * @return None
698  * @details This function is used to set BPWM clock source.
699  * @note All channels share channel 0's setting.
700  */
BPWM_SetClockSource(BPWM_T * bpwm,uint32_t u32ChannelNum,uint32_t u32ClkSrcSel)701 void BPWM_SetClockSource(BPWM_T *bpwm, uint32_t u32ChannelNum, uint32_t u32ClkSrcSel)
702 {
703     (bpwm)->CLKSRC = (u32ClkSrcSel);
704 }
705 
706 /**
707  * @brief Get the time-base counter reached its maximum value flag of selected channel
708  * @param[in] bpwm The pointer of the specified BPWM module
709  *                - BPWM0 : BPWM Group 0
710  *                - BPWM1 : BPWM Group 1
711  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
712  * @return Count to max interrupt flag of specified channel
713  * @retval 0 Count to max interrupt did not occur
714  * @retval 1 Count to max interrupt occurred
715  * @details This function is used to get the time-base counter reached its maximum value flag of selected channel.
716  * @note All channels share channel 0's setting.
717  */
BPWM_GetWrapAroundFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)718 uint32_t BPWM_GetWrapAroundFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
719 {
720     return (((bpwm)->STATUS & BPWM_STATUS_CNTMAX0_Msk) ? 1UL : 0UL);
721 }
722 
723 /**
724  * @brief Clear the time-base counter reached its maximum value flag of selected channel
725  * @param[in] bpwm The pointer of the specified BPWM module
726  *                - BPWM0 : BPWM Group 0
727  *                - BPWM1 : BPWM Group 1
728  * @param[in] u32ChannelNum BPWM channel number. This parameter is not used.
729  * @return None
730  * @details This function is used to clear the time-base counter reached its maximum value flag of selected channel.
731  * @note All channels share channel 0's setting.
732  */
BPWM_ClearWrapAroundFlag(BPWM_T * bpwm,uint32_t u32ChannelNum)733 void BPWM_ClearWrapAroundFlag(BPWM_T *bpwm, uint32_t u32ChannelNum)
734 {
735     (bpwm)->STATUS = BPWM_STATUS_CNTMAX0_Msk;
736 }
737 
738 
739 /*@}*/ /* end of group BPWM_EXPORTED_FUNCTIONS */
740 
741 /*@}*/ /* end of group BPWM_Driver */
742 
743 /*@}*/ /* end of group Standard_Driver */
744 
745 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
746