1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifndef _HARDWARE_GPIO_H
8 #define _HARDWARE_GPIO_H
9
10 #include "pico.h"
11 #include "hardware/structs/sio.h"
12 #include "hardware/structs/pads_bank0.h"
13 #include "hardware/structs/io_bank0.h"
14 #include "hardware/irq.h"
15
16 // PICO_CONFIG: PICO_USE_GPIO_COPROCESSOR, Enable/disable use of the GPIO coprocessor for GPIO access, type=bool, default=1, group=hardware_gpio
17 #if !defined(PICO_USE_GPIO_COPROCESSOR) && HAS_GPIO_COPROCESSOR
18 #define PICO_USE_GPIO_COPROCESSOR 1
19 #endif
20
21 #if PICO_USE_GPIO_COPROCESSOR
22 #include "hardware/gpio_coproc.h"
23 #endif
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_HARDWARE_GPIO, Enable/disable assertions in the hardware_gpio module, type=bool, default=0, group=hardware_gpio
30 #ifndef PARAM_ASSERTIONS_ENABLED_HARDWARE_GPIO
31 #ifdef PARAM_ASSERTIONS_ENABLED_GPIO // backwards compatibility with SDK < 2.0.0
32 #define PARAM_ASSERTIONS_ENABLED_HARDWARE_GPIO PARAM_ASSERTIONS_ENABLED_GPIO
33 #else
34 #define PARAM_ASSERTIONS_ENABLED_HARDWARE_GPIO 0
35 #endif
36 #endif
37
38 /** \file gpio.h
39 * \defgroup hardware_gpio hardware_gpio
40 *
41 * \brief General Purpose Input/Output (GPIO) API
42 *
43 * RP-series microcontrollers have two banks of General Purpose Input / Output (GPIO) pins, which are assigned as follows:
44 *
45 * \if rp2040-specific
46 * RP2040 has 30 user GPIO pins in bank 0, and 6 QSPI pins in the QSPI bank 1 (QSPI_SS, QSPI_SCLK and QSPI_SD0 to QSPI_SD3). The QSPI
47 * pins are used to execute code from an external flash device, leaving the User bank (GPIO0 to GPIO29) for the programmer to use.
48 * \endif
49 *
50 * \if rp2350-specific
51 * The number of GPIO pins available depends on the package. There are 30 user GPIOs in bank 0 in the QFN-60 package (RP2350A), or 48 user GPIOs
52 * in the QFN-80 package. Bank 1 contains the 6 QSPI pins and the USB DP/DM pins.
53 * \endif
54 *
55 * All GPIOs support digital input and output, but a subset can also be used as inputs to the chip’s Analogue to Digital
56 * Converter (ADC). The allocation of GPIO pins to the ADC depends on the packaging.
57 *
58 * RP2040 and RP2350 QFN-60 GPIO, ADC pins are 26-29.
59 * RP2350 QFN-80, ADC pins are 40-47.
60 *
61 * Each GPIO can be controlled directly by software running on the processors, or by a number of other functional blocks.
62 *
63 * The function allocated to each GPIO is selected by calling the \ref gpio_set_function function. \note Not all functions
64 * are available on all pins.
65 *
66 * Each GPIO can have one function selected at a time. Likewise, each peripheral input (e.g. UART0 RX) should only be selected on
67 * one _GPIO_ at a time. If the same peripheral input is connected to multiple GPIOs, the peripheral sees the logical OR of these
68 * GPIO inputs. Please refer to the datasheet for more information on GPIO function select.
69 *
70 * ### Function Select Table
71 *
72 * \if rp2040_specific
73 * On RP2040 the function selects are:
74 *
75 * | GPIO | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 |
76 * |--------|----------|-----------|----------|--------|-----|------|------|---------------|---------------|
77 * | 0 | SPI0 RX | UART0 TX | I2C0 SDA | PWM0 A | SIO | PIO0 | PIO1 | | USB OVCUR DET |
78 * | 1 | SPI0 CSn | UART0 RX | I2C0 SCL | PWM0 B | SIO | PIO0 | PIO1 | | USB VBUS DET |
79 * | 2 | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM1 A | SIO | PIO0 | PIO1 | | USB VBUS EN |
80 * | 3 | SPI0 TX | UART0 RTS | I2C1 SCL | PWM1 B | SIO | PIO0 | PIO1 | | USB OVCUR DET |
81 * | 4 | SPI0 RX | UART1 TX | I2C0 SDA | PWM2 A | SIO | PIO0 | PIO1 | | USB VBUS DET |
82 * | 5 | SPI0 CSn | UART1 RX | I2C0 SCL | PWM2 B | SIO | PIO0 | PIO1 | | USB VBUS EN |
83 * | 6 | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM3 A | SIO | PIO0 | PIO1 | | USB OVCUR DET |
84 * | 7 | SPI0 TX | UART1 RTS | I2C1 SCL | PWM3 B | SIO | PIO0 | PIO1 | | USB VBUS DET |
85 * | 8 | SPI1 RX | UART1 TX | I2C0 SDA | PWM4 A | SIO | PIO0 | PIO1 | | USB VBUS EN |
86 * | 9 | SPI1 CSn | UART1 RX | I2C0 SCL | PWM4 B | SIO | PIO0 | PIO1 | | USB OVCUR DET |
87 * | 10 | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM5 A | SIO | PIO0 | PIO1 | | USB VBUS DET |
88 * | 11 | SPI1 TX | UART1 RTS | I2C1 SCL | PWM5 B | SIO | PIO0 | PIO1 | | USB VBUS EN |
89 * | 12 | SPI1 RX | UART0 TX | I2C0 SDA | PWM6 A | SIO | PIO0 | PIO1 | | USB OVCUR DET |
90 * | 13 | SPI1 CSn | UART0 RX | I2C0 SCL | PWM6 B | SIO | PIO0 | PIO1 | | USB VBUS DET |
91 * | 14 | SPI1 SCK | UART0 CTS | I2C1 SDA | PWM7 A | SIO | PIO0 | PIO1 | | USB VBUS EN |
92 * | 15 | SPI1 TX | UART0 RTS | I2C1 SCL | PWM7 B | SIO | PIO0 | PIO1 | | USB OVCUR DET |
93 * | 16 | SPI0 RX | UART0 TX | I2C0 SDA | PWM0 A | SIO | PIO0 | PIO1 | | USB VBUS DET |
94 * | 17 | SPI0 CSn | UART0 RX | I2C0 SCL | PWM0 B | SIO | PIO0 | PIO1 | | USB VBUS EN |
95 * | 18 | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM1 A | SIO | PIO0 | PIO1 | | USB OVCUR DET |
96 * | 19 | SPI0 TX | UART0 RTS | I2C1 SCL | PWM1 B | SIO | PIO0 | PIO1 | | USB VBUS DET |
97 * | 20 | SPI0 RX | UART1 TX | I2C0 SDA | PWM2 A | SIO | PIO0 | PIO1 | CLOCK GPIN0 | USB VBUS EN |
98 * | 21 | SPI0 CSn | UART1 RX | I2C0 SCL | PWM2 B | SIO | PIO0 | PIO1 | CLOCK GPOUT0 | USB OVCUR DET |
99 * | 22 | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM3 A | SIO | PIO0 | PIO1 | CLOCK GPIN1 | USB VBUS DET |
100 * | 23 | SPI0 TX | UART1 RTS | I2C1 SCL | PWM3 B | SIO | PIO0 | PIO1 | CLOCK GPOUT1 | USB VBUS EN |
101 * | 24 | SPI1 RX | UART1 TX | I2C0 SDA | PWM4 A | SIO | PIO0 | PIO1 | CLOCK GPOUT2 | USB OVCUR DET |
102 * | 25 | SPI1 CSn | UART1 RX | I2C0 SCL | PWM4 B | SIO | PIO0 | PIO1 | CLOCK GPOUT3 | USB VBUS DET |
103 * | 26 | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM5 A | SIO | PIO0 | PIO1 | | USB VBUS EN |
104 * | 27 | SPI1 TX | UART1 RTS | I2C1 SCL | PWM5 B | SIO | PIO0 | PIO1 | | USB OVCUR DET |
105 * | 28 | SPI1 RX | UART0 TX | I2C0 SDA | PWM6 A | SIO | PIO0 | PIO1 | | USB VBUS DET |
106 * | 29 | SPI1 CSn | UART0 RX | I2C0 SCL | PWM6 B | SIO | PIO0 | PIO1 | | USB VBUS EN |
107 * \endif
108 * \if rp2350_specific
109 * On RP2350 the function selects are:
110 *
111 * | GPIO | F0 | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 |
112 * |-------|------|----------|-----------|----------|--------|-----|------|------|------|--------------|---------------|----------|
113 * | 0 | | SPI0 RX | UART0 TX | I2C0 SDA | PWM0 A | SIO | PIO0 | PIO1 | PIO2 | XIP_CS1n | USB OVCUR DET | |
114 * | 1 | | SPI0 CSn | UART0 RX | I2C0 SCL | PWM0 B | SIO | PIO0 | PIO1 | PIO2 | TRACECLK | USB VBUS DET | |
115 * | 2 | | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM1 A | SIO | PIO0 | PIO1 | PIO2 | TRACEDATA0 | USB VBUS EN | UART0 TX |
116 * | 3 | | SPI0 TX | UART0 RTS | I2C1 SCL | PWM1 B | SIO | PIO0 | PIO1 | PIO2 | TRACEDATA1 | USB OVCUR DET | UART0 RX |
117 * | 4 | | SPI0 RX | UART1 TX | I2C0 SDA | PWM2 A | SIO | PIO0 | PIO1 | PIO2 | TRACEDATA2 | USB VBUS DET | |
118 * | 5 | | SPI0 CSn | UART1 RX | I2C0 SCL | PWM2 B | SIO | PIO0 | PIO1 | PIO2 | TRACEDATA3 | USB VBUS EN | |
119 * | 6 | | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM3 A | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | UART1 TX |
120 * | 7 | | SPI0 TX | UART1 RTS | I2C1 SCL | PWM3 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | UART1 RX |
121 * | 8 | | SPI1 RX | UART1 TX | I2C0 SDA | PWM4 A | SIO | PIO0 | PIO1 | PIO2 | XIP_CS1n | USB VBUS EN | |
122 * | 9 | | SPI1 CSn | UART1 RX | I2C0 SCL | PWM4 B | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | |
123 * | 10 | | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM5 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | UART1 TX |
124 * | 11 | | SPI1 TX | UART1 RTS | I2C1 SCL | PWM5 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | UART1 RX |
125 * | 12 | HSTX | SPI1 RX | UART0 TX | I2C0 SDA | PWM6 A | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPIN0 | USB OVCUR DET | |
126 * | 13 | HSTX | SPI1 CSn | UART0 RX | I2C0 SCL | PWM6 B | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPOUT0 | USB VBUS DET | |
127 * | 14 | HSTX | SPI1 SCK | UART0 CTS | I2C1 SDA | PWM7 A | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPIN1 | USB VBUS EN | UART0 TX |
128 * | 15 | HSTX | SPI1 TX | UART0 RTS | I2C1 SCL | PWM7 B | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPOUT1 | USB OVCUR DET | UART0 RX |
129 * | 16 | HSTX | SPI0 RX | UART0 TX | I2C0 SDA | PWM0 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | |
130 * | 17 | HSTX | SPI0 CSn | UART0 RX | I2C0 SCL | PWM0 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | |
131 * | 18 | HSTX | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM1 A | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | UART0 TX |
132 * | 19 | HSTX | SPI0 TX | UART0 RTS | I2C1 SCL | PWM1 B | SIO | PIO0 | PIO1 | PIO2 | XIP_CS1n | USB VBUS DET | UART0 RX |
133 * | 20 | | SPI0 RX | UART1 TX | I2C0 SDA | PWM2 A | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPIN0 | USB VBUS EN | |
134 * | 21 | | SPI0 CSn | UART1 RX | I2C0 SCL | PWM2 B | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPOUT0 | USB OVCUR DET | |
135 * | 22 | | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM3 A | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPIN1 | USB VBUS DET | UART1 TX |
136 * | 23 | | SPI0 TX | UART1 RTS | I2C1 SCL | PWM3 B | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPOUT1 | USB VBUS EN | UART1 RX |
137 * | 24 | | SPI1 RX | UART1 TX | I2C0 SDA | PWM4 A | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPOUT2 | USB OVCUR DET | |
138 * | 25 | | SPI1 CSn | UART1 RX | I2C0 SCL | PWM4 B | SIO | PIO0 | PIO1 | PIO2 | CLOCK GPOUT3 | USB VBUS DET | |
139 * | 26 | | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM5 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | UART1 TX |
140 * | 27 | | SPI1 TX | UART1 RTS | I2C1 SCL | PWM5 B | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | UART1 RX |
141 * | 28 | | SPI1 RX | UART0 TX | I2C0 SDA | PWM6 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | |
142 * | 29 | | SPI1 CSn | UART0 RX | I2C0 SCL | PWM6 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | |
143 *
144 * GPIOs 30 through 47 are QFN-80 only:
145 *
146 * | GPIO | F0 | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 | F11 |
147 * |------|----|----------|----------|-----------|---------|-----|------|------|------|----------|---------------|----------|
148 * | 30 | | SPI1 SCK | UART0 CTS | I2C1 SDA | PWM7 A | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | UART0 TX |
149 * | 31 | | SPI1 TX | UART0 RTS | I2C1 SCL | PWM7 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | UART0 RX |
150 * | 32 | | SPI0 RX | UART0 TX | I2C0 SDA | PWM8 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | |
151 * | 33 | | SPI0 CSn | UART0 RX | I2C0 SCL | PWM8 B | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | |
152 * | 34 | | SPI0 SCK | UART0 CTS | I2C1 SDA | PWM9 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | UART0 TX |
153 * | 35 | | SPI0 TX | UART0 RTS | I2C1 SCL | PWM9 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | UART0 RX |
154 * | 36 | | SPI0 RX | UART1 TX | I2C0 SDA | PWM10 A | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | |
155 * | 37 | | SPI0 CSn | UART1 RX | I2C0 SCL | PWM10 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | |
156 * | 38 | | SPI0 SCK | UART1 CTS | I2C1 SDA | PWM11 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | UART1 TX |
157 * | 39 | | SPI0 TX | UART1 RTS | I2C1 SCL | PWM11 B | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | UART1 RX |
158 * | 40 | | SPI1 RX | UART1 TX | I2C0 SDA | PWM8 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | |
159 * | 41 | | SPI1 CSn | UART1 RX | I2C0 SCL | PWM8 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | |
160 * | 42 | | SPI1 SCK | UART1 CTS | I2C1 SDA | PWM9 A | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | UART1 TX |
161 * | 43 | | SPI1 TX | UART1 RTS | I2C1 SCL | PWM9 B | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | UART1 RX |
162 * | 44 | | SPI1 RX | UART0 TX | I2C0 SDA | PWM10 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS EN | |
163 * | 45 | | SPI1 CSn | UART0 RX | I2C0 SCL | PWM10 B | SIO | PIO0 | PIO1 | PIO2 | | USB OVCUR DET | |
164 * | 46 | | SPI1 SCK | UART0 CTS | I2C1 SDA | PWM11 A | SIO | PIO0 | PIO1 | PIO2 | | USB VBUS DET | UART0 TX |
165 * | 47 | | SPI1 TX | UART0 RTS | I2C1 SCL | PWM11 B | SIO | PIO0 | PIO1 | PIO2 | XIP_CS1n | USB VBUS EN | UART0 RX |
166 *
167 * \endif
168 */
169
170 enum gpio_dir {
171 GPIO_OUT = 1u, ///< set GPIO to output
172 GPIO_IN = 0u, ///< set GPIO to input
173 };
174
175 /*! \brief GPIO Interrupt level definitions (GPIO events)
176 * \ingroup hardware_gpio
177 * \brief GPIO Interrupt levels
178 *
179 * An interrupt can be generated for every GPIO pin in 4 scenarios:
180 *
181 * * Level High: the GPIO pin is a logical 1
182 * * Level Low: the GPIO pin is a logical 0
183 * * Edge High: the GPIO has transitioned from a logical 0 to a logical 1
184 * * Edge Low: the GPIO has transitioned from a logical 1 to a logical 0
185 *
186 * The level interrupts are not latched. This means that if the pin is a logical 1 and the level high interrupt is active, it will
187 * become inactive as soon as the pin changes to a logical 0. The edge interrupts are stored in the INTR register and can be
188 * cleared by writing to the INTR register.
189 */
190 enum gpio_irq_level {
191 GPIO_IRQ_LEVEL_LOW = 0x1u, ///< IRQ when the GPIO pin is a logical 0
192 GPIO_IRQ_LEVEL_HIGH = 0x2u, ///< IRQ when the GPIO pin is a logical 1
193 GPIO_IRQ_EDGE_FALL = 0x4u, ///< IRQ when the GPIO has transitioned from a logical 1 to a logical 0
194 GPIO_IRQ_EDGE_RISE = 0x8u, ///< IRQ when the GPIO has transitioned from a logical 0 to a logical 1
195 };
196
197 /*! Callback function type for GPIO events
198 * \ingroup hardware_gpio
199 *
200 * \param gpio Which GPIO caused this interrupt
201 * \param event_mask Which events caused this interrupt. See \ref gpio_irq_level for details.
202 * \sa gpio_set_irq_enabled_with_callback()
203 * \sa gpio_set_irq_callback()
204 */
205 typedef void (*gpio_irq_callback_t)(uint gpio, uint32_t event_mask);
206
207 enum gpio_override {
208 GPIO_OVERRIDE_NORMAL = 0, ///< peripheral signal selected via \ref gpio_set_function
209 GPIO_OVERRIDE_INVERT = 1, ///< invert peripheral signal selected via \ref gpio_set_function
210 GPIO_OVERRIDE_LOW = 2, ///< drive low/disable output
211 GPIO_OVERRIDE_HIGH = 3, ///< drive high/enable output
212 };
213
214 /*! \brief Slew rate limiting levels for GPIO outputs
215 * \ingroup hardware_gpio
216 *
217 * Slew rate limiting increases the minimum rise/fall time when a GPIO output
218 * is lightly loaded, which can help to reduce electromagnetic emissions.
219 * \sa gpio_set_slew_rate
220 */
221 enum gpio_slew_rate {
222 GPIO_SLEW_RATE_SLOW = 0, ///< Slew rate limiting enabled
223 GPIO_SLEW_RATE_FAST = 1 ///< Slew rate limiting disabled
224 };
225
226 /*! \brief Drive strength levels for GPIO outputs
227 * \ingroup hardware_gpio
228 *
229 * Drive strength levels for GPIO outputs.
230 * \sa gpio_set_drive_strength
231 */
232 enum gpio_drive_strength {
233 GPIO_DRIVE_STRENGTH_2MA = 0, ///< 2 mA nominal drive strength
234 GPIO_DRIVE_STRENGTH_4MA = 1, ///< 4 mA nominal drive strength
235 GPIO_DRIVE_STRENGTH_8MA = 2, ///< 8 mA nominal drive strength
236 GPIO_DRIVE_STRENGTH_12MA = 3 ///< 12 mA nominal drive strength
237 };
238
check_gpio_param(__unused uint gpio)239 static inline void check_gpio_param(__unused uint gpio) {
240 invalid_params_if(HARDWARE_GPIO, gpio >= NUM_BANK0_GPIOS);
241 }
242
243 // ----------------------------------------------------------------------------
244 // Pad Controls + IO Muxing
245 // ----------------------------------------------------------------------------
246 // Declarations for gpio.c
247
248 /*! \brief Select GPIO function
249 * \ingroup hardware_gpio
250 *
251 * \param gpio GPIO number
252 * \param fn Which GPIO function select to use from list \ref gpio_function_t
253 */
254 void gpio_set_function(uint gpio, gpio_function_t fn);
255
256 /*! \brief Select the function for multiple GPIOs
257 * \ingroup hardware_gpio
258 *
259 * \sa gpio_set_function
260 * \param gpio_mask Mask with 1 bit per GPIO number to set the function for
261 * \param fn Which GPIO function select to use from list \ref gpio_function_t
262 */
263 void gpio_set_function_masked(uint32_t gpio_mask, gpio_function_t fn);
264
265 /*! \brief Select the function for multiple GPIOs
266 * \ingroup hardware_gpio
267 *
268 * \sa gpio_set_function
269 * \param gpio_mask Mask with 1 bit per GPIO number to set the function for
270 * \param fn Which GPIO function select to use from list \ref gpio_function_t
271 */
272 void gpio_set_function_masked64(uint64_t gpio_mask, gpio_function_t fn);
273
274 /*! \brief Determine current GPIO function
275 * \ingroup hardware_gpio
276 *
277 * \param gpio GPIO number
278 * \return Which GPIO function is currently selected from list \ref gpio_function_t
279 */
280 gpio_function_t gpio_get_function(uint gpio);
281
282 /*! \brief Select up and down pulls on specific GPIO
283 * \ingroup hardware_gpio
284 *
285 * \param gpio GPIO number
286 * \param up If true set a pull up on the GPIO
287 * \param down If true set a pull down on the GPIO
288 *
289 * \note On the RP2040, setting both pulls enables a "bus keep" function,
290 * i.e. a weak pull to whatever is current high/low state of GPIO.
291 */
292 void gpio_set_pulls(uint gpio, bool up, bool down);
293
294 /*! \brief Set specified GPIO to be pulled up.
295 * \ingroup hardware_gpio
296 *
297 * \param gpio GPIO number
298 */
gpio_pull_up(uint gpio)299 static inline void gpio_pull_up(uint gpio) {
300 gpio_set_pulls(gpio, true, false);
301 }
302
303 /*! \brief Determine if the specified GPIO is pulled up.
304 * \ingroup hardware_gpio
305 *
306 * \param gpio GPIO number
307 * \return true if the GPIO is pulled up
308 */
gpio_is_pulled_up(uint gpio)309 static inline bool gpio_is_pulled_up(uint gpio) {
310 return (pads_bank0_hw->io[gpio] & PADS_BANK0_GPIO0_PUE_BITS) != 0;
311 }
312
313 /*! \brief Set specified GPIO to be pulled down.
314 * \ingroup hardware_gpio
315 *
316 * \param gpio GPIO number
317 */
gpio_pull_down(uint gpio)318 static inline void gpio_pull_down(uint gpio) {
319 gpio_set_pulls(gpio, false, true);
320 }
321
322 /*! \brief Determine if the specified GPIO is pulled down.
323 * \ingroup hardware_gpio
324 *
325 * \param gpio GPIO number
326 * \return true if the GPIO is pulled down
327 */
gpio_is_pulled_down(uint gpio)328 static inline bool gpio_is_pulled_down(uint gpio) {
329 return (pads_bank0_hw->io[gpio] & PADS_BANK0_GPIO0_PDE_BITS) != 0;
330 }
331
332 /*! \brief Disable pulls on specified GPIO
333 * \ingroup hardware_gpio
334 *
335 * \param gpio GPIO number
336 */
gpio_disable_pulls(uint gpio)337 static inline void gpio_disable_pulls(uint gpio) {
338 gpio_set_pulls(gpio, false, false);
339 }
340
341 /*! \brief Set GPIO IRQ override
342 * \ingroup hardware_gpio
343 *
344 * Optionally invert a GPIO IRQ signal, or drive it high or low
345 *
346 * \param gpio GPIO number
347 * \param value See \ref gpio_override
348 */
349 void gpio_set_irqover(uint gpio, uint value);
350
351 /*! \brief Set GPIO output override
352 * \ingroup hardware_gpio
353 *
354 * \param gpio GPIO number
355 * \param value See \ref gpio_override
356 */
357 void gpio_set_outover(uint gpio, uint value);
358
359 /*! \brief Select GPIO input override
360 * \ingroup hardware_gpio
361 *
362 * \param gpio GPIO number
363 * \param value See \ref gpio_override
364 */
365 void gpio_set_inover(uint gpio, uint value);
366
367 /*! \brief Select GPIO output enable override
368 * \ingroup hardware_gpio
369 *
370 * \param gpio GPIO number
371 * \param value See \ref gpio_override
372 */
373 void gpio_set_oeover(uint gpio, uint value);
374
375 /*! \brief Enable GPIO input
376 * \ingroup hardware_gpio
377 *
378 * \param gpio GPIO number
379 * \param enabled true to enable input on specified GPIO
380 */
381 void gpio_set_input_enabled(uint gpio, bool enabled);
382
383 /*! \brief Enable/disable GPIO input hysteresis (Schmitt trigger)
384 * \ingroup hardware_gpio
385 *
386 * Enable or disable the Schmitt trigger hysteresis on a given GPIO. This is
387 * enabled on all GPIOs by default. Disabling input hysteresis can lead to
388 * inconsistent readings when the input signal has very long rise or fall
389 * times, but slightly reduces the GPIO's input delay.
390 *
391 * \sa gpio_is_input_hysteresis_enabled
392 * \param gpio GPIO number
393 * \param enabled true to enable input hysteresis on specified GPIO
394 */
395 void gpio_set_input_hysteresis_enabled(uint gpio, bool enabled);
396
397 /*! \brief Determine whether input hysteresis is enabled on a specified GPIO
398 * \ingroup hardware_gpio
399 *
400 * \sa gpio_set_input_hysteresis_enabled
401 * \param gpio GPIO number
402 */
403 bool gpio_is_input_hysteresis_enabled(uint gpio);
404
405 /*! \brief Set slew rate for a specified GPIO
406 * \ingroup hardware_gpio
407 *
408 * \sa gpio_get_slew_rate
409 * \param gpio GPIO number
410 * \param slew GPIO output slew rate
411 */
412 void gpio_set_slew_rate(uint gpio, enum gpio_slew_rate slew);
413
414 /*! \brief Determine current slew rate for a specified GPIO
415 * \ingroup hardware_gpio
416 *
417 * \sa gpio_set_slew_rate
418 * \param gpio GPIO number
419 * \return Current slew rate of that GPIO
420 */
421 enum gpio_slew_rate gpio_get_slew_rate(uint gpio);
422
423 /*! \brief Set drive strength for a specified GPIO
424 * \ingroup hardware_gpio
425 *
426 * \sa gpio_get_drive_strength
427 * \param gpio GPIO number
428 * \param drive GPIO output drive strength
429 */
430 void gpio_set_drive_strength(uint gpio, enum gpio_drive_strength drive);
431
432 /*! \brief Determine current drive strength for a specified GPIO
433 * \ingroup hardware_gpio
434 *
435 * \sa gpio_set_drive_strength
436 * \param gpio GPIO number
437 * \return Current drive strength of that GPIO
438 */
439 enum gpio_drive_strength gpio_get_drive_strength(uint gpio);
440
441 /*! \brief Enable or disable specific interrupt events for specified GPIO
442 * \ingroup hardware_gpio
443 *
444 * This function sets which GPIO events cause a GPIO interrupt on the calling core. See
445 * \ref gpio_set_irq_callback, \ref gpio_set_irq_enabled_with_callback and
446 * \ref gpio_add_raw_irq_handler to set up a GPIO interrupt handler to handle the events.
447 *
448 * \note The IO IRQs are independent per-processor. This configures the interrupt events for
449 * the processor that calls the function.
450 *
451 * \param gpio GPIO number
452 * \param event_mask Which events will cause an interrupt
453 * \param enabled Enable or disable flag
454 *
455 * Events is a bitmask of the following \ref gpio_irq_level values:
456 *
457 * bit | constant | interrupt
458 * ----|---------------------|------------------------------------
459 * 0 | GPIO_IRQ_LEVEL_LOW | Continuously while level is low
460 * 1 | GPIO_IRQ_LEVEL_HIGH | Continuously while level is high
461 * 2 | GPIO_IRQ_EDGE_FALL | On each transition from high to low
462 * 3 | GPIO_IRQ_EDGE_RISE | On each transition from low to high
463 *
464 * which are specified in \ref gpio_irq_level
465 */
466 void gpio_set_irq_enabled(uint gpio, uint32_t event_mask, bool enabled);
467
468 // PICO_CONFIG: GPIO_IRQ_CALLBACK_ORDER_PRIORITY, IRQ priority order of the default IRQ callback, min=0, max=255, default=PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY, group=hardware_gpio
469 #ifndef GPIO_IRQ_CALLBACK_ORDER_PRIORITY
470 #define GPIO_IRQ_CALLBACK_ORDER_PRIORITY PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY
471 #endif
472
473 // PICO_CONFIG: GPIO_RAW_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY, IRQ priority order of raw IRQ handlers if the priority is not specified, min=0, max=255, default=PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY, group=hardware_gpio
474 #ifndef GPIO_RAW_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY
475 #define GPIO_RAW_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY
476 #endif
477
478 /*! \brief Set the generic callback used for GPIO IRQ events for the current core
479 * \ingroup hardware_gpio
480 *
481 * This function sets the callback used for all GPIO IRQs on the current core that are not explicitly
482 * hooked via \ref gpio_add_raw_irq_handler or other gpio_add_raw_irq_handler_ functions.
483 *
484 * This function is called with the GPIO number and event mask for each of the (not explicitly hooked)
485 * GPIOs that have events enabled and that are pending (see \ref gpio_get_irq_event_mask).
486 *
487 * \note The IO IRQs are independent per-processor. This function affects
488 * the processor that calls the function.
489 *
490 * \param callback default user function to call on GPIO irq. Note only one of these can be set per processor.
491 */
492 void gpio_set_irq_callback(gpio_irq_callback_t callback);
493
494 /*! \brief Convenience function which performs multiple GPIO IRQ related initializations
495 * \ingroup hardware_gpio
496 *
497 * This method is a slightly eclectic mix of initialization, that:
498 *
499 * \li Updates whether the specified events for the specified GPIO causes an interrupt on the calling core based
500 * on the enable flag.
501 *
502 * \li Sets the callback handler for the calling core to callback (or clears the handler if the callback is NULL).
503 *
504 * \li Enables GPIO IRQs on the current core if enabled is true.
505 *
506 * This method is commonly used to perform a one time setup, and following that any additional IRQs/events are enabled
507 * via \ref gpio_set_irq_enabled. All GPIOs/events added in this way on the same core share the same callback; for multiple
508 * independent handlers for different GPIOs you should use \ref gpio_add_raw_irq_handler and related functions.
509 *
510 * This method is equivalent to:
511 *
512 * \code{.c}
513 * gpio_set_irq_enabled(gpio, event_mask, enabled);
514 * gpio_set_irq_callback(callback);
515 * if (enabled) irq_set_enabled(IO_IRQ_BANK0, true);
516 * \endcode
517 *
518 * \note The IO IRQs are independent per-processor. This method affects only the processor that calls the function.
519 *
520 * \param gpio GPIO number
521 * \param event_mask Which events will cause an interrupt. See \ref gpio_irq_level for details.
522 * \param enabled Enable or disable flag
523 * \param callback user function to call on GPIO irq. if NULL, the callback is removed
524 */
525 void gpio_set_irq_enabled_with_callback(uint gpio, uint32_t event_mask, bool enabled, gpio_irq_callback_t callback);
526
527 /*! \brief Enable dormant wake up interrupt for specified GPIO and events
528 * \ingroup hardware_gpio
529 *
530 * This configures IRQs to restart the XOSC or ROSC when they are
531 * disabled in dormant mode
532 *
533 * \param gpio GPIO number
534 * \param event_mask Which events will cause an interrupt. See \ref gpio_irq_level for details.
535 * \param enabled Enable/disable flag
536 */
537 void gpio_set_dormant_irq_enabled(uint gpio, uint32_t event_mask, bool enabled);
538
539 /*! \brief Return the current interrupt status (pending events) for the given GPIO
540 * \ingroup hardware_gpio
541 *
542 * \param gpio GPIO number
543 * \return Bitmask of events that are currently pending for the GPIO. See \ref gpio_irq_level for details.
544 * \sa gpio_acknowledge_irq
545 */
gpio_get_irq_event_mask(uint gpio)546 static inline uint32_t gpio_get_irq_event_mask(uint gpio) {
547 check_gpio_param(gpio);
548 io_bank0_irq_ctrl_hw_t *irq_ctrl_base = get_core_num() ?
549 &io_bank0_hw->proc1_irq_ctrl : &io_bank0_hw->proc0_irq_ctrl;
550 io_ro_32 *status_reg = &irq_ctrl_base->ints[gpio >> 3u];
551 return (*status_reg >> (4 * (gpio & 7u))) & 0xfu;
552 }
553
554 /*! \brief Acknowledge a GPIO interrupt for the specified events on the calling core
555 * \ingroup hardware_gpio
556 *
557 * \note This may be called with a mask of any of valid bits specified in \ref gpio_irq_level, however
558 * it has no effect on \a level sensitive interrupts which remain pending while the GPIO is at the specified
559 * level. When handling \a level sensitive interrupts, you should generally disable the interrupt (see
560 * \ref gpio_set_irq_enabled) and then set it up again later once the GPIO level has changed (or to catch
561 * the opposite level).
562 *
563 * \param gpio GPIO number
564 *
565 * \note For callbacks set with \ref gpio_set_irq_enabled_with_callback, or \ref gpio_set_irq_callback, this function is called automatically.
566 * \param event_mask Bitmask of events to clear. See \ref gpio_irq_level for details.
567 */
568 void gpio_acknowledge_irq(uint gpio, uint32_t event_mask);
569
570 /*! \brief Adds a raw GPIO IRQ handler for the specified GPIOs on the current core
571 * \ingroup hardware_gpio
572 *
573 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
574 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default callback. The order
575 * relative to the default callback can be controlled via the order_priority parameter (the default callback has the priority
576 * \ref GPIO_IRQ_CALLBACK_ORDER_PRIORITY which defaults to the lowest priority with the intention of it running last).
577 *
578 * This method adds such an explicit GPIO IRQ handler, and disables the "default" callback for the specified GPIOs.
579 *
580 * \note Multiple raw handlers should not be added for the same GPIOs, and this method will assert if you attempt to.
581 * Internally, this function calls \ref irq_add_shared_handler, which will assert if the maximum number of shared handlers
582 * (configurable via PICO_MAX_IRQ_SHARED_HANDLERS) would be exceeded.
583 *
584 * A raw handler should check for whichever GPIOs and events it handles, and acknowledge them itself; it might look something like:
585 *
586 * \code{.c}
587 * void my_irq_handler(void) {
588 * if (gpio_get_irq_event_mask(my_gpio_num) & my_gpio_event_mask) {
589 * gpio_acknowledge_irq(my_gpio_num, my_gpio_event_mask);
590 * // handle the IRQ
591 * }
592 * if (gpio_get_irq_event_mask(my_gpio_num2) & my_gpio_event_mask2) {
593 * gpio_acknowledge_irq(my_gpio_num2, my_gpio_event_mask2);
594 * // handle the IRQ
595 * }
596 * }
597 * \endcode
598 *
599 * @param gpio_mask a bit mask of the GPIO numbers that will no longer be passed to the default callback for this core
600 * @param handler the handler to add to the list of GPIO IRQ handlers for this core
601 * @param order_priority the priority order to determine the relative position of the handler in the list of GPIO IRQ handlers for this core.
602 */
603 void gpio_add_raw_irq_handler_with_order_priority_masked(uint32_t gpio_mask, irq_handler_t handler, uint8_t order_priority);
604
605 /*! \brief Adds a raw GPIO IRQ handler for the specified GPIOs on the current core
606 * \ingroup hardware_gpio
607 *
608 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
609 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default callback. The order
610 * relative to the default callback can be controlled via the order_priority parameter (the default callback has the priority
611 * \ref GPIO_IRQ_CALLBACK_ORDER_PRIORITY which defaults to the lowest priority with the intention of it running last).
612 *
613 * This method adds such an explicit GPIO IRQ handler, and disables the "default" callback for the specified GPIOs.
614 *
615 * \note Multiple raw handlers should not be added for the same GPIOs, and this method will assert if you attempt to.
616 * Internally, this function calls \ref irq_add_shared_handler, which will assert if the maximum number of shared handlers
617 * (configurable via PICO_MAX_IRQ_SHARED_HANDLERS) would be exceeded.
618 *
619 * A raw handler should check for whichever GPIOs and events it handles, and acknowledge them itself; it might look something like:
620 *
621 * \code{.c}
622 * void my_irq_handler(void) {
623 * if (gpio_get_irq_event_mask(my_gpio_num) & my_gpio_event_mask) {
624 * gpio_acknowledge_irq(my_gpio_num, my_gpio_event_mask);
625 * // handle the IRQ
626 * }
627 * if (gpio_get_irq_event_mask(my_gpio_num2) & my_gpio_event_mask2) {
628 * gpio_acknowledge_irq(my_gpio_num2, my_gpio_event_mask2);
629 * // handle the IRQ
630 * }
631 * }
632 * \endcode
633 *
634 * @param gpio_mask a bit mask of the GPIO numbers that will no longer be passed to the default callback for this core
635 * @param handler the handler to add to the list of GPIO IRQ handlers for this core
636 * @param order_priority the priority order to determine the relative position of the handler in the list of GPIO IRQ handlers for this core.
637 */
638 void gpio_add_raw_irq_handler_with_order_priority_masked64(uint64_t gpio_mask, irq_handler_t handler, uint8_t order_priority);
639
640 /*! \brief Adds a raw GPIO IRQ handler for a specific GPIO on the current core
641 * \ingroup hardware_gpio
642 *
643 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
644 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default callback. The order
645 * relative to the default callback can be controlled via the order_priority parameter(the default callback has the priority
646 * \ref GPIO_IRQ_CALLBACK_ORDER_PRIORITY which defaults to the lowest priority with the intention of it running last).
647 *
648 * This method adds such a callback, and disables the "default" callback for the specified GPIO.
649 *
650 * \note Multiple raw handlers should not be added for the same GPIO, and this method will assert if you attempt to.
651 * Internally, this function calls \ref irq_add_shared_handler, which will assert if the maximum number of shared handlers
652 * (configurable via PICO_MAX_IRQ_SHARED_HANDLERS) would be exceeded.
653 *
654 * A raw handler should check for whichever GPIOs and events it handles, and acknowledge them itself; it might look something like:
655 *
656 * \code{.c}
657 * void my_irq_handler(void) {
658 * if (gpio_get_irq_event_mask(my_gpio_num) & my_gpio_event_mask) {
659 * gpio_acknowledge_irq(my_gpio_num, my_gpio_event_mask);
660 * // handle the IRQ
661 * }
662 * }
663 * \endcode
664 *
665 * @param gpio the GPIO number that will no longer be passed to the default callback for this core
666 * @param handler the handler to add to the list of GPIO IRQ handlers for this core
667 * @param order_priority the priority order to determine the relative position of the handler in the list of GPIO IRQ handlers for this core.
668 */
gpio_add_raw_irq_handler_with_order_priority(uint gpio,irq_handler_t handler,uint8_t order_priority)669 static inline void gpio_add_raw_irq_handler_with_order_priority(uint gpio, irq_handler_t handler, uint8_t order_priority) {
670 check_gpio_param(gpio);
671 #if NUM_BANK0_GPIOS > 32
672 gpio_add_raw_irq_handler_with_order_priority_masked64(1ull << gpio, handler, order_priority);
673 #else
674 gpio_add_raw_irq_handler_with_order_priority_masked(1u << gpio, handler, order_priority);
675 #endif
676 }
677
678 /*! \brief Adds a raw GPIO IRQ handler for the specified GPIOs on the current core
679 * \ingroup hardware_gpio
680 *
681 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
682 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default event callback.
683 *
684 * This method adds such a callback, and disables the "default" callback for the specified GPIOs.
685 *
686 * \note Multiple raw handlers should not be added for the same GPIOs, and this method will assert if you attempt to.
687 * Internally, this function calls \ref irq_add_shared_handler, which will assert if the maximum number of shared handlers
688 * (configurable via PICO_MAX_IRQ_SHARED_HANDLERS) would be exceeded.
689 *
690 * A raw handler should check for whichever GPIOs and events it handles, and acknowledge them itself; it might look something like:
691 *
692 * \code{.c}
693 * void my_irq_handler(void) {
694 * if (gpio_get_irq_event_mask(my_gpio_num) & my_gpio_event_mask) {
695 * gpio_acknowledge_irq(my_gpio_num, my_gpio_event_mask);
696 * // handle the IRQ
697 * }
698 * if (gpio_get_irq_event_mask(my_gpio_num2) & my_gpio_event_mask2) {
699 * gpio_acknowledge_irq(my_gpio_num2, my_gpio_event_mask2);
700 * // handle the IRQ
701 * }
702 * }
703 * \endcode
704 *
705 * @param gpio_mask a bit mask of the GPIO numbers that will no longer be passed to the default callback for this core
706 * @param handler the handler to add to the list of GPIO IRQ handlers for this core
707 */
708 void gpio_add_raw_irq_handler_masked(uint32_t gpio_mask, irq_handler_t handler);
709
710 /*! \brief Adds a raw GPIO IRQ handler for the specified GPIOs on the current core
711 * \ingroup hardware_gpio
712 *
713 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
714 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default event callback.
715 *
716 * This method adds such a callback, and disables the "default" callback for the specified GPIOs.
717 *
718 * \note Multiple raw handlers should not be added for the same GPIOs, and this method will assert if you attempt to.
719 * Internally, this function calls \ref irq_add_shared_handler, which will assert if the maximum number of shared handlers
720 * (configurable via PICO_MAX_IRQ_SHARED_HANDLERS) would be exceeded.
721 *
722 * A raw handler should check for whichever GPIOs and events it handles, and acknowledge them itself; it might look something like:
723 *
724 * \code{.c}
725 * void my_irq_handler(void) {
726 * if (gpio_get_irq_event_mask(my_gpio_num) & my_gpio_event_mask) {
727 * gpio_acknowledge_irq(my_gpio_num, my_gpio_event_mask);
728 * // handle the IRQ
729 * }
730 * if (gpio_get_irq_event_mask(my_gpio_num2) & my_gpio_event_mask2) {
731 * gpio_acknowledge_irq(my_gpio_num2, my_gpio_event_mask2);
732 * // handle the IRQ
733 * }
734 * }
735 * \endcode
736 *
737 * @param gpio_mask a 64 bit mask of the GPIO numbers that will no longer be passed to the default callback for this core
738 * @param handler the handler to add to the list of GPIO IRQ handlers for this core
739 */
740 void gpio_add_raw_irq_handler_masked64(uint64_t gpio_mask, irq_handler_t handler);
741
742 /*! \brief Adds a raw GPIO IRQ handler for a specific GPIO on the current core
743 * \ingroup hardware_gpio
744 *
745 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
746 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default event callback.
747 *
748 * This method adds such a callback, and disables the "default" callback for the specified GPIO.
749 *
750 * \note Multiple raw handlers should not be added for the same GPIO, and this method will assert if you attempt to.
751 * Internally, this function calls \ref irq_add_shared_handler, which will assert if the maximum number of shared handlers
752 * (configurable via PICO_MAX_IRQ_SHARED_HANDLERS) would be exceeded.
753 *
754 * A raw handler should check for whichever GPIOs and events it handles, and acknowledge them itself; it might look something like:
755 *
756 * \code{.c}
757 * void my_irq_handler(void) {
758 * if (gpio_get_irq_event_mask(my_gpio_num) & my_gpio_event_mask) {
759 * gpio_acknowledge_irq(my_gpio_num, my_gpio_event_mask);
760 * // handle the IRQ
761 * }
762 * }
763 * \endcode
764 *
765 * @param gpio the GPIO number that will no longer be passed to the default callback for this core
766 * @param handler the handler to add to the list of GPIO IRQ handlers for this core
767 */
gpio_add_raw_irq_handler(uint gpio,irq_handler_t handler)768 static inline void gpio_add_raw_irq_handler(uint gpio, irq_handler_t handler) {
769 check_gpio_param(gpio);
770 #if NUM_BANK0_GPIOS > 32
771 gpio_add_raw_irq_handler_masked64(1ull << gpio, handler);
772 #else
773 gpio_add_raw_irq_handler_masked(1u << gpio, handler);
774 #endif
775 }
776
777 /*! \brief Removes a raw GPIO IRQ handler for the specified GPIOs on the current core
778 * \ingroup hardware_gpio
779 *
780 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
781 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default event callback.
782 *
783 * This method removes such a callback, and enables the "default" callback for the specified GPIOs.
784 *
785 * @param gpio_mask a bit mask of the GPIO numbers that will now be passed to the default callback for this core
786 * @param handler the handler to remove from the list of GPIO IRQ handlers for this core
787 */
788 void gpio_remove_raw_irq_handler_masked(uint32_t gpio_mask, irq_handler_t handler);
789
790 /*! \brief Removes a raw GPIO IRQ handler for the specified GPIOs on the current core
791 * \ingroup hardware_gpio
792 *
793 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
794 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default event callback.
795 *
796 * This method removes such a callback, and enables the "default" callback for the specified GPIOs.
797 *
798 * @param gpio_mask a bit mask of the GPIO numbers that will now be passed to the default callback for this core
799 * @param handler the handler to remove from the list of GPIO IRQ handlers for this core
800 */
801 void gpio_remove_raw_irq_handler_masked64(uint64_t gpio_mask, irq_handler_t handler);
802
803 /*! \brief Removes a raw GPIO IRQ handler for the specified GPIO on the current core
804 * \ingroup hardware_gpio
805 *
806 * In addition to the default mechanism of a single GPIO IRQ event callback per core (see \ref gpio_set_irq_callback),
807 * it is possible to add explicit GPIO IRQ handlers which are called independent of the default event callback.
808 *
809 * This method removes such a callback, and enables the "default" callback for the specified GPIO.
810 *
811 * @param gpio the GPIO number that will now be passed to the default callback for this core
812 * @param handler the handler to remove from the list of GPIO IRQ handlers for this core
813 */
gpio_remove_raw_irq_handler(uint gpio,irq_handler_t handler)814 static inline void gpio_remove_raw_irq_handler(uint gpio, irq_handler_t handler) {
815 check_gpio_param(gpio);
816 #if NUM_BANK0_GPIOS > 32
817 gpio_remove_raw_irq_handler_masked64(1ull << gpio, handler);
818 #else
819 gpio_remove_raw_irq_handler_masked(1u << gpio, handler);
820 #endif
821 }
822
823 /*! \brief Initialise a GPIO for (enabled I/O and set func to GPIO_FUNC_SIO)
824 * \ingroup hardware_gpio
825 *
826 * Clear the output enable (i.e. set to input).
827 * Clear any output value.
828 *
829 * \param gpio GPIO number
830 */
831 void gpio_init(uint gpio);
832
833 /*! \brief Resets a GPIO back to the NULL function, i.e. disables it.
834 * \ingroup hardware_gpio
835 *
836 * \param gpio GPIO number
837 */
838 void gpio_deinit(uint gpio);
839
840 /*! \brief Initialise multiple GPIOs (enabled I/O and set func to GPIO_FUNC_SIO)
841 * \ingroup hardware_gpio
842 *
843 * Clear the output enable (i.e. set to input).
844 * Clear any output value.
845 *
846 * \param gpio_mask Mask with 1 bit per GPIO number to initialize
847 */
848 void gpio_init_mask(uint gpio_mask);
849 // ----------------------------------------------------------------------------
850 // Input
851 // ----------------------------------------------------------------------------
852
853 /*! \brief Get state of a single specified GPIO
854 * \ingroup hardware_gpio
855 *
856 * \param gpio GPIO number
857 * \return Current state of the GPIO. 0 for low, non-zero for high
858 */
gpio_get(uint gpio)859 static inline bool gpio_get(uint gpio) {
860 #if NUM_BANK0_GPIOS <= 32
861 return sio_hw->gpio_in & (1u << gpio);
862 #else
863 if (gpio < 32) {
864 return sio_hw->gpio_in & (1u << gpio);
865 } else {
866 return sio_hw->gpio_hi_in & (1u << (gpio - 32));
867 }
868 #endif
869 }
870
871 /*! \brief Get raw value of all GPIOs
872 * \ingroup hardware_gpio
873 *
874 * \return Bitmask of raw GPIO values
875 */
gpio_get_all(void)876 static inline uint32_t gpio_get_all(void) {
877 #if PICO_USE_GPIO_COPROCESSOR
878 return gpioc_lo_in_get();
879 #else
880 return sio_hw->gpio_in;
881 #endif
882 }
883
884 /*! \brief Get raw value of all GPIOs
885 * \ingroup hardware_gpio
886 *
887 * \return Bitmask of raw GPIO values
888 */
gpio_get_all64(void)889 static inline uint64_t gpio_get_all64(void) {
890 #if PICO_USE_GPIO_COPROCESSOR
891 return gpioc_hilo_in_get();
892 #elif NUM_BANK0_GPIOS <= 32
893 return sio_hw->gpio_in;
894 #else
895 return sio_hw->gpio_in | (((uint64_t)sio_hw->gpio_hi_in) << 32u);
896 #endif
897 }
898
899 // ----------------------------------------------------------------------------
900 // Output
901 // ----------------------------------------------------------------------------
902
903 /*! \brief Drive high every GPIO appearing in mask
904 * \ingroup hardware_gpio
905 *
906 * \param mask Bitmask of GPIO values to set
907 */
gpio_set_mask(uint32_t mask)908 static inline void gpio_set_mask(uint32_t mask) {
909 #if PICO_USE_GPIO_COPROCESSOR
910 gpioc_lo_out_set(mask);
911 #else
912 sio_hw->gpio_set = mask;
913 #endif
914 }
915
916 /*! \brief Drive high every GPIO appearing in mask
917 * \ingroup hardware_gpio
918 *
919 * \param mask Bitmask of GPIO values to set
920 */
gpio_set_mask64(uint64_t mask)921 static inline void gpio_set_mask64(uint64_t mask) {
922 #if PICO_USE_GPIO_COPROCESSOR
923 gpioc_hilo_out_set(mask);
924 #elif NUM_BANK0_GPIOS <= 32
925 sio_hw->gpio_set = (uint32_t)mask;
926 #else
927 sio_hw->gpio_set = (uint32_t)mask;
928 sio_hw->gpio_hi_set = (uint32_t)(mask >> 32u);
929 #endif
930 }
931
932 /*! \brief Drive high every GPIO appearing in mask
933 * \ingroup hardware_gpio
934 *
935 * \param n the base GPIO index of the mask to update. n == 0 means 0->31, n == 1 mean 32->63 etc.
936 * \param mask Bitmask of 32 GPIO values to set
937 */
gpio_set_mask_n(uint n,uint32_t mask)938 static inline void gpio_set_mask_n(uint n, uint32_t mask) {
939 if (!n) {
940 gpio_set_mask(mask);
941 } else if (n == 1) {
942 #if PICO_USE_GPIO_COPROCESSOR
943 gpioc_hi_out_set(mask);
944 #elif NUM_BANK0_GPIOS >= 32
945 sio_hw->gpio_hi_set = mask;
946 #endif
947 }
948 }
949
950 /*! \brief Drive low every GPIO appearing in mask
951 * \ingroup hardware_gpio
952 *
953 * \param mask Bitmask of GPIO values to clear
954 */
gpio_clr_mask(uint32_t mask)955 static inline void gpio_clr_mask(uint32_t mask) {
956 #if PICO_USE_GPIO_COPROCESSOR
957 gpioc_lo_out_clr(mask);
958 #else
959 sio_hw->gpio_clr = mask;
960 #endif
961 }
962
963 /*! \brief Drive low every GPIO appearing in mask
964 * \ingroup hardware_gpio
965 *
966 * \param mask Bitmask of GPIO values to clear
967 */
gpio_clr_mask64(uint64_t mask)968 static inline void gpio_clr_mask64(uint64_t mask) {
969 #if PICO_USE_GPIO_COPROCESSOR
970 gpioc_hilo_out_clr(mask);
971 #elif NUM_BANK0_GPIOS <= 32
972 sio_hw->gpio_clr = (uint32_t)mask;
973 #else
974 sio_hw->gpio_clr = (uint32_t)mask;
975 sio_hw->gpio_hi_clr = (uint32_t)(mask >> 32u);
976 #endif
977 }
978
979
980 /*! \brief Drive low every GPIO appearing in mask
981 * \ingroup hardware_gpio
982 *
983 * \param n the base GPIO index of the mask to update. n == 0 means 0->31, n == 1 mean 32->63 etc.
984 * \param mask Bitmask of 32 GPIO values to clear
985 */
gpio_clr_mask_n(uint n,uint32_t mask)986 static inline void gpio_clr_mask_n(uint n, uint32_t mask) {
987 if (!n) {
988 gpio_clr_mask(mask);
989 } else if (n == 1) {
990 #if PICO_USE_GPIO_COPROCESSOR
991 gpioc_hi_out_clr(mask);
992 #elif NUM_BANK0_GPIOS >= 32
993 sio_hw->gpio_hi_clr = mask;
994 #endif
995 }
996 }
997
998 /*! \brief Toggle every GPIO appearing in mask
999 * \ingroup hardware_gpio
1000 *
1001 * \param mask Bitmask of GPIO values to toggle
1002 */
gpio_xor_mask(uint32_t mask)1003 static inline void gpio_xor_mask(uint32_t mask) {
1004 #if PICO_USE_GPIO_COPROCESSOR
1005 gpioc_lo_out_xor(mask);
1006 #else
1007 sio_hw->gpio_togl = mask;
1008 #endif
1009 }
1010
1011 /*! \brief Toggle every GPIO appearing in mask
1012 * \ingroup hardware_gpio
1013 *
1014 * \param mask Bitmask of GPIO values to toggle
1015 */
gpio_xor_mask64(uint64_t mask)1016 static inline void gpio_xor_mask64(uint64_t mask) {
1017 #if PICO_USE_GPIO_COPROCESSOR
1018 gpioc_hilo_out_xor(mask);
1019 #elif NUM_BANK0_GPIOS <= 32
1020 sio_hw->gpio_togl = (uint32_t)mask;
1021 #else
1022 sio_hw->gpio_togl = (uint32_t)mask;
1023 sio_hw->gpio_hi_togl = (uint32_t)(mask >> 32u);
1024 #endif
1025 }
1026
1027 /*! \brief Toggle every GPIO appearing in mask
1028 * \ingroup hardware_gpio
1029 *
1030 * \param n the base GPIO index of the mask to update. n == 0 means 0->31, n == 1 mean 32->63 etc.
1031 * \param mask Bitmask of 32 GPIO values to toggle
1032 */
gpio_xor_mask_n(uint n,uint32_t mask)1033 static inline void gpio_xor_mask_n(uint n, uint32_t mask) {
1034 if (!n) {
1035 gpio_xor_mask(mask);
1036 } else if (n == 1) {
1037 #if PICO_USE_GPIO_COPROCESSOR
1038 gpioc_hi_out_xor(mask);
1039 #elif NUM_BANK0_GPIOS >= 32
1040 sio_hw->gpio_hi_togl = mask;
1041 #endif
1042 }
1043 }
1044
1045 /*! \brief Drive GPIOs high/low depending on parameters
1046 * \ingroup hardware_gpio
1047 *
1048 * \param mask Bitmask of GPIO values to change
1049 * \param value Value to set
1050 *
1051 * For each 1 bit in \p mask, drive that pin to the value given by
1052 * corresponding bit in \p value, leaving other pins unchanged.
1053 * Since this uses the TOGL alias, it is concurrency-safe with e.g. an IRQ
1054 * bashing different pins from the same core.
1055 */
gpio_put_masked(uint32_t mask,uint32_t value)1056 static inline void gpio_put_masked(uint32_t mask, uint32_t value) {
1057 #if PICO_USE_GPIO_COPROCESSOR
1058 gpioc_lo_out_xor((gpioc_lo_out_get() ^ value) & mask);
1059 #else
1060 sio_hw->gpio_togl = (sio_hw->gpio_out ^ value) & mask;
1061 #endif
1062 }
1063
1064 /*! \brief Drive GPIOs high/low depending on parameters
1065 * \ingroup hardware_gpio
1066 *
1067 * \param mask Bitmask of GPIO values to change
1068 * \param value Value to set
1069 *
1070 * For each 1 bit in \p mask, drive that pin to the value given by
1071 * corresponding bit in \p value, leaving other pins unchanged.
1072 * Since this uses the TOGL alias, it is concurrency-safe with e.g. an IRQ
1073 * bashing different pins from the same core.
1074 */
gpio_put_masked64(uint64_t mask,uint64_t value)1075 static inline void gpio_put_masked64(uint64_t mask, uint64_t value) {
1076 #if PICO_USE_GPIO_COPROCESSOR
1077 gpioc_hilo_out_xor((gpioc_hilo_out_get() ^ value) & mask);
1078 #elif NUM_BANK0_GPIOS <= 32
1079 sio_hw->gpio_togl = (sio_hw->gpio_out ^ (uint32_t)value) & (uint32_t)mask;
1080 #else
1081 sio_hw->gpio_togl = (sio_hw->gpio_out ^ (uint32_t)value) & (uint32_t)mask;
1082 sio_hw->gpio_hi_togl = (sio_hw->gpio_hi_out ^ (uint32_t)(value>>32u)) & (uint32_t)(mask>>32u);
1083 #endif
1084 }
1085
1086 /*! \brief Drive GPIOs high/low depending on parameters
1087 * \ingroup hardware_gpio
1088 *
1089 * \param n the base GPIO index of the mask to update. n == 0 means 0->31, n == 1 mean 32->63 etc.
1090 * \param mask Bitmask of GPIO values to change
1091 * \param value Value to set
1092 *
1093 * For each 1 bit in \p mask, drive that pin to the value given by
1094 * corresponding bit in \p value, leaving other pins unchanged.
1095 * Since this uses the TOGL alias, it is concurrency-safe with e.g. an IRQ
1096 * bashing different pins from the same core.
1097 */
gpio_put_masked_n(uint n,uint32_t mask,uint32_t value)1098 static inline void gpio_put_masked_n(uint n, uint32_t mask, uint32_t value) {
1099 if (!n) {
1100 gpio_put_masked(mask, value);
1101 } else if (n == 1) {
1102 #if PICO_USE_GPIO_COPROCESSOR
1103 gpioc_hi_out_xor((gpioc_hi_out_get() ^ value) & mask);
1104 #else
1105 sio_hw->gpio_hi_togl = (sio_hw->gpio_hi_out ^ value) & mask;
1106 #endif
1107 }
1108 }
1109
1110 /*! \brief Drive all pins simultaneously
1111 * \ingroup hardware_gpio
1112 *
1113 * \param value Bitmask of GPIO values to change
1114 */
gpio_put_all(uint32_t value)1115 static inline void gpio_put_all(uint32_t value) {
1116 #if PICO_USE_GPIO_COPROCESSOR
1117 gpioc_lo_out_put(value);
1118 #else
1119 sio_hw->gpio_out = value;
1120 #endif
1121 }
1122
1123 /*! \brief Drive all pins simultaneously
1124 * \ingroup hardware_gpio
1125 *
1126 * \param value Bitmask of GPIO values to change
1127 */
gpio_put_all64(uint64_t value)1128 static inline void gpio_put_all64(uint64_t value) {
1129 #if PICO_USE_GPIO_COPROCESSOR
1130 gpioc_hilo_out_put(value);
1131 #elif NUM_BANK0_GPIOS <= 32
1132 sio_hw->gpio_out = (uint32_t)value;
1133 #else
1134 sio_hw->gpio_out = (uint32_t)value;
1135 sio_hw->gpio_hi_out = (uint32_t)(value >> 32u);
1136 #endif
1137 }
1138
1139 /*! \brief Drive a single GPIO high/low
1140 * \ingroup hardware_gpio
1141 *
1142 * \param gpio GPIO number
1143 * \param value If false clear the GPIO, otherwise set it.
1144 */
gpio_put(uint gpio,bool value)1145 static inline void gpio_put(uint gpio, bool value) {
1146 #if PICO_USE_GPIO_COPROCESSOR
1147 gpioc_bit_out_put(gpio, value);
1148 #elif NUM_BANK0_GPIOS <= 32
1149 uint32_t mask = 1ul << gpio;
1150 if (value)
1151 gpio_set_mask(mask);
1152 else
1153 gpio_clr_mask(mask);
1154 #else
1155 uint32_t mask = 1ul << (gpio & 0x1fu);
1156 if (gpio < 32) {
1157 if (value) {
1158 sio_hw->gpio_set = mask;
1159 } else {
1160 sio_hw->gpio_clr = mask;
1161 }
1162 } else {
1163 if (value) {
1164 sio_hw->gpio_hi_set = mask;
1165 } else {
1166 sio_hw->gpio_hi_clr = mask;
1167 }
1168 }
1169 #endif
1170 }
1171
1172 /*! \brief Determine whether a GPIO is currently driven high or low
1173 * \ingroup hardware_gpio
1174 *
1175 * This function returns the high/low output level most recently assigned to a
1176 * GPIO via gpio_put() or similar. This is the value that is presented outward
1177 * to the IO muxing, *not* the input level back from the pad (which can be
1178 * read using gpio_get()).
1179 *
1180 * To avoid races, this function must not be used for read-modify-write
1181 * sequences when driving GPIOs -- instead functions like gpio_put() should be
1182 * used to atomically update GPIOs. This accessor is intended for debug use
1183 * only.
1184 *
1185 * \param gpio GPIO number
1186 * \return true if the GPIO output level is high, false if low.
1187 */
gpio_get_out_level(uint gpio)1188 static inline bool gpio_get_out_level(uint gpio) {
1189 #if NUM_BANK0_GPIOS <= 32
1190 return sio_hw->gpio_out & (1u << gpio);
1191 #else
1192 uint32_t bits = gpio < 32 ? sio_hw->gpio_out : sio_hw->gpio_hi_out;
1193 return bits & (1u << (gpio & 0x1fu));
1194 #endif
1195 }
1196
1197 // ----------------------------------------------------------------------------
1198 // Direction
1199 // ----------------------------------------------------------------------------
1200
1201 /*! \brief Set a number of GPIOs to output
1202 * \ingroup hardware_gpio
1203 *
1204 * Switch all GPIOs in "mask" to output
1205 *
1206 * \param mask Bitmask of GPIO to set to output
1207 */
gpio_set_dir_out_masked(uint32_t mask)1208 static inline void gpio_set_dir_out_masked(uint32_t mask) {
1209 #if PICO_USE_GPIO_COPROCESSOR
1210 gpioc_lo_oe_set(mask);
1211 #else
1212 sio_hw->gpio_oe_set = mask;
1213 #endif
1214 }
1215
1216 /*! \brief Set a number of GPIOs to output
1217 * \ingroup hardware_gpio
1218 *
1219 * Switch all GPIOs in "mask" to output
1220 *
1221 * \param mask Bitmask of GPIO to set to output
1222 */
gpio_set_dir_out_masked64(uint64_t mask)1223 static inline void gpio_set_dir_out_masked64(uint64_t mask) {
1224 #if PICO_USE_GPIO_COPROCESSOR
1225 gpioc_hilo_oe_set(mask);
1226 #elif NUM_BANK0_GPIOS <= 32
1227 sio_hw->gpio_oe_set = mask;
1228 #else
1229 sio_hw->gpio_oe_set = (uint32_t)mask;
1230 sio_hw->gpio_hi_oe_set = (uint32_t)(mask >> 32u);
1231 #endif
1232 }
1233
1234 /*! \brief Set a number of GPIOs to input
1235 * \ingroup hardware_gpio
1236 *
1237 * \param mask Bitmask of GPIO to set to input
1238 */
gpio_set_dir_in_masked(uint32_t mask)1239 static inline void gpio_set_dir_in_masked(uint32_t mask) {
1240 #if PICO_USE_GPIO_COPROCESSOR
1241 gpioc_lo_oe_clr(mask);
1242 #else
1243 sio_hw->gpio_oe_clr = mask;
1244 #endif
1245 }
1246
1247 /*! \brief Set a number of GPIOs to input
1248 * \ingroup hardware_gpio
1249 *
1250 * \param mask Bitmask of GPIO to set to input
1251 */
gpio_set_dir_in_masked64(uint64_t mask)1252 static inline void gpio_set_dir_in_masked64(uint64_t mask) {
1253 #if PICO_USE_GPIO_COPROCESSOR
1254 gpioc_hilo_oe_clr(mask);
1255 #elif NUM_BANK0_GPIOS <= 32
1256 sio_hw->gpio_oe_clr = mask;
1257 #else
1258 sio_hw->gpio_oe_clr = (uint32_t)mask;
1259 sio_hw->gpio_hi_oe_clr = (uint32_t)(mask >> 32u);
1260 #endif
1261 }
1262
1263 /*! \brief Set multiple GPIO directions
1264 * \ingroup hardware_gpio
1265 *
1266 * \param mask Bitmask of GPIO to set to input, as bits 0-29
1267 * \param value Values to set
1268 *
1269 * For each 1 bit in "mask", switch that pin to the direction given by
1270 * corresponding bit in "value", leaving other pins unchanged.
1271 * E.g. gpio_set_dir_masked(0x3, 0x2); -> set pin 0 to input, pin 1 to output,
1272 * simultaneously.
1273 */
gpio_set_dir_masked(uint32_t mask,uint32_t value)1274 static inline void gpio_set_dir_masked(uint32_t mask, uint32_t value) {
1275 #if PICO_USE_GPIO_COPROCESSOR
1276 gpioc_lo_oe_xor((gpioc_lo_oe_get() ^ value) & mask);
1277 #else
1278 sio_hw->gpio_oe_togl = (sio_hw->gpio_oe ^ value) & mask;
1279 #endif
1280 }
1281
1282 /*! \brief Set multiple GPIO directions
1283 * \ingroup hardware_gpio
1284 *
1285 * \param mask Bitmask of GPIO to set to input, as bits 0-29
1286 * \param value Values to set
1287 *
1288 * For each 1 bit in "mask", switch that pin to the direction given by
1289 * corresponding bit in "value", leaving other pins unchanged.
1290 * E.g. gpio_set_dir_masked(0x3, 0x2); -> set pin 0 to input, pin 1 to output,
1291 * simultaneously.
1292 */
gpio_set_dir_masked64(uint64_t mask,uint64_t value)1293 static inline void gpio_set_dir_masked64(uint64_t mask, uint64_t value) {
1294 #if PICO_USE_GPIO_COPROCESSOR
1295 gpioc_hilo_oe_xor((gpioc_hilo_oe_get() ^ value) & mask);
1296 #elif NUM_BANK0_GPIOS <= 32
1297 sio_hw->gpio_oe_togl = (sio_hw->gpio_oe ^ (uint32_t)value) & (uint32_t)mask;
1298 #else
1299 sio_hw->gpio_oe_togl = (sio_hw->gpio_oe ^ (uint32_t)value) & (uint32_t)mask;
1300 sio_hw->gpio_hi_oe_togl = (sio_hw->gpio_hi_oe ^ (uint32_t)(value >> 32u)) & (uint32_t)(mask >> 32u);
1301 #endif
1302 }
1303
1304
1305 /*! \brief Set direction of all pins simultaneously.
1306 * \ingroup hardware_gpio
1307 *
1308 * \param values individual settings for each gpio; for GPIO N, bit N is 1 for out, 0 for in
1309 */
gpio_set_dir_all_bits(uint32_t values)1310 static inline void gpio_set_dir_all_bits(uint32_t values) {
1311 #if PICO_USE_GPIO_COPROCESSOR
1312 gpioc_lo_oe_put(values);
1313 #else
1314 sio_hw->gpio_oe = values;
1315 #endif
1316 }
1317
1318 /*! \brief Set direction of all pins simultaneously.
1319 * \ingroup hardware_gpio
1320 *
1321 * \param values individual settings for each gpio; for GPIO N, bit N is 1 for out, 0 for in
1322 */
gpio_set_dir_all_bits64(uint64_t values)1323 static inline void gpio_set_dir_all_bits64(uint64_t values) {
1324 #if PICO_USE_GPIO_COPROCESSOR
1325 gpioc_hilo_oe_put(values);
1326 #elif NUM_BANK0_GPIOS <= 32
1327 sio_hw->gpio_oe = (uint32_t)values;
1328 #else
1329 sio_hw->gpio_oe = (uint32_t)values;
1330 sio_hw->gpio_hi_oe = (uint32_t)(values >> 32u);
1331 #endif
1332 }
1333
1334 /*! \brief Set a single GPIO direction
1335 * \ingroup hardware_gpio
1336 *
1337 * \param gpio GPIO number
1338 * \param out true for out, false for in
1339 */
gpio_set_dir(uint gpio,bool out)1340 static inline void gpio_set_dir(uint gpio, bool out) {
1341 #if PICO_USE_GPIO_COPROCESSOR
1342 gpioc_bit_oe_put(gpio, out);
1343 #elif PICO_RP2040 || NUM_BANK0_GPIOS <= 32
1344 uint32_t mask = 1ul << gpio;
1345 if (out)
1346 gpio_set_dir_out_masked(mask);
1347 else
1348 gpio_set_dir_in_masked(mask);
1349 #else
1350 uint32_t mask = 1u << (gpio & 0x1fu);
1351 if (gpio < 32) {
1352 if (out) {
1353 sio_hw->gpio_oe_set = mask;
1354 } else {
1355 sio_hw->gpio_oe_clr = mask;
1356 }
1357 } else {
1358 if (out) {
1359 sio_hw->gpio_hi_oe_set = mask;
1360 } else {
1361 sio_hw->gpio_hi_oe_clr = mask;
1362 }
1363 }
1364 #endif
1365 }
1366
1367 /*! \brief Check if a specific GPIO direction is OUT
1368 * \ingroup hardware_gpio
1369 *
1370 * \param gpio GPIO number
1371 * \return true if the direction for the pin is OUT
1372 */
gpio_is_dir_out(uint gpio)1373 static inline bool gpio_is_dir_out(uint gpio) {
1374 #if NUM_BANK0_GPIOS <= 32
1375 return sio_hw->gpio_oe & (1u << (gpio));
1376 #else
1377 uint32_t bits = gpio < 32 ? sio_hw->gpio_oe : sio_hw->gpio_hi_oe;
1378 return bits & (1u << (gpio & 0x1fu));
1379 #endif
1380 }
1381
1382 /*! \brief Get a specific GPIO direction
1383 * \ingroup hardware_gpio
1384 *
1385 * \param gpio GPIO number
1386 * \return 1 for out, 0 for in
1387 */
gpio_get_dir(uint gpio)1388 static inline uint gpio_get_dir(uint gpio) {
1389 return gpio_is_dir_out(gpio); // note GPIO_OUT is 1/true and GPIO_IN is 0/false anyway
1390 }
1391
1392 #if PICO_SECURE
gpio_assign_to_ns(uint gpio,bool ns)1393 static inline void gpio_assign_to_ns(uint gpio, bool ns) {
1394 check_gpio_param(gpio);
1395 if (ns) hw_set_bits(&accessctrl_hw->gpio_nsmask[gpio/32], 1u << (gpio & 0x1fu));
1396 else hw_clear_bits(&accessctrl_hw->gpio_nsmask[gpio/32], 1u << (gpio & 0x1fu));
1397 }
1398 #endif
1399 extern void gpio_debug_pins_init(void);
1400
1401 #ifdef __cplusplus
1402 }
1403 #endif
1404
1405
1406 // PICO_CONFIG: PICO_DEBUG_PIN_BASE, First pin to use for debug output (if enabled), min=0, max=31 on RP2350B, 29 otherwise, default=19, group=hardware_gpio
1407 #ifndef PICO_DEBUG_PIN_BASE
1408 #define PICO_DEBUG_PIN_BASE 19u
1409 #endif
1410
1411 // PICO_CONFIG: PICO_DEBUG_PIN_COUNT, Number of pins to use for debug output (if enabled), min=1, max=32 on RP2350B, 30 otherwise, default=3, group=hardware_gpio
1412 #ifndef PICO_DEBUG_PIN_COUNT
1413 #define PICO_DEBUG_PIN_COUNT 3u
1414 #endif
1415
1416 #ifndef __cplusplus
1417 // note these two macros may only be used once per and only apply per compilation unit (hence the CU_)
1418 #define CU_REGISTER_DEBUG_PINS(...) enum __unused DEBUG_PIN_TYPE { _none = 0, __VA_ARGS__ }; static enum DEBUG_PIN_TYPE __selected_debug_pins;
1419 #define CU_SELECT_DEBUG_PINS(x) static enum DEBUG_PIN_TYPE __selected_debug_pins = (x);
1420 #define DEBUG_PINS_ENABLED(p) (__selected_debug_pins == (p))
1421 #else
1422 #define CU_REGISTER_DEBUG_PINS(p...) \
1423 enum DEBUG_PIN_TYPE { _none = 0, p }; \
1424 template <enum DEBUG_PIN_TYPE> class __debug_pin_settings { \
1425 public: \
1426 static inline bool enabled() { return false; } \
1427 };
1428 #define CU_SELECT_DEBUG_PINS(x) template<> inline bool __debug_pin_settings<x>::enabled() { return true; };
1429 #define DEBUG_PINS_ENABLED(p) (__debug_pin_settings<p>::enabled())
1430 #endif
1431 #define DEBUG_PINS_SET(p, v) if (DEBUG_PINS_ENABLED(p)) gpio_set_mask((unsigned)(v)<<PICO_DEBUG_PIN_BASE)
1432 #define DEBUG_PINS_CLR(p, v) if (DEBUG_PINS_ENABLED(p)) gpio_clr_mask((unsigned)(v)<<PICO_DEBUG_PIN_BASE)
1433 #define DEBUG_PINS_XOR(p, v) if (DEBUG_PINS_ENABLED(p)) gpio_xor_mask((unsigned)(v)<<PICO_DEBUG_PIN_BASE)
1434
1435 #endif // _GPIO_H_
1436