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