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 * @defgroup usart_api USART peripheral API
3 * @ingroup peripheral_apis
4 * @brief <b>PAC55xxxx USART Driver</b>
5 * @author @htmlonly &copy; @endhtmlonly 2020 Kevin Stefanik <kevin@allocor.tech>
6 * @date February 25, 2020
7 *
8 * This library supports the USART module in the PAC55xx SoC from Qorvo.
9 *
10 * LGPL License Terms @ref lgpl_license
11 */
12/*
13 * This file is part of the libopencm3 project.
14 *
15 * This library is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this library. If not, see <http://www.gnu.org/licenses/>.
27 */
30
31/**@{*/
32
33/** @brief USART Set Baudrate
34The baud rate is computed assuming a peripheral clock of 150MHz.
35@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
36@param[in] baud unsigned 32 bit. Baud rate specified in Hz.
37@return Actual baud rate.
38*/
39uint32_t usart_set_baudrate(uint32_t usart, uint32_t baud) {
40 /* TODO Assumes 150MHz PCLK. Update this to ccs_get_peripheral_freq() like on other platforms */
41 const uint32_t pclk = 150000000;
42 uint32_t denom = (baud << 4); /* denominator is baud * 16. */
43 uint32_t dlr = 0xFFFFu & ((pclk + denom / 2) / denom);
44 USART_DLR(usart) = dlr;
45 return pclk / (dlr << 4); /* Baud Rate = PCLK / (16 * UARTADLR) */
46}
47
48/** @brief USART Configure Line Control Register
49This register sets the data bits, stop bits, and parity
50@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
51@param[in] data_bits unsigned 8 bit. One of USART_DATABITS_5/6/7/8.
52@param[in] stop_bits unsigned 8 bit. One of USART_STOPBITS_1/1P5/2.
53@param[in] parity unsigned 8 bit. One of USART_PARITY_DISABLE/ODD/EVEN/FORCE1/FORCE0
54*/
55void usart_configure_lcr(uint32_t usart, uint8_t data_bits, uint8_t stop_bits,
56 uint8_t parity) {
57 USART_LCR(usart) = USART_LCR_WLS(data_bits)
58 | ((stop_bits==USART_STOPBITS_2) ? USART_LCR_SBS : 0)
59 | USART_LCR_PSELPEN(parity);
60}
61
62/** @brief Enable Break Control
63Enables break control bit that forces TX pin to logic low.
64@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
65*/
66void usart_break_enable(uint32_t usart) {
67 USART_LCR(usart) |= USART_LCR_BCON;
68}
69
70/** @brief Disable Break Control
71Disables break control bit that forces TX pin to logic low.
72@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
73*/
74void usart_break_disable(uint32_t usart) {
75 USART_LCR(usart) &= ~USART_LCR_BCON;
76}
77
78/** @brief Enable Enhanced Mode
79Enable enhanced mode to generate interrupts when FIFO thresholds in FCR are reached.
80@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
81*/
82void usart_enhanced_enable(uint32_t usart) {
84}
85
86/** @brief Disable Enhanced Mode
87Disable enhanced mode to generate interrupts when FIFO thresholds in FCR are reached.
88@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
89*/
90void usart_enhanced_disable(uint32_t usart) {
91 USART_EFR(usart) &= ~USART_EFR_ENMODE;
92}
93
94/** @brief Enable FIFOs
95Enable both TX and RX FIFOs. This must be set before setting the trigger levels.
96@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
97*/
98void usart_fifo_enable(uint32_t usart) {
100}
101
102/** @brief Disable FIFOs
103Disable both TX and RX FIFOs.
104@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
105*/
106void usart_fifo_disable(uint32_t usart) {
107 USART_FCR(usart) &= ~USART_FCR_FIFOEN;
108}
109
110/** Set the TX and RX FIFO depth. This function also enables the FIFOs if not already.
111@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
112@param[in] tx_depth unsigned 8 bit. One of USART_FIFO_TRIG_1/2/4/14CHAR.
113@param[in] rx_depth unsigned 8 bit. One of USART_FIFO_TRIG_1/2/4/14CHAR.
114*/
115void usart_set_fifo_depth(uint32_t usart, uint8_t tx_depth, uint8_t rx_depth) {
116 USART_FCR(usart) |= USART_FCR_FIFOEN;
117 USART_FCR(usart) = USART_FCR_TXTL(tx_depth) | USART_FCR_RXTL(rx_depth) | USART_FCR_FIFOEN;
118}
119
120/** @brief Write byte to TX FIFO
121@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
122@param[in] data unsigned 8 bit. Data to write to the TX FIFO.
123*/
124void usart_send(uint32_t usart, uint8_t data) {
125 USART_THR(usart) = (uint32_t)data;
126}
127
128/** @brief Read byte from the RX FIFO
129@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
130@return Data read from the RX FIFO.
131*/
132uint8_t usart_recv(uint32_t usart) {
133 return (uint8_t)USART_RBR(usart);
134}
135
136/** @brief Enable RX Interrupts
137Enable both the Receive Data Available and Character Timeout interrupts.
138@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
139*/
140void usart_enable_rx_interrupt(uint32_t usart) {
141 USART_IER(usart) |= USART_IER_RBRIE;
142}
143
144/** @brief Disable RX Interrupts
145Disable both the Receive Data Available and Character Timeout interrupts.
146@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
147*/
148void usart_disable_rx_interrupt(uint32_t usart) {
149 USART_IER(usart) &= ~USART_IER_RBRIE;
150}
151
152/** @brief Enable TX Interrupt
153Enable the TX Holding Register Empty interrupt.
154@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
155*/
156void usart_enable_tx_interrupt(uint32_t usart) {
157 USART_IER(usart) |= USART_IER_THRIE;
158}
159
160/** @brief Disable TX Interrupt
161Disable the TX Holding Register Empty interrupt.
162@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
163*/
164void usart_disable_tx_interrupt(uint32_t usart) {
165 USART_IER(usart) &= ~USART_IER_THRIE;
166}
167
168/** @brief Enable RX Line Status Interrupt
169Enable the RX Line Status interrupt.
170@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
171*/
172void usart_enable_rls_interrupt(uint32_t usart) {
173 USART_IER(usart) |= USART_IER_RLSIE;
174}
175
176/** @brief Disable RX Line Status Interrupt
177Disable the RX Line Status interrupt.
178@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
179*/
180void usart_disable_rls_interrupt(uint32_t usart) {
181 USART_IER(usart) &= ~USART_IER_RLSIE;
182}
183
184/** @brief Clear the TX FIFO
185Clears the TX FIFO. The bit is self-clearing.
186@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
187*/
188void usart_clear_tx_fifo(uint32_t usart) {
190}
191
192/** @brief Clear the RX FIFO
193Clears the RX FIFO. The bit is self-clearing.
194@param[in] usart unsigned 32 bit. USART block register address base @ref usart_reg_base
195*/
196void usart_clear_rx_fifo(uint32_t usart) {
198}
199
200/**@}*/
#define USART_EFR_ENMODE
Enable Enhanced Mode to use TX and RX FIFO trigger level interrupts.
Definition: usart.h:185
void usart_enable_rls_interrupt(uint32_t usart)
Enable RX Line Status Interrupt Enable the RX Line Status interrupt.
Definition: usart.c:172
void usart_fifo_enable(uint32_t usart)
Enable FIFOs Enable both TX and RX FIFOs.
Definition: usart.c:98
uint8_t usart_recv(uint32_t usart)
Read byte from the RX FIFO.
Definition: usart.c:132
void usart_enable_tx_interrupt(uint32_t usart)
Enable TX Interrupt Enable the TX Holding Register Empty interrupt.
Definition: usart.c:156
void usart_set_fifo_depth(uint32_t usart, uint8_t tx_depth, uint8_t rx_depth)
Set the TX and RX FIFO depth.
Definition: usart.c:115
void usart_enhanced_enable(uint32_t usart)
Enable Enhanced Mode Enable enhanced mode to generate interrupts when FIFO thresholds in FCR are reac...
Definition: usart.c:82
void usart_clear_tx_fifo(uint32_t usart)
Clear the TX FIFO Clears the TX FIFO.
Definition: usart.c:188
void usart_disable_rx_interrupt(uint32_t usart)
Disable RX Interrupts Disable both the Receive Data Available and Character Timeout interrupts.
Definition: usart.c:148
uint32_t usart_set_baudrate(uint32_t usart, uint32_t baud)
USART Set Baudrate The baud rate is computed assuming a peripheral clock of 150MHz.
Definition: usart.c:39
void usart_send(uint32_t usart, uint8_t data)
Write byte to TX FIFO.
Definition: usart.c:124
void usart_configure_lcr(uint32_t usart, uint8_t data_bits, uint8_t stop_bits, uint8_t parity)
USART Configure Line Control Register This register sets the data bits, stop bits,...
Definition: usart.c:55
void usart_clear_rx_fifo(uint32_t usart)
Clear the RX FIFO Clears the RX FIFO.
Definition: usart.c:196
void usart_enable_rx_interrupt(uint32_t usart)
Enable RX Interrupts Enable both the Receive Data Available and Character Timeout interrupts.
Definition: usart.c:140
void usart_fifo_disable(uint32_t usart)
Disable FIFOs Disable both TX and RX FIFOs.
Definition: usart.c:106
void usart_break_enable(uint32_t usart)
Enable Break Control Enables break control bit that forces TX pin to logic low.
Definition: usart.c:66
void usart_disable_rls_interrupt(uint32_t usart)
Disable RX Line Status Interrupt Disable the RX Line Status interrupt.
Definition: usart.c:180
void usart_enhanced_disable(uint32_t usart)
Disable Enhanced Mode Disable enhanced mode to generate interrupts when FIFO thresholds in FCR are re...
Definition: usart.c:90
void usart_break_disable(uint32_t usart)
Disable Break Control Disables break control bit that forces TX pin to logic low.
Definition: usart.c:74
void usart_disable_tx_interrupt(uint32_t usart)
Disable TX Interrupt Disable the TX Holding Register Empty interrupt.
Definition: usart.c:164
#define USART_FCR_RXTL(rxtl)
RX Trigger Level.
Definition: usart.h:103
#define USART_FCR_RXFIFORST
RX FIFO Reset.
Definition: usart.h:93
#define USART_FCR_FIFOEN
Enable both UART RX and TX FIFOs, must be set before writing rest of FCR.
Definition: usart.h:91
#define USART_FCR_TXFIFORST
TX FIFO Reset.
Definition: usart.h:95
#define USART_FCR_TXTL(txtl)
TX Trigger Level.
Definition: usart.h:99
#define USART_IER_RBRIE
Enable the RX Buffer Register Interrupt.
Definition: usart.h:71
#define USART_IER_RLSIE
Enable RX line status interrupt.
Definition: usart.h:67
#define USART_IER_THRIE
Enable the TX Holding Empty interrupt.
Definition: usart.h:69
#define USART_LCR_BCON
Break Control: Enabling this bit forces TX to logic 0.
Definition: usart.h:156
#define USART_LCR_SBS
Set LCR:SBS for 1.5 or 2 stop bits, Clear for 1 stop bit.
Definition: usart.h:148
#define USART_LCR_WLS(wls)
Word length select: 5-8 databits.
Definition: usart.h:146
#define USART_LCR_PSELPEN(psel)
LCR:PSEL and LCR:PEN control parity.
Definition: usart.h:154
#define USART_STOPBITS_2
LCR:SBS Use 2 stop bits.
Definition: usart.h:143
#define USART_FCR(usart_base)
FIFO Control Register RW, default 0000 0000h.
Definition: usart.h:53
#define USART_LCR(usart_base)
Line control Register RW, default 0000 0000h.
Definition: usart.h:55
#define USART_THR(usart_base)
Transmit Holding Register WO, only bits 7:0 used.
Definition: usart.h:45
#define USART_DLR(usart_base)
Divisor Latch Register RW, default 0000 0001h, only bits 15:0 used.
Definition: usart.h:47
#define USART_EFR(usart_base)
Enhanced Mode Register RW, default 0000 000h.
Definition: usart.h:61
#define USART_IER(usart_base)
Interrupt Enable Register RW, default 0000 0000h.
Definition: usart.h:49
#define USART_RBR(usart_base)
Receive Buffer Register RO, only bits 7:0 used.
Definition: usart.h:43