1 /**************************************************************************//**
2  * @file     usci_uart.h
3  * @version  V3.00
4  * @brief    M480 series USCI UART (UUART) driver header file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8 *****************************************************************************/
9 #ifndef __USCI_UART_H__
10 #define __USCI_UART_H__
11 
12 
13 #ifdef __cplusplus
14 extern "C"
15 {
16 #endif
17 
18 
19 /** @addtogroup Standard_Driver Standard Driver
20   @{
21 */
22 
23 /** @addtogroup USCI_UART_Driver USCI_UART Driver
24   @{
25 */
26 
27 /** @addtogroup USCI_UART_EXPORTED_CONSTANTS USCI_UART Exported Constants
28   @{
29 */
30 
31 /*---------------------------------------------------------------------------------------------------------*/
32 /* UUART_LINECTL constants definitions                                                                     */
33 /*---------------------------------------------------------------------------------------------------------*/
34 #define UUART_WORD_LEN_6     (6ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 6 bits \hideinitializer */
35 #define UUART_WORD_LEN_7     (7ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 7 bits \hideinitializer */
36 #define UUART_WORD_LEN_8     (8ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 8 bits \hideinitializer */
37 #define UUART_WORD_LEN_9     (9ul << UUART_LINECTL_DWIDTH_Pos) /*!< UUART_LINECTL setting to set UART word length to 9 bits \hideinitializer */
38 
39 /*---------------------------------------------------------------------------------------------------------*/
40 /* UUART_PROTCTL constants definitions                                                                     */
41 /*---------------------------------------------------------------------------------------------------------*/
42 #define UUART_PARITY_NONE    (0x0ul << UUART_PROTCTL_PARITYEN_Pos)    /*!< UUART_PROTCTL setting to set UART as no parity \hideinitializer */
43 #define UUART_PARITY_ODD     (0x1ul << UUART_PROTCTL_PARITYEN_Pos)    /*!< UUART_PROTCTL setting to set UART as odd parity \hideinitializer */
44 #define UUART_PARITY_EVEN    (0x3ul << UUART_PROTCTL_PARITYEN_Pos)    /*!< UUART_PROTCTL setting to set UART as even parity \hideinitializer */
45 
46 #define UUART_STOP_BIT_1     (0x0ul) /*!< UUART_PROTCTL setting for one stop bit \hideinitializer */
47 #define UUART_STOP_BIT_2     (0x1ul) /*!< UUART_PROTCTL setting for two stop bit \hideinitializer */
48 
49 /*---------------------------------------------------------------------------------------------------------*/
50 /* USCI UART interrupt mask definitions                                                                    */
51 /*---------------------------------------------------------------------------------------------------------*/
52 #define UUART_ABR_INT_MASK      (0x002ul) /*!< Auto-baud rate interrupt mask \hideinitializer */
53 #define UUART_RLS_INT_MASK      (0x004ul) /*!< Receive line status interrupt mask \hideinitializer */
54 #define UUART_BUF_RXOV_INT_MASK (0x008ul) /*!< Buffer RX overrun interrupt mask \hideinitializer */
55 #define UUART_TXST_INT_MASK     (0x010ul) /*!< TX start interrupt mask \hideinitializer */
56 #define UUART_TXEND_INT_MASK    (0x020ul) /*!< Tx end interrupt mask \hideinitializer */
57 #define UUART_RXST_INT_MASK     (0x040ul) /*!< RX start interrupt mask \hideinitializer */
58 #define UUART_RXEND_INT_MASK    (0x080ul) /*!< RX end interrupt mask \hideinitializer */
59 
60 
61 /*@}*/ /* end of group USCI_UART_EXPORTED_CONSTANTS */
62 
63 
64 /** @addtogroup USCI_UART_EXPORTED_FUNCTIONS USCI_UART Exported Functions
65   @{
66 */
67 
68 
69 /**
70  *    @brief        Write USCI_UART data
71  *
72  *    @param[in]    uuart   The pointer of the specified USCI_UART module
73  *    @param[in]    u8Data  Data byte to transmit.
74  *
75  *    @return       None
76  *
77  *    @details      This macro write Data to Tx data register.
78  *    \hideinitializer
79  */
80 #define UUART_WRITE(uuart, u8Data)    ((uuart)->TXDAT = (u8Data))
81 
82 
83 /**
84  *    @brief        Read USCI_UART data
85  *
86  *    @param[in]    uuart    The pointer of the specified USCI_UART module
87  *
88  *    @return       The oldest data byte in RX buffer.
89  *
90  *    @details      This macro read Rx data register.
91  *    \hideinitializer
92  */
93 #define UUART_READ(uuart)    ((uuart)->RXDAT)
94 
95 
96 /**
97  *    @brief        Get Tx empty
98  *
99  *    @param[in]    uuart    The pointer of the specified USCI_UART module
100  *
101  *    @retval       0   Tx buffer is not empty
102  *    @retval       >=1 Tx buffer is empty
103  *
104  *    @details      This macro get Transmitter buffer empty register value.
105  *    \hideinitializer
106  */
107 #define UUART_GET_TX_EMPTY(uuart)    ((uuart)->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk)
108 
109 
110 /**
111  *    @brief        Get Rx empty
112  *
113  *    @param[in]    uuart    The pointer of the specified USCI_UART module
114  *
115  *    @retval       0   Rx buffer is not empty
116  *    @retval       >=1 Rx buffer is empty
117  *
118  *    @details      This macro get Receiver buffer empty register value.
119  *    \hideinitializer
120  */
121 #define UUART_GET_RX_EMPTY(uuart)    ((uuart)->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk)
122 
123 
124 /**
125  *    @brief        Check specified usci_uart port transmission is over.
126  *
127  *    @param[in]    uuart    The pointer of the specified USCI_UART module
128  *
129  *    @retval       0 Tx transmission is not over
130  *    @retval       1 Tx transmission is over
131  *
132  *    @details      This macro return Transmitter Empty Flag register bit value. \n
133  *                  It indicates if specified usci_uart port transmission is over nor not.
134  *    \hideinitializer
135  */
136 #define UUART_IS_TX_EMPTY(uuart)    (((uuart)->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) >> UUART_BUFSTS_TXEMPTY_Pos)
137 
138 
139 /**
140  *    @brief        Check specified usci_uart port receiver is empty.
141  *
142  *    @param[in]    uuart    The pointer of the specified USCI_UART module
143  *
144  *    @retval       0 Rx receiver is not empty
145  *    @retval       1 Rx receiver is empty
146  *
147  *    @details      This macro return Receive Empty Flag register bit value. \n
148  *                  It indicates if specified usci_uart port receiver is empty nor not.
149  *    \hideinitializer
150  */
151 #define UUART_IS_RX_EMPTY(uuart)    (((uuart)->BUFSTS & UUART_BUFSTS_RXEMPTY_Msk) >> UUART_BUFSTS_RXEMPTY_Pos)
152 
153 
154 /**
155  *    @brief        Wait specified usci_uart port transmission is over
156  *
157  *    @param[in]    uuart    The pointer of the specified USCI_UART module
158  *
159  *    @return       None
160  *
161  *    @details      This macro wait specified usci_uart port transmission is over.
162  *    \hideinitializer
163  */
164 #define UUART_WAIT_TX_EMPTY(uuart)    while(!((((uuart)->BUFSTS) & UUART_BUFSTS_TXEMPTY_Msk) >> UUART_BUFSTS_TXEMPTY_Pos))
165 
166 
167 /**
168  *    @brief        Check TX buffer is full or not
169  *
170  *    @param[in]    uuart    The pointer of the specified USCI_UART module
171  *
172  *    @retval       1 TX buffer is full
173  *    @retval       0 TX buffer is not full
174  *
175  *    @details      This macro check TX buffer is full or not.
176  *    \hideinitializer
177  */
178 #define UUART_IS_TX_FULL(uuart)    (((uuart)->BUFSTS & UUART_BUFSTS_TXFULL_Msk)>>UUART_BUFSTS_TXFULL_Pos)
179 
180 
181 /**
182  *    @brief        Check RX buffer is full or not
183  *
184  *    @param[in]    uuart    The pointer of the specified USCI_UART module
185  *
186  *    @retval       1 RX buffer is full
187  *    @retval       0 RX buffer is not full
188  *
189  *    @details      This macro check RX buffer is full or not.
190  *    \hideinitializer
191  */
192 #define UUART_IS_RX_FULL(uuart)    (((uuart)->BUFSTS & UUART_BUFSTS_RXFULL_Msk)>>UUART_BUFSTS_RXFULL_Pos)
193 
194 
195 /**
196  *    @brief        Get Tx full register value
197  *
198  *    @param[in]    uuart    The pointer of the specified USCI_UART module
199  *
200  *    @retval       0   Tx buffer is not full.
201  *    @retval       >=1 Tx buffer is full.
202  *
203  *    @details      This macro get Tx full register value.
204  *    \hideinitializer
205  */
206 #define UUART_GET_TX_FULL(uuart)    ((uuart)->BUFSTS & UUART_BUFSTS_TXFULL_Msk)
207 
208 
209 /**
210  *    @brief        Get Rx full register value
211  *
212  *    @param[in]    uuart    The pointer of the specified USCI_UART module
213  *
214  *    @retval       0   Rx buffer is not full.
215  *    @retval       >=1 Rx buffer is full.
216  *
217  *    @details      This macro get Rx full register value.
218  *    \hideinitializer
219  */
220 #define UUART_GET_RX_FULL(uuart)    ((uuart)->BUFSTS & UUART_BUFSTS_RXFULL_Msk)
221 
222 
223 /**
224  *    @brief        Enable specified USCI_UART protocol interrupt
225  *
226  *    @param[in]    uuart      The pointer of the specified USCI_UART module
227  *    @param[in]    u32IntSel  Interrupt type select
228  *                             - \ref UUART_PROTIEN_RLSIEN_Msk   : Rx Line status interrupt
229  *                             - \ref UUART_PROTIEN_ABRIEN_Msk   : Auto-baud rate interrupt
230  *
231  *    @return       None
232  *
233  *    @details      This macro enable specified USCI_UART protocol interrupt.
234  *    \hideinitializer
235  */
236 #define UUART_ENABLE_PROT_INT(uuart, u32IntSel)    ((uuart)->PROTIEN |= (u32IntSel))
237 
238 
239 /**
240  *    @brief        Disable specified USCI_UART protocol interrupt
241  *
242  *    @param[in]    uuart      The pointer of the specified USCI_UART module
243  *    @param[in]    u32IntSel  Interrupt type select
244  *                             - \ref UUART_PROTIEN_RLSIEN_Msk   : Rx Line status interrupt
245  *                             - \ref UUART_PROTIEN_ABRIEN_Msk   : Auto-baud rate interrupt
246  *
247  *    @return       None
248  *
249  *    @details      This macro disable specified USCI_UART protocol interrupt.
250  *    \hideinitializer
251  */
252 #define UUART_DISABLE_PROT_INT(uuart, u32IntSel)    ((uuart)->PROTIEN &= ~(u32IntSel))
253 
254 
255 /**
256  *    @brief        Enable specified USCI_UART buffer interrupt
257  *
258  *    @param[in]    uuart      The pointer of the specified USCI_UART module
259  *    @param[in]    u32IntSel  Interrupt type select
260  *                             - \ref UUART_BUFCTL_RXOVIEN_Msk     : Receive buffer overrun error interrupt
261  *
262  *    @return       None
263  *
264  *    @details      This macro enable specified USCI_UART buffer interrupt.
265  *    \hideinitializer
266  */
267 #define UUART_ENABLE_BUF_INT(uuart, u32IntSel)    ((uuart)->BUFCTL |= (u32IntSel))
268 
269 
270 /**
271  *    @brief        Disable specified USCI_UART buffer interrupt
272  *
273  *    @param[in]    uuart      The pointer of the specified USCI_UART module
274  *    @param[in]    u32IntSel  Interrupt type select
275  *                             - \ref UUART_BUFCTL_RXOVIEN_Msk     : Receive buffer overrun error interrupt
276  *
277  *    @return       None
278  *
279  *    @details      This macro disable specified USCI_UART buffer interrupt.
280  *    \hideinitializer
281  */
282 #define UUART_DISABLE_BUF_INT(uuart, u32IntSel)    ((uuart)->BUFCTL &= ~ (u32IntSel))
283 
284 
285 /**
286  *    @brief        Enable specified USCI_UART transfer interrupt
287  *
288  *    @param[in]    uuart      The pointer of the specified USCI_UART module
289  *    @param[in]    u32IntSel  Interrupt type select
290  *                             - \ref UUART_INTEN_RXENDIEN_Msk  : Receive end interrupt
291  *                             - \ref UUART_INTEN_RXSTIEN_Msk   : Receive start interrupt
292  *                             - \ref UUART_INTEN_TXENDIEN_Msk  : Transmit end interrupt
293  *                             - \ref UUART_INTEN_TXSTIEN_Msk   : Transmit start interrupt
294  *
295  *    @return       None
296  *
297  *    @details      This macro enable specified USCI_UART transfer interrupt.
298  *    \hideinitializer
299  */
300 #define UUART_ENABLE_TRANS_INT(uuart, u32IntSel)    ((uuart)->INTEN |= (u32IntSel))
301 
302 
303 /**
304  *    @brief        Disable specified USCI_UART transfer interrupt
305  *
306  *    @param[in]    uuart      The pointer of the specified USCI_UART module
307  *    @param[in]    u32IntSel  Interrupt type select
308  *                             - \ref UUART_INTEN_RXENDIEN_Msk  : Receive end interrupt
309  *                             - \ref UUART_INTEN_RXSTIEN_Msk   : Receive start interrupt
310  *                             - \ref UUART_INTEN_TXENDIEN_Msk  : Transmit end interrupt
311  *                             - \ref UUART_INTEN_TXSTIEN_Msk   : Transmit start interrupt
312  *
313  *    @return       None
314  *
315  *    @details      This macro disable specified USCI_UART transfer interrupt.
316  *    \hideinitializer
317  */
318 #define UUART_DISABLE_TRANS_INT(uuart, u32IntSel)    ((uuart)->INTEN &= ~(u32IntSel))
319 
320 
321 /**
322  *    @brief        Get protocol interrupt flag/status
323  *
324  *    @param[in]    uuart        The pointer of the specified USCI_UART module
325  *
326  *    @return       The interrupt flag/status of protocol status register.
327  *
328  *    @details      This macro get protocol status register value.
329  *    \hideinitializer
330  */
331 #define UUART_GET_PROT_STATUS(uuart)    ((uuart)->PROTSTS)
332 
333 
334 /**
335  *    @brief        Clear specified protocol interrupt flag
336  *
337  *    @param[in]    uuart           The pointer of the specified USCI_UART module
338  *    @param[in]    u32IntTypeFlag  Interrupt Type Flag, should be
339  *                                  - \ref UUART_PROTSTS_ABERRSTS_Msk    : Auto-baud Rate Error Interrupt Indicator
340  *                                  - \ref UUART_PROTSTS_ABRDETIF_Msk    : Auto-baud Rate Detected Interrupt Flag
341  *                                  - \ref UUART_PROTSTS_BREAK_Msk       : Break Flag
342  *                                  - \ref UUART_PROTSTS_FRMERR_Msk      : Framing Error Flag
343  *                                  - \ref UUART_PROTSTS_PARITYERR_Msk   : Parity Error Flag
344  *                                  - \ref UUART_PROTSTS_RXENDIF_Msk     : Receive End Interrupt Flag
345  *                                  - \ref UUART_PROTSTS_RXSTIF_Msk      : Receive Start Interrupt Flag
346  *                                  - \ref UUART_PROTSTS_TXENDIF_Msk     : Transmit End Interrupt Flag
347  *                                  - \ref UUART_PROTSTS_TXSTIF_Msk      : Transmit Start Interrupt Flag
348  *
349  *    @return       None
350  *
351  *    @details      This macro clear specified protocol interrupt flag.
352  *    \hideinitializer
353  */
354 #define UUART_CLR_PROT_INT_FLAG(uuart,u32IntTypeFlag)    ((uuart)->PROTSTS = (u32IntTypeFlag))
355 
356 
357 /**
358  *    @brief        Get transmit/receive buffer interrupt flag/status
359  *
360  *    @param[in]    uuart        The pointer of the specified USCI_UART module
361  *
362  *    @return       The interrupt flag/status of buffer status register.
363  *
364  *    @details      This macro get buffer status register value.
365  *    \hideinitializer
366  */
367 #define UUART_GET_BUF_STATUS(uuart)    ((uuart)->BUFSTS)
368 
369 
370 /**
371  *    @brief        Clear specified buffer interrupt flag
372  *
373  *    @param[in]    uuart           The pointer of the specified USCI_UART module
374  *    @param[in]    u32IntTypeFlag  Interrupt Type Flag, should be
375  *                                  - \ref UUART_BUFSTS_RXOVIF_Msk : Receive Buffer Over-run Error  Interrupt Indicator
376  *
377  *    @return       None
378  *
379  *    @details      This macro clear specified buffer interrupt flag.
380  *    \hideinitializer
381  */
382 #define UUART_CLR_BUF_INT_FLAG(uuart,u32IntTypeFlag)    ((uuart)->BUFSTS = (u32IntTypeFlag))
383 
384 
385 /**
386  *    @brief        Get wakeup flag
387  *
388  *    @param[in]    uuart    The pointer of the specified USCI_UART module
389  *
390  *    @retval       0       Chip did not wake up from power-down mode.
391  *    @retval       1       Chip waked up from power-down mode.
392  *
393  *    @details      This macro get wakeup flag.
394  *    \hideinitializer
395  */
396 #define UUART_GET_WAKEUP_FLAG(uuart)    ((uuart)->WKSTS & UUART_WKSTS_WKF_Msk ? 1: 0 )
397 
398 
399 /**
400  *    @brief        Clear wakeup flag
401  *
402  *    @param[in]    uuart        The pointer of the specified USCI_UART module
403  *
404  *    @return       None
405  *
406  *    @details      This macro clear wakeup flag.
407  *    \hideinitializer
408  */
409 #define UUART_CLR_WAKEUP_FLAG(uuart)    ((uuart)->WKSTS = UUART_WKSTS_WKF_Msk)
410 
411 /**
412  *    @brief        Enable specified USCI_UART PDMA function
413  *
414  *    @param[in]    uuart      The pointer of the specified USCI_UART module
415  *    @param[in]    u32FuncSel Combination of following functions
416  *                             - \ref UUART_PDMACTL_TXPDMAEN_Msk
417  *                             - \ref UUART_PDMACTL_RXPDMAEN_Msk
418  *                             - \ref UUART_PDMACTL_PDMAEN_Msk
419  *
420  *    @return       None
421  *
422  *    \hideinitializer
423  */
424 #define UUART_PDMA_ENABLE(uuart, u32FuncSel)   ((uuart)->PDMACTL |= (u32FuncSel))
425 
426 /**
427  *    @brief        Disable specified USCI_UART PDMA function
428  *
429  *    @param[in]    uuart      The pointer of the specified USCI_UART module
430  *    @param[in]    u32FuncSel Combination of following functions
431  *                             - \ref UUART_PDMACTL_TXPDMAEN_Msk
432  *                             - \ref UUART_PDMACTL_RXPDMAEN_Msk
433  *                             - \ref UUART_PDMACTL_PDMAEN_Msk
434  *
435  *    @return       None
436  *
437  *    \hideinitializer
438  */
439 #define UUART_PDMA_DISABLE(uuart, u32FuncSel)   ((uuart)->PDMACTL &= ~(u32FuncSel))
440 
441 
442 
443 
444 void UUART_ClearIntFlag(UUART_T* uuart, uint32_t u32Mask);
445 uint32_t UUART_GetIntFlag(UUART_T* uuart, uint32_t u32Mask);
446 void UUART_Close(UUART_T* uuart);
447 void UUART_DisableInt(UUART_T*  uuart, uint32_t u32Mask);
448 void UUART_EnableInt(UUART_T*  uuart, uint32_t u32Mask);
449 uint32_t UUART_Open(UUART_T* uuart, uint32_t u32baudrate);
450 uint32_t UUART_Read(UUART_T* uuart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes);
451 uint32_t UUART_SetLine_Config(UUART_T* uuart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits);
452 uint32_t UUART_Write(UUART_T* uuart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes);
453 void UUART_EnableWakeup(UUART_T* uuart, uint32_t u32WakeupMode);
454 void UUART_DisableWakeup(UUART_T* uuart);
455 void UUART_EnableFlowCtrl(UUART_T* uuart);
456 void UUART_DisableFlowCtrl(UUART_T* uuart);
457 
458 
459 /*@}*/ /* end of group USCI_UART_EXPORTED_FUNCTIONS */
460 
461 /*@}*/ /* end of group USCI_UART_Driver */
462 
463 /*@}*/ /* end of group Standard_Driver */
464 
465 #ifdef __cplusplus
466 }
467 #endif
468 
469 #endif /* __USCI_UART_H__ */
470 
471 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
472