libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
usart.c
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 * Copyright (C) 2015 Daniele Lacamera <root at danielinux.net>
6 *
7 * This library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22
23void usart_send(uint32_t usart, uint16_t data)
24{
25 USART_DR(usart) = data;
26}
27
28uint16_t usart_recv(uint32_t usart)
29{
30 return USART_DR(usart) & 0xff;
31}
32
33void usart_send_blocking(uint32_t usart, uint16_t data)
34{
35 while (!usart_is_send_ready(usart));
36 usart_send(usart, data);
37}
38
39bool usart_is_recv_ready(uint32_t usart)
40{
41 return ((USART_FR(usart) & USART_FR_RXFE) == 0);
42}
43
44bool usart_is_send_ready(uint32_t usart)
45{
46 return ((USART_FR(usart) & USART_FR_BUSY) == 0);
47}
48
49uint16_t usart_recv_blocking(uint32_t usart)
50{
51 while (!usart_is_recv_ready(usart));
52 return usart_recv(usart);
53}
54
55void usart_enable_rx_interrupt(uint32_t usart)
56{
57 USART_IM(usart) |= USART_IM_RX;
58}
59
60void usart_enable_tx_interrupt(uint32_t usart)
61{
62 USART_IM(usart) |= USART_IM_TX;
63}
64
65void usart_disable_rx_interrupt(uint32_t usart)
66{
67 USART_IM(usart) &= (~USART_IM_RX);
68}
69
70void usart_disable_tx_interrupt(uint32_t usart)
71{
72 USART_IM(usart) &= (~USART_IM_TX);
73}
74
75void usart_clear_rx_interrupt(uint32_t usart)
76{
77 USART_IC(usart) |= USART_IC_RX;
78}
79
80void usart_clear_tx_interrupt(uint32_t usart)
81{
82 USART_IC(usart) |= USART_IC_TX;
83}
84
85bool usart_get_interrupt_source(uint32_t usart, uint32_t flag)
86{
87 return ((USART_RIS(usart) & flag) != 0);
88}
uint16_t usart_recv(uint32_t usart)
Definition: usart.c:28
void usart_send(uint32_t usart, uint16_t data)
Definition: usart.c:23
void usart_enable_tx_interrupt(uint32_t usart)
Definition: usart.c:60
void usart_clear_rx_interrupt(uint32_t usart)
Definition: usart.c:75
void usart_disable_rx_interrupt(uint32_t usart)
Definition: usart.c:65
bool usart_get_interrupt_source(uint32_t usart, uint32_t flag)
Definition: usart.c:85
bool usart_is_recv_ready(uint32_t usart)
Definition: usart.c:39
uint16_t usart_recv_blocking(uint32_t usart)
Definition: usart.c:49
void usart_enable_rx_interrupt(uint32_t usart)
Definition: usart.c:55
void usart_clear_tx_interrupt(uint32_t usart)
Definition: usart.c:80
bool usart_is_send_ready(uint32_t usart)
Definition: usart.c:44
void usart_disable_tx_interrupt(uint32_t usart)
Definition: usart.c:70
void usart_send_blocking(uint32_t usart, uint16_t data)
Definition: usart.c:33
#define USART_IM_RX
Definition: usart.h:70
#define USART_IC_RX
Definition: usart.h:81
#define USART_IC(x)
Definition: usart.h:44
#define USART_IC_TX
Definition: usart.h:80
#define USART_IM_TX
Definition: usart.h:69
#define USART_FR(x)
Definition: usart.h:34
#define USART_FR_BUSY
Definition: usart.h:59
#define USART_DR(x)
Definition: usart.h:32
#define USART_RIS(x)
Definition: usart.h:42
#define USART_FR_RXFE
Definition: usart.h:58
#define USART_IM(x)
Definition: usart.h:41