libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
usart_common_all.c
Go to the documentation of this file.
1/** @addtogroup usart_file USART peripheral API
2@ingroup peripheral_apis
3
4@author @htmlonly &copy; @endhtmlonly 2009 Uwe Hermann <uwe@hermann-uwe.de>
5
6This library supports the USART/UART in the STM32F series
7of ARM Cortex Microcontrollers by ST Microelectronics.
8
9Devices can have up to 3 USARTs and 2 UARTs.
10
11*/
12
13/*
14 * This file is part of the libopencm3 project.
15 *
16 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
17 *
18 * This library is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with this library. If not, see <http://www.gnu.org/licenses/>.
30 */
31
32/**@{*/
33
36
37/*---------------------------------------------------------------------------*/
38/** @brief USART Set Baudrate.
39
40The baud rate is computed from the APB high-speed prescaler clock (for
41USART1/6) or the APB low-speed prescaler clock (for other USARTs). These values
42must be correctly set before calling this function (refer to the
43rcc_clock_setup-* functions in RCC).
44
45Note: For LPUART, baudrates over 2**24 (~16.7 Mbaud) may overflow
46the calculation and are therefore not supported by this function.
47
48@param[in] usart unsigned 32 bit. USART block register address base @ref
49usart_reg_base
50@param[in] baud unsigned 32 bit. Baud rate specified in Hz.
51*/
52
53void usart_set_baudrate(uint32_t usart, uint32_t baud)
54{
55 uint32_t clock = rcc_get_usart_clk_freq(usart);
56
57 /*
58 * Yes it is as simple as that. The reference manual is
59 * talking about fractional calculation but it seems to be only
60 * marketing babble to sound awesome. It is nothing else but a
61 * simple divider to generate the correct baudrate.
62 *
63 * Note: We round() the value rather than floor()ing it, for more
64 * accurate divisor selection.
65 */
66#ifdef LPUART1
67 if (usart == LPUART1) {
68 USART_BRR(usart) = (clock / baud) * 256
69 + ((clock % baud) * 256 + baud / 2) / baud;
70 return;
71 }
72#endif
73
74 USART_BRR(usart) = (clock + baud / 2) / baud;
75}
76
77/*---------------------------------------------------------------------------*/
78/** @brief USART Set Word Length.
79
80The word length is set to 8 or 9 bits. Note that the last bit will be a parity
81bit if parity is enabled, in which case the data length will be 7 or 8 bits
82respectively.
83
84@param[in] usart unsigned 32 bit. USART block register address base @ref
85usart_reg_base
86@param[in] bits unsigned 32 bit. Word length in bits 8 or 9.
87*/
88
89void usart_set_databits(uint32_t usart, uint32_t bits)
90{
91 if (bits == 8) {
92 USART_CR1(usart) &= ~USART_CR1_M; /* 8 data bits */
93 } else {
94 USART_CR1(usart) |= USART_CR1_M; /* 9 data bits */
95 }
96}
97
98/*---------------------------------------------------------------------------*/
99/** @brief USART Set Stop Bit(s).
100
101The stop bits are specified as 0.5, 1, 1.5 or 2.
102
103@param[in] usart unsigned 32 bit. USART block register address base @ref
104usart_reg_base
105@param[in] stopbits unsigned 32 bit. Stop bits @ref usart_cr2_stopbits.
106*/
107
108void usart_set_stopbits(uint32_t usart, uint32_t stopbits)
109{
110 uint32_t reg32;
111
112 reg32 = USART_CR2(usart);
113 reg32 = (reg32 & ~USART_CR2_STOPBITS_MASK) | stopbits;
114 USART_CR2(usart) = reg32;
115}
116
117/*---------------------------------------------------------------------------*/
118/** @brief USART Set Parity.
119
120The parity bit can be selected as none, even or odd.
121
122@param[in] usart unsigned 32 bit. USART block register address base @ref
123usart_reg_base
124@param[in] parity unsigned 32 bit. Parity @ref usart_cr1_parity.
125*/
126
127void usart_set_parity(uint32_t usart, uint32_t parity)
128{
129 uint32_t reg32;
130
131 reg32 = USART_CR1(usart);
132 reg32 = (reg32 & ~USART_PARITY_MASK) | parity;
133 USART_CR1(usart) = reg32;
134}
135
136/*---------------------------------------------------------------------------*/
137/** @brief USART Set Rx/Tx Mode.
138
139The mode can be selected as Rx only, Tx only or Rx+Tx.
140
141@param[in] usart unsigned 32 bit. USART block register address base @ref
142usart_reg_base
143@param[in] mode unsigned 32 bit. Mode @ref usart_cr1_mode.
144*/
145
146void usart_set_mode(uint32_t usart, uint32_t mode)
147{
148 uint32_t reg32;
149
150 reg32 = USART_CR1(usart);
151 reg32 = (reg32 & ~USART_MODE_MASK) | mode;
152 USART_CR1(usart) = reg32;
153}
154
155/*---------------------------------------------------------------------------*/
156/** @brief USART Set Hardware Flow Control.
157
158The flow control bit can be selected as none, RTS, CTS or RTS+CTS.
159
160@param[in] usart unsigned 32 bit. USART block register address base @ref
161usart_reg_base
162@param[in] flowcontrol unsigned 32 bit. Flowcontrol @ref usart_cr3_flowcontrol.
163*/
164
165void usart_set_flow_control(uint32_t usart, uint32_t flowcontrol)
166{
167 uint32_t reg32;
168
169 reg32 = USART_CR3(usart);
170 reg32 = (reg32 & ~USART_FLOWCONTROL_MASK) | flowcontrol;
171 USART_CR3(usart) = reg32;
172}
173
174/*---------------------------------------------------------------------------*/
175/** @brief USART Enable.
176
177@param[in] usart unsigned 32 bit. USART block register address base @ref
178usart_reg_base
179*/
180
181void usart_enable(uint32_t usart)
182{
183 USART_CR1(usart) |= USART_CR1_UE;
184}
185
186/*---------------------------------------------------------------------------*/
187/** @brief USART Disable.
188
189At the end of the current frame, the USART is disabled to reduce power.
190
191@param[in] usart unsigned 32 bit. USART block register address base @ref
192usart_reg_base
193*/
194
195void usart_disable(uint32_t usart)
196{
197 USART_CR1(usart) &= ~USART_CR1_UE;
198}
199
200/*---------------------------------------------------------------------------*/
201/** @brief USART Send Data Word with Blocking
202
203Blocks until the transmit data buffer becomes empty then writes the next data
204word for transmission.
205
206@param[in] usart unsigned 32 bit. USART block register address base @ref
207usart_reg_base
208@param[in] data unsigned 16 bit.
209*/
210
211void usart_send_blocking(uint32_t usart, uint16_t data)
212{
214 usart_send(usart, data);
215}
216
217/*---------------------------------------------------------------------------*/
218/** @brief USART Read a Received Data Word with Blocking.
219
220Wait until a data word has been received then return the word.
221
222@param[in] usart unsigned 32 bit. USART block register address base @ref
223usart_reg_base
224@returns unsigned 16 bit data word.
225*/
226
227uint16_t usart_recv_blocking(uint32_t usart)
228{
230
231 return usart_recv(usart);
232}
233
234/*---------------------------------------------------------------------------*/
235/** @brief USART Receiver DMA Enable.
236
237DMA is available on:
238@li USART1 Rx DMA1 channel 5.
239@li USART2 Rx DMA1 channel 6.
240@li USART3 Rx DMA1 channel 3.
241@li UART4 Rx DMA2 channel 3.
242
243@param[in] usart unsigned 32 bit. USART block register address base @ref
244usart_reg_base
245*/
246
247void usart_enable_rx_dma(uint32_t usart)
248{
249 USART_CR3(usart) |= USART_CR3_DMAR;
250}
251
252/*---------------------------------------------------------------------------*/
253/** @brief USART Receiver DMA Disable.
254
255@param[in] usart unsigned 32 bit. USART block register address base @ref
256usart_reg_base
257*/
258
259void usart_disable_rx_dma(uint32_t usart)
260{
261 USART_CR3(usart) &= ~USART_CR3_DMAR;
262}
263
264/*---------------------------------------------------------------------------*/
265/** @brief USART Transmitter DMA Enable.
266
267DMA is available on:
268@li USART1 Tx DMA1 channel 4.
269@li USART2 Tx DMA1 channel 7.
270@li USART3 Tx DMA1 channel 2.
271@li UART4 Tx DMA2 channel 5.
272
273@param[in] usart unsigned 32 bit. USART block register address base @ref
274usart_reg_base
275*/
276
277void usart_enable_tx_dma(uint32_t usart)
278{
279 USART_CR3(usart) |= USART_CR3_DMAT;
280}
281
282/*---------------------------------------------------------------------------*/
283/** @brief USART Transmitter DMA Disable.
284
285@param[in] usart unsigned 32 bit. USART block register address base @ref
286usart_reg_base
287*/
288
289void usart_disable_tx_dma(uint32_t usart)
290{
291 USART_CR3(usart) &= ~USART_CR3_DMAT;
292}
293
294/*---------------------------------------------------------------------------*/
295/** @brief USART Receiver Interrupt Enable.
296
297@param[in] usart unsigned 32 bit. USART block register address base @ref
298usart_reg_base
299*/
300
301void usart_enable_rx_interrupt(uint32_t usart)
302{
303 USART_CR1(usart) |= USART_CR1_RXNEIE;
304}
305
306
307/*---------------------------------------------------------------------------*/
308/** @brief USART Receiver Interrupt Disable.
309
310@param[in] usart unsigned 32 bit. USART block register address base @ref
311usart_reg_base
312*/
313
314void usart_disable_rx_interrupt(uint32_t usart)
315{
316 USART_CR1(usart) &= ~USART_CR1_RXNEIE;
317}
318
319/*---------------------------------------------------------------------------*/
320/** @brief USART Transmitter Interrupt Enable.
321
322@param[in] usart unsigned 32 bit. USART block register address base @ref
323usart_reg_base
324*/
325
326void usart_enable_tx_interrupt(uint32_t usart)
327{
328 USART_CR1(usart) |= USART_CR1_TXEIE;
329}
330
331/*---------------------------------------------------------------------------*/
332/** @brief USART Transmitter Interrupt Disable.
333
334@param[in] usart unsigned 32 bit. USART block register address base @ref
335usart_reg_base
336*/
337
338void usart_disable_tx_interrupt(uint32_t usart)
339{
340 USART_CR1(usart) &= ~USART_CR1_TXEIE;
341}
342
343/*---------------------------------------------------------------------------*/
344/**
345 * @brief USART Transmission Complete Interrupt Enable
346 *
347 * @param[in] usart unsigned 32 bit. USART block register address base @ref
348usart_reg_base
349 */
350
352{
353 USART_CR1(usart) |= USART_CR1_TCIE;
354}
355
356/*---------------------------------------------------------------------------*/
357/**
358 * @brief USART Transmission Complete Interrupt Disable
359 *
360 * @param[in] usart unsigned 32 bit. USART block register address base @ref
361usart_reg_base
362 */
363
365{
366 USART_CR1(usart) &= ~USART_CR1_TCIE;
367}
368
369/** @brief USART Idle Interrupt Enable.
370
371@param[in] usart unsigned 32 bit. USART block register address base @ref
372usart_reg_base
373*/
374
375void usart_enable_idle_interrupt(uint32_t usart)
376{
377 USART_CR1(usart) |= USART_CR1_IDLEIE;
378}
379
380/*---------------------------------------------------------------------------*/
381/** @brief USART Idle Interrupt Disable.
382
383@param[in] usart unsigned 32 bit. USART block register address base @ref
384usart_reg_base
385*/
386
388{
389 USART_CR1(usart) &= ~USART_CR1_IDLEIE;
390}
391
392/*---------------------------------------------------------------------------*/
393/** @brief USART Error Interrupt Enable.
394
395@param[in] usart unsigned 32 bit. USART block register address base @ref
396usart_reg_base
397*/
398
400{
401 USART_CR3(usart) |= USART_CR3_EIE;
402}
403
404/*---------------------------------------------------------------------------*/
405/** @brief USART Error Interrupt Disable.
406
407@param[in] usart unsigned 32 bit. USART block register address base @ref
408usart_reg_base
409*/
410
412{
413 USART_CR3(usart) &= ~USART_CR3_EIE;
414}
415
416/**@}*/
417
uint32_t rcc_get_usart_clk_freq(uint32_t usart)
Get the peripheral clock speed for the USART at base specified.
Definition: rcc.c:508
#define USART_CR1_UE
UE: USART enable.
#define USART_CR1_IDLEIE
IDLEIE: IDLE interrupt enable.
#define USART_CR1_TXEIE
TXEIE: Interrupt enable.
#define USART_CR1_RXNEIE
RXNEIE: RXNE interrupt enable.
#define USART_CR1_M
#define USART_CR1_TCIE
TCIE: Transmission complete interrupt enable.
#define USART_CR3_DMAT
DMAT: DMA enable transmitter.
#define USART_CR3_EIE
EIE: Error interrupt enable.
#define USART_CR3_DMAR
DMAR: DMA enable receiver.
uint16_t usart_recv(uint32_t usart)
USART Read a Received Data Word.
void usart_send(uint32_t usart, uint16_t data)
USART Send a Data Word.
void usart_wait_recv_ready(uint32_t usart)
USART Wait for Received Data Available.
void usart_wait_send_ready(uint32_t usart)
USART Wait for Transmit Data Buffer Empty.
void usart_disable_idle_interrupt(uint32_t usart)
USART Idle Interrupt Disable.
void usart_enable_error_interrupt(uint32_t usart)
USART Error Interrupt Enable.
void usart_enable_rx_dma(uint32_t usart)
USART Receiver DMA Enable.
void usart_enable_tx_interrupt(uint32_t usart)
USART Transmitter Interrupt Enable.
void usart_set_parity(uint32_t usart, uint32_t parity)
USART Set Parity.
void usart_disable_tx_dma(uint32_t usart)
USART Transmitter DMA Disable.
void usart_disable_rx_dma(uint32_t usart)
USART Receiver DMA Disable.
void usart_disable_rx_interrupt(uint32_t usart)
USART Receiver Interrupt Disable.
void usart_disable(uint32_t usart)
USART Disable.
void usart_set_mode(uint32_t usart, uint32_t mode)
USART Set Rx/Tx Mode.
void usart_set_databits(uint32_t usart, uint32_t bits)
USART Set Word Length.
void usart_set_baudrate(uint32_t usart, uint32_t baud)
USART Set Baudrate.
uint16_t usart_recv_blocking(uint32_t usart)
USART Read a Received Data Word with Blocking.
void usart_enable_rx_interrupt(uint32_t usart)
USART Receiver Interrupt Enable.
void usart_enable_idle_interrupt(uint32_t usart)
USART Idle Interrupt Enable.
void usart_disable_tx_complete_interrupt(uint32_t usart)
USART Transmission Complete Interrupt Disable.
void usart_set_stopbits(uint32_t usart, uint32_t stopbits)
USART Set Stop Bit(s).
void usart_enable(uint32_t usart)
USART Enable.
void usart_disable_error_interrupt(uint32_t usart)
USART Error Interrupt Disable.
void usart_enable_tx_dma(uint32_t usart)
USART Transmitter DMA Enable.
void usart_enable_tx_complete_interrupt(uint32_t usart)
USART Transmission Complete Interrupt Enable.
void usart_set_flow_control(uint32_t usart, uint32_t flowcontrol)
USART Set Hardware Flow Control.
void usart_disable_tx_interrupt(uint32_t usart)
USART Transmitter Interrupt Disable.
void usart_send_blocking(uint32_t usart, uint16_t data)
USART Send Data Word with Blocking.
#define USART_CR3(usart_base)
Control register 3 (USARTx_CR3)
#define USART_CR1(usart_base)
Control register 1 (USARTx_CR1)
#define USART_BRR(usart_base)
Baud rate register (USARTx_BRR)
#define USART_CR2(usart_base)
Control register 2 (USARTx_CR2)