libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
uart.c
Go to the documentation of this file.
1/** @addtogroup uart_file UART peripheral API
2 *
3 * @brief <b>Access functions for the UART controller</b>
4 *
5 * @ingroup peripheral_apis
6 * LGPL License Terms @ref lgpl_license
7 * @author @htmlonly &copy; @endhtmlonly 2016
8 * Maxim Sloyko <maxims@google.com>
9 *
10 */
11
12/*
13 * This file is part of the libopencm3 project.
14 *
15 * Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
16 * Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
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#include <libopencm3/nrf/uart.h>
33#include <libopencm3/nrf/gpio.h>
34/**@{*/
35
36/** @brief Enable the peripheral
37 *
38 * @param[in] uart uint32_t uart base
39 */
40void uart_enable(uint32_t uart)
41{
43}
44
45/** @brief Disable the peripheral
46 *
47 * @param[in] uart uint32_t uart base
48 */
49void uart_disable(uint32_t uart)
50{
52}
53
54/** @brief Configure UART parameters in single call
55 *
56 * @details Any pin number can be set to 0xff (or any number larger than UART_MAX_PIN)
57 * to disconnect that pin.
58 *
59 * @param[in] uart uint32_t uart base
60 * @param[in] tx_pin uint8_t TX pin number
61 * @param[in] rx_pin uint8_t RX pin number
62 * @param[in] rts_pin uint8_t RTS pin number
63 * @param[in] cts_pin uint8_t CTS pin number
64 * @param[in] br enum uart_baud baud rate
65 * @param[in] enable_parity bool If true, enable parity bit
66 */
67void uart_configure(uint32_t uart,
68 uint32_t tx_pin, uint32_t rx_pin, uint32_t rts_pin, uint32_t cts_pin,
69 enum uart_baud br, bool enable_parity)
70{
71 uart_set_pins(uart, rx_pin, tx_pin, cts_pin, rts_pin);
72
73 uint32_t reg_config = enable_parity ? UART_CONFIG_PARITY : 0;
74 if (rts_pin <= UART_MAX_PIN || cts_pin <= UART_MAX_PIN) {
75 reg_config |= UART_CONFIG_HWFC;
76 }
77
78 UART_CONFIG(uart) = reg_config;
79 uart_set_baudrate(uart, br);
80}
81
82/** @brief Select GPIO pins to be used by this peripheral.
83 *
84 * This needs to be configured while UART peripheral is disabled.
85 *
86 * @param[in] uart uart peripheral base.
87 * @param[in] rx RX pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
88 * if signal shall not be connected to any pin.
89 * @param[in] tx TX pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
90 * if signal shall not be connected to any pin.
91 * @param[in] cts CTS pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
92 * if signal shall not be connected to any pin.
93 * @param[in] rts RTS pin. Use GPIO defines in @ref gpio_pin_id or GPIO_UNCONNECTED
94 * if signal shall not be connected to any pin.
95
96 */
97void uart_set_pins(uint32_t uart, uint32_t rx, uint32_t tx, uint32_t cts, uint32_t rts)
98{
99 if (rx != GPIO_UNCONNECTED) {
100 UART_PSELRXD(uart) = __GPIO2PIN(rx);
101 } else {
102 UART_PSELRXD(uart) = rx;
103 }
104
105 if (tx != GPIO_UNCONNECTED) {
106 UART_PSELTXD(uart) = __GPIO2PIN(tx);
107 } else {
108 UART_PSELTXD(uart) = tx;
109 }
110
111 if (cts != GPIO_UNCONNECTED) {
112 UART_PSELCTS(uart) = __GPIO2PIN(cts);
113 } else {
114 UART_PSELCTS(uart) = cts;
115 }
116
117 if (rts != GPIO_UNCONNECTED) {
118 UART_PSELRTS(uart) = __GPIO2PIN(rts);
119 } else {
120 UART_PSELRTS(uart) = rts;
121 }
122}
123
124#undef _LOG2
125
126void uart_set_baudrate(uint32_t uart, enum uart_baud br)
127{
128 UART_BAUDRATE(uart) = br;
129}
130
131void uart_set_parity(uint32_t uart, int parity)
132{
133 UART_CONFIG(uart) |= parity ? UART_CONFIG_PARITY : 0;
134}
135
136void uart_set_flow_control(uint32_t uart, int flow)
137{
138 UART_CONFIG(uart) |= flow ? UART_CONFIG_HWFC : 0;
139}
140
141void uart_start_tx(uint32_t uart)
142{
144}
145
146void uart_send(uint32_t uart, uint16_t byte)
147{
148 UART_TXD((uart)) = byte;
149}
150
151void uart_stop_tx(uint32_t uart)
152{
154}
155
156void uart_start_rx(uint32_t uart)
157{
159}
160
161uint16_t uart_recv(uint32_t uart)
162{
163 return UART_RXD(uart);
164}
165
166void uart_stop_rx(uint32_t uart)
167{
169}
170/**@}*/
171
#define __GPIO2PIN(x)
This is an approximation of log2.
#define PERIPH_TRIGGER_TASK(task)
#define GPIO_UNCONNECTED
Mark the signal as not connected to any pin.
#define UART_TASK_STOPRX(uart)
Definition: common/uart.h:48
#define UART_PSELRXD(uart)
Definition: common/uart.h:72
#define UART_MAX_PIN
Definition: common/uart.h:103
#define UART_RXD(uart)
Definition: common/uart.h:73
#define UART_ENABLE(uart)
Definition: common/uart.h:68
#define UART_TASK_STARTRX(uart)
Definition: common/uart.h:47
uart_baud
Definition: common/uart.h:107
#define UART_PSELTXD(uart)
Definition: common/uart.h:70
#define UART_PSELCTS(uart)
Definition: common/uart.h:71
#define UART_ENABLE_ENABLED
Definition: common/uart.h:97
#define UART_BAUDRATE(uart)
Definition: common/uart.h:75
#define UART_ENABLE_DISABLED
Definition: common/uart.h:98
#define UART_CONFIG(uart)
Definition: common/uart.h:76
#define UART_CONFIG_HWFC
Definition: common/uart.h:99
#define UART_CONFIG_PARITY
Definition: common/uart.h:100
#define UART_TASK_STARTTX(uart)
Definition: common/uart.h:49
#define UART_TXD(uart)
Definition: common/uart.h:74
#define UART_TASK_STOPTX(uart)
Definition: common/uart.h:50
#define UART_PSELRTS(uart)
Definition: common/uart.h:69
void uart_disable(uint32_t uart)
Disable the peripheral.
Definition: uart.c:49
void uart_set_parity(uint32_t uart, int parity)
Definition: uart.c:131
void uart_set_pins(uint32_t uart, uint32_t rx, uint32_t tx, uint32_t cts, uint32_t rts)
Select GPIO pins to be used by this peripheral.
Definition: uart.c:97
void uart_stop_tx(uint32_t uart)
Definition: uart.c:151
uint16_t uart_recv(uint32_t uart)
Definition: uart.c:161
void uart_set_baudrate(uint32_t uart, enum uart_baud br)
Definition: uart.c:126
void uart_stop_rx(uint32_t uart)
Definition: uart.c:166
void uart_set_flow_control(uint32_t uart, int flow)
Definition: uart.c:136
void uart_send(uint32_t uart, uint16_t byte)
Definition: uart.c:146
void uart_enable(uint32_t uart)
Enable the peripheral.
Definition: uart.c:40
void uart_start_tx(uint32_t uart)
Definition: uart.c:141
void uart_configure(uint32_t uart, uint32_t tx_pin, uint32_t rx_pin, uint32_t rts_pin, uint32_t cts_pin, enum uart_baud br, bool enable_parity)
Configure UART parameters in single call.
Definition: uart.c:67
void uart_start_rx(uint32_t uart)
Definition: uart.c:156