1 /**************************************************************************//**
2  * @file     usci_uart.c
3  * @version  V3.00
4  * @brief    M480 series USCI UART (UUART) driver source file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8 *****************************************************************************/
9 
10 #include <stdio.h>
11 #include "NuMicro.h"
12 
13 /** @addtogroup Standard_Driver Standard Driver
14   @{
15 */
16 
17 /** @addtogroup USCI_UART_Driver USCI_UART Driver
18   @{
19 */
20 
21 /** @addtogroup USCI_UART_EXPORTED_FUNCTIONS USCI_UART Exported Functions
22   @{
23 */
24 
25 /**
26  *    @brief        Clear USCI_UART specified interrupt flag
27  *
28  *    @param[in]    uuart   The pointer of the specified USCI_UART module.
29  *    @param[in]    u32Mask The combination of all related interrupt sources.
30  *                          Each bit corresponds to a interrupt source.
31  *                          This parameter decides which interrupt flags will be cleared. It could be the combination of:
32  *                          - \ref UUART_ABR_INT_MASK
33  *                          - \ref UUART_RLS_INT_MASK
34  *                          - \ref UUART_BUF_RXOV_INT_MASK
35  *                          - \ref UUART_TXST_INT_MASK
36  *                          - \ref UUART_TXEND_INT_MASK
37  *                          - \ref UUART_RXST_INT_MASK
38  *                          - \ref UUART_RXEND_INT_MASK
39  *
40  *    @return       None
41  *
42  *    @details      The function is used to clear USCI_UART related interrupt flags specified by u32Mask parameter.
43  */
44 
UUART_ClearIntFlag(UUART_T * uuart,uint32_t u32Mask)45 void UUART_ClearIntFlag(UUART_T* uuart, uint32_t u32Mask)
46 {
47 
48     if(u32Mask & UUART_ABR_INT_MASK)   /* Clear Auto-baud Rate Interrupt */
49     {
50         uuart->PROTSTS = UUART_PROTSTS_ABRDETIF_Msk;
51     }
52 
53     if(u32Mask & UUART_RLS_INT_MASK)   /* Clear Receive Line Status Interrupt */
54     {
55         uuart->PROTSTS = (UUART_PROTSTS_BREAK_Msk | UUART_PROTSTS_FRMERR_Msk | UUART_PROTSTS_PARITYERR_Msk);
56     }
57 
58     if(u32Mask & UUART_BUF_RXOV_INT_MASK)   /* Clear Receive Buffer Over-run Error Interrupt */
59     {
60         uuart->BUFSTS = UUART_BUFSTS_RXOVIF_Msk;
61     }
62 
63     if(u32Mask & UUART_TXST_INT_MASK)   /* Clear Transmit Start Interrupt */
64     {
65         uuart->PROTSTS = UUART_PROTSTS_TXSTIF_Msk;
66     }
67 
68     if(u32Mask & UUART_TXEND_INT_MASK)   /* Clear Transmit End Interrupt */
69     {
70         uuart->PROTSTS = UUART_PROTSTS_TXENDIF_Msk;
71     }
72 
73     if(u32Mask & UUART_RXST_INT_MASK)   /* Clear Receive Start Interrupt */
74     {
75         uuart->PROTSTS = UUART_PROTSTS_RXSTIF_Msk;
76     }
77 
78     if(u32Mask & UUART_RXEND_INT_MASK)   /* Clear Receive End Interrupt */
79     {
80         uuart->PROTSTS = UUART_PROTSTS_RXENDIF_Msk;
81     }
82 
83 }
84 
85 /**
86  *    @brief        Get USCI_UART specified interrupt flag
87  *
88  *    @param[in]    uuart   The pointer of the specified USCI_UART module.
89  *    @param[in]    u32Mask The combination of all related interrupt sources.
90  *                          Each bit corresponds to a interrupt source.
91  *                          This parameter decides which interrupt flags will be read. It is combination of:
92  *                          - \ref UUART_ABR_INT_MASK
93  *                          - \ref UUART_RLS_INT_MASK
94  *                          - \ref UUART_BUF_RXOV_INT_MASK
95  *                          - \ref UUART_TXST_INT_MASK
96  *                          - \ref UUART_TXEND_INT_MASK
97  *                          - \ref UUART_RXST_INT_MASK
98  *                          - \ref UUART_RXEND_INT_MASK
99  *
100  *    @return       Interrupt flags of selected sources.
101  *
102  *    @details      The function is used to get USCI_UART related interrupt flags specified by u32Mask parameter.
103  */
104 
UUART_GetIntFlag(UUART_T * uuart,uint32_t u32Mask)105 uint32_t UUART_GetIntFlag(UUART_T* uuart, uint32_t u32Mask)
106 {
107     uint32_t u32IntFlag = 0ul;
108     uint32_t u32Tmp1, u32Tmp2;
109 
110     /* Check Auto-baud Rate Interrupt Flag */
111     u32Tmp1 = (u32Mask & UUART_ABR_INT_MASK);
112     u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_ABRDETIF_Msk);
113     if(u32Tmp1 && u32Tmp2)
114     {
115         u32IntFlag |= UUART_ABR_INT_MASK;
116     }
117 
118     /* Check Receive Line Status Interrupt Flag */
119     u32Tmp1 = (u32Mask & UUART_RLS_INT_MASK);
120     u32Tmp2 = (uuart->PROTSTS & (UUART_PROTSTS_BREAK_Msk | UUART_PROTSTS_FRMERR_Msk | UUART_PROTSTS_PARITYERR_Msk));
121     if(u32Tmp1 && u32Tmp2)
122     {
123         u32IntFlag |= UUART_RLS_INT_MASK;
124     }
125 
126     /* Check Receive Buffer Over-run Error Interrupt Flag */
127     u32Tmp1 = (u32Mask & UUART_BUF_RXOV_INT_MASK);
128     u32Tmp2 = (uuart->BUFSTS & UUART_BUFSTS_RXOVIF_Msk);
129     if(u32Tmp1 && u32Tmp2)
130     {
131         u32IntFlag |= UUART_BUF_RXOV_INT_MASK;
132     }
133 
134     /* Check Transmit Start Interrupt Flag */
135     u32Tmp1 = (u32Mask & UUART_TXST_INT_MASK);
136     u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_TXSTIF_Msk);
137     if(u32Tmp1 && u32Tmp2)
138     {
139         u32IntFlag |= UUART_TXST_INT_MASK;
140     }
141 
142     /* Check Transmit End Interrupt Flag */
143     u32Tmp1 = (u32Mask & UUART_TXEND_INT_MASK);
144     u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_TXENDIF_Msk);
145     if(u32Tmp1 && u32Tmp2)
146     {
147         u32IntFlag |= UUART_TXEND_INT_MASK;
148     }
149 
150     /* Check Receive Start Interrupt Flag */
151     u32Tmp1 = (u32Mask & UUART_RXST_INT_MASK);
152     u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_RXSTIF_Msk);
153     if(u32Tmp1 && u32Tmp2)
154     {
155         u32IntFlag |= UUART_RXST_INT_MASK;
156     }
157 
158     /* Check Receive End Interrupt Flag */
159     u32Tmp1 = (u32Mask & UUART_RXEND_INT_MASK);
160     u32Tmp2 = (uuart->PROTSTS & UUART_PROTSTS_RXENDIF_Msk);
161     if(u32Tmp1 && u32Tmp2)
162     {
163         u32IntFlag |= UUART_RXEND_INT_MASK;
164     }
165 
166     return u32IntFlag;
167 
168 }
169 
170 
171 /**
172  *  @brief      Disable USCI_UART function mode
173  *
174  *  @param[in]  uuart The pointer of the specified USCI_UART module.
175  *
176  *  @return     None
177  *
178  *  @details    The function is used to disable USCI_UART function mode.
179  */
UUART_Close(UUART_T * uuart)180 void UUART_Close(UUART_T* uuart)
181 {
182     uuart->CTL = 0ul;
183 }
184 
185 
186 /**
187  *    @brief        Disable interrupt function.
188  *
189  *    @param[in]    uuart   The pointer of the specified USCI_UART module.
190  *    @param[in]    u32Mask The combination of all related interrupt enable bits.
191  *                          Each bit corresponds to a interrupt enable bit.
192  *                          This parameter decides which interrupts will be disabled. It is combination of:
193  *                          - \ref UUART_ABR_INT_MASK
194  *                          - \ref UUART_RLS_INT_MASK
195  *                          - \ref UUART_BUF_RXOV_INT_MASK
196  *                          - \ref UUART_TXST_INT_MASK
197  *                          - \ref UUART_TXEND_INT_MASK
198  *                          - \ref UUART_RXST_INT_MASK
199  *                          - \ref UUART_RXEND_INT_MASK
200  *
201  *    @return       None
202  *
203  *    @details      The function is used to disabled USCI_UART related interrupts specified by u32Mask parameter.
204  */
UUART_DisableInt(UUART_T * uuart,uint32_t u32Mask)205 void UUART_DisableInt(UUART_T* uuart, uint32_t u32Mask)
206 {
207 
208     /* Disable Auto-baud rate interrupt flag */
209     if((u32Mask & UUART_ABR_INT_MASK) == UUART_ABR_INT_MASK)
210     {
211         uuart->PROTIEN &= ~UUART_PROTIEN_ABRIEN_Msk;
212     }
213 
214     /* Disable receive line status interrupt flag */
215     if((u32Mask & UUART_RLS_INT_MASK) == UUART_RLS_INT_MASK)
216     {
217         uuart->PROTIEN &= ~UUART_PROTIEN_RLSIEN_Msk;
218     }
219 
220     /* Disable RX overrun interrupt flag */
221     if((u32Mask & UUART_BUF_RXOV_INT_MASK) == UUART_BUF_RXOV_INT_MASK)
222     {
223         uuart->BUFCTL &= ~UUART_BUFCTL_RXOVIEN_Msk;
224     }
225 
226     /* Disable TX start interrupt flag */
227     if((u32Mask & UUART_TXST_INT_MASK) == UUART_TXST_INT_MASK)
228     {
229         uuart->INTEN &= ~UUART_INTEN_TXSTIEN_Msk;
230     }
231 
232     /* Disable TX end interrupt flag */
233     if((u32Mask & UUART_TXEND_INT_MASK) == UUART_TXEND_INT_MASK)
234     {
235         uuart->INTEN &= ~UUART_INTEN_TXENDIEN_Msk;
236     }
237 
238     /* Disable RX start interrupt flag */
239     if((u32Mask & UUART_RXST_INT_MASK) == UUART_RXST_INT_MASK)
240     {
241         uuart->INTEN &= ~UUART_INTEN_RXSTIEN_Msk;
242     }
243 
244     /* Disable RX end interrupt flag */
245     if((u32Mask & UUART_RXEND_INT_MASK) == UUART_RXEND_INT_MASK)
246     {
247         uuart->INTEN &= ~UUART_INTEN_RXENDIEN_Msk;
248     }
249 }
250 
251 
252 /**
253  *    @brief        Enable interrupt function.
254  *
255  *    @param[in]    uuart       The pointer of the specified USCI_UART module.
256  *    @param[in]    u32Mask     The combination of all related interrupt enable bits.
257  *                              Each bit corresponds to a interrupt enable bit.
258  *                              This parameter decides which interrupts will be enabled. It is combination of:
259  *                              - \ref UUART_ABR_INT_MASK
260  *                              - \ref UUART_RLS_INT_MASK
261  *                              - \ref UUART_BUF_RXOV_INT_MASK
262  *                              - \ref UUART_TXST_INT_MASK
263  *                              - \ref UUART_TXEND_INT_MASK
264  *                              - \ref UUART_RXST_INT_MASK
265  *                              - \ref UUART_RXEND_INT_MASK
266  *
267  *    @return       None
268  *
269  *    @details      The function is used to enable USCI_UART related interrupts specified by u32Mask parameter.
270  */
UUART_EnableInt(UUART_T * uuart,uint32_t u32Mask)271 void UUART_EnableInt(UUART_T*  uuart, uint32_t u32Mask)
272 {
273     /* Enable Auto-baud rate interrupt flag */
274     if((u32Mask & UUART_ABR_INT_MASK) == UUART_ABR_INT_MASK)
275     {
276         uuart->PROTIEN |= UUART_PROTIEN_ABRIEN_Msk;
277     }
278 
279     /* Enable receive line status interrupt flag */
280     if((u32Mask & UUART_RLS_INT_MASK) == UUART_RLS_INT_MASK)
281     {
282         uuart->PROTIEN |= UUART_PROTIEN_RLSIEN_Msk;
283     }
284 
285     /* Enable RX overrun interrupt flag */
286     if((u32Mask & UUART_BUF_RXOV_INT_MASK) == UUART_BUF_RXOV_INT_MASK)
287     {
288         uuart->BUFCTL |= UUART_BUFCTL_RXOVIEN_Msk;
289     }
290 
291     /* Enable TX start interrupt flag */
292     if((u32Mask & UUART_TXST_INT_MASK) == UUART_TXST_INT_MASK)
293     {
294         uuart->INTEN |= UUART_INTEN_TXSTIEN_Msk;
295     }
296 
297     /* Enable TX end interrupt flag */
298     if((u32Mask & UUART_TXEND_INT_MASK) == UUART_TXEND_INT_MASK)
299     {
300         uuart->INTEN |= UUART_INTEN_TXENDIEN_Msk;
301     }
302 
303     /* Enable RX start interrupt flag */
304     if((u32Mask & UUART_RXST_INT_MASK) == UUART_RXST_INT_MASK)
305     {
306         uuart->INTEN |= UUART_INTEN_RXSTIEN_Msk;
307     }
308 
309     /* Enable RX end interrupt flag */
310     if((u32Mask & UUART_RXEND_INT_MASK) == UUART_RXEND_INT_MASK)
311     {
312         uuart->INTEN |= UUART_INTEN_RXENDIEN_Msk;
313     }
314 }
315 
316 
317 /**
318  *    @brief        Open and set USCI_UART function
319  *
320  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
321  *    @param[in]    u32baudrate     The baud rate of USCI_UART module.
322  *
323  *    @return       Real baud rate of USCI_UART module.
324  *
325  *    @details      This function use to enable USCI_UART function and set baud-rate.
326  */
UUART_Open(UUART_T * uuart,uint32_t u32baudrate)327 uint32_t UUART_Open(UUART_T* uuart, uint32_t u32baudrate)
328 {
329     uint32_t u32PCLKFreq, u32PDSCnt, u32DSCnt, u32ClkDiv;
330     uint32_t u32Tmp, u32Tmp2, u32Min, u32MinClkDiv, u32MinDSCnt;
331     uint32_t u32Div;
332 
333     /* Get PCLK frequency */
334     if( uuart == UUART0)
335     {
336         u32PCLKFreq = CLK_GetPCLK0Freq();
337     }
338     else
339     {
340         u32PCLKFreq = CLK_GetPCLK1Freq();
341     }
342 
343     u32Div = u32PCLKFreq / u32baudrate;
344     u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate;
345     u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div+1ul));
346 
347     if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul;
348 
349     u32Tmp = 0x400ul * 0x10ul;
350     for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++)
351     {
352         if(u32Div <= (u32Tmp * u32PDSCnt)) break;
353     }
354 
355     if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul;
356 
357     u32Div = u32Div / u32PDSCnt;
358 
359     /* Find best solution */
360     u32Min = (uint32_t) - 1;
361     u32MinDSCnt = 0ul;
362     u32MinClkDiv = 0ul;
363     u32Tmp = 0ul;
364 
365     for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++)   /* DSCNT could be 0x5~0xF */
366     {
367 
368         u32ClkDiv = u32Div / u32DSCnt;
369 
370         if(u32ClkDiv > 0x400ul)
371         {
372             u32ClkDiv = 0x400ul;
373             u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
374             u32Tmp2 = u32Tmp + 1ul;
375         }
376         else
377         {
378             u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
379             u32Tmp2 = ((u32ClkDiv+1ul) * u32DSCnt) - u32Div;
380         }
381 
382         if(u32Tmp >= u32Tmp2)
383         {
384             u32ClkDiv = u32ClkDiv + 1ul;
385         }
386         else u32Tmp2 = u32Tmp;
387 
388         if(u32Tmp2 < u32Min)
389         {
390             u32Min = u32Tmp2;
391             u32MinDSCnt = u32DSCnt;
392             u32MinClkDiv = u32ClkDiv;
393 
394             /* Break when get good results */
395             if(u32Min == 0ul)
396             {
397                 break;
398             }
399         }
400     }
401 
402     /* Enable USCI_UART protocol */
403     uuart->CTL &= ~UUART_CTL_FUNMODE_Msk;
404     uuart->CTL = 2ul << UUART_CTL_FUNMODE_Pos;
405 
406     /* Set USCI_UART line configuration */
407     uuart->LINECTL = UUART_WORD_LEN_8 | UUART_LINECTL_LSB_Msk;
408     uuart->DATIN0 = (2ul << UUART_DATIN0_EDGEDET_Pos);  /* Set falling edge detection */
409 
410     /* Set USCI_UART baud rate */
411     uuart->BRGEN = ((u32MinClkDiv-1ul) << UUART_BRGEN_CLKDIV_Pos) |
412                    ((u32MinDSCnt-1ul) << UUART_BRGEN_DSCNT_Pos) |
413                    ((u32PDSCnt-1ul) << UUART_BRGEN_PDSCNT_Pos);
414 
415     uuart->PROTCTL |= UUART_PROTCTL_PROTEN_Msk;
416 
417     return (u32PCLKFreq/u32PDSCnt/u32MinDSCnt/u32MinClkDiv);
418 }
419 
420 
421 /**
422  *    @brief        Read USCI_UART data
423  *
424  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
425  *    @param[in]    pu8RxBuf        The buffer to receive the data of receive buffer.
426  *    @param[in]    u32ReadBytes    The read bytes number of data.
427  *
428  *    @return       Receive byte count
429  *
430  *    @details      The function is used to read Rx data from RX buffer and the data will be stored in pu8RxBuf.
431  */
UUART_Read(UUART_T * uuart,uint8_t pu8RxBuf[],uint32_t u32ReadBytes)432 uint32_t UUART_Read(UUART_T* uuart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes)
433 {
434     uint32_t  u32Count, u32delayno;
435 
436     for(u32Count = 0ul; u32Count < u32ReadBytes; u32Count++)
437     {
438         u32delayno = 0ul;
439 
440         while(uuart->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk)   /* Check RX empty => failed */
441         {
442             u32delayno++;
443             if(u32delayno >= 0x40000000ul)
444             {
445                 break;
446             }
447         }
448 
449         if(u32delayno >= 0x40000000ul)
450         {
451             break;
452         }
453 
454         pu8RxBuf[u32Count] = (uint8_t)uuart->RXDAT;    /* Get Data from USCI RX  */
455     }
456 
457     return u32Count;
458 
459 }
460 
461 
462 /**
463  *    @brief        Set USCI_UART line configuration
464  *
465  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
466  *    @param[in]    u32baudrate     The register value of baud rate of USCI_UART module.
467  *                                  If u32baudrate = 0, USCI_UART baud rate will not change.
468  *    @param[in]    u32data_width   The data length of USCI_UART module.
469  *                                  - \ref UUART_WORD_LEN_6
470  *                                  - \ref UUART_WORD_LEN_7
471  *                                  - \ref UUART_WORD_LEN_8
472  *                                  - \ref UUART_WORD_LEN_9
473  *    @param[in]    u32parity       The parity setting (none/odd/even) of USCI_UART module.
474  *                                  - \ref UUART_PARITY_NONE
475  *                                  - \ref UUART_PARITY_ODD
476  *                                  - \ref UUART_PARITY_EVEN
477  *    @param[in]    u32stop_bits    The stop bit length (1/2 bit) of USCI_UART module.
478  *                                  - \ref UUART_STOP_BIT_1
479  *                                  - \ref UUART_STOP_BIT_2
480  *
481  *    @return       Real baud rate of USCI_UART module.
482  *
483  *    @details      This function use to config USCI_UART line setting.
484  */
UUART_SetLine_Config(UUART_T * uuart,uint32_t u32baudrate,uint32_t u32data_width,uint32_t u32parity,uint32_t u32stop_bits)485 uint32_t UUART_SetLine_Config(UUART_T* uuart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
486 {
487     uint32_t u32PCLKFreq, u32PDSCnt, u32DSCnt, u32ClkDiv;
488     uint32_t u32Tmp, u32Tmp2, u32Min, u32MinClkDiv, u32MinDSCnt;
489     uint32_t u32Div;
490 
491     /* Get PCLK frequency */
492     if(uuart == UUART0)
493     {
494         u32PCLKFreq = CLK_GetPCLK0Freq();
495     }
496     else     /* UUART1 */
497     {
498         u32PCLKFreq = CLK_GetPCLK1Freq();
499     }
500 
501     if(u32baudrate != 0ul)
502     {
503         u32Div = u32PCLKFreq / u32baudrate;
504         u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate;
505         u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div+1ul));
506 
507         if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul;
508 
509         u32Tmp = 0x400ul * 0x10ul;
510         for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++)
511         {
512             if(u32Div <= (u32Tmp * u32PDSCnt)) break;
513         }
514 
515         if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul;
516 
517         u32Div = u32Div / u32PDSCnt;
518 
519         /* Find best solution */
520         u32Min = (uint32_t) - 1;
521         u32MinDSCnt = 0ul;
522         u32MinClkDiv = 0ul;
523 
524         for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++)   /* DSCNT could be 0x5~0xF */
525         {
526             u32ClkDiv = u32Div / u32DSCnt;
527 
528             if(u32ClkDiv > 0x400ul)
529             {
530                 u32ClkDiv = 0x400ul;
531                 u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
532                 u32Tmp2 = u32Tmp + 1ul;
533             }
534             else
535             {
536                 u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
537                 u32Tmp2 = ((u32ClkDiv+1ul) * u32DSCnt) - u32Div;
538             }
539 
540             if(u32Tmp >= u32Tmp2)
541             {
542                 u32ClkDiv = u32ClkDiv + 1ul;
543             }
544             else u32Tmp2 = u32Tmp;
545 
546             if(u32Tmp2 < u32Min)
547             {
548                 u32Min = u32Tmp2;
549                 u32MinDSCnt = u32DSCnt;
550                 u32MinClkDiv = u32ClkDiv;
551 
552                 /* Break when get good results */
553                 if(u32Min == 0ul)
554                 {
555                     break;
556                 }
557             }
558         }
559 
560         /* Set USCI_UART baud rate */
561         uuart->BRGEN = ((u32MinClkDiv-1ul) << UUART_BRGEN_CLKDIV_Pos) |
562                        ((u32MinDSCnt-1ul) << UUART_BRGEN_DSCNT_Pos) |
563                        ((u32PDSCnt-1ul) << UUART_BRGEN_PDSCNT_Pos);
564     }
565     else
566     {
567         u32PDSCnt = ((uuart->BRGEN & UUART_BRGEN_PDSCNT_Msk) >> UUART_BRGEN_PDSCNT_Pos) + 1ul;
568         u32MinDSCnt = ((uuart->BRGEN & UUART_BRGEN_DSCNT_Msk) >> UUART_BRGEN_DSCNT_Pos) + 1ul;
569         u32MinClkDiv = ((uuart->BRGEN & UUART_BRGEN_CLKDIV_Msk) >> UUART_BRGEN_CLKDIV_Pos) + 1ul;
570     }
571 
572     /* Set USCI_UART line configuration */
573     uuart->LINECTL = (uuart->LINECTL & ~UUART_LINECTL_DWIDTH_Msk) | u32data_width;
574     uuart->PROTCTL = (uuart->PROTCTL & ~(UUART_PROTCTL_STICKEN_Msk | UUART_PROTCTL_EVENPARITY_Msk |
575                                          UUART_PROTCTL_PARITYEN_Msk)) | u32parity;
576     uuart->PROTCTL = (uuart->PROTCTL & ~UUART_PROTCTL_STOPB_Msk ) | u32stop_bits;
577 
578     return (u32PCLKFreq/u32PDSCnt/u32MinDSCnt/u32MinClkDiv);
579 }
580 
581 
582 /**
583  *    @brief        Write USCI_UART data
584  *
585  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
586  *    @param[in]    pu8TxBuf        The buffer to send the data to USCI transmission buffer.
587  *    @param[out]   u32WriteBytes   The byte number of data.
588  *
589  *    @return       Transfer byte count
590  *
591  *    @details      The function is to write data into TX buffer to transmit data by USCI_UART.
592  */
UUART_Write(UUART_T * uuart,uint8_t pu8TxBuf[],uint32_t u32WriteBytes)593 uint32_t UUART_Write(UUART_T* uuart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes)
594 {
595     uint32_t  u32Count, u32delayno;
596 
597     for(u32Count = 0ul; u32Count != u32WriteBytes; u32Count++)
598     {
599         u32delayno = 0ul;
600         while((uuart->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) == 0ul)   /* Wait Tx empty */
601         {
602             u32delayno++;
603             if(u32delayno >= 0x40000000ul)
604             {
605                 break;
606             }
607         }
608 
609         if(u32delayno >= 0x40000000ul)
610         {
611             break;
612         }
613 
614         uuart->TXDAT = (uint8_t)pu8TxBuf[u32Count];    /* Send USCI_UART Data to buffer */
615     }
616 
617     return u32Count;
618 
619 }
620 
621 
622 /**
623  *    @brief        Enable USCI_UART Wake-up Function
624  *
625  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
626  *    @param[in]    u32WakeupMode   The wakeup mode of USCI_UART module.
627  *                                   - \ref UUART_PROTCTL_DATWKEN_Msk    : Data wake-up Mode
628  *                                   - \ref UUART_PROTCTL_CTSWKEN_Msk    : nCTS wake-up Mode
629  *
630  *    @return       None
631  *
632  *    @details      The function is used to enable Wake-up function of USCI_UART.
633  */
UUART_EnableWakeup(UUART_T * uuart,uint32_t u32WakeupMode)634 void UUART_EnableWakeup(UUART_T* uuart, uint32_t u32WakeupMode)
635 {
636     uuart->PROTCTL |= u32WakeupMode;
637     uuart->WKCTL |= UUART_WKCTL_WKEN_Msk;
638 }
639 
640 
641 /**
642  *    @brief        Disable USCI_UART Wake-up Function
643  *
644  *    @param[in]    uuart   The pointer of the specified USCI_UART module.
645  *
646  *    @return       None
647  *
648  *    @details      The function is used to disable Wake-up function of USCI_UART.
649  */
UUART_DisableWakeup(UUART_T * uuart)650 void UUART_DisableWakeup(UUART_T* uuart)
651 {
652     uuart->PROTCTL &= ~(UUART_PROTCTL_DATWKEN_Msk|UUART_PROTCTL_CTSWKEN_Msk);
653     uuart->WKCTL &= ~UUART_WKCTL_WKEN_Msk;
654 }
655 
656 /**
657  *    @brief        Enable USCI_UART auto flow control
658  *
659  *    @param[in]    uuart   The pointer of the specified USCI_UART module.
660  *
661  *    @return       None
662  *
663  *    @details      The function is used to enable USCI_UART auto flow control.
664  */
UUART_EnableFlowCtrl(UUART_T * uuart)665 void UUART_EnableFlowCtrl(UUART_T* uuart)
666 {
667     /* Set RTS signal is low level active */
668     uuart->LINECTL &= ~UUART_LINECTL_CTLOINV_Msk;
669 
670     /* Set CTS signal is low level active */
671     uuart->CTLIN0 &= ~UUART_CTLIN0_ININV_Msk;
672 
673     /* Enable CTS and RTS auto flow control function */
674     uuart->PROTCTL |= UUART_PROTCTL_RTSAUTOEN_Msk|UUART_PROTCTL_CTSAUTOEN_Msk;
675 }
676 
677 /**
678  *    @brief        Disable USCI_UART auto flow control
679  *
680  *    @param[in]    uuart    The pointer of the specified USCI_UART module.
681  *
682  *    @return       None
683  *
684  *    @details      The function is used to disable USCI_UART auto flow control.
685  */
UUART_DisableFlowCtrl(UUART_T * uuart)686 void UUART_DisableFlowCtrl(UUART_T* uuart)
687 {
688     /* Disable CTS and RTS auto flow control function */
689     uuart->PROTCTL &= ~(UUART_PROTCTL_RTSAUTOEN_Msk|UUART_PROTCTL_CTSAUTOEN_Msk);
690 }
691 
692 
693 
694 
695 /*@}*/ /* end of group USCI_UART_EXPORTED_FUNCTIONS */
696 
697 /*@}*/ /* end of group USCI_UART_Driver */
698 
699 /*@}*/ /* end of group Standard_Driver */
700 
701 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
702 
703