1 /**************************************************************************//**
2  * @file     usci_uart.c
3  * @version  V3.00
4  * @brief    M460 series USCI UART (UUART) driver source file
5  *
6  * @copyright SPDX-License-Identifier: Apache-2.0
7  * @copyright Copyright (C) 2021 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     u32PCLKFreq = CLK_GetPCLK0Freq();
335 
336     /* Calculate baud rate divider */
337     u32Div = u32PCLKFreq / u32baudrate;
338     u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate;
339     u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div + 1ul));
340 
341     if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul;
342 
343     if(u32Div >= 65536ul)
344     {
345 
346         /* Set the smallest baud rate that USCI_UART can generate */
347         u32PDSCnt = 0x4ul;
348         u32MinDSCnt = 0x10ul;
349         u32MinClkDiv = 0x400ul;
350 
351     }
352     else
353     {
354 
355         u32Tmp = 0x400ul * 0x10ul;
356         for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++)
357         {
358             if(u32Div <= (u32Tmp * u32PDSCnt)) break;
359         }
360 
361         if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul;
362 
363         u32Div = u32Div / u32PDSCnt;
364 
365         /* Find best solution */
366         u32Min = (uint32_t) - 1;
367         u32MinDSCnt = 0ul;
368         u32MinClkDiv = 0ul;
369         u32Tmp = 0ul;
370 
371         for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++)   /* DSCNT could be 0x5~0xF */
372         {
373 
374             u32ClkDiv = u32Div / u32DSCnt;
375 
376             if(u32ClkDiv > 0x400ul)
377             {
378                 u32ClkDiv = 0x400ul;
379                 u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
380                 u32Tmp2 = u32Tmp + 1ul;
381             }
382             else
383             {
384                 u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
385                 u32Tmp2 = ((u32ClkDiv+1ul) * u32DSCnt) - u32Div;
386             }
387 
388             if(u32Tmp >= u32Tmp2)
389             {
390                 u32ClkDiv = u32ClkDiv + 1ul;
391             }
392             else u32Tmp2 = u32Tmp;
393 
394             if(u32Tmp2 < u32Min)
395             {
396                 u32Min = u32Tmp2;
397                 u32MinDSCnt = u32DSCnt;
398                 u32MinClkDiv = u32ClkDiv;
399 
400                 /* Break when get good results */
401                 if(u32Min == 0ul)
402                 {
403                     break;
404                 }
405             }
406         }
407     }
408 
409     /* Enable USCI_UART protocol */
410     uuart->CTL &= ~UUART_CTL_FUNMODE_Msk;
411     uuart->CTL = 2ul << UUART_CTL_FUNMODE_Pos;
412 
413     /* Set USCI_UART line configuration */
414     uuart->LINECTL = UUART_WORD_LEN_8 | UUART_LINECTL_LSB_Msk;
415     uuart->DATIN0 = (2ul << UUART_DATIN0_EDGEDET_Pos);  /* Set falling edge detection */
416 
417     /* Set USCI_UART baud rate */
418     uuart->BRGEN = ((u32MinClkDiv-1ul) << UUART_BRGEN_CLKDIV_Pos) |
419                    ((u32MinDSCnt-1ul) << UUART_BRGEN_DSCNT_Pos) |
420                    ((u32PDSCnt-1ul) << UUART_BRGEN_PDSCNT_Pos);
421 
422     uuart->PROTCTL |= UUART_PROTCTL_PROTEN_Msk;
423 
424     return (u32PCLKFreq/u32PDSCnt/u32MinDSCnt/u32MinClkDiv);
425 }
426 
427 
428 /**
429  *    @brief        Read USCI_UART data
430  *
431  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
432  *    @param[in]    pu8RxBuf        The buffer to receive the data of receive buffer.
433  *    @param[in]    u32ReadBytes    The read bytes number of data.
434  *
435  *    @return       Receive byte count
436  *
437  *    @details      The function is used to read Rx data from RX buffer and the data will be stored in pu8RxBuf.
438  */
UUART_Read(UUART_T * uuart,uint8_t pu8RxBuf[],uint32_t u32ReadBytes)439 uint32_t UUART_Read(UUART_T* uuart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes)
440 {
441     uint32_t  u32Count, u32delayno;
442 
443     for(u32Count = 0ul; u32Count < u32ReadBytes; u32Count++)
444     {
445         u32delayno = 0ul;
446 
447         while(uuart->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk)   /* Check RX empty => failed */
448         {
449             u32delayno++;
450             if(u32delayno >= 0x40000000ul)
451             {
452                 break;
453             }
454         }
455 
456         if(u32delayno >= 0x40000000ul)
457         {
458             break;
459         }
460 
461         pu8RxBuf[u32Count] = (uint8_t)uuart->RXDAT;    /* Get Data from USCI RX  */
462     }
463 
464     return u32Count;
465 
466 }
467 
468 
469 /**
470  *    @brief        Set USCI_UART line configuration
471  *
472  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
473  *    @param[in]    u32baudrate     The register value of baud rate of USCI_UART module.
474  *                                  If u32baudrate = 0, USCI_UART baud rate will not change.
475  *    @param[in]    u32data_width   The data length of USCI_UART module.
476  *                                  - \ref UUART_WORD_LEN_6
477  *                                  - \ref UUART_WORD_LEN_7
478  *                                  - \ref UUART_WORD_LEN_8
479  *                                  - \ref UUART_WORD_LEN_9
480  *    @param[in]    u32parity       The parity setting (none/odd/even) of USCI_UART module.
481  *                                  - \ref UUART_PARITY_NONE
482  *                                  - \ref UUART_PARITY_ODD
483  *                                  - \ref UUART_PARITY_EVEN
484  *    @param[in]    u32stop_bits    The stop bit length (1/2 bit) of USCI_UART module.
485  *                                  - \ref UUART_STOP_BIT_1
486  *                                  - \ref UUART_STOP_BIT_2
487  *
488  *    @return       Real baud rate of USCI_UART module.
489  *
490  *    @details      This function use to config USCI_UART line setting.
491  */
UUART_SetLine_Config(UUART_T * uuart,uint32_t u32baudrate,uint32_t u32data_width,uint32_t u32parity,uint32_t u32stop_bits)492 uint32_t UUART_SetLine_Config(UUART_T* uuart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
493 {
494     uint32_t u32PCLKFreq, u32PDSCnt, u32DSCnt, u32ClkDiv;
495     uint32_t u32Tmp, u32Tmp2, u32Min, u32MinClkDiv, u32MinDSCnt;
496     uint32_t u32Div;
497 
498     /* Get PCLK frequency */
499     u32PCLKFreq = CLK_GetPCLK0Freq();
500 
501 
502     if(u32baudrate != 0ul)
503     {
504 
505         /* Calculate baud rate divider */
506         u32Div = u32PCLKFreq / u32baudrate;
507         u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate;
508         u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div+1ul));
509 
510         if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul;
511 
512         if(u32Div >= 65536ul)
513         {
514 
515             /* Set the smallest baud rate that USCI_UART can generate */
516             u32PDSCnt = 0x4ul;
517             u32MinDSCnt = 0x10ul;
518             u32MinClkDiv = 0x400ul;
519 
520         }
521         else
522         {
523         u32Tmp = 0x400ul * 0x10ul;
524         for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++)
525         {
526             if(u32Div <= (u32Tmp * u32PDSCnt)) break;
527         }
528 
529             if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul;
530 
531             u32Div = u32Div / u32PDSCnt;
532 
533             /* Find best solution */
534             u32Min = (uint32_t) - 1;
535             u32MinDSCnt = 0ul;
536             u32MinClkDiv = 0ul;
537 
538             for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++)   /* DSCNT could be 0x5~0xF */
539             {
540                 u32ClkDiv = u32Div / u32DSCnt;
541 
542                 if(u32ClkDiv > 0x400ul)
543                 {
544                     u32ClkDiv = 0x400ul;
545                     u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
546                     u32Tmp2 = u32Tmp + 1ul;
547                 }
548                 else
549                 {
550                     u32Tmp = u32Div - (u32ClkDiv * u32DSCnt);
551                     u32Tmp2 = ((u32ClkDiv+1ul) * u32DSCnt) - u32Div;
552                 }
553 
554                 if(u32Tmp >= u32Tmp2)
555                 {
556                     u32ClkDiv = u32ClkDiv + 1ul;
557                 }
558                 else u32Tmp2 = u32Tmp;
559 
560                 if(u32Tmp2 < u32Min)
561                 {
562                     u32Min = u32Tmp2;
563                     u32MinDSCnt = u32DSCnt;
564                     u32MinClkDiv = u32ClkDiv;
565 
566                     /* Break when get good results */
567                     if(u32Min == 0ul)
568                     {
569                         break;
570                     }
571                 }
572             }
573 
574         }
575 
576         /* Set USCI_UART baud rate */
577         uuart->BRGEN = ((u32MinClkDiv - 1ul) << UUART_BRGEN_CLKDIV_Pos) |
578                        ((u32MinDSCnt - 1ul) << UUART_BRGEN_DSCNT_Pos) |
579                        ((u32PDSCnt - 1ul) << UUART_BRGEN_PDSCNT_Pos);
580     }
581     else
582     {
583         u32PDSCnt = ((uuart->BRGEN & UUART_BRGEN_PDSCNT_Msk) >> UUART_BRGEN_PDSCNT_Pos) + 1ul;
584         u32MinDSCnt = ((uuart->BRGEN & UUART_BRGEN_DSCNT_Msk) >> UUART_BRGEN_DSCNT_Pos) + 1ul;
585         u32MinClkDiv = ((uuart->BRGEN & UUART_BRGEN_CLKDIV_Msk) >> UUART_BRGEN_CLKDIV_Pos) + 1ul;
586     }
587 
588     /* Set USCI_UART line configuration */
589     uuart->LINECTL = (uuart->LINECTL & ~UUART_LINECTL_DWIDTH_Msk) | u32data_width;
590     uuart->PROTCTL = (uuart->PROTCTL & ~(UUART_PROTCTL_STICKEN_Msk | UUART_PROTCTL_EVENPARITY_Msk |
591                                          UUART_PROTCTL_PARITYEN_Msk)) | u32parity;
592     uuart->PROTCTL = (uuart->PROTCTL & ~UUART_PROTCTL_STOPB_Msk ) | u32stop_bits;
593 
594     return (u32PCLKFreq/u32PDSCnt/u32MinDSCnt/u32MinClkDiv);
595 }
596 
597 
598 /**
599  *    @brief        Write USCI_UART data
600  *
601  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
602  *    @param[in]    pu8TxBuf        The buffer to send the data to USCI transmission buffer.
603  *    @param[out]   u32WriteBytes   The byte number of data.
604  *
605  *    @return       Transfer byte count
606  *
607  *    @details      The function is to write data into TX buffer to transmit data by USCI_UART.
608  */
UUART_Write(UUART_T * uuart,uint8_t pu8TxBuf[],uint32_t u32WriteBytes)609 uint32_t UUART_Write(UUART_T* uuart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes)
610 {
611     uint32_t  u32Count, u32delayno;
612 
613     for(u32Count = 0ul; u32Count != u32WriteBytes; u32Count++)
614     {
615         u32delayno = 0ul;
616         while((uuart->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) == 0ul)   /* Wait Tx empty */
617         {
618             u32delayno++;
619             if(u32delayno >= 0x40000000ul)
620             {
621                 break;
622             }
623         }
624 
625         if(u32delayno >= 0x40000000ul)
626         {
627             break;
628         }
629 
630         uuart->TXDAT = (uint8_t)pu8TxBuf[u32Count];    /* Send USCI_UART Data to buffer */
631     }
632 
633     return u32Count;
634 
635 }
636 
637 
638 /**
639  *    @brief        Enable USCI_UART Wake-up Function
640  *
641  *    @param[in]    uuart           The pointer of the specified USCI_UART module.
642  *    @param[in]    u32WakeupMode   The wakeup mode of USCI_UART module.
643  *                                  - \ref UUART_PROTCTL_DATWKEN_Msk    : Data wake-up Mode
644  *                                  - \ref UUART_PROTCTL_CTSWKEN_Msk    : nCTS wake-up Mode
645  *
646  *    @return       None
647  *
648  *    @details      The function is used to enable Wake-up function of USCI_UART.
649  */
UUART_EnableWakeup(UUART_T * uuart,uint32_t u32WakeupMode)650 void UUART_EnableWakeup(UUART_T* uuart, uint32_t u32WakeupMode)
651 {
652     uuart->PROTCTL |= u32WakeupMode;
653     uuart->WKCTL |= UUART_WKCTL_WKEN_Msk;
654 }
655 
656 
657 /**
658  *    @brief        Disable USCI_UART Wake-up Function
659  *
660  *    @param[in]    uuart   The pointer of the specified USCI_UART module.
661  *
662  *    @return       None
663  *
664  *    @details      The function is used to disable Wake-up function of USCI_UART.
665  */
UUART_DisableWakeup(UUART_T * uuart)666 void UUART_DisableWakeup(UUART_T* uuart)
667 {
668     uuart->PROTCTL &= ~(UUART_PROTCTL_DATWKEN_Msk|UUART_PROTCTL_CTSWKEN_Msk);
669     uuart->WKCTL &= ~UUART_WKCTL_WKEN_Msk;
670 }
671 
672 /**
673  *    @brief        Enable USCI_UART auto flow control
674  *
675  *    @param[in]    uuart   The pointer of the specified USCI_UART module.
676  *
677  *    @return       None
678  *
679  *    @details      The function is used to enable USCI_UART auto flow control.
680  */
UUART_EnableFlowCtrl(UUART_T * uuart)681 void UUART_EnableFlowCtrl(UUART_T* uuart)
682 {
683     /* Set RTS signal is low level active */
684     uuart->LINECTL &= ~UUART_LINECTL_CTLOINV_Msk;
685 
686     /* Set CTS signal is low level active */
687     uuart->CTLIN0 &= ~UUART_CTLIN0_ININV_Msk;
688 
689     /* Enable CTS and RTS auto flow control function */
690     uuart->PROTCTL |= UUART_PROTCTL_RTSAUTOEN_Msk|UUART_PROTCTL_CTSAUTOEN_Msk;
691 }
692 
693 /**
694  *    @brief        Disable USCI_UART auto flow control
695  *
696  *    @param[in]    uuart    The pointer of the specified USCI_UART module.
697  *
698  *    @return       None
699  *
700  *    @details      The function is used to disable USCI_UART auto flow control.
701  */
UUART_DisableFlowCtrl(UUART_T * uuart)702 void UUART_DisableFlowCtrl(UUART_T* uuart)
703 {
704     /* Disable CTS and RTS auto flow control function */
705     uuart->PROTCTL &= ~(UUART_PROTCTL_RTSAUTOEN_Msk|UUART_PROTCTL_CTSAUTOEN_Msk);
706 }
707 
708 
709 /*@}*/ /* end of group USCI_UART_EXPORTED_FUNCTIONS */
710 
711 /*@}*/ /* end of group USCI_UART_Driver */
712 
713 /*@}*/ /* end of group Standard_Driver */
714