uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_uart.h
Go to the documentation of this file.
1 /***********************************************************************/
21 /* Peripheral group ----------------------------------------------------------- */
27 #ifndef __LPC17XX_UART_H
28 #define __LPC17XX_UART_H
29 
30 /* Includes ------------------------------------------------------------------- */
31 #include "LPC17xx.h"
32 #include "lpc_types.h"
33 
34 
35 #ifdef __cplusplus
36 extern "C"
37 {
38 #endif
39 
40 /* Public Macros -------------------------------------------------------------- */
48 #define UART_BLOCKING_TIMEOUT (0xFFFFFFFFUL)
49 
54 /* Private Macros ------------------------------------------------------------- */
59 /* Accepted Error baud rate value (in percent unit) */
60 #define UART_ACCEPTED_BAUDRATE_ERROR (3)
63 /* --------------------- BIT DEFINITIONS -------------------------------------- */
64 /*********************************************************************/
67 #define UART_RBR_MASKBIT ((uint8_t)0xFF)
69 /*********************************************************************/
72 #define UART_THR_MASKBIT ((uint8_t)0xFF)
74 /*********************************************************************/
77 #define UART_LOAD_DLL(div) ((div) & 0xFF)
78 #define UART_DLL_MASKBIT ((uint8_t)0xFF)
80 /*********************************************************************/
83 #define UART_DLM_MASKBIT ((uint8_t)0xFF)
84 #define UART_LOAD_DLM(div) (((div) >> 8) & 0xFF)
86 /*********************************************************************/
89 #define UART_IER_RBRINT_EN ((uint32_t)(1<<0))
90 #define UART_IER_THREINT_EN ((uint32_t)(1<<1))
91 #define UART_IER_RLSINT_EN ((uint32_t)(1<<2))
92 #define UART1_IER_MSINT_EN ((uint32_t)(1<<3))
93 #define UART1_IER_CTSINT_EN ((uint32_t)(1<<7))
94 #define UART_IER_ABEOINT_EN ((uint32_t)(1<<8))
95 #define UART_IER_ABTOINT_EN ((uint32_t)(1<<9))
96 #define UART_IER_BITMASK ((uint32_t)(0x307))
97 #define UART1_IER_BITMASK ((uint32_t)(0x38F))
99 /*********************************************************************/
102 #define UART_IIR_INTSTAT_PEND ((uint32_t)(1<<0))
103 #define UART_IIR_INTID_RLS ((uint32_t)(3<<1))
104 #define UART_IIR_INTID_RDA ((uint32_t)(2<<1))
105 #define UART_IIR_INTID_CTI ((uint32_t)(6<<1))
106 #define UART_IIR_INTID_THRE ((uint32_t)(1<<1))
107 #define UART1_IIR_INTID_MODEM ((uint32_t)(0<<1))
108 #define UART_IIR_INTID_MASK ((uint32_t)(7<<1))
109 #define UART_IIR_FIFO_EN ((uint32_t)(3<<6))
110 #define UART_IIR_ABEO_INT ((uint32_t)(1<<8))
111 #define UART_IIR_ABTO_INT ((uint32_t)(1<<9))
112 #define UART_IIR_BITMASK ((uint32_t)(0x3CF))
114 /*********************************************************************/
117 #define UART_FCR_FIFO_EN ((uint8_t)(1<<0))
118 #define UART_FCR_RX_RS ((uint8_t)(1<<1))
119 #define UART_FCR_TX_RS ((uint8_t)(1<<2))
120 #define UART_FCR_DMAMODE_SEL ((uint8_t)(1<<3))
121 #define UART_FCR_TRG_LEV0 ((uint8_t)(0))
122 #define UART_FCR_TRG_LEV1 ((uint8_t)(1<<6))
123 #define UART_FCR_TRG_LEV2 ((uint8_t)(2<<6))
124 #define UART_FCR_TRG_LEV3 ((uint8_t)(3<<6))
125 #define UART_FCR_BITMASK ((uint8_t)(0xCF))
126 #define UART_TX_FIFO_SIZE (16)
127 
128 /*********************************************************************/
131 #define UART_LCR_WLEN5 ((uint8_t)(0))
132 #define UART_LCR_WLEN6 ((uint8_t)(1<<0))
133 #define UART_LCR_WLEN7 ((uint8_t)(2<<0))
134 #define UART_LCR_WLEN8 ((uint8_t)(3<<0))
135 #define UART_LCR_STOPBIT_SEL ((uint8_t)(1<<2))
136 #define UART_LCR_PARITY_EN ((uint8_t)(1<<3))
137 #define UART_LCR_PARITY_ODD ((uint8_t)(0))
138 #define UART_LCR_PARITY_EVEN ((uint8_t)(1<<4))
139 #define UART_LCR_PARITY_F_1 ((uint8_t)(2<<4))
140 #define UART_LCR_PARITY_F_0 ((uint8_t)(3<<4))
141 #define UART_LCR_BREAK_EN ((uint8_t)(1<<6))
142 #define UART_LCR_DLAB_EN ((uint8_t)(1<<7))
143 #define UART_LCR_BITMASK ((uint8_t)(0xFF))
145 /*********************************************************************/
148 #define UART1_MCR_DTR_CTRL ((uint8_t)(1<<0))
149 #define UART1_MCR_RTS_CTRL ((uint8_t)(1<<1))
150 #define UART1_MCR_LOOPB_EN ((uint8_t)(1<<4))
151 #define UART1_MCR_AUTO_RTS_EN ((uint8_t)(1<<6))
152 #define UART1_MCR_AUTO_CTS_EN ((uint8_t)(1<<7))
153 #define UART1_MCR_BITMASK ((uint8_t)(0x0F3))
155 /*********************************************************************/
158 #define UART_LSR_RDR ((uint8_t)(1<<0))
159 #define UART_LSR_OE ((uint8_t)(1<<1))
160 #define UART_LSR_PE ((uint8_t)(1<<2))
161 #define UART_LSR_FE ((uint8_t)(1<<3))
162 #define UART_LSR_BI ((uint8_t)(1<<4))
163 #define UART_LSR_THRE ((uint8_t)(1<<5))
164 #define UART_LSR_TEMT ((uint8_t)(1<<6))
165 #define UART_LSR_RXFE ((uint8_t)(1<<7))
166 #define UART_LSR_BITMASK ((uint8_t)(0xFF))
168 /*********************************************************************/
171 #define UART1_MSR_DELTA_CTS ((uint8_t)(1<<0))
172 #define UART1_MSR_DELTA_DSR ((uint8_t)(1<<1))
173 #define UART1_MSR_LO2HI_RI ((uint8_t)(1<<2))
174 #define UART1_MSR_DELTA_DCD ((uint8_t)(1<<3))
175 #define UART1_MSR_CTS ((uint8_t)(1<<4))
176 #define UART1_MSR_DSR ((uint8_t)(1<<5))
177 #define UART1_MSR_RI ((uint8_t)(1<<6))
178 #define UART1_MSR_DCD ((uint8_t)(1<<7))
179 #define UART1_MSR_BITMASK ((uint8_t)(0xFF))
181 /*********************************************************************/
184 #define UART_SCR_BIMASK ((uint8_t)(0xFF))
186 /*********************************************************************/
189 #define UART_ACR_START ((uint32_t)(1<<0))
190 #define UART_ACR_MODE ((uint32_t)(1<<1))
191 #define UART_ACR_AUTO_RESTART ((uint32_t)(1<<2))
192 #define UART_ACR_ABEOINT_CLR ((uint32_t)(1<<8))
193 #define UART_ACR_ABTOINT_CLR ((uint32_t)(1<<9))
194 #define UART_ACR_BITMASK ((uint32_t)(0x307))
196 /*********************************************************************/
199 #define UART_ICR_IRDAEN ((uint32_t)(1<<0))
200 #define UART_ICR_IRDAINV ((uint32_t)(1<<1))
201 #define UART_ICR_FIXPULSE_EN ((uint32_t)(1<<2))
202 #define UART_ICR_PULSEDIV(n) ((uint32_t)((n&0x07)<<3))
203 #define UART_ICR_BITMASK ((uint32_t)(0x3F))
205 /*********************************************************************/
208 #define UART_FDR_DIVADDVAL(n) ((uint32_t)(n&0x0F))
209 #define UART_FDR_MULVAL(n) ((uint32_t)((n<<4)&0xF0))
210 #define UART_FDR_BITMASK ((uint32_t)(0xFF))
212 /*********************************************************************/
215 #define UART_TER_TXEN ((uint8_t)(1<<7))
216 #define UART_TER_BITMASK ((uint8_t)(0x80))
218 /*********************************************************************/
221 #define UART1_RS485CTRL_NMM_EN ((uint32_t)(1<<0))
223 #define UART1_RS485CTRL_RX_DIS ((uint32_t)(1<<1))
224 #define UART1_RS485CTRL_AADEN ((uint32_t)(1<<2))
225 #define UART1_RS485CTRL_SEL_DTR ((uint32_t)(1<<3))
227 #define UART1_RS485CTRL_DCTRL_EN ((uint32_t)(1<<4))
228 #define UART1_RS485CTRL_OINV_1 ((uint32_t)(1<<5))
231 #define UART1_RS485CTRL_BITMASK ((uint32_t)(0x3F))
233 /*********************************************************************/
236 #define UART1_RS485ADRMATCH_BITMASK ((uint8_t)(0xFF))
238 /*********************************************************************/
241 /* Macro defines for UART1 RS-485 Delay value register */
242 #define UART1_RS485DLY_BITMASK ((uint8_t)(0xFF))
244 /*********************************************************************/
247 #define UART_FIFOLVL_RXFIFOLVL(n) ((uint32_t)(n&0x0F))
248 #define UART_FIFOLVL_TXFIFOLVL(n) ((uint32_t)((n>>8)&0x0F))
249 #define UART_FIFOLVL_BITMASK ((uint32_t)(0x0F0F))
252 /* ---------------- CHECK PARAMETER DEFINITIONS ---------------------------- */
255 #define PARAM_UART_DATABIT(databit) ((databit==UART_DATABIT_5) || (databit==UART_DATABIT_6)\
256 || (databit==UART_DATABIT_7) || (databit==UART_DATABIT_8))
257 
259 #define PARAM_UART_STOPBIT(stopbit) ((stopbit==UART_STOPBIT_1) || (stopbit==UART_STOPBIT_2))
260 
262 #define PARAM_UART_PARITY(parity) ((parity==UART_PARITY_NONE) || (parity==UART_PARITY_ODD) \
263 || (parity==UART_PARITY_EVEN) || (parity==UART_PARITY_SP_1) \
264 || (parity==UART_PARITY_SP_0))
265 
267 #define PARAM_UART_FIFO_LEVEL(fifo) ((fifo==UART_FIFO_TRGLEV0) \
268 || (fifo==UART_FIFO_TRGLEV1) || (fifo==UART_FIFO_TRGLEV2) \
269 || (fifo==UART_FIFO_TRGLEV3))
270 
272 #define PARAM_UART_INTCFG(IntCfg) ((IntCfg==UART_INTCFG_RBR) || (IntCfg==UART_INTCFG_THRE) \
273 || (IntCfg==UART_INTCFG_RLS) || (IntCfg==UART_INTCFG_ABEO) \
274 || (IntCfg==UART_INTCFG_ABTO))
275 
277 #define PARAM_UART1_INTCFG(IntCfg) ((IntCfg==UART1_INTCFG_MS) || (IntCfg==UART1_INTCFG_CTS))
278 
280 #define PARAM_UART_AUTOBAUD_MODE(ABmode) ((ABmode==UART_AUTOBAUD_MODE0) || (ABmode==UART_AUTOBAUD_MODE1))
283 #define PARAM_UART_AUTOBAUD_INTSTAT(ABIntStat) ((ABIntStat==UART_AUTOBAUD_INTSTAT_ABEO) || \
284  (ABIntStat==UART_AUTOBAUD_INTSTAT_ABTO))
285 
287 #define PARAM_UART_IrDA_PULSEDIV(PulseDiv) ((PulseDiv==UART_IrDA_PULSEDIV2) || (PulseDiv==UART_IrDA_PULSEDIV4) \
288 || (PulseDiv==UART_IrDA_PULSEDIV8) || (PulseDiv==UART_IrDA_PULSEDIV16) \
289 || (PulseDiv==UART_IrDA_PULSEDIV32) || (PulseDiv==UART_IrDA_PULSEDIV64) \
290 || (PulseDiv==UART_IrDA_PULSEDIV128) || (PulseDiv==UART_IrDA_PULSEDIV256))
292 /* Macro to check the input UART1_SignalState parameters */
293 #define PARAM_UART1_SIGNALSTATE(x) ((x==INACTIVE) || (x==ACTIVE))
294 
296 #define PARAM_UART1_MODEM_PIN(x) ((x==UART1_MODEM_PIN_DTR) || (x==UART1_MODEM_PIN_RTS))
299 #define PARAM_UART1_MODEM_MODE(x) ((x==UART1_MODEM_MODE_LOOPBACK) || (x==UART1_MODEM_MODE_AUTO_RTS) \
300 || (x==UART1_MODEM_MODE_AUTO_CTS))
301 
303 #define PARAM_UART_RS485_DIRCTRL_PIN(x) ((x==UART1_RS485_DIRCTRL_RTS) || (x==UART1_RS485_DIRCTRL_DTR))
304 
305 /* Macro to determine if it is valid UART port number */
306 #define PARAM_UARTx(x) ((((uint32_t *)x)==((uint32_t *)LPC_UART0)) \
307 || (((uint32_t *)x)==((uint32_t *)LPC_UART1)) \
308 || (((uint32_t *)x)==((uint32_t *)LPC_UART2)) \
309 || (((uint32_t *)x)==((uint32_t *)LPC_UART3)))
310 #define PARAM_UART_IrDA(x) (((uint32_t *)x)==((uint32_t *)LPC_UART3))
311 #define PARAM_UART1_MODEM(x) (((uint32_t *)x)==((uint32_t *)LPC_UART1))
312 
314 #define PARAM_UART1_RS485_CFG_MATCHADDRVALUE(x) ((x<0xFF))
317 #define PARAM_UART1_RS485_CFG_DELAYVALUE(x) ((x<0xFF))
324 /* Public Types --------------------------------------------------------------- */
332 typedef enum {
333  UART_DATABIT_5 = 0,
342 typedef enum {
343  UART_STOPBIT_1 = (0),
350 typedef enum {
351  UART_PARITY_NONE = 0,
361 typedef enum {
362  UART_FIFO_TRGLEV0 = 0,
368 /********************************************************************/
371 typedef enum {
372  UART_INTCFG_RBR = 0,
384 typedef enum {
398 typedef enum {
399  UART_AUTOBAUD_MODE0 = 0,
406 typedef struct {
407  UART_AB_MODE_Type ABMode;
408  FunctionalState AutoRestart;
414 typedef enum {
422 typedef enum {
423  UART_IrDA_PULSEDIV2 = 0,
440 
441 /********************************************************************/
444 typedef enum {
445  INACTIVE = 0, /* In-active state */
446  ACTIVE = !INACTIVE /* Active state */
452 typedef enum {
466 typedef enum {
467  UART1_MODEM_PIN_DTR = 0,
474 typedef enum {
483 typedef enum {
488 /********************************************************************/
491 typedef struct {
492  uint32_t Baud_rate;
493  UART_PARITY_Type Parity;
500  UART_DATABIT_Type Databits;
506  UART_STOPBIT_Type Stopbits;
511 
512 /********************************************************************/
516 typedef struct {
517  FunctionalState FIFO_ResetRxBuf;
521  FunctionalState FIFO_ResetTxBuf;
525  FunctionalState FIFO_DMAMode;
529  UART_FITO_LEVEL_Type FIFO_Level;
536 
537 /********************************************************************/
540 typedef struct {
541  FunctionalState NormalMultiDropMode_State;
544  FunctionalState Rx_State;
547  FunctionalState AutoAddrDetect_State;
550  FunctionalState AutoDirCtrl_State;
553  UART_RS485_DIRCTRL_PIN_Type DirCtrlPin;
558  SetState DirCtrlPol_Level;
564  uint8_t MatchAddrValue;
565  uint8_t DelayValue;
567 
573 /* Public Functions ----------------------------------------------------------- */
577 /* UART Init/DeInit functions --------------------------------------------------*/
578 void UART_Init(LPC_UART_TypeDef *UARTx, UART_CFG_Type *UART_ConfigStruct);
579 void UART_DeInit(LPC_UART_TypeDef* UARTx);
580 void UART_ConfigStructInit(UART_CFG_Type *UART_InitStruct);
581 
582 /* UART Send/Receive functions -------------------------------------------------*/
583 void UART_SendByte(LPC_UART_TypeDef* UARTx, uint8_t Data);
584 uint8_t UART_ReceiveByte(LPC_UART_TypeDef* UARTx);
585 uint32_t UART_Send(LPC_UART_TypeDef *UARTx, uint8_t *txbuf,
586  uint32_t buflen, TRANSFER_BLOCK_Type flag);
587 uint32_t UART_Receive(LPC_UART_TypeDef *UARTx, uint8_t *rxbuf, \
588  uint32_t buflen, TRANSFER_BLOCK_Type flag);
589 
590 /* UART FIFO functions ----------------------------------------------------------*/
592 void UART_FIFOConfigStructInit(UART_FIFO_CFG_Type *UART_FIFOInitStruct);
593 
594 /* UART get information functions -----------------------------------------------*/
595 uint32_t UART_GetIntId(LPC_UART_TypeDef* UARTx);
596 uint8_t UART_GetLineStatus(LPC_UART_TypeDef* UARTx);
597 
598 /* UART operate functions -------------------------------------------------------*/
599 void UART_IntConfig(LPC_UART_TypeDef *UARTx, UART_INT_Type UARTIntCfg, \
600  FunctionalState NewState);
601 void UART_TxCmd(LPC_UART_TypeDef *UARTx, FunctionalState NewState);
603 void UART_ForceBreak(LPC_UART_TypeDef* UARTx);
604 
605 /* UART Auto-baud functions -----------------------------------------------------*/
607 void UART_ABCmd(LPC_UART_TypeDef *UARTx, UART_AB_CFG_Type *ABConfigStruct, \
608  FunctionalState NewState);
609 
610 /* UART1 FullModem functions ----------------------------------------------------*/
612  UART1_SignalState NewState);
614  FunctionalState NewState);
616 
617 /* UART RS485 functions ----------------------------------------------------------*/
618 void UART_RS485Config(LPC_UART1_TypeDef *UARTx, \
619  UART1_RS485_CTRLCFG_Type *RS485ConfigStruct);
621 void UART_RS485SendSlvAddr(LPC_UART1_TypeDef *UARTx, uint8_t SlvAddr);
622 uint32_t UART_RS485SendData(LPC_UART1_TypeDef *UARTx, uint8_t *pData, uint32_t size);
623 
624 /* UART IrDA functions-------------------------------------------------------------*/
626 void UART_IrDACmd(LPC_UART_TypeDef* UARTx, FunctionalState NewState);
633 #ifdef __cplusplus
634 }
635 #endif
636 
637 
638 #endif /* __LPC17XX_UART_H */
639 
644 /* --------------------------------- End Of File ------------------------------ */