Datasheet

Table Of Contents
2.18.5.1. Select an IO function
An IO pin can perform many different functions and must be configured before use. For example, you may want it to be a
UART_TX pin, or a PWM output. The Pico SDK provides gpio_set_function for this purpose. Many Pico SDK examples will call
gpio_set_function at the beginning so that it can print to a UART.
The Pico SDK starts by defining a structure to represent the registers of IO bank 0, the User IO bank. Each IO has a status
register, followed by a control register. There are 30 IOs, so the structure containing a status and control register is
instantiated as io[30] to repeat it 30 times.
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2040/hardware_structs/include/hardware/structs/iobank0.h Lines 21 - 30
21 typedef struct {
22 struct {
23 io_rw_32 status;
24 io_rw_32 ctrl;
25 } io[30];
26 io_rw_32 intr[4];
27 io_irq_ctrl_hw_t proc0_irq_ctrl;
28 io_irq_ctrl_hw_t proc1_irq_ctrl;
29 io_irq_ctrl_hw_t dormant_wake_irq_ctrl;
30 } iobank0_hw_t;
A similar structure is defined for the pad control registers for IO bank 1. By default, all pads come out of reset ready to
use, with their input enabled and output disable set to 0. Regardless, gpio_set_function in the Pico SDK sets these to make
sure the pad is ready to use by the selected function. Finally, the desired function select is written to the IO control register
(see GPIO0_CTRL for an example of an IO control register).
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2_common/hardware_gpio/gpio.c Lines 27 - 40
27 // Select function for this GPIO, and ensure input/output are enabled at the pad.
28 // This also clears the input/output/irq override bits.
29 void gpio_set_function(uint gpio, enum gpio_function fn) {
30 invalid_params_if(GPIO, gpio >= N_GPIOS);
31 invalid_params_if(GPIO, fn << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB &
Ê ~IO_BANK0_GPIO0_CTRL_FUNCSEL_BITS);
32 // Set input enable on, output disable off
33 hw_write_masked(&padsbank0_hw->io[gpio],
34 PADS_BANK0_GPIO0_IE_BITS,
35 PADS_BANK0_GPIO0_IE_BITS | PADS_BANK0_GPIO0_OD_BITS
36 );
37 // Zero all fields apart from fsel; we want this IO to do what the peripheral tells it.
38 // This doesn't affect e.g. pullup/pulldown, as these are in pad controls.
39 iobank0_hw->io[gpio].ctrl = fn << IO_BANK0_GPIO0_CTRL_FUNCSEL_LSB;
40 }
2.18.5.2. Enable a GPIO interrupt
The SDK provides a method of being interrupted when a GPIO pin changes state:
Pico SDK: https://github.com/raspberrypi/pico-sdk/tree/pre_release/src/rp2_common/hardware_gpio/gpio.c Lines 114 - 120
114 void gpio_set_irq_enabled(uint gpio, uint32_t events, bool enable) {
115 // Separate mask/force/status per-core, so check which core called, and
116 // set the relevant IRQ controls.
117 io_irq_ctrl_hw_t *irq_ctrl_base = get_core_num() ?
118 &iobank0_hw->proc1_irq_ctrl : &iobank0_hw-
Ê >proc0_irq_ctrl;
RP2040 Datasheet
2.18. GPIO 213