libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
uart.h
Go to the documentation of this file.
1/*
2* This file is part of the libopencm3 project.
3*
4* Copyright (C) 2012 Benjamin Vernoux <titanmkd@gmail.com>
5*
6* This library is free software: you can redistribute it and/or modify
7* it under the terms of the GNU Lesser General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* This library is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU Lesser General Public License for more details.
15*
16* You should have received a copy of the GNU Lesser General Public License
17* along with this library. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef LPC43XX_UART_H
21#define LPC43XX_UART_H
22
25
26/* --- Convenience macros -------------------------------------------------- */
27
28/* UART port base addresses (for convenience) */
29#define UART0 USART0_BASE /* APB0 */
30#define UART1 UART1_BASE /* APB0 */
31#define UART2 USART2_BASE /* APB2 */
32#define UART3 USART3_BASE /* APB2 */
33
34/* --- UART registers ------------------------------------------------------- */
35
36/* Receiver Buffer Register (DLAB=0) Read Only */
37#define UART_RBR(port) MMIO32((port) + 0x000) /* 8bits */
38
39/* Transmitter Holding Register (DLAB=0) Write Only */
40#define UART_THR(port) MMIO32((port) + 0x000) /* 8bits */
41
42/* Divisor Latch LSB Register (DLAB=1) */
43#define UART_DLL(port) MMIO32((port) + 0x000) /* 8bits */
44
45/* Divisor Latch MSB Register (DLAB=1) */
46#define UART_DLM(port) MMIO32((port) + 0x004) /* 8bits */
47
48/* Interrupt Enable Register (DLAB=0) */
49#define UART_IER(port) MMIO32((port) + 0x004)
50
51/* Interrupt ID Register Read Only */
52#define UART_IIR(port) MMIO32((port) + 0x008)
53
54/* FIFO Control Register Write Only */
55#define UART_FCR(port) MMIO32((port) + 0x008)
56
57/* Line Control Register */
58#define UART_LCR(port) MMIO32((port) + 0x00C)
59
60/* MCR only for UART1 */
61
62/* Line Status Register */
63#define UART_LSR(port) MMIO32((port) + 0x014)
64
65/* Auto Baud Control Register */
66#define UART_ACR(port) MMIO32((port) + 0x020)
67
68/* IrDA Control Register only for UART0/2/3 */
69#define UART_ICR(port) MMIO32((port) + 0x024)
70
71/* Fractional Divider Register */
72#define UART_FDR(port) MMIO32((port) + 0x028)
73
74/* Oversampling Register only for UART0/2/3 */
75#define UART_OSR(port) MMIO32((port) + 0x02C)
76
77/* Half-Duplex enable Register only for UART0/2/3 */
78#define UART_HDEN(port) MMIO32((port) + 0x040)
79
80/* Smart card Interface Register Only for UART0/2/3 */
81#define UART_SCICTRL(port) MMIO32((port) + 0x048)
82
83/* RS-485/EIA-485 Control Register */
84#define UART_RS485CTRL(port) MMIO32((port) + 0x04C)
85
86/* RS-485/EIA-485 Address Match Register */
87#define UART_RS485ADRMATCH(port) MMIO32((port) + 0x050)
88
89/* RS-485/EIA-485 Direction Control Delay Register */
90#define UART_RS485DLY(port) MMIO32((port) + 0x054)
91
92/* Synchronous Mode Control Register only for UART0/2/3 */
93#define UART_SYNCCTRL(port) MMIO32((port) + 0x058)
94
95/* Transmit Enable Register */
96#define UART_TER(port) MMIO32((port) + 0x05C)
97
98/* --------------------- BIT DEFINITIONS ----------------------------------- */
99/***********************************************************************
100* Macro defines for Macro defines for UARTn Receiver Buffer Register
101**********************************************************************/
102/* UART Received Buffer mask bit (8 bits) */
103#define UART_RBR_MASKBIT ((uint8_t)0xFF)
104
105/***********************************************************************
106* Macro defines for Macro defines for UARTn Transmit Holding Register
107**********************************************************************/
108/* UART Transmit Holding mask bit (8 bits) */
109#define UART_THR_MASKBIT ((uint8_t)0xFF)
110
111/***********************************************************************
112* Macro defines for Macro defines for UARTn Divisor Latch LSB register
113**********************************************************************/
114/* Macro for loading least significant halfs of divisors */
115#define UART_LOAD_DLL(div) ((div) & 0xFF)
116
117/* Divisor latch LSB bit mask */
118#define UART_DLL_MASKBIT ((uint8_t)0xFF)
119
120/***********************************************************************
121* Macro defines for Macro defines for UARTn Divisor Latch MSB register
122**********************************************************************/
123/* Divisor latch MSB bit mask */
124#define UART_DLM_MASKBIT ((uint8_t)0xFF)
125
126/* Macro for loading most significant halfs of divisors */
127#define UART_LOAD_DLM(div) (((div) >> 8) & 0xFF)
128
129/***********************************************************************
130* Macro defines for Macro defines for UART interrupt enable register
131**********************************************************************/
132/* RBR Interrupt enable*/
133#define UART_IER_RBRINT_EN (1 << 0)
134/* THR Interrupt enable*/
135#define UART_IER_THREINT_EN (1 << 1)
136/* RX line status interrupt enable*/
137#define UART_IER_RLSINT_EN (1 << 2)
138/* Modem status interrupt enable */
139#define UART1_IER_MSINT_EN (1 << 3)
140/* CTS1 signal transition interrupt enable */
141#define UART1_IER_CTSINT_EN (1 << 7)
142/* Enables the end of auto-baud interrupt */
143#define UART_IER_ABEOINT_EN (1 << 8)
144/* Enables the auto-baud time-out interrupt */
145#define UART_IER_ABTOINT_EN (1 << 9)
146/* UART interrupt enable register bit mask */
147#define UART_IER_BITMASK ((uint32_t)(0x307))
148/* UART1 interrupt enable register bit mask */
149#define UART1_IER_BITMASK ((uint32_t)(0x38F))
150
151/**********************************************************************
152* Macro defines for Macro defines for UART interrupt identification register
153**********************************************************************/
154
155/* Interrupt Status - Active low */
156#define UART_IIR_INTSTAT_PEND (1 << 0)
157/* Interrupt identification: Modem interrupt*/
158#define UART1_IIR_INTID_MODEM (0 << 1)
159/* Interrupt identification: THRE interrupt*/
160#define UART_IIR_INTID_THRE (1 << 1)
161/* Interrupt identification: Receive data available*/
162#define UART_IIR_INTID_RDA (2 << 1)
163/* Interrupt identification: Receive line status*/
164#define UART_IIR_INTID_RLS (3 << 1)
165/* Interrupt identification: Character time-out indicator*/
166#define UART_IIR_INTID_CTI (6 << 1)
167/* Interrupt identification: Interrupt ID mask */
168#define UART_IIR_INTID_MASK (7 << 1)
169/* These bits are equivalent to UnFCR[0] */
170#define UART_IIR_FIFO_EN (3 << 6)
171/* End of auto-baud interrupt */
172#define UART_IIR_ABEO_INT (1 << 8)
173/* Auto-baud time-out interrupt */
174#define UART_IIR_ABTO_INT (1 << 9)
175/* UART interrupt identification register bit mask */
176#define UART_IIR_BITMASK ((uint32_t)(0x3CF))
177
178/**********************************************************************
179* Macro defines for Macro defines for UART FIFO control register
180**********************************************************************/
181/* UART FIFO enable */
182#define UART_FCR_FIFO_EN (1 << 0)
183/* UART FIFO RX reset */
184#define UART_FCR_RX_RS (1 << 1)
185/* UART FIFO TX reset */
186#define UART_FCR_TX_RS (1 << 2)
187/* UART DMA mode selection */
188#define UART_FCR_DMAMODE_SEL (1 << 3)
189/* UART FIFO trigger level 0: 1 character */
190#define UART_FCR_TRG_LEV0 (0 << 6)
191/* UART FIFO trigger level 1: 4 character */
192#define UART_FCR_TRG_LEV1 (1 << 6)
193/* UART FIFO trigger level 2: 8 character */
194#define UART_FCR_TRG_LEV2 (2 << 6)
195/* UART FIFO trigger level 3: 14 character */
196#define UART_FCR_TRG_LEV3 (3 << 6)
197/* UART FIFO control bit mask */
198#define UART_FCR_BITMASK ((uint8_t)(0xCF))
199#define UART_TX_FIFO_SIZE (16)
200
201/**********************************************************************
202* Macro defines for Macro defines for UART line control register
203**********************************************************************/
204/* UART 5 bit data mode */
205#define UART_LCR_WLEN5 (0 << 0)
206/* UART 6 bit data mode */
207#define UART_LCR_WLEN6 (1 << 0)
208/* UART 7 bit data mode */
209#define UART_LCR_WLEN7 (2 << 0)
210/* UART 8 bit data mode */
211#define UART_LCR_WLEN8 (3 << 0)
212/* UART One Stop Bits */
213#define UART_LCR_ONE_STOPBIT (0 << 2)
214/* UART Two Stop Bits */
215#define UART_LCR_TWO_STOPBIT (1 << 2)
216
217/* UART Parity Disabled / No Parity */
218#define UART_LCR_NO_PARITY (0 << 3)
219/* UART Parity Enable */
220#define UART_LCR_PARITY_EN (1 << 3)
221/* UART Odd Parity Select */
222#define UART_LCR_PARITY_ODD (0 << 4)
223/* UART Even Parity Select */
224#define UART_LCR_PARITY_EVEN (1 << 4)
225/* UART force 1 stick parity */
226#define UART_LCR_PARITY_SP_1 (1 << 5)
227/* UART force 0 stick parity */
228#define UART_LCR_PARITY_SP_0 ((1 << 5) | (1 << 4))
229/* UART Transmission Break enable */
230#define UART_LCR_BREAK_EN (1 << 6)
231/* UART Divisor Latches Access bit enable */
232#define UART_LCR_DLAB_EN (1 << 7)
233/* UART line control bit mask */
234#define UART_LCR_BITMASK ((uint8_t)(0xFF))
235
236/**********************************************************************
237* Macro defines for Macro defines for UART line status register
238**********************************************************************/
239/* Line status register: Receive data ready */
240#define UART_LSR_RDR (1 << 0)
241/* Line status register: Overrun error */
242#define UART_LSR_OE (1 << 1)
243/* Line status register: Parity error */
244#define UART_LSR_PE (1 << 2)
245/* Line status register: Framing error */
246#define UART_LSR_FE (1 << 3)
247/* Line status register: Break interrupt */
248#define UART_LSR_BI (1 << 4)
249/* Line status register: Transmit holding register empty */
250#define UART_LSR_THRE (1 << 5)
251/* Line status register: Transmitter empty */
252#define UART_LSR_TEMT (1 << 6)
253/* Error in RX FIFO */
254#define UART_LSR_RXFE (1 << 7)
255/* UART Line status bit mask */
256#define UART_LSR_BITMASK ((uint8_t)(0xFF))
257#define UART_LSR_ERROR_MASK \
258 (UART_LSR_OE | UART_LSR_PE | UART_LSR_FE | UART_LSR_BI | UART_LSR_RXFE)
259
260/**********************************************************************
261* Macro defines for Macro defines for UART Scratch Pad Register
262**********************************************************************/
263
264/* UART Scratch Pad bit mask */
265#define UART_SCR_BIMASK ((uint8_t)(0xFF))
266
267/***********************************************************************
268* Macro defines for Macro defines for UART Auto baudrate control register
269**********************************************************************/
270
271/* UART Auto-baud start */
272#define UART_ACR_START (1 << 0)
273/* UART Auto baudrate Mode 1 */
274#define UART_ACR_MODE (1 << 1)
275/* UART Auto baudrate restart */
276#define UART_ACR_AUTO_RESTART (1 << 2)
277/* UART End of auto-baud interrupt clear */
278#define UART_ACR_ABEOINT_CLR (1 << 8)
279/* UART Auto-baud time-out interrupt clear */
280#define UART_ACR_ABTOINT_CLR (1 << 9)
281/* UART Auto Baudrate register bit mask */
282#define UART_ACR_BITMASK ((uint32_t)(0x307))
283
284/*********************************************************************
285* Macro defines for Macro defines for UART IrDA control register
286**********************************************************************/
287/* IrDA mode enable */
288#define UART_ICR_IRDAEN (1 << 0)
289/* IrDA serial input inverted */
290#define UART_ICR_IRDAINV (1 << 1)
291/* IrDA fixed pulse width mode */
292#define UART_ICR_FIXPULSE_EN (1 << 2)
293/* PulseDiv - Configures the pulse when FixPulseEn = 1 */
294#define UART_ICR_PULSEDIV(n) ((uint32_t)(((n)&0x07)<<3))
295/* UART IRDA bit mask */
296#define UART_ICR_BITMASK ((uint32_t)(0x3F))
297
298/**********************************************************************
299* Macro defines for Macro defines for UART half duplex register
300**********************************************************************/
301/* enable half-duplex mode*/
302#define UART_HDEN_HDEN (1 << 0)
303
304/**********************************************************************
305* Macro defines for Macro defines for UART smart card interface control register
306**********************************************************************/
307/* enable asynchronous half-duplex smart card interface*/
308#define UART_SCICTRL_SCIEN (1 << 0)
309/* NACK response is inhibited*/
310#define UART_SCICTRL_NACKDIS (1 << 1)
311/* ISO7816-3 protocol T1 is selected*/
312#define UART_SCICTRL_PROTSEL_T1 (1 << 2)
313/* number of retransmission*/
314#define UART_SCICTRL_TXRETRY(n) ((uint32_t)(((n)&0x07)<<5))
315/* Extra guard time*/
316#define UART_SCICTRL_GUARDTIME(n) ((uint32_t)(((n)&0xFF)<<8))
317
318/*********************************************************************
319* Macro defines for Macro defines for UART synchronous control register
320**********************************************************************/
321/* enable synchronous mode*/
322#define UART_SYNCCTRL_SYNC (1 << 0)
323/* synchronous master mode*/
324#define UART_SYNCCTRL_CSRC_MASTER (1 << 1)
325/* sample on falling edge*/
326#define UART_SYNCCTRL_FES (1 << 2)
327/* to be defined*/
328#define UART_SYNCCTRL_TSBYPASS (1 << 3)
329/* continuous running clock enable (master mode only) */
330#define UART_SYNCCTRL_CSCEN (1 << 4)
331/* Do not send start/stop bit */
332#define UART_SYNCCTRL_NOSTARTSTOP (1 << 5)
333/* stop continuous clock */
334#define UART_SYNCCTRL_CCCLR (1 << 6)
335
336/*********************************************************************
337* Macro defines for Macro defines for UART Fractional divider register
338**********************************************************************/
339
340/* Baud-rate generation pre-scaler divisor */
341#define UART_FDR_DIVADDVAL(n) ((uint32_t)((n)&0x0F))
342/* Baud-rate pre-scaler multiplier value */
343#define UART_FDR_MULVAL(n) ((uint32_t)(((n)<<4)&0xF0))
344/* UART Fractional Divider register bit mask */
345#define UART_FDR_BITMASK ((uint32_t)(0xFF))
346
347/*********************************************************************
348* Macro defines for Macro defines for UART Tx Enable register
349**********************************************************************/
350
351#define UART_TER_TXEN (1 << 0) /* Transmit enable bit */
352
353/**********************************************************************
354* Macro defines for Macro defines for UART FIFO Level register
355**********************************************************************/
356/* Reflects the current level of the UART receiver FIFO */
357#define UART_FIFOLVL_RX(n) ((uint32_t)((n)&0x0F))
358/* Reflects the current level of the UART transmitter FIFO */
359#define UART_FIFOLVL_TX(n) ((uint32_t)(((n)>>8)&0x0F))
360/* UART FIFO Level Register bit mask */
361#define UART_FIFOLVL_BITMASK ((uint32_t)(0x0F0F))
362
363/*********************************************************************
364* UART enum
365**********************************************************************/
366
367/*
368* UART Databit type definitions
369*/
370typedef enum {
371 UART_DATABIT_5 = UART_LCR_WLEN5,/* UART 5 bit data mode */
372 UART_DATABIT_6 = UART_LCR_WLEN6,/* UART 6 bit data mode */
373 UART_DATABIT_7 = UART_LCR_WLEN7,/* UART 7 bit data mode */
374 UART_DATABIT_8 = UART_LCR_WLEN8/* UART 8 bit data mode */
376
377/*
378* UART Stop bit type definitions
379*/
380typedef enum {
381 /* UART 1 Stop Bits Select */
383 /* UART 2 Stop Bits Select */
386
387/*
388* UART Parity type definitions
389*/
390typedef enum {
391 /* No parity */
393 /* Odd parity */
395 /* Even parity */
397 /* Forced 1 stick parity */
399 /* Forced 0 stick parity */
402
403typedef enum {
409
410typedef enum {
414
415typedef enum {
420
421/* function prototypes */
422
424
425/* Init UART and set PLL1 as clock source (PCLK) */
426void uart_init(uart_num_t uart_num, uart_databit_t data_nb_bits,
427 uart_stopbit_t data_nb_stop, uart_parity_t data_parity,
428 uint16_t uart_divisor, uint8_t uart_divaddval, uint8_t uart_mulval);
429
431uint8_t uart_read(uart_num_t uart_num);
432uint8_t uart_read_timeout(uart_num_t uart_num, uint32_t rx_timeout_nb_cycles,
433 uart_error_t *error);
434void uart_write(uart_num_t uart_num, uint8_t data);
435
437
438#endif
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
uart_rx_data_ready_t uart_rx_data_ready(uart_num_t uart_num)
#define UART0
Definition: uart.h:29
uart_databit_t
Definition: uart.h:370
@ UART_DATABIT_6
Definition: uart.h:372
@ UART_DATABIT_8
Definition: uart.h:374
@ UART_DATABIT_7
Definition: uart.h:373
@ UART_DATABIT_5
Definition: uart.h:371
#define UART_LCR_TWO_STOPBIT
Definition: uart.h:215
#define UART_LCR_WLEN5
Definition: uart.h:205
#define UART_LCR_PARITY_EVEN
Definition: uart.h:224
#define UART_LCR_PARITY_EN
Definition: uart.h:220
#define UART_LCR_PARITY_ODD
Definition: uart.h:222
uart_parity_t
Definition: uart.h:390
@ UART_PARITY_SP_0
Definition: uart.h:400
@ UART_PARITY_NONE
Definition: uart.h:392
@ UART_PARITY_EVEN
Definition: uart.h:396
@ UART_PARITY_ODD
Definition: uart.h:394
@ UART_PARITY_SP_1
Definition: uart.h:398
#define UART_LCR_WLEN8
Definition: uart.h:211
#define UART_LCR_NO_PARITY
Definition: uart.h:218
uart_error_t
Definition: uart.h:410
@ UART_TIMEOUT_ERROR
Definition: uart.h:412
@ UART_NO_ERROR
Definition: uart.h:411
#define UART_LCR_WLEN7
Definition: uart.h:209
uart_rx_data_ready_t
Definition: uart.h:415
@ UART_RX_DATA_ERROR
Definition: uart.h:418
@ UART_RX_DATA_READY
Definition: uart.h:417
@ UART_RX_NO_DATA
Definition: uart.h:416
#define UART2
Definition: uart.h:31
#define UART1
Definition: uart.h:30
#define UART_LCR_WLEN6
Definition: uart.h:207
#define UART3
Definition: uart.h:32
uint8_t uart_read(uart_num_t uart_num)
uint8_t uart_read_timeout(uart_num_t uart_num, uint32_t rx_timeout_nb_cycles, uart_error_t *error)
#define UART_LCR_PARITY_SP_0
Definition: uart.h:228
uart_stopbit_t
Definition: uart.h:380
@ UART_STOPBIT_1
Definition: uart.h:382
@ UART_STOPBIT_2
Definition: uart.h:384
uart_num_t
Definition: uart.h:403
@ UART0_NUM
Definition: uart.h:404
@ UART1_NUM
Definition: uart.h:405
@ UART2_NUM
Definition: uart.h:406
@ UART3_NUM
Definition: uart.h:407
void uart_write(uart_num_t uart_num, uint8_t data)
#define UART_LCR_ONE_STOPBIT
Definition: uart.h:213
#define UART_LCR_PARITY_SP_1
Definition: uart.h:226
void uart_init(uart_num_t uart_num, uart_databit_t data_nb_bits, uart_stopbit_t data_nb_stop, uart_parity_t data_parity, uint16_t uart_divisor, uint8_t uart_divaddval, uint8_t uart_mulval)