The RP2040 microcontroller from Raspberry Pi offers two built-in UART peripherals for serial communication. UART (Universal Asynchronous Receiver Transmitter) facilitates serial communication between the RP2040 and other devices like computers or sensors.
Data is transmitted one bit at a time, without a dedicated clock signal. Each transmitted character (frame) includes timing information to ensure proper reception. Data bits are transmitted sequentially, one after another, over a single wire (typically with a ground wire for reference).
For transmission the data is prepared in your program as a byte (8-bit) array. The data is sent bit by bit through the TX (Transmit) pin of the RP2040. Additional control bits (Start bit and Stop bits) are added to the data stream for synchronization. The baud rate determines the speed at which bits are transmitted (e.g., 9600 baud signifies 9600 bits per second).
Similarly for reception The RP2040 receives data bit by bit through the RX (Receive) pin. Start and Stop bits are used to identify the beginning and end of a character. The received data bits are assembled back into a byte. The received byte is stored in the UART's receive buffer, and an interrupt can be triggered if enabled.
Each transmitted/received character (byte) is encapsulated in a frame structure:
The time it takes to transmit a character depends on the baud rate. Transmission time (seconds) = Number of bits in frame / Baud rate (bits/second) Example:
Baud rate = 9600 baud Frame size (Start + 8 data bits + 1 stop bit) = 10 bits Transmission time = 10 bits / 9600 baud ≈ 0.001042 seconds
Here's a basic introduction to using UART with Pico-SDK, including configuration, sending, and receiving data:
#include <hardware/uart.h>
// Configure UART0 at 115200 baud, 8N1 format int baudrate = 115200; uart_init(uart0, baudrate); //uart_set_format(uart, 8, 1, UART_PARITY_NONE); // Set the TX and RX pins by using the function select on the GPIO 0 and 1 gpio_set_function(0, GPIO_FUNC_UART); gpio_set_function(1, GPIO_FUNC_UART);
const char* message = "Hello from RP2040!\n"; // Sending a string with uart_puts uart_puts(uart0, message); // 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; // Reading a single character while (uart_is_readable(uart0)) { received_char = uart_getc(uart0); // Process received character (e.g., print it) } // 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 }
The RP2040's UART peripherals can generate interrupts for various events, such as:
You can define interrupt handler functions to handle these events. For example, a receive interrupt handler could read the received character and process it in your program.
A hardware interrupt is a type of exception which is a synchronous or asynchronous signal from a peripheral that signals the occurrence of an event that must be handled by the processor. Interrupt handling has the effect of suspending a program's normal thread of execution and launching an interrupt service routine (ISR).
Generally, to associate an interrupt with a specific routine in the program, the processor uses the interrupt vector table (IVT). In this table, each interrupt is associated with the address to which the program will jump when the interrupt is triggered. These addresses are predefined and are mapped in program memory.
When an interrupt request happens the first thing that the processor does is to memorize its current state. For ARM Cortex-M0 this happens by pushing 8 words or registered data into the main stack to provide the information need to return the processor to what it was doing before before the interrupt request was called. This part is called the stack frame and it includes registers 0 through 3, register 12, the link register, the program counter and the program status register.
ARM Cortex-M microcontrollers also use a Nested Vectored Interrupt Controller (NVIC). The NVIC is specifically designed to handle these interrupts more efficiently. Interrupt addresses in the NVIC memory region are set according to their priority: the lower the address, the higher the priority. As suggested by the “Nested” in its name, the NVIC supports nested interrupts. This means that if a higher priority interrupt occurs while another interrupt is being processed, the controller can pause the current interrupt service routine (ISR), handle the higher priority interrupt, and then resume the interrupted ISR. This feature is crucial for responsive and real-time processing.
The RP2040 boots from an internal bootloader that sets the initial interrupt IVT. The before starting the actual code written into Flash, the internal bootloader loads a secondary bootloader that is written in Flash (the first 256 bytes) together with the developer's app. The IVT that the Flash application provides start after the secondary bootloader, at address 0x100.
The RP2040 chip has two cores (processors), and each core has its own NVIC. Each core's NVIC is connected to the same set of hardware interrupt lines with one exception: IO Interrupts. In the RP2040, IO interrupts are organized by banks, and each core has its own set of IO interrupts for each bank. The IO interrupts for each core are completely independent. For instance, Processor 0 (Core 0) can be interrupted by an event on GPIO pin 0 in bank 0, while Processor 1 (Core 1) can be interrupted by a separate event on GPIO pin 1 in the same bank. Each processor responds only to its own interrupts, allowing them to operate independently or to handle different tasks simultaneously without interfering with each other.
On RP2040, only the lower 26 IRQ signals are connected on the NVIC, as seen in the table below, and IRQs 26 to 31 are tied to zero (never firing). The core can still be forced to enter the relevant interrupt handler by writing bits 26 to 31 in the NVIC ISPR register.
The priority order is determined for these signals is determined by :
All GPIO pins in Raspberry Pi Pico support interrupts. The interrupts can be classified into three types:
Here's a basic introduction to using interrupts on RP2040 with Pico-SDK, including setup, trigger sources, and handler functions.
#include <hardware/irq.h>
// Set up a RX interrupt int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ; // And set up and enable the interrupt handlers irq_set_exclusive_handler(UART_IRQ, on_uart_rx); irq_set_enabled(UART_IRQ, true);
// RX interrupt handler void on_uart_rx() { // Perform action based on the interrupt // Clear the interrupt flag (important) }
Timers are hardware components that generate periodic interrupts or control signals at defined intervals. Timers offer a wide range of functionalities, including:
The system timer is intended to provide a global timebase for software. RP2040 has a number of other programmable counter resources which can provide regular interrupts, or trigger DMA transfers.
The timer has a 64-bit counter, but RP2040 only has a 32-bit data bus. This means that the TIME value is accessed through a pair of registers. These are:
These pairs are used by accessing the lower register, L, followed by the higher register, H. In the read case, reading the L register latches the value in the H register so that an accurate time can be read. Alternatively, TIMERAWH and TIMERAWL can be used to read the raw time without any latching.
The timer has 4 alarms, and outputs a separate interrupt for each alarm. The alarms match on the lower 32 bits of the 64-bit counter which means they can be fired at a maximum of 232 microseconds into the future. This is equivalent to:
To enable an alarm:
Once the alarm has fired, the ARMED bit will be set to 0. To clear the latched interrupt, write a 1 to the appropriate bit in INTR.
For using timers on RP2040 with Pico-SDK include the hardware/timer.h header from Pico-SDK:
#include <hardware/timer.h>