Skip to content

Commit 65d8bb6

Browse files
committed
docs: gpio: Add GPIO docs
Add more complete documentation page for GPIO Signed-off-by: Benjamin Cabé <[email protected]>
1 parent 0c9d8b7 commit 65d8bb6

File tree

1 file changed

+135
-2
lines changed

1 file changed

+135
-2
lines changed

doc/hardware/peripherals/gpio.rst

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,151 @@
11
.. _gpio_api:
22

3-
43
General-Purpose Input/Output (GPIO)
54
###################################
65

76
Overview
87
********
98

9+
A General-Purpose Input/Output (GPIO) is a digital signal pin that does not have a specific function
10+
but can be controlled by software to function as an input or an output.
11+
12+
The GPIO API provides a generic method to interact with General Purpose Input/Output (GPIO)
13+
pins. It allows applications to configure pins as inputs or outputs, read and write
14+
their state, and manage interrupts. Key features include:
15+
16+
**Pin Configuration**
17+
Configure pins as input, output, or disconnected.
18+
Support for internal pull-up/pull-down resistors and drive strength configuration.
19+
20+
**Data Access**
21+
Read input values and write output values.
22+
23+
**Interrupts**
24+
Configure interrupts on pin state changes (rising edge, falling edge,
25+
level low, level high) and register callbacks to handle these interrupts.
26+
27+
**Devicetree Integration**
28+
GPIOs are typically defined in the Devicetree, allowing
29+
drivers and applications to reference them in a hardware-agnostic way using
30+
:c:struct:`gpio_dt_spec`.
31+
32+
Devicetree Configuration
33+
************************
34+
35+
GPIO controllers are defined in the Devicetree as nodes with the ``gpio-controller`` property.
36+
The ``#gpio-cells`` property typically specifies that 2 cells are used to describe a GPIO: the pin
37+
number and the flags.
38+
39+
Example of a GPIO controller definition:
40+
41+
.. code-block:: devicetree
42+
43+
gpio0: gpio@40022000 {
44+
compatible = "ti,cc13xx-cc26xx-gpio";
45+
reg = <0x40022000 0x400>;
46+
interrupts = <0 0>;
47+
gpio-controller;
48+
#gpio-cells = <2>;
49+
};
50+
51+
Example of referencing a GPIO:
52+
53+
.. code-block:: devicetree
54+
55+
leds {
56+
compatible = "gpio-leds";
57+
led0: led_0 {
58+
gpios = <&gpio0 25 GPIO_ACTIVE_HIGH>;
59+
label = "Green LED";
60+
};
61+
};
62+
63+
Basic Operation
64+
***************
65+
66+
GPIO operations are usually performed on a :c:struct:`gpio_dt_spec` structure, which is a container
67+
for the GPIO pin information specified in the Devicetree.
68+
69+
This structure is typically populated using :c:macro:`GPIO_DT_SPEC_GET` macro (or any of its
70+
variants).
71+
72+
.. code-block:: c
73+
:caption: Populating a gpio_dt_spec structure for a GPIO pin defined as alias ``led0``
74+
75+
#define LED0_NODE DT_ALIAS(led0)
76+
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
77+
78+
The :c:struct:`gpio_dt_spec` structure can then be used to perform GPIO operations.
79+
80+
.. code-block:: c
81+
:caption: Configuring a GPIO pin (``led`` from the previous snippet) as output and setting it
82+
to its active level
83+
84+
int ret;
85+
86+
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
87+
if (ret < 0) {
88+
return ret;
89+
}
90+
91+
ret = gpio_pin_set_dt(&led, 1);
92+
if (ret < 0) {
93+
return ret;
94+
}
95+
96+
Refer to :zephyr:code-sample:`blinky` for a complete example of basic operations on GPIOs using the
97+
:c:struct:`gpio_dt_spec` structure.
98+
99+
GPIO operations can also be performed directly on a GPIO controller device, in which case you will
100+
use the GPIO API functions that take a device pointer as an argument. For example
101+
:c:func:`gpio_pin_configure` instead of :c:func:`gpio_pin_configure_dt`.
102+
103+
GPIO Hogs
104+
*********
105+
106+
GPIO hogs provide a mechanism to automatically configure GPIO pins during system initialization.
107+
This is useful for pins that need to be set to a specific state (e.g., reset lines, power enables)
108+
and don't require runtime control by an application.
109+
110+
Hogs are defined in the Devicetree as children of a GPIO controller node.
111+
112+
- The ``gpio-hog`` property marks the node as a hog.
113+
- The ``gpios`` property specifies the pin and its active state.
114+
- One of ``input``, ``output-low``, or ``output-high`` properties specifies the configuration.
115+
116+
Example Devicetree overlay:
117+
118+
.. code-block:: devicetree
119+
120+
&gpio0 {
121+
hog1 {
122+
gpio-hog;
123+
gpios = <1 GPIO_ACTIVE_LOW>;
124+
output-high;
125+
};
126+
127+
hog2 {
128+
gpio-hog;
129+
gpios = <2 GPIO_ACTIVE_HIGH>;
130+
output-low;
131+
};
132+
};
133+
134+
If you need runtime control of a pin configured as a hog beyond the initial state being
135+
automatically set during system initialization, you may consider using the :ref:`regulator_api`
136+
instead, with a :dtcompatible:`regulator-fixed` Devicetree node.
137+
10138
Configuration Options
11139
*********************
12140

13-
Related configuration options:
141+
Main configuration options:
14142

15143
* :kconfig:option:`CONFIG_GPIO`
144+
* :kconfig:option:`CONFIG_GPIO_SHELL`
145+
* :kconfig:option:`CONFIG_GPIO_GET_DIRECTION`
146+
* :kconfig:option:`CONFIG_GPIO_GET_CONFIG`
147+
* :kconfig:option:`CONFIG_GPIO_HOGS`
148+
* :kconfig:option:`CONFIG_GPIO_ENABLE_DISABLE_INTERRUPT`
16149

17150
API Reference
18151
*************

0 commit comments

Comments
 (0)