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