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