libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
usart_common_f124.c
Go to the documentation of this file.
1/** @addtogroup usart_file
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 Send a Data Word.
39
40@param[in] usart unsigned 32 bit. USART block register address base @ref
41usart_reg_base
42@param[in] data unsigned 16 bit.
43*/
44
45void usart_send(uint32_t usart, uint16_t data)
46{
47 /* Send data. */
48 USART_DR(usart) = (data & USART_DR_MASK);
49}
50
51/*---------------------------------------------------------------------------*/
52/** @brief USART Read a Received Data Word.
53
54If parity is enabled the MSB (bit 7 or 8 depending on the word length) is the
55parity bit.
56
57@param[in] usart unsigned 32 bit. USART block register address base @ref
58usart_reg_base
59@returns unsigned 16 bit data word.
60*/
61
62uint16_t usart_recv(uint32_t usart)
63{
64 /* Receive data. */
65 return USART_DR(usart) & USART_DR_MASK;
66}
67
68/*---------------------------------------------------------------------------*/
69/** @brief USART Wait for Transmit Data Buffer Empty
70
71Blocks until the transmit data buffer becomes empty and is ready to accept the
72next data word.
73
74@param[in] usart unsigned 32 bit. USART block register address base @ref
75usart_reg_base
76*/
77
78void usart_wait_send_ready(uint32_t usart)
79{
80 /* Wait until the data has been transferred into the shift register. */
81 while ((USART_SR(usart) & USART_SR_TXE) == 0);
82}
83
84/*---------------------------------------------------------------------------*/
85/** @brief USART Wait for Received Data Available
86
87Blocks until the receive data buffer holds a valid received data word.
88
89@param[in] usart unsigned 32 bit. USART block register address base @ref
90usart_reg_base
91*/
92
93void usart_wait_recv_ready(uint32_t usart)
94{
95 /* Wait until the data is ready to be received. */
96 while ((USART_SR(usart) & USART_SR_RXNE) == 0);
97}
98
99/*---------------------------------------------------------------------------*/
100/** @brief USART Read a Status Flag.
101
102@param[in] usart unsigned 32 bit. USART block register address base @ref
103usart_reg_base
104@param[in] flag Unsigned int32. Status register flag @ref usart_sr_flags.
105@returns boolean: flag set.
106*/
107
108bool usart_get_flag(uint32_t usart, uint32_t flag)
109{
110 return ((USART_SR(usart) & flag) != 0);
111}
112
113
114/**@}*/
#define USART_DR_MASK
#define USART_SR(usart_base)
#define USART_DR(usart_base)
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.
bool usart_get_flag(uint32_t usart, uint32_t flag)
USART Read a Status Flag.
#define USART_SR_TXE
TXE: Transmit data buffer empty.
#define USART_SR_RXNE
RXNE: Read data register not empty.