7#ifndef _HARDWARE_UART_H
8#define _HARDWARE_UART_H
12#include "hardware/structs/uart.h"
15#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_UART
16#ifdef PARAM_ASSERTIONS_ENABLED_UART
17#define PARAM_ASSERTIONS_ENABLED_HARDWARE_UART PARAM_ASSERTIONS_ENABLED_UART
19#define PARAM_ASSERTIONS_ENABLED_HARDWARE_UART 0
28#ifndef PICO_UART_ENABLE_CRLF_SUPPORT
29#define PICO_UART_ENABLE_CRLF_SUPPORT 1
33#ifndef PICO_UART_DEFAULT_CRLF
34#define PICO_UART_DEFAULT_CRLF 0
42#ifndef PICO_DEFAULT_UART_BAUD_RATE
43#define PICO_DEFAULT_UART_BAUD_RATE 115200
92#define uart0 ((uart_inst_t *)uart0_hw)
93#define uart1 ((uart_inst_t *)uart1_hw)
103#if !defined(PICO_DEFAULT_UART_INSTANCE) && defined(PICO_DEFAULT_UART)
104#define PICO_DEFAULT_UART_INSTANCE() (__CONCAT(uart,PICO_DEFAULT_UART))
114#ifdef PICO_DEFAULT_UART_INSTANCE
115#define uart_default PICO_DEFAULT_UART_INSTANCE()
127static_assert(NUM_UARTS == 2,
"");
128#define UART_NUM(uart) ((uart) == uart1)
140static_assert(NUM_UARTS == 2,
"");
141#define UART_INSTANCE(num) ((num) ? uart1 : uart0)
152#ifndef UART_IS_INSTANCE
153static_assert(NUM_UARTS == 2,
"");
154#define UART_IS_INSTANCE(uart) ((uart) == uart0 || (uart) == uart1)
167#include "hardware/regs/dreq.h"
171#define UART_DREQ_NUM(uart, is_tx) ({ \
172 DREQ_UART0_TX + UART_NUM(uart) * 2 + !(is_tx); \
184#ifndef UART_CLOCK_NUM
185#define UART_CLOCK_NUM(uart) clk_peri
196#ifndef UART_FUNCSEL_NUM
198#define UART_FUNCSEL_NUM(uart, gpio) GPIO_FUNC_UART
200#define UART_FUNCSEL_NUM(uart, gpio) ((gpio) & 0x2 ? GPIO_FUNC_UART_AUX : GPIO_FUNC_UART)
213#include "hardware/regs/intctrl.h"
215#define UART_IRQ_NUM(uart) (UART0_IRQ + UART_NUM(uart))
226#ifndef UART_RESET_NUM
228#define UART_RESET_NUM(uart) (uart_get_index(uart) ? RESET_UART1 : RESET_UART0)
249 invalid_params_if(HARDWARE_UART, num >= NUM_UARTS);
342 (bool_to_bit(cts) << UART_UARTCR_CTSEN_LSB) | (bool_to_bit(rts) << UART_UARTCR_RTSEN_LSB),
343 UART_UARTCR_RTSEN_BITS | UART_UARTCR_CTSEN_BITS);
384 uart_get_hw(uart)->imsc = (bool_to_bit(tx_needs_data) << UART_UARTIMSC_TXIM_LSB) |
385 (bool_to_bit(rx_has_data) << UART_UARTIMSC_RXIM_LSB) |
386 (bool_to_bit(rx_has_data) << UART_UARTIMSC_RTIM_LSB);
390 UART_UARTIFLS_RXIFLSEL_BITS);
395 UART_UARTIFLS_TXIFLSEL_BITS);
400static inline void uart_set_irq_enables(
uart_inst_t *uart,
bool rx_has_data,
bool tx_needs_data) {
411 return uart_get_hw(uart)->cr & UART_UARTCR_UARTEN_BITS;
442 return !(
uart_get_hw(uart)->fr & UART_UARTFR_TXFF_BITS);
463 return !(
uart_get_hw(uart)->fr & UART_UARTFR_RXFE_BITS);
479 for (
size_t i = 0; i < len; ++i) {
496 for (
size_t i = 0; i < len; ++i) {
527#if PICO_UART_ENABLE_CRLF_SUPPORT
528 extern short uart_char_to_line_feed[NUM_UARTS];
544#if PICO_UART_ENABLE_CRLF_SUPPORT
545 bool last_was_cr =
false;
552 last_was_cr = *s++ ==
'\r';
630static inline uint uart_get_dreq(
uart_inst_t *uart,
bool is_tx) {
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:171
@ DREQ_UART0_RX
Select UART0's RX FIFO as DREQ.
Definition dreq.h:111
@ DREQ_UART0_TX
Select UART0's TX FIFO as DREQ.
Definition dreq.h:110
@ DREQ_UART1_TX
Select UART1's TX FIFO as DREQ.
Definition dreq.h:112
@ DREQ_UART1_RX
Select UART1's RX FIFO as DREQ.
Definition dreq.h:113
@ UART1_IRQ
Select UART1's IRQ output.
Definition intctrl.h:108
@ UART0_IRQ
Select UART0's IRQ output.
Definition intctrl.h:107
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:229
static uint uart_get_index(uart_inst_t *uart)
Convert UART instance to hardware instance number.
Definition uart.h:237
static uint uart_get_reset_num(uart_inst_t *uart)
Return the reset_num_t to use to reset a particular UART instance.
Definition uart.h:625
void uart_set_format(uart_inst_t *uart, uint data_bits, uint stop_bits, uart_parity_t parity)
Set UART data format.
Definition uart.c:183
#define UART_DREQ_NUM(uart, is_tx)
Returns the dreq_num_t used for pacing DMA transfers to or from this UART instance....
Definition uart.h:171
#define UART_NUM(uart)
Returns the UART number for a UART instance.
Definition uart.h:128
static void uart_set_irqs_enabled(uart_inst_t *uart, bool rx_has_data, bool tx_needs_data)
Enable/Disable UART interrupt outputs.
Definition uart.h:379
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:526
struct uart_inst uart_inst_t
Opaque type representing a UART instance.
Definition uart.h:83
static uart_hw_t * uart_get_hw(uart_inst_t *uart)
Get the real hardware UART instance from a UART instance.
Definition uart.h:262
static bool uart_is_readable(uart_inst_t *uart)
Determine whether data is waiting in the RX FIFO.
Definition uart.h:461
void uart_set_translate_crlf(uart_inst_t *uart, bool translate)
Set CR/LF conversion on UART.
Definition uart.c:221
static void uart_set_hw_flow(uart_inst_t *uart, bool cts, bool rts)
Set UART flow control CTS/RTS.
Definition uart.h:340
uint uart_init(uart_inst_t *uart, uint baudrate)
Initialise a UART.
Definition uart.c:42
static bool uart_is_enabled(uart_inst_t *uart)
Test if specific UART is enabled.
Definition uart.h:410
static void uart_putc_raw(uart_inst_t *uart, char c)
Write single character to UART for transmission.
Definition uart.h:514
static char uart_getc(uart_inst_t *uart)
Read a single character from the UART.
Definition uart.h:568
uart_parity_t
UART Parity enumeration.
Definition uart.h:270
void uart_deinit(uart_inst_t *uart)
DeInitialise a UART.
Definition uart.c:95
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:543
static bool uart_is_writable(uart_inst_t *uart)
Determine if space is available in the TX FIFO.
Definition uart.h:441
static void uart_default_tx_wait_blocking(void)
Wait for the default UART's TX FIFO to be drained.
Definition uart.h:593
#define UART_IS_INSTANCE(uart)
Returns true if the UART instance is one of the h/w UART instances.
Definition uart.h:154
uint uart_set_baudrate(uart_inst_t *uart, uint baudrate)
Set UART baud rate.
Definition uart.c:155
void uart_set_fifo_enabled(uart_inst_t *uart, bool enabled)
Enable/Disable the FIFOs on specified UART.
Definition uart.c:199
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:478
#define UART_INSTANCE(num)
Returns the UART instance with the given UART number.
Definition uart.h:141
void uart_set_break(uart_inst_t *uart, bool en)
Assert a break condition on the UART transmission.
Definition uart.c:210
static void uart_read_blocking(uart_inst_t *uart, uint8_t *dst, size_t len)
Read from the UART.
Definition uart.h:495
static uint uart_get_dreq_num(uart_inst_t *uart, bool is_tx)
Return the dreq_num_t to use for pacing transfers to/from a particular UART instance.
Definition uart.h:616
static uart_inst_t * uart_get_instance(uint num)
Get the UART instance from an instance number.
Definition uart.h:248
static void uart_tx_wait_blocking(uart_inst_t *uart)
Wait for the UART TX fifo to be drained.
Definition uart.h:450
#define UART_RESET_NUM(uart)
Returns the reset_num_t used to reset a given UART instance.
Definition uart.h:228