| M480 BSP V3.05.001 The Board Support Package for M480 Series

uart.h
Go to the documentation of this file.
1 /**************************************************************************/
9 #ifndef __UART_H__
10 #define __UART_H__
11
12
13 #ifdef __cplusplus
14 extern "C"
15 {
16 #endif
17
18
31 /*---------------------------------------------------------------------------------------------------------*/
32 /* UART FIFO size constants definitions */
33 /*---------------------------------------------------------------------------------------------------------*/
34
35 #define UART0_FIFO_SIZE 16ul
36 #define UART1_FIFO_SIZE 16ul
37 #define UART2_FIFO_SIZE 16ul
38 #define UART3_FIFO_SIZE 16ul
39 #define UART4_FIFO_SIZE 16ul
40 #define UART5_FIFO_SIZE 16ul
42 /*---------------------------------------------------------------------------------------------------------*/
43 /* UART_FIFO constants definitions */
44 /*---------------------------------------------------------------------------------------------------------*/
45
46 #define UART_FIFO_RFITL_1BYTE (0x0ul << UART_FIFO_RFITL_Pos)
47 #define UART_FIFO_RFITL_4BYTES (0x1ul << UART_FIFO_RFITL_Pos)
48 #define UART_FIFO_RFITL_8BYTES (0x2ul << UART_FIFO_RFITL_Pos)
49 #define UART_FIFO_RFITL_14BYTES (0x3ul << UART_FIFO_RFITL_Pos)
51 #define UART_FIFO_RTSTRGLV_1BYTE (0x0ul << UART_FIFO_RTSTRGLV_Pos)
52 #define UART_FIFO_RTSTRGLV_4BYTES (0x1ul << UART_FIFO_RTSTRGLV_Pos)
53 #define UART_FIFO_RTSTRGLV_8BYTES (0x2ul << UART_FIFO_RTSTRGLV_Pos)
54 #define UART_FIFO_RTSTRGLV_14BYTES (0x3ul << UART_FIFO_RTSTRGLV_Pos)
56 /*---------------------------------------------------------------------------------------------------------*/
57 /* UART_LINE constants definitions */
58 /*---------------------------------------------------------------------------------------------------------*/
59 #define UART_WORD_LEN_5 (0ul)
60 #define UART_WORD_LEN_6 (1ul)
61 #define UART_WORD_LEN_7 (2ul)
62 #define UART_WORD_LEN_8 (3ul)
64 #define UART_PARITY_NONE (0x0ul << UART_LINE_PBE_Pos)
65 #define UART_PARITY_ODD (0x1ul << UART_LINE_PBE_Pos)
66 #define UART_PARITY_EVEN (0x3ul << UART_LINE_PBE_Pos)
67 #define UART_PARITY_MARK (0x5ul << UART_LINE_PBE_Pos)
68 #define UART_PARITY_SPACE (0x7ul << UART_LINE_PBE_Pos)
70 #define UART_STOP_BIT_1 (0x0ul << UART_LINE_NSB_Pos)
71 #define UART_STOP_BIT_1_5 (0x1ul << UART_LINE_NSB_Pos)
72 #define UART_STOP_BIT_2 (0x1ul << UART_LINE_NSB_Pos)
75 /*---------------------------------------------------------------------------------------------------------*/
76 /* UART RTS ACTIVE LEVEL constants definitions */
77 /*---------------------------------------------------------------------------------------------------------*/
78 #define UART_RTS_IS_LOW_LEV_ACTIVE (0x1ul << UART_MODEM_RTSACTLV_Pos)
79 #define UART_RTS_IS_HIGH_LEV_ACTIVE (0x0ul << UART_MODEM_RTSACTLV_Pos)
82 /*---------------------------------------------------------------------------------------------------------*/
83 /* UART_IRDA constants definitions */
84 /*---------------------------------------------------------------------------------------------------------*/
85 #define UART_IRDA_TXEN (0x1ul << UART_IRDA_TXEN_Pos)
86 #define UART_IRDA_RXEN (0x0ul << UART_IRDA_TXEN_Pos)
89 /*---------------------------------------------------------------------------------------------------------*/
90 /* UART_FUNCSEL constants definitions */
91 /*---------------------------------------------------------------------------------------------------------*/
92 #define UART_FUNCSEL_UART (0x0ul << UART_FUNCSEL_FUNCSEL_Pos)
93 #define UART_FUNCSEL_LIN (0x1ul << UART_FUNCSEL_FUNCSEL_Pos)
94 #define UART_FUNCSEL_IrDA (0x2ul << UART_FUNCSEL_FUNCSEL_Pos)
95 #define UART_FUNCSEL_RS485 (0x3ul << UART_FUNCSEL_FUNCSEL_Pos)
98 /*---------------------------------------------------------------------------------------------------------*/
99 /* UART_LINCTL constants definitions */
100 /*---------------------------------------------------------------------------------------------------------*/
101 #define UART_LINCTL_BRKFL(x) (((x)-1) << UART_LINCTL_BRKFL_Pos)
102 #define UART_LINCTL_BSL(x) (((x)-1) << UART_LINCTL_BSL_Pos)
103 #define UART_LINCTL_HSEL_BREAK (0x0UL << UART_LINCTL_HSEL_Pos)
104 #define UART_LINCTL_HSEL_BREAK_SYNC (0x1UL << UART_LINCTL_HSEL_Pos)
105 #define UART_LINCTL_HSEL_BREAK_SYNC_ID (0x2UL << UART_LINCTL_HSEL_Pos)
106 #define UART_LINCTL_PID(x) ((x) << UART_LINCTL_PID_Pos)
109 /*---------------------------------------------------------------------------------------------------------*/
110 /* UART BAUDRATE MODE constants definitions */
111 /*---------------------------------------------------------------------------------------------------------*/
112 #define UART_BAUD_MODE0 (0ul)
113 #define UART_BAUD_MODE2 (UART_BAUD_BAUDM1_Msk | UART_BAUD_BAUDM0_Msk)
116 /* end of group UART_EXPORTED_CONSTANTS */
117
118
135 #define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate) ((((u32SrcFreq) + ((u32BaudRate)*8ul)) / (u32BaudRate) >> 4ul)-2ul)
136
137
149 #define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate) ((((u32SrcFreq) + ((u32BaudRate)/2ul)) / (u32BaudRate))-2ul)
150
151
163 #define UART_WRITE(uart, u8Data) ((uart)->DAT = (u8Data))
164
165
176 #define UART_READ(uart) ((uart)->DAT)
177
178
190 #define UART_GET_TX_EMPTY(uart) ((uart)->FIFOSTS & UART_FIFOSTS_TXEMPTY_Msk)
191
192
204 #define UART_GET_RX_EMPTY(uart) ((uart)->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk)
205
206
219 #define UART_IS_TX_EMPTY(uart) (((uart)->FIFOSTS & UART_FIFOSTS_TXEMPTYF_Msk) >> UART_FIFOSTS_TXEMPTYF_Pos)
220
221
232 #define UART_WAIT_TX_EMPTY(uart) while(!((((uart)->FIFOSTS) & UART_FIFOSTS_TXEMPTYF_Msk) >> UART_FIFOSTS_TXEMPTYF_Pos))
233
234
246 #define UART_IS_RX_READY(uart) (((uart)->INTSTS & UART_INTSTS_RDAIF_Msk)>>UART_INTSTS_RDAIF_Pos)
247
248
260 #define UART_IS_TX_FULL(uart) (((uart)->FIFOSTS & UART_FIFOSTS_TXFULL_Msk)>>UART_FIFOSTS_TXFULL_Pos)
261
262
274 #define UART_IS_RX_FULL(uart) (((uart)->FIFOSTS & UART_FIFOSTS_RXFULL_Msk)>>UART_FIFOSTS_RXFULL_Pos)
275
276
288 #define UART_GET_TX_FULL(uart) ((uart)->FIFOSTS & UART_FIFOSTS_TXFULL_Msk)
289
290
302 #define UART_GET_RX_FULL(uart) ((uart)->FIFOSTS & UART_FIFOSTS_RXFULL_Msk)
303
304
325 #define UART_ENABLE_INT(uart, u32eIntSel) ((uart)->INTEN |= (u32eIntSel))
326
327
348 #define UART_DISABLE_INT(uart, u32eIntSel) ((uart)->INTEN &= ~ (u32eIntSel))
349
350
385 #define UART_GET_INT_FLAG(uart,u32eIntTypeFlag) (((uart)->INTSTS & (u32eIntTypeFlag))?1:0)
386
387
398 #define UART_RS485_CLEAR_ADDR_FLAG(uart) ((uart)->FIFOSTS = UART_FIFOSTS_ADDRDETF_Msk)
399
400
412 #define UART_RS485_GET_ADDR_FLAG(uart) (((uart)->FIFOSTS & UART_FIFOSTS_ADDRDETF_Msk) >> UART_FIFOSTS_ADDRDETF_Pos)
413
414 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
415 __STATIC_INLINE void UART_CLEAR_RTS(UART_T* uart);
416 __STATIC_INLINE void UART_SET_RTS(UART_T* uart);
417
418
428 __STATIC_INLINE void UART_CLEAR_RTS(UART_T* uart)
429 {
430 uart->MODEM |= UART_MODEM_RTSACTLV_Msk;
431 uart->MODEM &= ~UART_MODEM_RTS_Msk;
432 }
433
434
444 __STATIC_INLINE void UART_SET_RTS(UART_T* uart)
445 {
446 uart->MODEM |= UART_MODEM_RTSACTLV_Msk | UART_MODEM_RTS_Msk;
447 }
448
461 #define UART_PDMA_ENABLE(uart, u32FuncSel) ((uart)->INTEN |= (u32FuncSel))
462
474 #define UART_PDMA_DISABLE(uart, u32FuncSel) ((uart)->INTEN &= ~(u32FuncSel))
475
476
477 void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag);
478 void UART_Close(UART_T* uart);
479 void UART_DisableFlowCtrl(UART_T* uart);
480 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag);
481 void UART_EnableFlowCtrl(UART_T* uart);
482 void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag);
483 void UART_Open(UART_T* uart, uint32_t u32baudrate);
484 uint32_t UART_Read(UART_T* uart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes);
485 void UART_SetLineConfig(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits);
486 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC);
487 void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction);
488 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr);
489 void UART_SelectLINMode(UART_T* uart, uint32_t u32Mode, uint32_t u32BreakLength);
490 uint32_t UART_Write(UART_T* uart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes);
491
492
493
494 /* end of group UART_EXPORTED_FUNCTIONS */
496 /* end of group UART_Driver */
498 /* end of group Standard_Driver */
500
501 #ifdef __cplusplus
502 }
503 #endif
504
505 #endif /*__UART_H__*/
506
507 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
UART_CLEAR_RTS
__STATIC_INLINE void UART_CLEAR_RTS(UART_T *uart)
Set RTS pin to low.
Definition: uart.h:428
UART_EnableFlowCtrl
void UART_EnableFlowCtrl(UART_T *uart)
Enable UART auto flow control function.
Definition: uart.c:144
UART_DisableFlowCtrl
void UART_DisableFlowCtrl(UART_T *uart)
Disable UART auto flow control function.
Definition: uart.c:104
UART_MODEM_RTSACTLV_Msk
#define UART_MODEM_RTSACTLV_Msk
Definition: uart_reg.h:1946
UART_MODEM_RTS_Msk
#define UART_MODEM_RTS_Msk
Definition: uart_reg.h:1943
UART_DisableInt
void UART_DisableInt(UART_T *uart, uint32_t u32InterruptFlag)
Disable UART specified interrupt.
Definition: uart.c:128
UART_SelectIrDAMode
void UART_SelectIrDAMode(UART_T *uart, uint32_t u32Buadrate, uint32_t u32Direction)
Select and configure IrDA function.
Definition: uart.c:460
UART_SelectLINMode
void UART_SelectLINMode(UART_T *uart, uint32_t u32Mode, uint32_t u32BreakLength)
Select and configure LIN function.
Definition: uart.c:590
UART_SetLineConfig
void UART_SetLineConfig(UART_T *uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
Set UART line configuration.
Definition: uart.c:348
UART_SET_RTS
__STATIC_INLINE void UART_SET_RTS(UART_T *uart)
Set RTS pin to high.
Definition: uart.h:444
UART_SelectRS485Mode
void UART_SelectRS485Mode(UART_T *uart, uint32_t u32Mode, uint32_t u32Addr)
Select and configure RS485 function.
Definition: uart.c:566
UART_Close
void UART_Close(UART_T *uart)
Disable UART interrupt.
Definition: uart.c:89
UART_Write
uint32_t UART_Write(UART_T *uart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes)
Write UART data.
Definition: uart.c:612
UART_Open
void UART_Open(UART_T *uart, uint32_t u32baudrate)
Open and set UART function.
Definition: uart.c:192
UART_T
Definition: uart_reg.h:26
UART_T::MODEM
__IO uint32_t MODEM
Definition: uart_reg.h:1826
UART_EnableInt
void UART_EnableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to enable UART specified interrupt and enable NVIC UART IRQ.
Definition: uart.c:175
UART_ClearIntFlag
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
Clear UART specified interrupt flag.
Definition: uart.c:41
UART_Read
uint32_t UART_Read(UART_T *uart, uint8_t pu8RxBuf[], uint32_t u32ReadBytes)
Read UART data.
Definition: uart.c:285
UART_SetTimeoutCnt
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
Set Rx timeout count.
Definition: uart.c:437