This is an old revision of the document!


RP2040 internals

UART

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 necessary libraries: In your C/C++ code, include the hardware/uart.h header from Pico-SDK:
#include <hardware/uart.h>
  • Initialize UART: Use the uart_init function to configure the desired UART peripheral (e.g., uart0 or uart1) and set parameters like baud rate, parity, and stop bits:
// Example: Configure UART0 at 115200 baud, 8N1 format
int baudrate = 115200;
uart_init(uart0, baudrate, UART_PARITY_NONE, UART_STOP_BITS_1);
  • Sending data:
    • Prepare data: The data you want to send must be an array of characters (char*).
    • Transmit data: Use the uart_puts function for sending a null-terminated string (\0 at the end). Alternatively, uart_write_blocking offers more control over the data buffer and transmission:
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));
  • Receiving data:
    • Read data: Use the uart_getc function to read a single character from the receive buffer.
    • Check for available data: Before reading, use uart_is_readable to ensure data is present in the buffer to avoid errors.
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
}

Interrupts

Timers

Exercises

  1. Write a program for a Raspberry Pi Pico to communicate with another device via UART. The program will set up the UART with desired settings, send a predefined message, receive incoming data, and process it (like printing or storing).
  2. Prepare your setup for Marble Pico interaction with VS Code and pico SDK or use Arduino IDE support according to instructions available here: Ardushop Marble Pico.

References

eap/laboratoare/02.1721221277.txt.gz ยท Last modified: 2024/07/17 16:01 by jan.vaduva
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0