Skip to content

Commit 7828b7b

Browse files
Wolfram SangBartosz Golaszewski
authored andcommitted
gpio: add sloppy logic analyzer using polling
This is a sloppy logic analyzer using GPIOs. It comes with a script to isolate a CPU for polling. While this is definitely not a production level analyzer, it can be a helpful first view when remote debugging. Read the documentation for details. Signed-off-by: Wolfram Sang <[email protected]> Reviewed-by: Linus Walleij <[email protected]> Link: https://lore.kernel.org/r/[email protected] [Bartosz: moved the Kconfig entry into a different category] Signed-off-by: Bartosz Golaszewski <[email protected]>
1 parent 6a9c150 commit 7828b7b

File tree

6 files changed

+704
-0
lines changed

6 files changed

+704
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
.. SPDX-License-Identifier: GPL-2.0
2+
3+
=============================================
4+
Linux Kernel GPIO based sloppy logic analyzer
5+
=============================================
6+
7+
:Author: Wolfram Sang
8+
9+
Introduction
10+
============
11+
12+
This document briefly describes how to run the GPIO based in-kernel sloppy
13+
logic analyzer running on an isolated CPU.
14+
15+
The sloppy logic analyzer will utilize a few GPIO lines in input mode on a
16+
system to rapidly sample these digital lines, which will, if the Nyquist
17+
criteria is met, result in a time series log with approximate waveforms as they
18+
appeared on these lines. One way to use it is to analyze external traffic
19+
connected to these GPIO lines with wires (i.e. digital probes), acting as a
20+
common logic analyzer.
21+
22+
Another feature is to snoop on on-chip peripherals if the I/O cells of these
23+
peripherals can be used in GPIO input mode at the same time as they are being
24+
used as inputs or outputs for the peripheral. That means you could e.g. snoop
25+
I2C traffic without any wiring (if your hardware supports it). In the pin
26+
control subsystem such pin controllers are called "non-strict": a certain pin
27+
can be used with a certain peripheral and as a GPIO input line at the same
28+
time.
29+
30+
Note that this is a last resort analyzer which can be affected by latencies,
31+
non-deterministic code paths and non-maskable interrupts. It is called 'sloppy'
32+
for a reason. However, for e.g. remote development, it may be useful to get a
33+
first view and aid further debugging.
34+
35+
Setup
36+
=====
37+
38+
Your kernel must have CONFIG_DEBUG_FS and CONFIG_CPUSETS enabled. Ideally, your
39+
runtime environment does not utilize cpusets otherwise, then isolation of a CPU
40+
core is easiest. If you do need cpusets, check that helper script for the
41+
sloppy logic analyzer does not interfere with your other settings.
42+
43+
Tell the kernel which GPIOs are used as probes. For a Device Tree based system,
44+
you need to use the following bindings. Because these bindings are only for
45+
debugging, there is no official schema::
46+
47+
i2c-analyzer {
48+
compatible = "gpio-sloppy-logic-analyzer";
49+
probe-gpios = <&gpio6 21 GPIO_OPEN_DRAIN>, <&gpio6 4 GPIO_OPEN_DRAIN>;
50+
probe-names = "SCL", "SDA";
51+
};
52+
53+
Note that you must provide a name for every GPIO specified. Currently a
54+
maximum of 8 probes are supported. 32 are likely possible but are not
55+
implemented yet.
56+
57+
Usage
58+
=====
59+
60+
The logic analyzer is configurable via files in debugfs. However, it is
61+
strongly recommended to not use them directly, but to use the script
62+
``tools/gpio/gpio-sloppy-logic-analyzer``. Besides checking parameters more
63+
extensively, it will isolate the CPU core so you will have the least
64+
disturbance while measuring.
65+
66+
The script has a help option explaining the parameters. For the above DT
67+
snippet which analyzes an I2C bus at 400kHz on a Renesas Salvator-XS board, the
68+
following settings are used: The isolated CPU shall be CPU1 because it is a big
69+
core in a big.LITTLE setup. Because CPU1 is the default, we don't need a
70+
parameter. The bus speed is 400kHz. So, the sampling theorem says we need to
71+
sample at least at 800kHz. However, falling edges of both signals in an I2C
72+
start condition happen faster, so we need a higher sampling frequency, e.g.
73+
``-s 1500000`` for 1.5MHz. Also, we don't want to sample right away but wait
74+
for a start condition on an idle bus. So, we need to set a trigger to a falling
75+
edge on SDA while SCL stays high, i.e. ``-t 1H+2F``. Last is the duration, let
76+
us assume 15ms here which results in the parameter ``-d 15000``. So,
77+
altogether::
78+
79+
gpio-sloppy-logic-analyzer -s 1500000 -t 1H+2F -d 15000
80+
81+
Note that the process will return you back to the prompt but a sub-process is
82+
still sampling in the background. Unless this has finished, you will not find a
83+
result file in the current or specified directory. For the above example, we
84+
will then need to trigger I2C communication::
85+
86+
i2cdetect -y -r <your bus number>
87+
88+
Result is a .sr file to be consumed with PulseView or sigrok-cli from the free
89+
`sigrok`_ project. It is a zip file which also contains the binary sample data
90+
which may be consumed by other software. The filename is the logic analyzer
91+
instance name plus a since-epoch timestamp.
92+
93+
.. _sigrok: https://sigrok.org/

Documentation/dev-tools/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Documentation/dev-tools/testing-overview.rst
3232
kunit/index
3333
ktap
3434
checkuapi
35+
gpio-sloppy-logic-analyzer
3536

3637

3738
.. only:: subproject and html

drivers/gpio/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1891,4 +1891,23 @@ config GPIO_SIM
18911891

18921892
endmenu
18931893

1894+
menu "GPIO Debugging utilities"
1895+
1896+
config GPIO_SLOPPY_LOGIC_ANALYZER
1897+
tristate "Sloppy GPIO logic analyzer"
1898+
depends on (GPIOLIB || COMPILE_TEST) && CPUSETS && DEBUG_FS && EXPERT
1899+
help
1900+
This option enables support for a sloppy logic analyzer using polled
1901+
GPIOs. Use the 'tools/gpio/gpio-sloppy-logic-analyzer' script with
1902+
this driver. The script will make it easier to use and will also
1903+
isolate a CPU for the polling task. Note that this is a last resort
1904+
analyzer which can be affected by latencies, non-deterministic code
1905+
paths, or NMIs. However, for e.g. remote development, it may be useful
1906+
to get a first view and aid further debugging.
1907+
1908+
If this driver is built as a module it will be called
1909+
'gpio-sloppy-logic-analyzer'.
1910+
1911+
endmenu
1912+
18941913
endif

drivers/gpio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ obj-$(CONFIG_GPIO_SIFIVE) += gpio-sifive.o
150150
obj-$(CONFIG_GPIO_SIM) += gpio-sim.o
151151
obj-$(CONFIG_GPIO_SIOX) += gpio-siox.o
152152
obj-$(CONFIG_GPIO_SL28CPLD) += gpio-sl28cpld.o
153+
obj-$(CONFIG_GPIO_SLOPPY_LOGIC_ANALYZER) += gpio-sloppy-logic-analyzer.o
153154
obj-$(CONFIG_GPIO_SODAVILLE) += gpio-sodaville.o
154155
obj-$(CONFIG_GPIO_SPEAR_SPICS) += gpio-spear-spics.o
155156
obj-$(CONFIG_GPIO_SPRD) += gpio-sprd.o

0 commit comments

Comments
 (0)