libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
usart.h
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2013 Gareth McMullin <gareth@blacksphere.co.nz>
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 SAM_USART_H
21#define SAM_USART_H
22
25
26#define USART0 USART0_BASE
27#define USART1 USART1_BASE
28#define USART2 USART2_BASE
29#define USART3 USART3_BASE
30
31/* --- Universal Synchronous Asynchronous Receiver Transmitter (USART) */
32#define USART_CR(x) MMIO32((x) + 0x0000)
33#define USART_MR(x) MMIO32((x) + 0x0004)
34#define USART_IER(x) MMIO32((x) + 0x0008)
35#define USART_IDR(x) MMIO32((x) + 0x000C)
36#define USART_IMR(x) MMIO32((x) + 0x0010)
37#define USART_CSR(x) MMIO32((x) + 0x0014)
38#define USART_RHR(x) MMIO32((x) + 0x0018)
39#define USART_THR(x) MMIO32((x) + 0x001C)
40#define USART_BRGR(x) MMIO32((x) + 0x0020)
41#define USART_RTOR(x) MMIO32((x) + 0x0024)
42#define USART_TTGR(x) MMIO32((x) + 0x0028)
43/* 0x002C:0x003C - Reserved */
44#define USART_FIDI(x) MMIO32((x) + 0x0040)
45#define USART_NER(x) MMIO32((x) + 0x0044)
46/* 0x0048 - Reserved */
47#define USART_IF(x) MMIO32((x) + 0x004C)
48#define USART_MAN(x) MMIO32((x) + 0x0050)
49#define USART_LINMR(x) MMIO32((x) + 0x0054)
50#define USART_LINIR(x) MMIO32((x) + 0x0058)
51/* 0x005C:0x00E0 - Reserved */
52#define USART_WPMR(x) MMIO32((x) + 0x00E4)
53#define USART_WPSR(x) MMIO32((x) + 0x00E8)
54/* 0x00EC:0x00F8 - Reserved */
55#define USART_VERSION(x) MMIO32((x) + 0x00FC)
56/* 0x0100:0x0124 - PDC Area */
57
58
59/* USART Control Register (USART_CR) */
60/* Bits [31:22] - Reserved */
61#define USART_CR_LINWKUP (0x01 << 21)
62#define USART_CR_LINABT (0x01 << 20)
63#define USART_CR_RTSDIS (0x01 << 19)
64#define USART_CR_RCS (0x01 << 19)
65#define USART_CR_RTSEN (0x01 << 18)
66#define USART_CR_FCS (0x01 << 18)
67/* Bits [17:16] - Reserved */
68#define USART_CR_RETTO (0x01 << 15)
69#define USART_CR_RSTNACK (0x01 << 14)
70#define USART_CR_RSTIT (0x01 << 13)
71#define USART_CR_SENDA (0x01 << 12)
72#define USART_CR_STTTO (0x01 << 11)
73#define USART_CR_STPBRK (0x01 << 10)
74#define USART_CR_STTBRK (0x01 << 9)
75#define USART_CR_RSTSTA (0x01 << 8)
76#define USART_CR_TXDIS (0x01 << 7)
77#define USART_CR_TXEN (0x01 << 6)
78#define USART_CR_RXDIS (0x01 << 5)
79#define USART_CR_RXEN (0x01 << 4)
80#define USART_CR_RSTTX (0x01 << 3)
81#define USART_CR_RSTRX (0x01 << 2)
82/* Bits [1:0] - Reserved */
83
84/* USART Mode Register (USART_MR) */
85#define USART_MR_ONEBIT (0x01 << 31)
86#define USART_MR_MODSYNC (0x01 << 30)
87#define USART_MR_MAN (0x01 << 29)
88#define USART_MR_FILTER (0x01 << 28)
89/* Bit [27] - Reserved */
90#define USART_MR_MAX_ITERATION_MASK (0x07 << 24)
91#define USART_MR_INVDATA (0x01 << 23)
92#define USART_MR_VAR_SYNC (0x01 << 22)
93#define USART_MR_DSNACK (0x01 << 21)
94#define USART_MR_INACK (0x01 << 20)
95#define USART_MR_OVER (0x01 << 19)
96#define USART_MR_CLKO (0x01 << 18)
97#define USART_MR_MODE9 (0x01 << 17)
98#define USART_MR_MSBF (0x01 << 16)
99#define USART_MR_CPOL (0x01 << 16)
100#define USART_MR_CHMODE_MASK (0x03 << 14)
101#define USART_MR_CHMODE_NORMAL (0x00 << 14)
102#define USART_MR_CHMODE_AUTOMATIC (0x01 << 14)
103#define USART_MR_CHMODE_LOCAL_LOOPBACK (0x02 << 14)
104#define USART_MR_CHMODE_REMOTE_LOOPBACK (0x03 << 14)
105#define USART_MR_NBSTOP_MASK (0x03 << 12)
106#define USART_MR_NBSTOP_1_BIT (0x00 << 12)
107#define USART_MR_NBSTOP_1_5_BIT (0x01 << 12)
108#define USART_MR_NBSTOP_2_BIT (0x02 << 12)
109/* Bits [13:12] - Reserved */
110#define USART_MR_PAR_MASK (0x07 << 9)
111#define USART_MR_PAR_EVEN (0x00 << 9)
112#define USART_MR_PAR_ODD (0x01 << 9)
113#define USART_MR_PAR_SPACE (0x02 << 9)
114#define USART_MR_PAR_MARK (0x03 << 9)
115#define USART_MR_PAR_NO (0x04 << 9)
116/* Bits [8:0] - Reserved */
117#define USART_MR_SYNC (0x01 << 8)
118#define USART_MR_CPHA (0x01 << 8)
119#define USART_MR_CHRL_SHIFT (6)
120#define USART_MR_CHRL_MASK (0x03 << USART_MR_CHRL_SHIFT)
121#define USART_MR_CHRL_5BIT (0x00 << USART_MR_CHRL_SHIFT)
122#define USART_MR_CHRL_6BIT (0x01 << USART_MR_CHRL_SHIFT)
123#define USART_MR_CHRL_7BIT (0x02 << USART_MR_CHRL_SHIFT)
124#define USART_MR_CHRL_8BIT (0x03 << USART_MR_CHRL_SHIFT)
125#define USART_MR_USCLKS_SHIFT (4)
126#define USART_MR_USCLKS_MASK (0x03 << USART_MR_USCLKS_SHIFT)
127#define USART_MR_USCLKS_MCK (0x00 << 4)
128#define USART_MR_USCLKS_DIV (0x01 << 4)
129#define USART_MR_USCLKS_SCK (0x03 << 4)
130#define USART_MR_MODE_MASK (0x0F << 0)
131#define USART_MR_MODE_NORMAL (0x00 << 0)
132#define USART_MR_MODE_RS485 (0x01 << 0)
133#define USART_MR_MODE_HW_HANDSHAKING (0x02 << 0)
134#define USART_MR_MODE_ISO7816_T_0 (0x03 << 0)
135#define USART_MR_MODE_ISO7816_T_1 (0x04 << 0)
136#define USART_MR_MODE_IRDA (0x06 << 0)
137#define USART_MR_MODE_LIN_MASTER (0x0A << 0)
138#define USART_MR_MODE_LIN_SLAVE (0x0B << 0)
139#define USART_MR_MODE_SPI_MASTER (0x0E << 0)
140#define USART_MR_MODE_SPI_SLAVE (0x0F << 0)
141
142/* USART Status Register (USART_CSR) */
143/* Bits [31:30] - Reserved */
144#define USART_CSR_LINSNRE (0x01 << 29)
145#define USART_CSR_LINCE (0x01 << 28)
146#define USART_CSR_LINIPE (0x01 << 27)
147#define USART_CSR_LINSFE (0x01 << 26)
148#define USART_CSR_LINBE (0x01 << 25)
149#define USART_CSR_MANERR (0x01 << 24)
150#define USART_CSR_CTS (0x01 << 23)
151#define USART_CSR_LINBLS (0x01 << 23)
152/* Bits [22:20] - Reserved */
153#define USART_CSR_CTSIC (0x01 << 19)
154/* Bits [18:16] - Reserved */
155#define USART_CSR_LINTC (0x01 << 15)
156#define USART_CSR_LINID (0x01 << 14)
157#define USART_CSR_NACK (0x01 << 13)
158#define USART_CSR_LINBK (0x01 << 13)
159#define USART_CSR_RXBUFF (0x01 << 12)
160#define USART_CSR_TXBUFE (0x01 << 11)
161/* Bit [10] - Reserved */
162#define USART_CSR_TXEMPTY (0x01 << 9)
163/* Bit [8] - Reserved */
164#define USART_CSR_PARE (0x01 << 7)
165#define USART_CSR_FRAME (0x01 << 6)
166#define USART_CSR_OVRE (0x01 << 5)
167#define USART_CSR_ENDTX (0x01 << 4)
168#define USART_CSR_ENDRX (0x01 << 3)
169/* Bit [2] - Reserved */
170#define USART_CSR_TXRDY (0x01 << 1)
171#define USART_CSR_RXRDY (0x01 << 0)
172
173#define USART_WPMR_KEY (0x555341 << 8)
174#define USART_WPMR_WPEN (0x01 << 0)
175
180};
181
189};
190
196};
197
201};
202
207};
208
214};
215
217
218void usart_set_baudrate(uint32_t usart, uint32_t baud);
219void usart_set_databits(uint32_t usart, int bits);
220void usart_set_stopbits(uint32_t usart, enum usart_stopbits);
221void usart_set_parity(uint32_t usart, enum usart_parity);
222void usart_set_mode(uint32_t usart, enum usart_mode);
223void usart_set_character_length(uint32_t usart, enum usart_chrl chrl);
224void usart_set_flow_control(uint32_t usart, enum usart_flowcontrol);
225void usart_enable(uint32_t usart);
226void usart_disable(uint32_t usart);
227void usart_send(uint32_t usart, uint16_t data);
228uint16_t usart_recv(uint32_t usart);
229void usart_wait_send_ready(uint32_t usart);
230void usart_wait_recv_ready(uint32_t usart);
231void usart_send_blocking(uint32_t usart, uint16_t data);
232uint16_t usart_recv_blocking(uint32_t usart);
233void usart_enable_rx_interrupt(uint32_t usart);
234void usart_disable_rx_interrupt(uint32_t usart);
235void usart_wp_disable(uint32_t usart);
236void usart_wp_enable(uint32_t usart);
237void usart_select_clock(uint32_t usart, enum usart_clock clk);
238
240
241#endif
242
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
uint16_t usart_recv(uint32_t usart)
usart_mode
Definition: usart.h:191
@ USART_MODE_DISABLED
Definition: usart.h:192
@ USART_MODE_TX_RX
Definition: usart.h:195
@ USART_MODE_RX
Definition: usart.h:193
@ USART_MODE_TX
Definition: usart.h:194
void usart_send(uint32_t usart, uint16_t data)
usart_clock
Definition: usart.h:203
@ USART_CLOCK_CLK_USART
Definition: usart.h:204
@ USART_CLOCK_CLK
Definition: usart.h:206
@ USART_CLOCK_CLK_USART_DIV
Definition: usart.h:205
void usart_wait_recv_ready(uint32_t usart)
void usart_wp_disable(uint32_t usart)
void usart_set_character_length(uint32_t usart, enum usart_chrl chrl)
void usart_wait_send_ready(uint32_t usart)
void usart_disable_rx_interrupt(uint32_t usart)
void usart_disable(uint32_t usart)
void usart_set_databits(uint32_t usart, int bits)
void usart_wp_enable(uint32_t usart)
void usart_set_baudrate(uint32_t usart, uint32_t baud)
usart_parity
Definition: usart.h:182
@ USART_PARITY_SPACE
Definition: usart.h:185
@ USART_PARITY_ODD
Definition: usart.h:184
@ USART_PARITY_MULTIDROP
Definition: usart.h:188
@ USART_PARITY_MARK
Definition: usart.h:186
@ USART_PARITY_EVEN
Definition: usart.h:183
@ USART_PARITY_NONE
Definition: usart.h:187
uint16_t usart_recv_blocking(uint32_t usart)
void usart_enable_rx_interrupt(uint32_t usart)
void usart_set_parity(uint32_t usart, enum usart_parity)
void usart_enable(uint32_t usart)
void usart_set_flow_control(uint32_t usart, enum usart_flowcontrol)
usart_stopbits
Definition: usart.h:176
@ USART_STOPBITS_2
Definition: usart.h:179
@ USART_STOPBITS_1
Definition: usart.h:177
@ USART_STOPBITS_1_5
Definition: usart.h:178
void usart_select_clock(uint32_t usart, enum usart_clock clk)
void usart_set_mode(uint32_t usart, enum usart_mode)
void usart_set_stopbits(uint32_t usart, enum usart_stopbits)
void usart_send_blocking(uint32_t usart, uint16_t data)
usart_chrl
Definition: usart.h:209
@ USART_CHRL_7BIT
Definition: usart.h:212
@ USART_CHRL_6BIT
Definition: usart.h:211
@ USART_CHRL_5BIT
Definition: usart.h:210
@ USART_CHRL_8BIT
Definition: usart.h:213
usart_flowcontrol
Definition: usart.h:198
@ USART_FLOWCONTROL_RTS_CTS
Definition: usart.h:200
@ USART_FLOWCONTROL_NONE
Definition: usart.h:199