xref: /aosp_15_r20/external/pigweed/pw_digital_io_linux/docs.rst (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker.. _module-pw_digital_io_linux:
2*61c4878aSAndroid Build Coastguard Worker
3*61c4878aSAndroid Build Coastguard Worker.. cpp:namespace-push:: pw::digital_io
4*61c4878aSAndroid Build Coastguard Worker
5*61c4878aSAndroid Build Coastguard Worker===================
6*61c4878aSAndroid Build Coastguard Workerpw_digital_io_linux
7*61c4878aSAndroid Build Coastguard Worker===================
8*61c4878aSAndroid Build Coastguard Worker.. pigweed-module::
9*61c4878aSAndroid Build Coastguard Worker   :name: pw_digital_io_linux
10*61c4878aSAndroid Build Coastguard Worker
11*61c4878aSAndroid Build Coastguard Worker``pw_digital_io_linux`` implements the :ref:`module-pw_digital_io` interface
12*61c4878aSAndroid Build Coastguard Workerusing the `Linux userspace gpio-cdev interface
13*61c4878aSAndroid Build Coastguard Worker<https://www.kernel.org/doc/Documentation/ABI/testing/gpio-cdev>`_.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker.. note::
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker   Currently only the v1 userspace ABI is supported (https://pwbug.dev/328268007).
18*61c4878aSAndroid Build Coastguard Worker
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker-------------
21*61c4878aSAndroid Build Coastguard WorkerAPI reference
22*61c4878aSAndroid Build Coastguard Worker-------------
23*61c4878aSAndroid Build Coastguard WorkerThe following classes make up the public API:
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker``LinuxDigitalIoChip``
26*61c4878aSAndroid Build Coastguard Worker======================
27*61c4878aSAndroid Build Coastguard WorkerRepresents an open handle to a GPIO *chip* (e.g.  ``/dev/gpiochip0``).
28*61c4878aSAndroid Build Coastguard Worker
29*61c4878aSAndroid Build Coastguard WorkerThis can be created using an existing file descriptor
30*61c4878aSAndroid Build Coastguard Worker(``LinuxDigitalIoChip(fd)``) or by opening a given path
31*61c4878aSAndroid Build Coastguard Worker(``LinuxDigitalIoChip::Open(path)``).
32*61c4878aSAndroid Build Coastguard Worker
33*61c4878aSAndroid Build Coastguard Worker``LinuxDigitalIn``
34*61c4878aSAndroid Build Coastguard Worker==================
35*61c4878aSAndroid Build Coastguard WorkerRepresents a single input line and implements :cpp:class:`DigitalIn`.
36*61c4878aSAndroid Build Coastguard Worker
37*61c4878aSAndroid Build Coastguard WorkerThis is acquired by calling ``chip.GetInputLine()`` with an appropriate
38*61c4878aSAndroid Build Coastguard Worker``LinuxInputConfig``.
39*61c4878aSAndroid Build Coastguard Worker
40*61c4878aSAndroid Build Coastguard Worker``LinuxDigitalInInterrupt``
41*61c4878aSAndroid Build Coastguard Worker===========================
42*61c4878aSAndroid Build Coastguard WorkerRepresents a single input line configured for interrupts and implements
43*61c4878aSAndroid Build Coastguard Worker:cpp:class:`DigitalInInterrupt`.
44*61c4878aSAndroid Build Coastguard Worker
45*61c4878aSAndroid Build Coastguard WorkerThis is acquired by calling ``chip.GetInterruptLine()`` with an appropriate
46*61c4878aSAndroid Build Coastguard Worker``LinuxInputConfig``.
47*61c4878aSAndroid Build Coastguard Worker
48*61c4878aSAndroid Build Coastguard Worker``LinuxDigitalOut``
49*61c4878aSAndroid Build Coastguard Worker===================
50*61c4878aSAndroid Build Coastguard WorkerRepresents a single input line and implements :cpp:class:`DigitalOut`.
51*61c4878aSAndroid Build Coastguard Worker
52*61c4878aSAndroid Build Coastguard WorkerThis is acquired by calling ``chip.GetOutputLine()`` with an appropriate
53*61c4878aSAndroid Build Coastguard Worker``LinuxOutputConfig``.
54*61c4878aSAndroid Build Coastguard Worker
55*61c4878aSAndroid Build Coastguard Worker.. warning::
56*61c4878aSAndroid Build Coastguard Worker
57*61c4878aSAndroid Build Coastguard Worker   The ``Disable()`` implementation only closes the GPIO handle file
58*61c4878aSAndroid Build Coastguard Worker   descriptor, relying on the underlying GPIO driver / pinctrl to restore
59*61c4878aSAndroid Build Coastguard Worker   the state of the line. This may or may not be implemented depending on
60*61c4878aSAndroid Build Coastguard Worker   the GPIO driver.
61*61c4878aSAndroid Build Coastguard Worker
62*61c4878aSAndroid Build Coastguard Worker
63*61c4878aSAndroid Build Coastguard Worker------
64*61c4878aSAndroid Build Coastguard WorkerGuides
65*61c4878aSAndroid Build Coastguard Worker------
66*61c4878aSAndroid Build Coastguard WorkerExample code to use GPIO pins:
67*61c4878aSAndroid Build Coastguard Worker
68*61c4878aSAndroid Build Coastguard WorkerConfigure an input pin and get its state
69*61c4878aSAndroid Build Coastguard Worker========================================
70*61c4878aSAndroid Build Coastguard Worker
71*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/input.cc
72*61c4878aSAndroid Build Coastguard Worker   :language: cpp
73*61c4878aSAndroid Build Coastguard Worker   :linenos:
74*61c4878aSAndroid Build Coastguard Worker   :lines: 15-
75*61c4878aSAndroid Build Coastguard Worker
76*61c4878aSAndroid Build Coastguard Worker
77*61c4878aSAndroid Build Coastguard WorkerConfigure an output pin and set its state
78*61c4878aSAndroid Build Coastguard Worker=========================================
79*61c4878aSAndroid Build Coastguard Worker
80*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/output.cc
81*61c4878aSAndroid Build Coastguard Worker   :language: cpp
82*61c4878aSAndroid Build Coastguard Worker   :linenos:
83*61c4878aSAndroid Build Coastguard Worker   :lines: 15-
84*61c4878aSAndroid Build Coastguard Worker
85*61c4878aSAndroid Build Coastguard Worker
86*61c4878aSAndroid Build Coastguard WorkerConfigure an interrupt pin and handle events
87*61c4878aSAndroid Build Coastguard Worker============================================
88*61c4878aSAndroid Build Coastguard Worker
89*61c4878aSAndroid Build Coastguard Worker.. literalinclude:: examples/interrupt.cc
90*61c4878aSAndroid Build Coastguard Worker   :language: cpp
91*61c4878aSAndroid Build Coastguard Worker   :linenos:
92*61c4878aSAndroid Build Coastguard Worker   :lines: 15-
93*61c4878aSAndroid Build Coastguard Worker
94*61c4878aSAndroid Build Coastguard Worker.. _module-pw_digital_io_linux-cli:
95*61c4878aSAndroid Build Coastguard Worker
96*61c4878aSAndroid Build Coastguard Worker----------------------
97*61c4878aSAndroid Build Coastguard WorkerCommand-Line Interface
98*61c4878aSAndroid Build Coastguard Worker----------------------
99*61c4878aSAndroid Build Coastguard WorkerThis module also provides a tool also named ``pw_digital_io_linux`` which
100*61c4878aSAndroid Build Coastguard Workerprovides a basic command-line interface to the library. It provides the
101*61c4878aSAndroid Build Coastguard Workerfollowing sub-commands:
102*61c4878aSAndroid Build Coastguard Worker
103*61c4878aSAndroid Build Coastguard Worker``get``
104*61c4878aSAndroid Build Coastguard Worker=======
105*61c4878aSAndroid Build Coastguard WorkerConfigure a GPIO line as an input and get its value.
106*61c4878aSAndroid Build Coastguard Worker
107*61c4878aSAndroid Build Coastguard WorkerUsage:
108*61c4878aSAndroid Build Coastguard Worker
109*61c4878aSAndroid Build Coastguard Worker.. code-block:: none
110*61c4878aSAndroid Build Coastguard Worker
111*61c4878aSAndroid Build Coastguard Worker   get [-i] CHIP LINE
112*61c4878aSAndroid Build Coastguard Worker
113*61c4878aSAndroid Build Coastguard WorkerOptions:
114*61c4878aSAndroid Build Coastguard Worker
115*61c4878aSAndroid Build Coastguard Worker* ``-i``: Invert; configure as active-low.
116*61c4878aSAndroid Build Coastguard Worker
117*61c4878aSAndroid Build Coastguard WorkerArguments:
118*61c4878aSAndroid Build Coastguard Worker
119*61c4878aSAndroid Build Coastguard Worker* ``CHIP``: gpiochip path (e.g. ``/dev/gpiochip0``)
120*61c4878aSAndroid Build Coastguard Worker* ``LINE``: GPIO line number (e.g. ``1``)
121*61c4878aSAndroid Build Coastguard Worker
122*61c4878aSAndroid Build Coastguard Worker``set``
123*61c4878aSAndroid Build Coastguard Worker=======
124*61c4878aSAndroid Build Coastguard WorkerConfigure a GPIO line as an output and set its value.
125*61c4878aSAndroid Build Coastguard Worker
126*61c4878aSAndroid Build Coastguard Worker.. warning::
127*61c4878aSAndroid Build Coastguard Worker
128*61c4878aSAndroid Build Coastguard Worker   After this process exits, the GPIO line could immediately return to its
129*61c4878aSAndroid Build Coastguard Worker   hardware-controlled default state (depending on the GPIO driver).
130*61c4878aSAndroid Build Coastguard Worker
131*61c4878aSAndroid Build Coastguard WorkerUsage:
132*61c4878aSAndroid Build Coastguard Worker
133*61c4878aSAndroid Build Coastguard Worker.. code-block:: none
134*61c4878aSAndroid Build Coastguard Worker
135*61c4878aSAndroid Build Coastguard Worker   set [-i] CHIP LINE VALUE
136*61c4878aSAndroid Build Coastguard Worker
137*61c4878aSAndroid Build Coastguard WorkerOptions:
138*61c4878aSAndroid Build Coastguard Worker
139*61c4878aSAndroid Build Coastguard Worker* ``-i``: Invert; configure as active-low.
140*61c4878aSAndroid Build Coastguard Worker
141*61c4878aSAndroid Build Coastguard WorkerArguments:
142*61c4878aSAndroid Build Coastguard Worker
143*61c4878aSAndroid Build Coastguard Worker* ``CHIP``: gpiochip path (e.g. ``/dev/gpiochip0``)
144*61c4878aSAndroid Build Coastguard Worker* ``LINE``: GPIO line number (e.g. ``1``)
145*61c4878aSAndroid Build Coastguard Worker* ``VALUE``: the value to set (``0`` = inactive or ``1`` = active)
146*61c4878aSAndroid Build Coastguard Worker
147*61c4878aSAndroid Build Coastguard Worker.. _module-pw_digital_io_linux-cli-watch:
148*61c4878aSAndroid Build Coastguard Worker
149*61c4878aSAndroid Build Coastguard Worker``watch``
150*61c4878aSAndroid Build Coastguard Worker=========
151*61c4878aSAndroid Build Coastguard WorkerConfigure a GPIO line as an input and watch for interrupt events.
152*61c4878aSAndroid Build Coastguard Worker
153*61c4878aSAndroid Build Coastguard WorkerBy default, both rising and falling edges will trigger an event.
154*61c4878aSAndroid Build Coastguard WorkerThis can be changed via the ``-t`` option.
155*61c4878aSAndroid Build Coastguard Worker
156*61c4878aSAndroid Build Coastguard WorkerUsage:
157*61c4878aSAndroid Build Coastguard Worker
158*61c4878aSAndroid Build Coastguard Worker.. code-block:: none
159*61c4878aSAndroid Build Coastguard Worker
160*61c4878aSAndroid Build Coastguard Worker   watch [-i] [{-ta,-tb,-td}] CHIP LINE
161*61c4878aSAndroid Build Coastguard Worker
162*61c4878aSAndroid Build Coastguard WorkerOptions:
163*61c4878aSAndroid Build Coastguard Worker
164*61c4878aSAndroid Build Coastguard Worker* ``-i``: Invert; configure as active-low.
165*61c4878aSAndroid Build Coastguard Worker* ``-t``: Trigger for an interrupt:
166*61c4878aSAndroid Build Coastguard Worker
167*61c4878aSAndroid Build Coastguard Worker   * ``-ta`` - activating edge
168*61c4878aSAndroid Build Coastguard Worker   * ``-tb`` - both edges (default)
169*61c4878aSAndroid Build Coastguard Worker   * ``-td`` - deactivating edge
170*61c4878aSAndroid Build Coastguard Worker
171*61c4878aSAndroid Build Coastguard WorkerArguments:
172*61c4878aSAndroid Build Coastguard Worker
173*61c4878aSAndroid Build Coastguard Worker* ``CHIP``: gpiochip path (e.g. ``/dev/gpiochip0``)
174*61c4878aSAndroid Build Coastguard Worker* ``LINE``: GPIO line number (e.g. ``1``)
175*61c4878aSAndroid Build Coastguard Worker
176*61c4878aSAndroid Build Coastguard Worker.. cpp:namespace-pop::
177