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