Skip to content

Commit 44106ac

Browse files
rruuaanngnashif
authored andcommitted
edtlib: binding: Add a examples keyword
Add an `examples` keyword to the binding to provide a minimal example node for the binding. Signed-off-by: James Roy <rruuaanng@outlook.com>
1 parent d770c6c commit 44106ac

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

doc/build/dts/bindings-syntax.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ like this:
6262
# bindings.
6363
on-bus: spi
6464
65+
examples:
66+
# You can put a sample node here showing how to use the binding.
67+
# - |
68+
# ...
69+
# or
70+
# - ...
71+
6572
foo-cells:
6673
# "Specifier" cell names for the 'foo' domain go here; example 'foo'
6774
# values are 'gpio', 'pwm', and 'dma'. See below for more information.
@@ -678,6 +685,31 @@ Only ``sensor@79`` can have a ``use-clock-stretching`` property. The
678685
bus-sensitive logic ignores :file:`manufacturer,sensor-i2c.yaml` when searching
679686
for a binding for ``sensor@0``.
680687

688+
.. _dt-bindings-examples:
689+
690+
Examples
691+
********
692+
693+
If you feel you want to provide a minimal example for your binding, you can use
694+
it like this:
695+
696+
.. code-block:: yaml
697+
698+
description: ...
699+
700+
properties:
701+
...
702+
703+
examples:
704+
- |
705+
leds {
706+
compatible = "gpio-leds";
707+
708+
uled: led {
709+
gpios = <&gpioe 12 GPIO_ACTIVE_HIGH>;
710+
};
711+
};
712+
681713
.. _dt-bindings-cells:
682714

683715
Specifier cell names (\*-cells)

doc/build/dts/bindings-upstream.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,26 @@ This ``|`` style prevents YAML parsers from removing the newlines in
9494
multi-line descriptions. This in turn makes these long strings
9595
display properly in the :ref:`devicetree_binding_index`.
9696

97+
If using the binding’s properties gets complicated, you can use examples
98+
to provide a minimal node. e.g.:
99+
100+
.. code-block:: yaml
101+
102+
description: ...
103+
104+
properties:
105+
...
106+
107+
examples:
108+
- |
109+
leds {
110+
compatible = "gpio-leds";
111+
112+
uled: led {
113+
gpios = <&gpioe 12 GPIO_ACTIVE_HIGH>;
114+
};
115+
};
116+
97117
Naming conventions
98118
==================
99119

scripts/dts/python-devicetree/src/devicetree/edtlib.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ class Binding:
137137
from node properties. It can also be None for Binding objects created
138138
using 'child-binding:' with no compatible.
139139
140+
examples:
141+
Provides a minimal example node illustrating the binding (optional).
142+
Like this:
143+
144+
examples:
145+
- |
146+
/ {
147+
model = "This is a sample node";
148+
...
149+
};
150+
140151
prop2specs:
141152
A dict mapping property names to PropertySpec objects
142153
describing those properties' values.
@@ -290,6 +301,11 @@ def bus(self) -> Union[None, str, list[str]]:
290301
"See the class docstring"
291302
return self.raw.get('bus')
292303

304+
@property
305+
def examples(self) -> Optional[list[str]]:
306+
"See the class docstring"
307+
return self.raw.get('examples')
308+
293309
@property
294310
def buses(self) -> list[str]:
295311
"See the class docstring"
@@ -420,7 +436,7 @@ def _check(self, require_compatible: bool, require_description: bool,
420436
# Allowed top-level keys. The 'include' key should have been
421437
# removed by _load_raw() already.
422438
ok_top = {"title", "description", "compatible", "bus",
423-
"on-bus", "properties", "child-binding"}
439+
"on-bus", "properties", "child-binding", "examples"}
424440

425441
# Descriptive errors for legacy bindings.
426442
legacy_errors = {

scripts/dts/python-devicetree/tests/test-bindings/defaults.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,19 @@ properties:
3636
type: int
3737
required: false
3838
default: 123
39+
40+
examples:
41+
- |
42+
/ {
43+
leds {
44+
compatible = "gpio-leds";
45+
46+
uled: led {
47+
gpios = <&gpioe 12 GPIO_ACTIVE_HIGH>;
48+
};
49+
};
50+
51+
aliases {
52+
led0 = &uled;
53+
};
54+
};

scripts/dts/python-devicetree/tests/test_edtlib.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from logging import WARNING
88
import os
99
from pathlib import Path
10+
import textwrap
1011

1112
import pytest
1213

@@ -568,10 +569,26 @@ def test_binding_top_key():
568569
title = binding.title
569570
description = binding.description
570571
compatible = binding.compatible
572+
examples = binding.examples[0]
571573

572574
assert title == "Test binding"
573575
assert description == "Property default value test"
574576
assert compatible == "defaults"
577+
assert examples == textwrap.dedent("""\
578+
/ {
579+
leds {
580+
compatible = "gpio-leds";
581+
582+
uled: led {
583+
gpios = <&gpioe 12 GPIO_ACTIVE_HIGH>;
584+
};
585+
};
586+
587+
aliases {
588+
led0 = &uled;
589+
};
590+
};
591+
""")
575592

576593
def test_child_binding():
577594
'''Test 'child-binding:' in bindings'''

0 commit comments

Comments
 (0)