1.. _devicetree_api: 2 3Devicetree API 4############## 5 6This is a reference page for the ``<devicetree.h>`` API. The API is macro 7based. Use of these macros has no impact on scheduling. They can be used from 8any calling context and at file scope. 9 10Some of these require a special macro named ``DT_DRV_COMPAT`` to be defined 11before they can be used; these are discussed individually below. These macros 12are generally meant for use within :ref:`device drivers <device_model_api>`, 13though they can be used outside of drivers with appropriate care. 14 15.. _devicetree-generic-apis: 16 17Generic APIs 18************ 19 20The APIs in this section can be used anywhere and do not require 21``DT_DRV_COMPAT`` to be defined. 22 23Node identifiers and helpers 24============================ 25 26A *node identifier* is a way to refer to a devicetree node at C preprocessor 27time. While node identifiers are not C values, you can use them to access 28devicetree data in C rvalue form using, for example, the 29:ref:`devicetree-property-access` API. 30 31The root node ``/`` has node identifier ``DT_ROOT``. You can create node 32identifiers for other devicetree nodes using :c:func:`DT_PATH`, 33:c:func:`DT_NODELABEL`, :c:func:`DT_ALIAS`, and :c:func:`DT_INST`. 34 35There are also :c:func:`DT_PARENT` and :c:func:`DT_CHILD` macros which can be 36used to create node identifiers for a given node's parent node or a particular 37child node, respectively. 38 39The following macros create or operate on node identifiers. 40 41.. doxygengroup:: devicetree-generic-id 42 43.. _devicetree-property-access: 44 45Property access 46=============== 47 48The following general-purpose macros can be used to access node properties. 49There are special-purpose APIs for accessing the :ref:`devicetree-reg-property` 50and :ref:`devicetree-interrupts-property`. 51 52Property values can be read using these macros even if the node is disabled, 53as long as it has a matching binding. 54 55.. doxygengroup:: devicetree-generic-prop 56 57.. _devicetree-reg-property: 58 59``reg`` property 60================ 61 62Use these APIs instead of :ref:`devicetree-property-access` to access the 63``reg`` property. Because this property's semantics are defined by the 64devicetree specification, these macros can be used even for nodes without 65matching bindings. 66 67.. doxygengroup:: devicetree-reg-prop 68 69.. _devicetree-interrupts-property: 70 71``interrupts`` property 72======================= 73 74Use these APIs instead of :ref:`devicetree-property-access` to access the 75``interrupts`` property. 76 77Because this property's semantics are defined by the devicetree specification, 78some of these macros can be used even for nodes without matching bindings. This 79does not apply to macros which take cell names as arguments. 80 81.. doxygengroup:: devicetree-interrupts-prop 82 83For-each macros 84=============== 85 86There is currently only one "generic" for-each macro, 87:c:func:`DT_FOREACH_CHILD`, which allows iterating over the children of a 88devicetree node. 89 90There are special-purpose for-each macros, like 91:c:func:`DT_INST_FOREACH_STATUS_OKAY`, but these require ``DT_DRV_COMPAT`` to 92be defined before use. 93 94.. doxygengroup:: devicetree-generic-foreach 95 96Existence checks 97================ 98 99This section documents miscellaneous macros that can be used to test if a node 100exists, how many nodes of a certain type exist, whether a node has certain 101properties, etc. Some macros used for special purposes (such as 102:c:func:`DT_IRQ_HAS_IDX` and all macros which require ``DT_DRV_COMPAT``) are 103documented elsewhere on this page. 104 105.. doxygengroup:: devicetree-generic-exist 106 107.. _devicetree-dep-ord: 108 109Inter-node dependencies 110======================= 111 112The ``devicetree.h`` API has some support for tracking dependencies between 113nodes. Dependency tracking relies on a binary "depends on" relation between 114devicetree nodes, which is defined as the `transitive closure 115<https://en.wikipedia.org/wiki/Transitive_closure>`_ of the following "directly 116depends on" relation: 117 118- every non-root node directly depends on its parent node 119- a node directly depends on any nodes its properties refer to by phandle 120- a node directly depends on its ``interrupt-parent`` if it has an 121 ``interrupts`` property 122 123A *dependency ordering* of a devicetree is a list of its nodes, where each node 124``n`` appears earlier in the list than any nodes that depend on ``n``. A node's 125*dependency ordinal* is then its zero-based index in that list. Thus, for two 126distinct devicetree nodes ``n1`` and ``n2`` with dependency ordinals ``d1`` and 127``d2``, we have: 128 129- ``d1 != d2`` 130- if ``n1`` depends on ``n2``, then ``d1 > d2`` 131- ``d1 > d2`` does **not** necessarily imply that ``n1`` depends on ``n2`` 132 133The Zephyr build system chooses a dependency ordering of the final devicetree 134and assigns a dependency ordinal to each node. Dependency related information 135can be accessed using the following macros. The exact dependency ordering 136chosen is an implementation detail, but cyclic dependencies are detected and 137cause errors, so it's safe to assume there are none when using these macros. 138 139There are instance number-based conveniences as well; see 140:c:func:`DT_INST_DEP_ORD` and subsequent documentation. 141 142.. doxygengroup:: devicetree-dep-ord 143 144Bus helpers 145=========== 146 147Zephyr's devicetree bindings language supports a ``bus:`` key which allows 148bindings to declare that nodes with a given compatible describe system buses. 149In this case, child nodes are considered to be on a bus of the given type, and 150the following APIs may be used. 151 152.. doxygengroup:: devicetree-generic-bus 153 154.. _devicetree-inst-apis: 155 156Instance-based APIs 157******************* 158 159These are recommended for use within device drivers. To use them, define 160``DT_DRV_COMPAT`` to the lowercase-and-underscores compatible the device driver 161implements support for. Here is an example devicetree fragment: 162 163.. code-block:: devicetree 164 165 serial@40001000 { 166 compatible = "vnd,serial"; 167 status = "okay"; 168 current-speed = <115200>; 169 }; 170 171Example usage, assuming serial@40001000 is the only enabled node 172with compatible "vnd,serial": 173 174.. code-block:: c 175 176 #define DT_DRV_COMPAT vnd_serial 177 DT_DRV_INST(0) // node identifier for serial@40001000 178 DT_INST_PROP(0, current_speed) // 115200 179 180.. warning:: 181 182 Be careful making assumptions about instance numbers. See :c:func:`DT_INST` 183 for the API guarantees. 184 185As shown above, the ``DT_INST_*`` APIs are conveniences for addressing nodes by 186instance number. They are almost all defined in terms of one of the 187:ref:`devicetree-generic-apis`. The equivalent generic API can be found by 188removing ``INST_`` from the macro name. For example, ``DT_INST_PROP(inst, 189prop)`` is equivalent to ``DT_PROP(DT_DRV_INST(inst), prop)``. Similarly, 190``DT_INST_REG_ADDR(inst)`` is equivalent to ``DT_REG_ADDR(DT_DRV_INST(inst))``, 191and so on. There are some exceptions: :c:func:`DT_ANY_INST_ON_BUS_STATUS_OKAY` 192and :c:func:`DT_INST_FOREACH_STATUS_OKAY` are special-purpose helpers without 193straightforward generic equivalents. 194 195Since ``DT_DRV_INST()`` requires ``DT_DRV_COMPAT`` to be defined, it's an error 196to use any of these without that macro defined. 197 198Note that there are also helpers available for 199specific hardware; these are documented in :ref:`devicetree-hw-api`. 200 201.. doxygengroup:: devicetree-inst 202 203.. _devicetree-hw-api: 204 205Hardware specific APIs 206********************** 207 208The following APIs can also be used by including ``<devicetree.h>``; 209no additional include is needed. 210 211Clocks 212====== 213 214These conveniences may be used for nodes which describe clock sources, and 215properties related to them. 216 217.. doxygengroup:: devicetree-clocks 218 219DMA 220=== 221 222These conveniences may be used for nodes which describe direct memory access 223controllers or channels, and properties related to them. 224 225.. doxygengroup:: devicetree-dmas 226 227.. _devicetree-flash-api: 228 229Fixed flash partitions 230====================== 231 232These conveniences may be used for the special-purpose ``fixed-partitions`` 233compatible used to encode information about flash memory partitions in the 234device tree. See :zephyr_file:`dts/bindings/mtd/partition.yaml` for this 235compatible's binding. 236 237.. doxygengroup:: devicetree-fixed-partition 238 239.. _devicetree-gpio-api: 240 241GPIO 242==== 243 244These conveniences may be used for nodes which describe GPIO controllers/pins, 245and properties related to them. 246 247.. doxygengroup:: devicetree-gpio 248 249IO channels 250=========== 251 252These are commonly used by device drivers which need to use IO 253channels (e.g. ADC or DAC channels) for conversion. 254 255.. doxygengroup:: devicetree-io-channels 256 257.. _devicetree-pinctrl-api: 258 259Pinctrl (pin control) 260===================== 261 262These are used to access pin control properties by name or index. 263 264Devicetree nodes may have properties which specify pin control (sometimes known 265as pin mux) settings. These are expressed using ``pinctrl-<index>`` properties 266within the node, where the ``<index>`` values are contiguous integers starting 267from 0. These may also be named using the ``pinctrl-names`` property. 268 269Here is an example: 270 271.. code-block:: DTS 272 273 node { 274 ... 275 pinctrl-0 = <&foo &bar ...>; 276 pinctrl-1 = <&baz ...>; 277 pinctrl-names = "default", "sleep"; 278 }; 279 280Above, ``pinctrl-0`` has name ``"default"``, and ``pinctrl-1`` has name 281``"sleep"``. The ``pinctrl-<index>`` property values contain phandles. The 282``&foo``, ``&bar``, etc. phandles within the properties point to nodes whose 283contents vary by platform, and which describe a pin configuration for the node. 284 285.. doxygengroup:: devicetree-pinctrl 286 287PWM 288=== 289 290These conveniences may be used for nodes which describe PWM controllers and 291properties related to them. 292 293.. doxygengroup:: devicetree-pwms 294 295SPI 296=== 297 298These conveniences may be used for nodes which describe either SPI controllers 299or devices, depending on the case. 300 301.. doxygengroup:: devicetree-spi 302 303.. _devicetree-chosen-nodes: 304 305Chosen nodes 306************ 307 308The special ``/chosen`` node contains properties whose values describe 309system-wide settings. The :c:func:`DT_CHOSEN()` macro can be used to get a node 310identifier for a chosen node. 311 312.. doxygengroup:: devicetree-generic-chosen 313 :project: Zephyr 314 315There are also conveniences for commonly used zephyr-specific properties of the 316``/chosen`` node. 317 318.. doxygengroup:: devicetree-zephyr 319 :project: Zephyr 320 321The following table documents some commonly used Zephyr-specific chosen nodes. 322 323Sometimes, a chosen node's label property will be used to set the default value 324of a Kconfig option which in turn configures a hardware-specific device. This 325is usually for backwards compatibility in cases when the Kconfig option 326predates devicetree support in Zephyr. In other cases, there is no Kconfig 327option, and the devicetree node is used directly in the source code to select a 328device. 329 330.. Documentation maintainers: please keep this sorted by property name 331 332.. list-table:: Zephyr-specific chosen properties 333 :header-rows: 1 334 335 * - Property 336 - Purpose 337 * - zephyr,bt-c2h-uart 338 - Selects the UART used for host communication in the 339 :ref:`bluetooth-hci-uart-sample` 340 * - zephyr,bt-mon-uart 341 - Sets UART device used for the Bluetooth monitor logging 342 * - zephyr,bt-uart 343 - Sets UART device used by Bluetooth 344 * - zephyr,can-primary 345 - Sets the primary CAN controller 346 * - zephyr,ccm 347 - Core-Coupled Memory node on some STM32 SoCs 348 * - zephyr,code-partition 349 - Flash partition that the Zephyr image's text section should be linked 350 into 351 * - zephyr,console 352 - Sets UART device used by console driver 353 * - zephyr,dtcm 354 - Data Tightly Coupled Memory node on some Arm SoCs 355 * - zephyr,entropy 356 - A device which can be used as a system-wide entropy source 357 * - zephyr,flash 358 - A node whose ``reg`` is sometimes used to set the defaults for 359 :kconfig:`CONFIG_FLASH_BASE_ADDRESS` and :kconfig:`CONFIG_FLASH_SIZE` 360 * - zephyr,flash-controller 361 - The node corresponding to the flash controller device for 362 the ``zephyr,flash`` node 363 * - zephyr,ipc 364 - Used by the OpenAMP subsystem to specify the inter-process communication 365 (IPC) device 366 * - zephyr,ipc_shm 367 - A node whose ``reg`` is used by the OpenAMP subsystem to determine the 368 base address and size of the shared memory (SHM) usable for 369 interprocess-communication (IPC) 370 * - zephyr,itcm 371 - Instruction Tightly Coupled Memory node on some Arm SoCs 372 * - zephyr,ot-uart 373 - Used by the OpenThread to specify UART device for Spinel protocol 374 * - zephyr,shell-uart 375 - Sets UART device used by serial shell backend 376 * - zephyr,sram 377 - A node whose ``reg`` sets the base address and size of SRAM memory 378 available to the Zephyr image, used during linking 379 * - zephyr,uart-mcumgr 380 - UART used for :ref:`device_mgmt` 381 * - zephyr,uart-pipe 382 - Sets default :kconfig:`CONFIG_UART_PIPE_ON_DEV_NAME` 383 * - zephyr,usb-device 384 - USB device node. If defined and has a ``vbus-gpios`` property, these 385 will be used by the USB subsystem to enable/disable VBUS 386