The Raspberry Pi Pico supports two UARTs, UART0 and UART1. The RX and TX pins used for each UART is configurable, and this is documented in Table 278 General Purpose Input/Output (GPIO) User Bank Functions:
|UART||Option 1||Option 2||Option 3||Option 4|
Likewise UART CTS and RTS (if needed/used) can also be assigned to 4 different pin options.
To set the appropriate pin via the C SDK, one uses the gpio_set_function function. Here’s the function call:
void gpio_set_function (uint gpio, enum gpio_function fn)
To set pin 0 to UART (and by implication UART0 TX) use:
To set pin 21 to UART (by definition UART1 RX) use:
What happens if you set more than one pin for the same UART TX or RX? According to the RP2040 datasheet (which says you shouldn’t do this), for TX lines the signal output is a logical OR of the combined pins. Presumably (although this is not stated by the datasheet), the same is true for the a UART RX connected to multiple GPIOs simultaneously.
As described here you can also configure the USB serial interface (which is handy for logging), to use either UART0 or UART1. To do so, add this to your CMakeLists.txt:
add_definitions(-DPICO_DEFAULT_UART=1 -DPICO_DEFAULT_UART_TX_PIN=4 -DPICO_DEFAULT_UART_RX_PIN=5 ) pico_enable_stdio_usb(uarts 1)
The PICO_DEFAULT_UART_TX_PIN and PICO_DEFAULT_UART_TX_PIN #defines may be unnecessary here, as UART1 defaults to pins 4/5.
Finally, when configuring non-default options for your UART, it may be a good idea to add binary information to your image, indicating this has been done. For example, in your main.c, you could add this to indicate that UART1 is using 24/25 for TX/RX:
bi_decl(bi_2pins_with_func(24, 25, GPIO_FUNC_UART));