1.. _dt-trouble:
2
3Troubleshooting devicetree
4##########################
5
6Here are some tips for fixing misbehaving devicetree related code.
7
8See :ref:`dt-howtos` for other "HOWTO" style information.
9
10.. _dt-trouble-try-pristine:
11
12Try again with a pristine build directory
13*****************************************
14
15.. important:: Try this first, before doing anything else.
16
17See :ref:`west-building-pristine` for examples, or just delete the build
18directory completely and retry.
19
20This is general advice which is especially applicable to debugging devicetree
21issues, because the outputs are created during the CMake configuration phase,
22and are not always regenerated when one of their inputs changes.
23
24Make sure <devicetree.h> is included
25************************************
26
27Unlike Kconfig symbols, the :file:`devicetree.h` header must be included
28explicitly.
29
30Many Zephyr header files rely on information from devicetree, so including some
31other API may transitively include :file:`devicetree.h`, but that's not
32guaranteed.
33
34undefined reference to ``__device_dts_ord_<N>``
35***********************************************
36
37This usually happens on a line like this:
38
39.. code-block:: c
40
41   const struct device *dev = DEVICE_DT_GET(NODE_ID);
42
43where ``NODE_ID`` is a valid :ref:`node identifier <dt-node-identifiers>`, but
44no device driver has allocated a ``struct device`` for this devicetree node.
45You thus get a linker error, because you're asking for a pointer to a device
46that isn't defined.
47
48To fix it, you need to make sure that:
49
501. The node is enabled: the node must have ``status = "okay";``.
51
52   (Recall that a missing ``status`` property means the same thing as ``status
53   = "okay";``; see :ref:`dt-important-props` for more information about
54   ``status``).
55
562. A device driver responsible for allocating the ``struct device`` is enabled.
57   That is, the Kconfig option which makes the build system compile the driver
58   sources into your application needs to be set to ``y``.
59
60   (See :ref:`setting_configuration_values` for more information on setting
61   Kconfig options.)
62
63Below, ``<build>`` means your build directory.
64
65**Making sure the node is enabled**:
66
67To find the devicetree node you need to check, use the number ``<N>`` from the
68linker error. Look for this number in the list of nodes at the top of
69:file:`<build>/zephyr/include/generated/devicetree_generated.h`. For example, if
70``<N>`` is 15, and your :file:`devicetree_generated.h` file looks like this,
71the node you are interested in is ``/soc/i2c@deadbeef``:
72
73.. code-block:: none
74
75   /*
76    * Generated by gen_defines.py
77    *
78    * DTS input file:
79    *   <build>/zephyr/zephyr.dts.pre
80    *
81    * Directories with bindings:
82    *   $ZEPHYR_BASE/dts/bindings
83    *
84    * Node dependency ordering (ordinal and path):
85    *   0   /
86    *   1   /aliases
87   [...]
88    *   15  /soc/i2c@deadbeef
89   [...]
90
91Now look for this node in :file:`<build>/zephyr/zephyr.dts`, which is the final
92devicetree for your application build. (See :ref:`get-devicetree-outputs` for
93information and examples.)
94
95If the node has ``status = "disabled";`` in :file:`zephyr.dts`, then you need
96to enable it by setting ``status = "okay";``, probably by using a devicetree
97:ref:`overlay <set-devicetree-overlays>`. For example, if :file:`zephyr.dts`
98looks like this:
99
100.. code-block:: DTS
101
102   i2c0: i2c@deadbeef {
103           status = "disabled";
104   };
105
106Then you should put this into your devicetree overlay and
107:ref:`dt-trouble-try-pristine`:
108
109.. code-block:: DTS
110
111   &i2c0 {
112           status = "okay";
113   };
114
115Make sure that you see ``status = "okay";`` in :file:`zephyr.dts` after you
116rebuild.
117
118**Making sure the device driver is enabled**:
119
120The first step is to figure out which device driver is responsible for handling
121your devicetree node and allocating devices for it. To do this, you need to
122start with the ``compatible`` property in your devicetree node, and find the
123driver that allocates ``struct device`` instances for that compatible.
124
125If you're not familiar with how devices are allocated from devicetree nodes
126based on compatible properties, the ZDS 2021 talk `A deep dive into the Zephyr
1272.5 device model`_ may be a useful place to start, along with the
128:ref:`device_model_api` pages. See :ref:`dt-important-props` and the Devicetree
129specification for more information about ``compatible``.
130
131.. _A deep dive into the Zephyr 2.5 device model:
132   https://www.youtube.com/watch?v=sWaxQyIgEBY
133
134There is currently no documentation for what device drivers exist and which
135devicetree compatibles they are associated with. You will have to figure this
136out by reading the source code:
137
138- Look in :zephyr_file:`drivers` for the appropriate subdirectory that
139  corresponds to the API your device implements
140- Look inside that directory for relevant files until you figure out what the
141  driver is, or realize there is no such driver.
142
143Often, but not always, you can find the driver by looking for a file that sets
144the ``DT_DRV_COMPAT`` macro to match your node's ``compatible`` property,
145except lowercased and with special characters converted to underscores. For
146example, if your node's compatible is ``vnd,foo-device``, look for a file with this
147line:
148
149.. code-block:: C
150
151   #define DT_DRV_COMPAT vnd_foo_device
152
153.. important::
154
155   This **does not always work** since not all drivers use ``DT_DRV_COMPAT``.
156
157If you find a driver, you next need to make sure the Kconfig option that
158compiles it is enabled. (If you don't find a driver, and you are sure the
159compatible property is correct, then you need to write a driver. Writing
160drivers is outside the scope of this documentation page.)
161
162Continuing the above example, if your devicetree node looks like this now:
163
164.. code-block:: DTS
165
166   i2c0: i2c@deadbeef {
167           compatible = "nordic,nrf-twim";
168           status = "okay";
169   };
170
171Then you would look inside of :zephyr_file:`drivers/i2c` for the driver file
172that handles the compatible ``nordic,nrf-twim``. In this case, that is
173:zephyr_file:`drivers/i2c/i2c_nrfx_twim.c`. Notice how even in cases where
174``DT_DRV_COMPAT`` is not set, you can use information like driver file names as
175clues.
176
177Once you know the driver you want to enable, you need to make sure its Kconfig
178option is set to ``y``. You can figure out which Kconfig option is needed by
179looking for a line similar to this one in the :file:`CMakeLists.txt` file in
180the drivers subdirectory. Continuing the above example,
181:zephyr_file:`drivers/i2c/CMakeLists.txt` has a line that looks like this:
182
183.. code-block:: cmake
184
185   zephyr_library_sources_ifdef(CONFIG_NRFX_TWIM       i2c_nrfx_twim.c)
186
187This means that :kconfig:option:`CONFIG_NRFX_TWIM` must be set to ``y`` in
188:file:`<build>/zephyr/.config` file.
189
190If your driver's Kconfig is not set to ``y``, you need to figure out what you
191need to do to make that happen. Often, this will happen automatically as soon
192as you enable the devicetree node. Otherwise, it is sometimes as simple as
193adding a line like this to your application's :file:`prj.conf` file and then
194making sure to :ref:`dt-trouble-try-pristine`:
195
196.. code-block:: none
197
198   CONFIG_FOO=y
199
200where ``CONFIG_FOO`` is the option that :file:`CMakeLists.txt` uses to decide
201whether or not to compile the driver.
202
203However, there may be other problems in your way, such as unmet Kconfig
204dependencies that you also have to enable before you can enable your driver.
205
206Consult the Kconfig file that defines ``CONFIG_FOO`` (for your value of
207``FOO``) for more information.
208
209.. _dt-use-the-right-names:
210
211Make sure you're using the right names
212**************************************
213
214Remember that:
215
216- In C/C++, devicetree names must be lowercased and special characters must be
217  converted to underscores. Zephyr's generated devicetree header has DTS names
218  converted in this way into the C tokens used by the preprocessor-based
219  ``<devicetree.h>`` API.
220- In overlays, use devicetree node and property names the same way they
221  would appear in any DTS file. Zephyr overlays are just DTS fragments.
222
223For example, if you're trying to **get** the ``clock-frequency`` property of a
224node with path ``/soc/i2c@12340000`` in a C/C++ file:
225
226.. code-block:: c
227
228   /*
229    * foo.c: lowercase-and-underscores names
230    */
231
232   /* Don't do this: */
233   #define MY_CLOCK_FREQ DT_PROP(DT_PATH(soc, i2c@1234000), clock-frequency)
234   /*                                           ^               ^
235    *                                        @ should be _     - should be _  */
236
237   /* Do this instead: */
238   #define MY_CLOCK_FREQ DT_PROP(DT_PATH(soc, i2c_1234000), clock_frequency)
239   /*                                           ^               ^           */
240
241And if you're trying to **set** that property in a devicetree overlay:
242
243.. code-block:: none
244
245   /*
246    * foo.overlay: DTS names with special characters, etc.
247    */
248
249   /* Don't do this; you'll get devicetree errors. */
250   &{/soc/i2c_12340000/} {
251   	clock_frequency = <115200>;
252   };
253
254   /* Do this instead. Overlays are just DTS fragments. */
255   &{/soc/i2c@12340000/} {
256   	clock-frequency = <115200>;
257   };
258
259Look at the preprocessor output
260*******************************
261
262To save preprocessor output files, enable the
263:kconfig:option:`CONFIG_COMPILER_SAVE_TEMPS` option. For example, to build
264:ref:`hello_world` with west with this option set, use:
265
266.. code-block:: sh
267
268   west build -b BOARD samples/hello_world -- -DCONFIG_COMPILER_SAVE_TEMPS=y
269
270This will create a preprocessor output file named :file:`foo.c.i` in the build
271directory for each source file :file:`foo.c`.
272
273You can then search for the file in the build directory to see what your
274devicetree macros expanded to. For example, on macOS and Linux, using ``find``
275to find :file:`main.c.i`:
276
277.. code-block:: sh
278
279   $ find build -name main.c.i
280   build/CMakeFiles/app.dir/src/main.c.i
281
282It's usually easiest to run a style formatter on the results before opening
283them. For example, to use ``clang-format`` to reformat the file in place:
284
285.. code-block:: sh
286
287   clang-format -i build/CMakeFiles/app.dir/src/main.c.i
288
289You can then open the file in your favorite editor to view the final C results
290after preprocessing.
291
292Do not track macro expansion
293****************************
294
295Compiler messages for devicetree errors can sometimes be very long. This
296typically happens when the compiler prints a message for every step of a
297complex macro expansion that has several intermediate expansion steps.
298
299To prevent the compiler from doing this, you can disable the
300:kconfig:option:`CONFIG_COMPILER_TRACK_MACRO_EXPANSION` option. This typically
301reduces the output to one message per error.
302
303For example, to build :ref:`hello_world` with west and this option disabled,
304use:
305
306.. code-block:: sh
307
308   west build -b BOARD samples/hello_world -- -DCONFIG_COMPILER_TRACK_MACRO_EXPANSION=n
309
310Validate properties
311*******************
312
313If you're getting a compile error reading a node property, check your node
314identifier and property. For example, if you get a build error on a line that
315looks like this:
316
317.. code-block:: c
318
319   int baud_rate = DT_PROP(DT_NODELABEL(my_serial), current_speed);
320
321Try checking the node by adding this to the file and recompiling:
322
323.. code-block:: c
324
325   #if !DT_NODE_EXISTS(DT_NODELABEL(my_serial))
326   #error "whoops"
327   #endif
328
329If you see the "whoops" error message when you rebuild, the node identifier
330isn't referring to a valid node. :ref:`get-devicetree-outputs` and debug from
331there.
332
333Some hints for what to check next if you don't see the "whoops" error message:
334
335- did you :ref:`dt-use-the-right-names`?
336- does the :ref:`property exist <dt-checking-property-exists>`?
337- does the node have a :ref:`matching binding <dt-bindings>`?
338- does the binding define the property?
339
340.. _missing-dt-binding:
341
342Check for missing bindings
343**************************
344
345See :ref:`dt-bindings` for information about bindings, and
346:ref:`devicetree_binding_index` for information on bindings built into Zephyr.
347
348If the build fails to :ref:`dts-find-binding` for a node, then either the
349node's ``compatible`` property is not defined, or its value has no matching
350binding. If the property is set, check for typos in its name. In a devicetree
351source file, ``compatible`` should look like ``"vnd,some-device"`` --
352:ref:`dt-use-the-right-names`.
353
354If your binding file is not under :file:`zephyr/dts`, you may need to set
355:ref:`DTS_ROOT <dts_root>`; see :ref:`dt-where-bindings-are-located`.
356
357Errors with DT_INST_() APIs
358***************************
359
360If you're using an API like :c:func:`DT_INST_PROP`, you must define
361``DT_DRV_COMPAT`` to the lowercase-and-underscores version of the compatible
362you are interested in. See :ref:`dt-create-devices-inst`.
363