libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
usart_common_v2.c
Go to the documentation of this file.
1/** @addtogroup usart_file
2@ingroup peripheral_apis
3
4 @author @htmlonly &copy; @endhtmlonly 2016 Cem Basoglu <cem.basoglu@web.de>
5
6 */
7
8/*
9 * This file is part of the libopencm3 project.
10 *
11 * Copyright (C) 2016 Cem Basoglu <cem.basoglu@web.de>
12 *
13 * This library is free software: you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation, either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this library. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27/**@{*/
28
30
31/*---------------------------------------------------------------------------*/
32/** @brief USART enable data inversion
33
34 Logical data from the data register are send/received in negative/inverse
35 logic. (1=L, 0=H). The parity bit is also inverted.
36
37 @note This bit field can only be written when the USART is disabled.
38
39 @param[in] usart USART block register address base @ref usart_reg_base
40 */
41void usart_enable_data_inversion(uint32_t usart)
42{
44}
45
46/*---------------------------------------------------------------------------*/
47/** @brief USART disable data inversion
48
49 Logical data from the data register are send/received in positive/direct logic.
50 (1=H, 0=L)
51
52 @note This bit field can only be written when the USART is disabled.
53
54 @param[in] usart USART block register address base @ref usart_reg_base
55 */
56void usart_disable_data_inversion(uint32_t usart)
57{
58 USART_CR2(usart) &= ~USART_CR2_DATAINV;
59}
60
61/*---------------------------------------------------------------------------*/
62/** @brief USART Enable TX pin active level inversion
63
64 TX pin signal values are inverted. (VDD =0/mark, Gnd=1/idle).
65
66 @note This bit field can only be written when the USART is disabled.
67
68 @param[in] usart USART block register address base @ref usart_reg_base
69 */
70void usart_enable_tx_inversion(uint32_t usart)
71{
72 USART_CR2(usart) |= USART_CR2_TXINV;
73}
74
75/*---------------------------------------------------------------------------*/
76/** @brief USART Disable TX pin active level inversion
77
78 TX pin signal works using the standard logic levels (VDD =1/idle, Gnd=0/mark)
79
80 @note This bit field can only be written when the USART is disabled.
81
82 @param[in] usart USART block register address base @ref usart_reg_base
83 */
84void usart_disable_tx_inversion(uint32_t usart)
85{
86 USART_CR2(usart) &= ~USART_CR2_TXINV;
87}
88
89/*---------------------------------------------------------------------------*/
90/** @brief USART Enable RX pin active level inversion
91
92 RX pin signal values are inverted. (VDD =0/mark, Gnd=1/idle).
93
94 This bit field can only be written when the USART is disabled.
95
96 @param[in] usart USART block register address base @ref usart_reg_base
97 */
98void usart_enable_rx_inversion(uint32_t usart)
99{
100 USART_CR2(usart) |= USART_CR2_RXINV;
101}
102
103/*---------------------------------------------------------------------------*/
104/** @brief USART Disable RX pin active level inversion
105
106 RX pin signal works using the standard logic levels (VDD =1/idle, Gnd=0/mark)
107
108 This bit field can only be written when the USART is disabled.
109
110 @param[in] usart USART block register address base @ref usart_reg_base
111 */
112void usart_disable_rx_inversion(uint32_t usart)
113{
114
115 USART_CR2(usart) &= ~USART_CR2_RXINV;
116}
117
118/*---------------------------------------------------------------------------*/
119/** @brief USART Enable Half-duplex
120
121 - The TX and RX lines are internally connected.
122 - The RX pin is no longer used
123 - The TX pin is always released when no data is transmitted. Thus,
124 it acts as a standard I/O in idle or in reception. It means
125 that the I/O must be configured so that TX is configured as
126 alternate function open-drain with an external pull-up.
127
128 Apart from this, the communication protocol is similar to normal USART mode.
129 Any conflicts on the line must be managed by software
130
131 This bit field can only be written when the USART is disabled.
132
133 @param[in] usart USART block register address base @ref usart_reg_base
134 */
135void usart_enable_halfduplex(uint32_t usart)
136{
137 USART_CR3(usart) |= USART_CR3_HDSEL;
138}
139
140/*---------------------------------------------------------------------------*/
141/** @brief USART Disable Half-duplex
142
143 This bit field can only be written when the USART is disabled.
144
145 @param[in] usart USART block register address base @ref usart_reg_base
146 */
147void usart_disable_halfduplex(uint32_t usart)
148{
149 USART_CR3(usart) &= ~USART_CR3_HDSEL;
150}
151
152/*---------------------------------------------------------------------------*/
153/** @brief USART Set receiver timeout value
154
155 Sets the receive timeout value in terms of number of bit duration.
156 The @ref USART_ISR_RTOF is set if, after the last received character,
157 no new start bit is detected for more than the receive timeout value.
158
159 @note The timeout value can also be written when USART is enabled.
160 If the new value is lower/equals the internal hardware counter,
161 the RTOF flag will be set.
162
163 @param[in] usart USART block register address base @ref usart_reg_base
164 @param[in] value The receive timeout value in terms of number of bit duration.
165 */
166void usart_set_rx_timeout_value(uint32_t usart, uint32_t value)
167{
168 uint32_t reg;
169 reg = USART_RTOR(usart) & ~USART_RTOR_RTO_MASK;
170 reg |= (USART_RTOR_RTO_VAL(value) & USART_RTOR_RTO_MASK);
171 USART_RTOR(usart) = reg;
172}
173
174/*---------------------------------------------------------------------------*/
175/** @brief USART enable receive timeout function
176
177 @note If the USART does not support the Receiver timeout feature,
178 this bit is reserved and forced by hardware to ‘0’.
179
180 @param[in] usart USART block register address base @ref usart_reg_base
181 */
182void usart_enable_rx_timeout(uint32_t usart)
183{
184 USART_CR2(usart) |= USART_CR2_RTOEN;
185}
186
187/*---------------------------------------------------------------------------*/
188/** @brief USART disable receive timeout function
189
190 @note If the USART does not support the Receiver timeout feature,
191 this bit is reserved and forced by hardware to ‘0’.
192
193 @param[in] usart USART block register address base @ref usart_reg_base
194 */
195void usart_disable_rx_timeout(uint32_t usart)
196{
197 USART_CR2(usart) &= ~USART_CR2_RTOEN;
198}
199
200/*---------------------------------------------------------------------------*/
201/** @brief USART enable receive timeout interrupt
202
203 An interrupt is generated when the RTOF Flag is set
204 in the @ref USART_ISR register.
205
206 @note If the USART does not support the Receiver timeout feature,
207 this bit is reserved and forced by hardware to ‘0’.
208
209 @param[in] usart USART block register address base @ref usart_reg_base
210 */
212{
213 USART_CR1(usart) |= USART_CR1_RTOIE;
214}
215
216/*---------------------------------------------------------------------------*/
217/** @brief USART disable receive timeout interrupt
218
219 @note If the USART does not support the Receiver timeout feature,
220 this bit is reserved and forced by hardware to ‘0’.
221
222 @param[in] usart USART block register address base @ref usart_reg_base
223 */
225{
226 USART_CR1(usart) &= ~USART_CR1_RTOIE;
227}
228
229/*---------------------------------------------------------------------------*/
230/** @brief USART Send a Data Word.
231 *
232 * @param[in] usart unsigned 32 bit. USART block register address base @ref
233 * usart_reg_base
234 * @param[in] data unsigned 16 bit.
235 */
236
237void usart_send(uint32_t usart, uint16_t data)
238{
239 /* Send data. */
240 USART_TDR(usart) = (data & USART_TDR_MASK);
241}
242
243/*---------------------------------------------------------------------------*/
244/** @brief USART Read a Received Data Word.
245 *
246 * If parity is enabled the MSB (bit 7 or 8 depending on the word length) is
247 * the parity bit.
248 *
249 * @param[in] usart unsigned 32 bit. USART block register address base @ref
250 * usart_reg_base
251 * @returns unsigned 16 bit data word.
252 */
253
254uint16_t usart_recv(uint32_t usart)
255{
256 /* Receive data. */
257 return USART_RDR(usart) & USART_RDR_MASK;
258}
259
260/*---------------------------------------------------------------------------*/
261/** @brief USART Wait for Transmit Data Buffer Empty
262 *
263 * Blocks until the transmit data buffer becomes empty and is ready to accept
264 * the next data word.
265 *
266 * @param[in] usart unsigned 32 bit. USART block register address base @ref
267 * usart_reg_base
268 */
269
270void usart_wait_send_ready(uint32_t usart)
271{
272 /* Wait until the data has been transferred into the shift register. */
273 while ((USART_ISR(usart) & USART_ISR_TXE) == 0);
274}
275
276/*---------------------------------------------------------------------------*/
277/** @brief USART Wait for Received Data Available
278 *
279 * Blocks until the receive data buffer holds a valid received data word.
280 *
281 * @param[in] usart unsigned 32 bit. USART block register address base @ref
282 * usart_reg_base
283 */
284
285void usart_wait_recv_ready(uint32_t usart)
286{
287 /* Wait until the data is ready to be received. */
288 while ((USART_ISR(usart) & USART_ISR_RXNE) == 0);
289}
290
291/*---------------------------------------------------------------------------*/
292/** @brief USART Read a Status Flag.
293 *
294 * @param[in] usart unsigned 32 bit. USART block register address base @ref
295 * usart_reg_base
296 * @param[in] flag Unsigned int32. Status register flag @ref usart_isr_values.
297 * @returns boolean: flag set.
298 */
299
300bool usart_get_flag(uint32_t usart, uint32_t flag)
301{
302 return ((USART_ISR(usart) & flag) != 0);
303}
304
305
306/**@}*/
#define USART_CR1_RTOIE
RTOIE: Receiver timeout interrupt enable.
#define USART_CR2_DATAINV
DATAINV: Binary data inversion.
#define USART_CR2_RTOEN
RTOEN: Receiver timeout enable.
#define USART_CR2_TXINV
TXINV: TX pin active level inversion.
#define USART_CR2_RXINV
RXINV: RX pin active level inversion.
#define USART_CR3_HDSEL
HDSEL: Half-duplex selection.
#define USART_RDR_MASK
RDR[8:0]: Receive data value.
#define USART_TDR_MASK
TDR[8:0]: Transmit data value.
uint16_t usart_recv(uint32_t usart)
USART Read a Received Data Word.
void usart_enable_data_inversion(uint32_t usart)
USART enable data inversion.
void usart_disable_halfduplex(uint32_t usart)
USART Disable Half-duplex.
void usart_enable_rx_timeout(uint32_t usart)
USART enable receive timeout function.
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_set_rx_timeout_value(uint32_t usart, uint32_t value)
USART Set receiver timeout value.
void usart_enable_halfduplex(uint32_t usart)
USART Enable Half-duplex.
void usart_wait_send_ready(uint32_t usart)
USART Wait for Transmit Data Buffer Empty.
void usart_disable_rx_timeout_interrupt(uint32_t usart)
USART disable receive timeout interrupt.
void usart_disable_tx_inversion(uint32_t usart)
USART Disable TX pin active level inversion.
void usart_disable_rx_timeout(uint32_t usart)
USART disable receive timeout function.
void usart_disable_rx_inversion(uint32_t usart)
USART Disable RX pin active level inversion.
void usart_enable_rx_timeout_interrupt(uint32_t usart)
USART enable receive timeout interrupt.
void usart_enable_rx_inversion(uint32_t usart)
USART Enable RX pin active level inversion.
void usart_disable_data_inversion(uint32_t usart)
USART disable data inversion.
void usart_enable_tx_inversion(uint32_t usart)
USART Enable TX pin active level inversion.
bool usart_get_flag(uint32_t usart, uint32_t flag)
USART Read a Status Flag.
#define USART_ISR_RXNE
RXNE: Read data register not empty.
#define USART_ISR_TXE
TXE: Transmit data register empty.
#define USART_CR3(usart_base)
Control register 3 (USARTx_CR3)
#define USART_CR1(usart_base)
Control register 1 (USARTx_CR1)
#define USART_TDR(usart_base)
Transmit data register (USART_TDR)
#define USART_RTOR(usart_base)
Receiver timeout register (USART_RTOR)
#define USART_RDR(usart_base)
Receive data register (USART_RDR)
#define USART_ISR(usart_base)
Interrupt & status register (USART_ISR)
#define USART_CR2(usart_base)
Control register 2 (USARTx_CR2)
#define USART_RTOR_RTO_MASK
#define USART_RTOR_RTO_VAL(x)