This is an old revision of the document!
The RP2040 microcontroller from Raspberry Pi offers two built-in UART peripherals for serial communication. Here's a basic introduction to using UART with Pico-SDK, including configuration, sending, and receiving data:
#include <hardware/uart.h>
// Example: Configure UART0 at 115200 baud, 8N1 format int baudrate = 115200; uart_init(uart0, baudrate, UART_PARITY_NONE, UART_STOP_BITS_1);
const char* message = "Hello from RP2040!\n";
// Example 1: Sending a string with uart_puts
uart_puts(uart0, message);
// Example 2: Sending raw data with uart_write_blocking
uint8_t data[] = {0x41, 0x42, 0x43}; // Example data bytes
int written = uart_write_blocking(uart0, data, sizeof(data));
int received_char;
// Example 1: Reading a single character
while (uart_is_readable(uart0)) {
received_char = uart_getc(uart0);
// Process received character (e.g., print it)
}
// Example 2: Reading data into a buffer with a timeout
uint8_t buffer[10];
int num_read = uart_read_blocking(uart0, buffer, sizeof(buffer), 100); // Wait 100ms for data
if (num_read > 0) {
// Process received data in the buffer
}