gpio.h
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#ifdef __cplusplus
11extern "C" {
12#endif
13
14#include "pico.h"
15
16enum gpio_function {
17 GPIO_FUNC_XIP = 0,
18 GPIO_FUNC_SPI = 1,
19 GPIO_FUNC_UART = 2,
20 GPIO_FUNC_I2C = 3,
21 GPIO_FUNC_PWM = 4,
22 GPIO_FUNC_SIO = 5,
23 GPIO_FUNC_PIO0 = 6,
24 GPIO_FUNC_PIO1 = 7,
25 GPIO_FUNC_GPCK = 8,
26 GPIO_FUNC_USB = 9,
27 GPIO_FUNC_NULL = 0xf,
28};
29
30enum gpio_slew_rate {
33};
34
35enum gpio_drive_strength {
40};
41
42#define GPIO_OUT 1
43#define GPIO_IN 0
44
45#define NUM_BANK0_GPIOS 30
46
47// ----------------------------------------------------------------------------
48// Pad Controls + IO Muxing
49// ----------------------------------------------------------------------------
50// Declarations for gpio.c
51
52void gpio_set_function(uint gpio, enum gpio_function fn);
53
54enum gpio_function gpio_get_function(uint gpio);
55
56void gpio_pull_up(uint gpio);
57
58void gpio_pull_down(uint gpio);
59
60void gpio_disable_pulls(uint gpio);
61
62void gpio_set_pulls(uint gpio, bool up, bool down);
63
64void gpio_set_irqover(uint gpio, uint value);
65
66void gpio_set_outover(uint gpio, uint value);
67
68void gpio_set_inover(uint gpio, uint value);
69
70void gpio_set_oeover(uint gpio, uint value);
71
72void gpio_set_input_enabled(uint gpio, bool enable);
73
74void gpio_set_input_hysteresis_enabled(uint gpio, bool enabled);
75
77
78void gpio_set_slew_rate(uint gpio, enum gpio_slew_rate slew);
79
80enum gpio_slew_rate gpio_get_slew_rate(uint gpio);
81
82void gpio_set_drive_strength(uint gpio, enum gpio_drive_strength drive);
83
84enum gpio_drive_strength gpio_get_drive_strength(uint gpio);
85
86// Configure a GPIO for direct input/output from software
87void gpio_init(uint gpio);
88
89void gpio_init_mask(uint gpio_mask);
90
91// ----------------------------------------------------------------------------
92// Input
93// ----------------------------------------------------------------------------
94
95// Get the value of a single GPIO
96bool gpio_get(uint gpio);
97
98// Get raw value of all
99uint32_t gpio_get_all();
100
101// ----------------------------------------------------------------------------
102// Output
103// ----------------------------------------------------------------------------
104
105// Drive high every GPIO appearing in mask
106void gpio_set_mask(uint32_t mask);
107
108void gpio_clr_mask(uint32_t mask);
109
110// Toggle every GPIO appearing in mask
111void gpio_xor_mask(uint32_t mask);
112
113
114// For each 1 bit in "mask", drive that pin to the value given by
115// corresponding bit in "value", leaving other pins unchanged.
116// Since this uses the TOGL alias, it is concurrency-safe with e.g. an IRQ
117// bashing different pins from the same core.
118void gpio_put_masked(uint32_t mask, uint32_t value);
119
120// Drive all pins simultaneously
121void gpio_put_all(uint32_t value);
122
123
124// Drive a single GPIO high/low
125void gpio_put(uint gpio, int value);
126
127// ----------------------------------------------------------------------------
128// Direction
129// ----------------------------------------------------------------------------
130
131// Switch all GPIOs in "mask" to output
132void gpio_set_dir_out_masked(uint32_t mask);
133
134// Switch all GPIOs in "mask" to input
135void gpio_set_dir_in_masked(uint32_t mask);
136
137// For each 1 bit in "mask", switch that pin to the direction given by
138// corresponding bit in "value", leaving other pins unchanged.
139// E.g. gpio_set_dir_masked(0x3, 0x2); -> set pin 0 to input, pin 1 to output,
140// simultaneously.
141void gpio_set_dir_masked(uint32_t mask, uint32_t value);
142
143// Set direction of all pins simultaneously.
144// For each bit in value,
145// 1 = out
146// 0 = in
147void gpio_set_dir_all_bits(uint32_t value);
148
149// Set a single GPIO to input/output.
150// true = out
151// 0 = in
152void gpio_set_dir(uint gpio, bool out);
153
154// debugging
155#ifndef PICO_DEBUG_PIN_BASE
156#define PICO_DEBUG_PIN_BASE 19u
157#endif
158
159// note these two macros may only be used once per compilation unit
160#define CU_REGISTER_DEBUG_PINS(p, ...)
161#define CU_SELECT_DEBUG_PINS(x)
162#define DEBUG_PINS_ENABLED(p) false
163
164#define DEBUG_PINS_SET(p, v) ((void)0)
165#define DEBUG_PINS_CLR(p, v) ((void)0)
166#define DEBUG_PINS_XOR(p, v) ((void)0)
167
168void gpio_debug_pins_init();
169
170#ifdef __cplusplus
171}
172#endif
173
174#endif
void gpio_set_drive_strength(uint gpio, enum gpio_drive_strength drive)
Set drive strength for a specified GPIO.
Definition: gpio.c:62
enum gpio_drive_strength gpio_get_drive_strength(uint gpio)
Determine current slew rate for a specified GPIO.
Definition: gpio.c:66
void gpio_set_input_hysteresis_enabled(uint gpio, bool enabled)
Enable/disable GPIO input hysteresis (Schmitt trigger)
Definition: gpio.c:46
void gpio_set_oeover(uint gpio, uint value)
Select GPIO output enable override.
Definition: gpio.c:42
void gpio_init_mask(uint gpio_mask)
Initialise multiple GPIOs (enabled I/O and set func to GPIO_FUNC_SIO)
Definition: gpio.c:145
static void gpio_pull_up(uint gpio)
Set specified GPIO to be pulled up.
Definition: gpio.h:206
enum gpio_slew_rate gpio_get_slew_rate(uint gpio)
Determine current slew rate for a specified GPIO.
Definition: gpio.c:58
static void gpio_set_dir_all_bits(uint32_t values)
Set direction of all pins simultaneously.
Definition: gpio.h:582
void gpio_set_inover(uint gpio, uint value)
Select GPIO input override.
Definition: gpio.c:38
static void gpio_xor_mask(uint32_t mask)
Toggle every GPIO appearing in mask.
Definition: gpio.h:476
static void gpio_set_dir(uint gpio, bool out)
Set a single GPIO direction.
Definition: gpio.h:592
static void gpio_clr_mask(uint32_t mask)
Drive low every GPIO appearing in mask.
Definition: gpio.h:467
static void gpio_put(uint gpio, bool value)
Drive a single GPIO high/low.
Definition: gpio.h:510
void gpio_set_input_enabled(uint gpio, bool enabled)
Enable GPIO input.
Definition: gpio.c:141
static void gpio_set_dir_in_masked(uint32_t mask)
Set a number of GPIOs to input.
Definition: gpio.h:558
static void gpio_put_all(uint32_t value)
Drive all pins simultaneously.
Definition: gpio.h:500
static void gpio_set_dir_out_masked(uint32_t mask)
Set a number of GPIOs to output.
Definition: gpio.h:549
void gpio_set_outover(uint gpio, uint value)
Set GPIO output override.
Definition: gpio.c:34
void gpio_init(uint gpio)
Initialise a GPIO for (enabled I/O and set func to GPIO_FUNC_SIO)
Definition: gpio.c:79
void gpio_set_irqover(uint gpio, uint value)
Set GPIO IRQ override.
Definition: gpio.c:30
static void gpio_set_dir_masked(uint32_t mask, uint32_t value)
Set multiple GPIO directions.
Definition: gpio.h:573
void gpio_set_function(uint gpio, enum gpio_function fn)
Select GPIO function.
Definition: gpio.c:10
void gpio_set_pulls(uint gpio, bool up, bool down)
Select up and down pulls on specific GPIO.
Definition: gpio.c:26
enum gpio_function gpio_get_function(uint gpio)
Determine current GPIO function.
Definition: gpio.c:44
bool gpio_is_input_hysteresis_enabled(uint gpio)
Determine whether input hysteresis is enabled on a specified GPIO.
Definition: gpio.c:50
static void gpio_put_masked(uint32_t mask, uint32_t value)
Drive GPIO high/low depending on parameters.
Definition: gpio.h:491
static void gpio_disable_pulls(uint gpio)
Disable pulls on specified GPIO.
Definition: gpio.h:244
static uint32_t gpio_get_all(void)
Get raw value of all GPIOs.
Definition: gpio.h:445
static void gpio_pull_down(uint gpio)
Set specified GPIO to be pulled down.
Definition: gpio.h:225
void gpio_set_slew_rate(uint gpio, enum gpio_slew_rate slew)
Set slew rate for a specified GPIO.
Definition: gpio.c:54
static bool gpio_get(uint gpio)
Get state of a single specified GPIO.
Definition: gpio.h:436
static void gpio_set_mask(uint32_t mask)
Drive high every GPIO appearing in mask.
Definition: gpio.h:458
@ GPIO_DRIVE_STRENGTH_2MA
2 mA nominal drive strength
Definition: gpio.h:36
@ GPIO_DRIVE_STRENGTH_8MA
8 mA nominal drive strength
Definition: gpio.h:38
@ GPIO_DRIVE_STRENGTH_12MA
12 mA nominal drive strength
Definition: gpio.h:39
@ GPIO_DRIVE_STRENGTH_4MA
4 mA nominal drive strength
Definition: gpio.h:37
@ GPIO_SLEW_RATE_FAST
Slew rate limiting disabled.
Definition: gpio.h:32
@ GPIO_SLEW_RATE_SLOW
Slew rate limiting enabled.
Definition: gpio.h:31