Skip to content
systemcoding.dev
RTOS

Read a devicetree property with DT_PROP

DT_PROP reads a devicetree property as a compile-time constant, with dashes in the name written as underscores, in Zephyr 4.2.

1 min read
Summarize in AI

Instead of hard-coding a bus speed in C, read it from the devicetree so the code follows the board. DT_PROP(node, prop) expands to the property's value at compile time. The rule that trips people is the name: lower-case it and turn every dash into an underscore, so clock-frequency becomes clock_frequency.

src/main.cc
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>

int main(void)
{
	printk("i2c1 clock-frequency = %u\n",
	       DT_PROP(DT_NODELABEL(i2c1), clock_frequency));
	return 0;
}
Text
*** Booting Zephyr OS build v4.2.0 ***
i2c1 clock-frequency = 400000

The output assumes an overlay that sets clock-frequency = <400000> on &i2c1 and enables the node. Strings come back as string literals, booleans as 0 or 1, and an integer array needs DT_PROP_BY_IDX. Asking for a property that is not in the node is a compile error, not a zero, which is usually what you want. When a missing property is legitimate, DT_PROP_OR(node, prop, default) supplies the fallback. The macro is documented in the devicetree API reference.

Taggedczephyr
Share

More snippets

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.