7#ifndef _HARDWARE_UART_H
8#define _HARDWARE_UART_H
11#include "hardware/structs/uart.h"
12#include "hardware/regs/dreq.h"
15#ifndef PARAM_ASSERTIONS_ENABLED_UART
16#define PARAM_ASSERTIONS_ENABLED_UART 0
24#ifndef PICO_UART_ENABLE_CRLF_SUPPORT
25#define PICO_UART_ENABLE_CRLF_SUPPORT 1
29#ifndef PICO_UART_DEFAULT_CRLF
30#define PICO_UART_DEFAULT_CRLF 0
38#ifndef PICO_DEFAULT_UART_BAUD_RATE
39#define PICO_DEFAULT_UART_BAUD_RATE 115200
72typedef struct uart_inst uart_inst_t;
81#define uart0 ((uart_inst_t * const)uart0_hw)
82#define uart1 ((uart_inst_t * const)uart1_hw)
86#if !defined(PICO_DEFAULT_UART_INSTANCE) && defined(PICO_DEFAULT_UART)
87#define PICO_DEFAULT_UART_INSTANCE (__CONCAT(uart,PICO_DEFAULT_UART))
90#ifdef PICO_DEFAULT_UART_INSTANCE
91#define uart_default PICO_DEFAULT_UART_INSTANCE
101 invalid_params_if(UART, uart !=
uart0 && uart !=
uart1);
102 return uart ==
uart1 ? 1 : 0;
105static inline uart_inst_t *uart_get_instance(uint instance) {
106 static_assert(NUM_UARTS == 2,
"");
107 invalid_params_if(UART, instance >= NUM_UARTS);
111static inline uart_hw_t *uart_get_hw(uart_inst_t *uart) {
141uint
uart_init(uart_inst_t *uart, uint baudrate);
173 (bool_to_bit(cts) << UART_UARTCR_CTSEN_LSB) | (bool_to_bit(rts) << UART_UARTCR_RTSEN_LSB),
174 UART_UARTCR_RTSEN_BITS | UART_UARTCR_CTSEN_BITS);
187static inline void uart_set_format(uart_inst_t *uart, uint data_bits, uint stop_bits, uart_parity_t parity) {
188 invalid_params_if(UART, data_bits < 5 || data_bits > 8);
189 invalid_params_if(UART, stop_bits != 1 && stop_bits != 2);
190 invalid_params_if(UART, parity != UART_PARITY_NONE && parity != UART_PARITY_EVEN && parity != UART_PARITY_ODD);
192 ((data_bits - 5u) << UART_UARTLCR_H_WLEN_LSB) |
193 ((stop_bits - 1u) << UART_UARTLCR_H_STP2_LSB) |
194 (bool_to_bit(parity != UART_PARITY_NONE) << UART_UARTLCR_H_PEN_LSB) |
195 (bool_to_bit(parity == UART_PARITY_EVEN) << UART_UARTLCR_H_EPS_LSB),
196 UART_UARTLCR_H_WLEN_BITS |
197 UART_UARTLCR_H_STP2_BITS |
198 UART_UARTLCR_H_PEN_BITS |
199 UART_UARTLCR_H_EPS_BITS);
217 uart_get_hw(uart)->imsc = (bool_to_bit(tx_needs_data) << UART_UARTIMSC_TXIM_LSB) |
218 (bool_to_bit(rx_has_data) << UART_UARTIMSC_RXIM_LSB) |
219 (bool_to_bit(rx_has_data) << UART_UARTIMSC_RTIM_LSB);
222 hw_write_masked(&uart_get_hw(uart)->ifls, 0 << UART_UARTIFLS_RXIFLSEL_LSB,
223 UART_UARTIFLS_RXIFLSEL_BITS);
227 hw_write_masked(&uart_get_hw(uart)->ifls, 0 << UART_UARTIFLS_TXIFLSEL_LSB,
228 UART_UARTIFLS_TXIFLSEL_BITS);
239 return !!(uart_get_hw(uart)->cr & UART_UARTCR_UARTEN_BITS);
250 (bool_to_bit(enabled) << UART_UARTLCR_H_FEN_LSB),
251 UART_UARTLCR_H_FEN_BITS);
265 return !(uart_get_hw(uart)->fr & UART_UARTFR_TXFF_BITS);
287 return !(uart_get_hw(uart)->fr & UART_UARTFR_RXFE_BITS);
300 for (
size_t i = 0; i < len; ++i) {
303 uart_get_hw(uart)->dr = *src++;
317 for (
size_t i = 0; i < len; ++i) {
320 *dst++ = (uint8_t) uart_get_hw(uart)->dr;
347static inline void uart_putc(uart_inst_t *uart,
char c) {
348#if PICO_UART_ENABLE_CRLF_SUPPORT
349 extern short uart_char_to_line_feed[NUM_UARTS];
364static inline void uart_puts(uart_inst_t *uart,
const char *s) {
365#if PICO_UART_ENABLE_CRLF_SUPPORT
366 bool last_was_cr =
false;
373 last_was_cr = *s++ ==
'\r';
403 hw_set_bits(&uart_get_hw(uart)->lcr_h, UART_UARTLCR_H_BRK_BITS);
405 hw_clear_bits(&uart_get_hw(uart)->lcr_h, UART_UARTLCR_H_BRK_BITS);
443 static_assert(DREQ_UART0_RX == DREQ_UART0_TX + 1,
"");
444 static_assert(DREQ_UART1_RX == DREQ_UART1_TX + 1,
"");
445 static_assert(DREQ_UART1_TX == DREQ_UART0_TX + 2,
"");
static __force_inline void hw_set_bits(io_rw_32 *addr, uint32_t mask)
Atomically set the specified bits to 1 in a HW register.
Definition: address_mapped.h:121
static __force_inline void hw_write_masked(io_rw_32 *addr, uint32_t values, uint32_t write_mask)
Set new values for a sub-set of the bits in a HW register.
Definition: address_mapped.h:157
static __force_inline void hw_clear_bits(io_rw_32 *addr, uint32_t mask)
Atomically clear the specified bits to 0 in a HW register.
Definition: address_mapped.h:131
bool uart_is_readable_within_us(uart_inst_t *uart, uint32_t us)
Wait for up to a certain number of microseconds for the RX FIFO to be non empty.
Definition: uart.c:110
static uint uart_get_index(uart_inst_t *uart)
Convert UART instance to hardware instance number.
Definition: uart.h:100
static void uart_set_fifo_enabled(uart_inst_t *uart, bool enabled)
Enable/Disable the FIFOs on specified UART.
Definition: uart.h:248
static void uart_putc(uart_inst_t *uart, char c)
Write single character to UART for transmission, with optional CR/LF conversions.
Definition: uart.h:347
static void uart_set_format(uart_inst_t *uart, uint data_bits, uint stop_bits, uart_parity_t parity)
Set UART data format.
Definition: uart.h:187
static bool uart_is_readable(uart_inst_t *uart)
Determine whether data is waiting in the RX FIFO.
Definition: uart.h:285
#define uart0
The UART identifiers for use in UART functions.
Definition: uart.h:81
void uart_set_translate_crlf(uart_inst_t *uart, bool translate)
Set CR/LF conversion on UART.
Definition: uart.c:102
static void uart_set_hw_flow(uart_inst_t *uart, bool cts, bool rts)
Set UART flow control CTS/RTS.
Definition: uart.h:171
static void uart_set_break(uart_inst_t *uart, bool en)
Assert a break condition on the UART transmission.
Definition: uart.h:401
uint uart_init(uart_inst_t *uart, uint baudrate)
Initialise a UART.
Definition: uart.c:75
static bool uart_is_enabled(uart_inst_t *uart)
Test if specific UART is enabled.
Definition: uart.h:238
static void uart_putc_raw(uart_inst_t *uart, char c)
Write single character to UART for transmission.
Definition: uart.h:335
static char uart_getc(uart_inst_t *uart)
Read a single character to UART.
Definition: uart.h:389
uart_parity_t
UART Parity enumeration.
Definition: uart.h:119
void uart_deinit(uart_inst_t *uart)
DeInitialise a UART.
Definition: uart.c:67
static void uart_set_irq_enables(uart_inst_t *uart, bool rx_has_data, bool tx_needs_data)
Setup UART interrupts.
Definition: uart.h:212
static void uart_puts(uart_inst_t *uart, const char *s)
Write string to UART for transmission, doing any CR/LF conversions.
Definition: uart.h:364
static bool uart_is_writable(uart_inst_t *uart)
Determine if space is available in the TX FIFO.
Definition: uart.h:264
static void uart_default_tx_wait_blocking(void)
Wait for the default UART's TX FIFO to be drained.
Definition: uart.h:419
uint uart_set_baudrate(uart_inst_t *uart, uint baudrate)
Set UART baud rate.
Definition: uart.c:73
#define uart1
Identifier for UART instance 1.
Definition: uart.h:82
static void uart_write_blocking(uart_inst_t *uart, const uint8_t *src, size_t len)
Write to the UART for transmission.
Definition: uart.h:299
static void uart_read_blocking(uart_inst_t *uart, uint8_t *dst, size_t len)
Read from the UART.
Definition: uart.h:316
static uint uart_get_dreq(uart_inst_t *uart, bool is_tx)
Return the DREQ to use for pacing transfers to/from a particular UART instance.
Definition: uart.h:442
static void uart_tx_wait_blocking(uart_inst_t *uart)
Wait for the UART TX fifo to be drained.
Definition: uart.h:273