Loading...
Searching...
No Matches
uart.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef _HARDWARE_UART_H
8#define _HARDWARE_UART_H
9
10#include "pico.h"
11#include "hardware/resets.h"
12#include "hardware/structs/uart.h"
13
14// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_UART, Enable/disable assertions in the hardware_uart module, type=bool, default=0, group=hardware_uart
15#ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_UART
16#ifdef PARAM_ASSERTIONS_ENABLED_UART // backwards compatibility with SDK < 2.0.0
17#define PARAM_ASSERTIONS_ENABLED_HARDWARE_UART PARAM_ASSERTIONS_ENABLED_UART
18#else
19#define PARAM_ASSERTIONS_ENABLED_HARDWARE_UART 0
20#endif
21#endif
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27// PICO_CONFIG: PICO_UART_ENABLE_CRLF_SUPPORT, Enable/disable CR/LF translation support, type=bool, default=1, group=hardware_uart
28#ifndef PICO_UART_ENABLE_CRLF_SUPPORT
29#define PICO_UART_ENABLE_CRLF_SUPPORT 1
30#endif
31
32// PICO_CONFIG: PICO_UART_DEFAULT_CRLF, Enable/disable CR/LF translation on UART, type=bool, default=0, depends=PICO_UART_ENABLE_CRLF_SUPPORT, group=hardware_uart
33#ifndef PICO_UART_DEFAULT_CRLF
34#define PICO_UART_DEFAULT_CRLF 0
35#endif
36
37// PICO_CONFIG: PICO_DEFAULT_UART, Define the default UART used for printf etc, min=0, max=1, default=Usually provided via board header, group=hardware_uart
38// PICO_CONFIG: PICO_DEFAULT_UART_TX_PIN, Define the default UART TX pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_uart
39// PICO_CONFIG: PICO_DEFAULT_UART_RX_PIN, Define the default UART RX pin, min=0, max=47 on RP2350B, 29 otherwise, default=Usually provided via board header, group=hardware_uart
40
41// PICO_CONFIG: PICO_DEFAULT_UART_BAUD_RATE, Define the default UART baudrate, max=921600, default=115200, group=hardware_uart
42#ifndef PICO_DEFAULT_UART_BAUD_RATE
43#define PICO_DEFAULT_UART_BAUD_RATE 115200
44#endif
45
76
77// Currently always a pointer to hw but it might not be in the future
83typedef struct uart_inst uart_inst_t;
84
92#define uart0 ((uart_inst_t *)uart0_hw)
93#define uart1 ((uart_inst_t *)uart1_hw)
94
96
103#if !defined(PICO_DEFAULT_UART_INSTANCE) && defined(PICO_DEFAULT_UART)
104#define PICO_DEFAULT_UART_INSTANCE() (__CONCAT(uart,PICO_DEFAULT_UART))
105#endif
106
113
114#ifdef PICO_DEFAULT_UART_INSTANCE
115#define uart_default PICO_DEFAULT_UART_INSTANCE()
116#endif
117
126#ifndef UART_NUM
127static_assert(NUM_UARTS == 2, "");
128#define UART_NUM(uart) ((uart) == uart1)
129#endif
130
139#ifndef UART_INSTANCE
140static_assert(NUM_UARTS == 2, "");
141#define UART_INSTANCE(num) ((num) ? uart1 : uart0)
142#endif
143
152#ifndef UART_IS_INSTANCE
153static_assert(NUM_UARTS == 2, "");
154#define UART_IS_INSTANCE(uart) ((uart) == uart0 || (uart) == uart1)
155#endif
156
166#ifndef UART_DREQ_NUM
167#include "hardware/regs/dreq.h"
168static_assert(DREQ_UART0_RX == DREQ_UART0_TX + 1, "");
169static_assert(DREQ_UART1_RX == DREQ_UART1_TX + 1, "");
170static_assert(DREQ_UART1_TX == DREQ_UART0_TX + 2, "");
171#define UART_DREQ_NUM(uart, is_tx) ({ \
172 DREQ_UART0_TX + UART_NUM(uart) * 2 + !(is_tx); \
173})
174#endif
175
184#ifndef UART_CLOCK_NUM
185#define UART_CLOCK_NUM(uart) clk_peri
186#endif
187
196#ifndef UART_FUNCSEL_NUM
197#if PICO_RP2040
198#define UART_FUNCSEL_NUM(uart, gpio) GPIO_FUNC_UART
199#else
200#define UART_FUNCSEL_NUM(uart, gpio) ((gpio) & 0x2 ? GPIO_FUNC_UART_AUX : GPIO_FUNC_UART)
201#endif
202#endif
203
212#ifndef UART_IRQ_NUM
213#include "hardware/regs/intctrl.h"
214static_assert(UART1_IRQ == UART0_IRQ + 1, "");
215#define UART_IRQ_NUM(uart) (UART0_IRQ + UART_NUM(uart))
216#endif
217
226#ifndef UART_RESET_NUM
227#include "hardware/resets.h"
228#define UART_RESET_NUM(uart) (uart_get_index(uart) ? RESET_UART1 : RESET_UART0)
229#endif
230
237static inline uint uart_get_index(uart_inst_t *uart) {
238 valid_params_if(HARDWARE_UART, UART_IS_INSTANCE(uart));
239 return UART_NUM(uart);
240}
241
248static inline uart_inst_t *uart_get_instance(uint num) {
249 invalid_params_if(HARDWARE_UART, num >= NUM_UARTS);
250 return UART_INSTANCE(num);
251}
252
262static inline uart_hw_t *uart_get_hw(uart_inst_t *uart) {
263 uart_get_index(uart); // check it is a hw uart
264 return (uart_hw_t *)uart;
265}
266
270typedef enum {
271 UART_PARITY_NONE,
272 UART_PARITY_EVEN,
273 UART_PARITY_ODD
275
276// ----------------------------------------------------------------------------
277// Setup
278
299uint uart_init(uart_inst_t *uart, uint baudrate);
300
309void uart_deinit(uart_inst_t *uart);
310
331uint uart_set_baudrate(uart_inst_t *uart, uint baudrate);
332
340static inline void uart_set_hw_flow(uart_inst_t *uart, bool cts, bool rts) {
341 hw_write_masked(&uart_get_hw(uart)->cr,
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);
344}
345
367void uart_set_format(uart_inst_t *uart, uint data_bits, uint stop_bits, uart_parity_t parity);
368
379static inline void uart_set_irqs_enabled(uart_inst_t *uart, bool rx_has_data, bool tx_needs_data) {
380 // Both UARTRXINTR (RX) and UARTRTINTR (RX timeout) interrupts are
381 // required for rx_has_data. RX asserts when >=4 characters are in the RX
382 // FIFO (for RXIFLSEL=0). RT asserts when there are >=1 characters and no
383 // more have been received for 32 bit periods.
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);
387 if (rx_has_data) {
388 // Set minimum threshold
389 hw_write_masked(&uart_get_hw(uart)->ifls, 0 << UART_UARTIFLS_RXIFLSEL_LSB,
390 UART_UARTIFLS_RXIFLSEL_BITS);
391 }
392 if (tx_needs_data) {
393 // Set maximum threshold
394 hw_write_masked(&uart_get_hw(uart)->ifls, 0 << UART_UARTIFLS_TXIFLSEL_LSB,
395 UART_UARTIFLS_TXIFLSEL_BITS);
396 }
397}
398
399// backwards compatibility with SDK version < 2.0.0
400static inline void uart_set_irq_enables(uart_inst_t *uart, bool rx_has_data, bool tx_needs_data) {
401 uart_set_irqs_enabled(uart, rx_has_data, tx_needs_data);
402}
403
410static inline bool uart_is_enabled(uart_inst_t *uart) {
411 return uart_get_hw(uart)->cr & UART_UARTCR_UARTEN_BITS;
412}
413
430void uart_set_fifo_enabled(uart_inst_t *uart, bool enabled);
431
432// ----------------------------------------------------------------------------
433// Generic input/output
434
441static inline bool uart_is_writable(uart_inst_t *uart) {
442 return !(uart_get_hw(uart)->fr & UART_UARTFR_TXFF_BITS);
443}
444
450static inline void uart_tx_wait_blocking(uart_inst_t *uart) {
451 while (uart_get_hw(uart)->fr & UART_UARTFR_BUSY_BITS) tight_loop_contents();
452}
453
461static inline bool uart_is_readable(uart_inst_t *uart) {
462 // PL011 doesn't expose levels directly, so return values are only 0 or 1
463 return !(uart_get_hw(uart)->fr & UART_UARTFR_RXFE_BITS);
464}
465
478static inline void uart_write_blocking(uart_inst_t *uart, const uint8_t *src, size_t len) {
479 for (size_t i = 0; i < len; ++i) {
480 while (!uart_is_writable(uart))
482 uart_get_hw(uart)->dr = *src++;
483 }
484}
485
495static inline void uart_read_blocking(uart_inst_t *uart, uint8_t *dst, size_t len) {
496 for (size_t i = 0; i < len; ++i) {
497 while (!uart_is_readable(uart))
499 *dst++ = (uint8_t) uart_get_hw(uart)->dr;
500 }
501}
502
503// ----------------------------------------------------------------------------
504// UART-specific operations and aliases
505
514static inline void uart_putc_raw(uart_inst_t *uart, char c) {
515 uart_write_blocking(uart, (const uint8_t *) &c, 1);
516}
517
526static inline void uart_putc(uart_inst_t *uart, char c) {
527#if PICO_UART_ENABLE_CRLF_SUPPORT
528 extern short uart_char_to_line_feed[NUM_UARTS];
529 if (uart_char_to_line_feed[uart_get_index(uart)] == c)
530 uart_putc_raw(uart, '\r');
531#endif
532 uart_putc_raw(uart, c);
533}
534
543static inline void uart_puts(uart_inst_t *uart, const char *s) {
544#if PICO_UART_ENABLE_CRLF_SUPPORT
545 bool last_was_cr = false;
546 while (*s) {
547 // Don't add extra carriage returns if one is present
548 if (last_was_cr)
549 uart_putc_raw(uart, *s);
550 else
551 uart_putc(uart, *s);
552 last_was_cr = *s++ == '\r';
553 }
554#else
555 while (*s)
556 uart_putc(uart, *s++);
557#endif
558}
559
568static inline char uart_getc(uart_inst_t *uart) {
569 char c;
570 uart_read_blocking(uart, (uint8_t *) &c, 1);
571 return c;
572}
573
580void uart_set_break(uart_inst_t *uart, bool en);
581
588void uart_set_translate_crlf(uart_inst_t *uart, bool translate);
589
593static inline void uart_default_tx_wait_blocking(void) {
594#ifdef uart_default
595 uart_tx_wait_blocking(uart_default);
596#else
597 assert(false);
598#endif
599}
600
608bool uart_is_readable_within_us(uart_inst_t *uart, uint32_t us);
609
616static inline uint uart_get_dreq_num(uart_inst_t *uart, bool is_tx) {
617 return UART_DREQ_NUM(uart, is_tx);
618}
619
625static inline uint uart_get_reset_num(uart_inst_t *uart) {
626 return UART_RESET_NUM(uart);
627}
628
629// backwards compatibility
630static inline uint uart_get_dreq(uart_inst_t *uart, bool is_tx) {
631 return uart_get_dreq_num(uart, is_tx);
632}
633
634#ifdef __cplusplus
635}
636#endif
637
638#endif
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
static __force_inline void tight_loop_contents(void)
No-op function for the body of tight loops.
Definition common.h:79
Definition uart.h:26