1 /*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8 #include <zephyr/devicetree.h>
9 #include <zephyr/device.h>
10 #include <zephyr/drivers/adc.h>
11 #include <zephyr/drivers/gpio.h>
12 #include <zephyr/drivers/mbox.h>
13
14 #include <stdlib.h>
15
16 #define TEST_CHILDREN DT_PATH(test, test_children)
17 #define TEST_DEADBEEF DT_PATH(test, gpio_deadbeef)
18 #define TEST_ABCD1234 DT_PATH(test, gpio_abcd1234)
19 #define TEST_ALIAS DT_ALIAS(test_alias)
20 #define TEST_NODELABEL DT_NODELABEL(test_nodelabel)
21 #define TEST_INST DT_INST(0, vnd_gpio_device)
22 #define TEST_ARRAYS DT_NODELABEL(test_arrays)
23 #define TEST_PH DT_NODELABEL(test_phandles)
24 #define TEST_INTC DT_NODELABEL(test_intc)
25 #define TEST_IRQ DT_NODELABEL(test_irq)
26 #define TEST_IRQ_EXT DT_NODELABEL(test_irq_extended)
27 #define TEST_TEMP DT_NODELABEL(test_temp_sensor)
28 #define TEST_REG DT_NODELABEL(test_reg)
29 #define TEST_VENDOR DT_NODELABEL(test_vendor)
30 #define TEST_MODEL DT_NODELABEL(test_vendor)
31 #define TEST_ENUM_0 DT_NODELABEL(test_enum_0)
32 #define TEST_64BIT DT_NODELABEL(test_reg_64)
33 #define TEST_INTC DT_NODELABEL(test_intc)
34
35 #define TEST_I2C DT_NODELABEL(test_i2c)
36 #define TEST_I2C_DEV DT_PATH(test, i2c_11112222, test_i2c_dev_10)
37 #define TEST_I2C_BUS DT_BUS(TEST_I2C_DEV)
38
39 #define TEST_I2C_MUX DT_NODELABEL(test_i2c_mux)
40 #define TEST_I2C_MUX_CTLR_1 DT_CHILD(TEST_I2C_MUX, i2c_mux_ctlr_1)
41 #define TEST_I2C_MUX_CTLR_2 DT_CHILD(TEST_I2C_MUX, i2c_mux_ctlr_2)
42 #define TEST_MUXED_I2C_DEV_1 DT_NODELABEL(test_muxed_i2c_dev_1)
43 #define TEST_MUXED_I2C_DEV_2 DT_NODELABEL(test_muxed_i2c_dev_2)
44
45 #define TEST_I3C DT_NODELABEL(test_i3c)
46 #define TEST_I3C_DEV DT_PATH(test, i3c_88889999, test_i3c_dev_420000abcd12345678)
47 #define TEST_I3C_BUS DT_BUS(TEST_I3C_DEV)
48
49 #define TEST_GPIO_1 DT_NODELABEL(test_gpio_1)
50 #define TEST_GPIO_2 DT_NODELABEL(test_gpio_2)
51 #define TEST_GPIO_4 DT_NODELABEL(test_gpio_4)
52
53 #define TEST_GPIO_HOG_1 DT_PATH(test, gpio_deadbeef, test_gpio_hog_1)
54 #define TEST_GPIO_HOG_2 DT_PATH(test, gpio_deadbeef, test_gpio_hog_2)
55 #define TEST_GPIO_HOG_3 DT_PATH(test, gpio_abcd1234, test_gpio_hog_3)
56
57 #define TEST_SPI DT_NODELABEL(test_spi)
58
59 #define TEST_SPI_DEV_0 DT_PATH(test, spi_33334444, test_spi_dev_0)
60 #define TEST_SPI_BUS_0 DT_BUS(TEST_SPI_DEV_0)
61
62 #define TEST_SPI_DEV_1 DT_PATH(test, spi_33334444, test_spi_dev_1)
63 #define TEST_SPI_BUS_1 DT_BUS(TEST_SPI_DEV_1)
64
65 #define TEST_SPI_NO_CS DT_NODELABEL(test_spi_no_cs)
66 #define TEST_SPI_DEV_NO_CS DT_NODELABEL(test_spi_no_cs)
67
68 #define TEST_PWM_CTLR_1 DT_NODELABEL(test_pwm1)
69 #define TEST_PWM_CTLR_2 DT_NODELABEL(test_pwm2)
70
71 #define TEST_CAN_CTRL_0 DT_NODELABEL(test_can0)
72 #define TEST_CAN_CTRL_1 DT_NODELABEL(test_can1)
73 #define TEST_CAN_CTRL_2 DT_NODELABEL(test_can2)
74 #define TEST_CAN_CTRL_3 DT_NODELABEL(test_can3)
75
76 #define TEST_DMA_CTLR_1 DT_NODELABEL(test_dma1)
77 #define TEST_DMA_CTLR_2 DT_NODELABEL(test_dma2)
78
79 #define TEST_VIDEO2 DT_NODELABEL(test_video2)
80 #define TEST_VIDEO2_PORT0 DT_NODELABEL(test_video2_port0)
81 #define TEST_VIDEO2_PORT0_IN0 DT_NODELABEL(test_video2_port0_in0)
82 #define TEST_VIDEO2_PORT0_IN1 DT_NODELABEL(test_video2_port0_in1)
83 #define TEST_VIDEO2_PORT1 DT_NODELABEL(test_video2_port1)
84 #define TEST_VIDEO2_PORT1_IN DT_NODELABEL(test_video2_port1_in)
85 #define TEST_VIDEO0 DT_NODELABEL(test_video0)
86 #define TEST_VIDEO0_OUT DT_NODELABEL(test_video0_out)
87 #define TEST_VIDEO0_PORT DT_NODELABEL(test_video0_port)
88 #define TEST_VIDEO1 DT_NODELABEL(test_video1)
89 #define TEST_VIDEO1_OUT0 DT_NODELABEL(test_video1_out0)
90 #define TEST_VIDEO1_OUT1 DT_NODELABEL(test_video1_out1)
91 #define TEST_VIDEO1_PORT DT_NODELABEL(test_video1_port)
92
93 #define TEST_IO_CHANNEL_CTLR_1 DT_NODELABEL(test_adc_1)
94 #define TEST_IO_CHANNEL_CTLR_2 DT_NODELABEL(test_adc_2)
95
96 #define TEST_RANGES_PCIE DT_NODELABEL(test_ranges_pcie)
97 #define TEST_RANGES_OTHER DT_NODELABEL(test_ranges_other)
98 #define TEST_RANGES_EMPTY DT_NODELABEL(test_ranges_empty)
99
100 #define TEST_MTD_0 DT_PATH(test, test_mtd_ffeeddcc)
101 #define TEST_MTD_1 DT_PATH(test, test_mtd_33221100)
102
103 #define TEST_MEM_0 DT_CHILD(TEST_MTD_0, flash_20000000)
104
105 #define TEST_PARTITION_0 DT_PATH(test, test_mtd_ffeeddcc, flash_20000000, partitions, partition_0)
106 #define TEST_PARTITION_1 DT_PATH(test, test_mtd_ffeeddcc, flash_20000000, partitions, partition_c0)
107 #define TEST_PARTITION_2 DT_PATH(test, test_mtd_33221100, partitions, partition_6ff80)
108
109 #define ZEPHYR_USER DT_PATH(zephyr_user)
110
111 #define TA_HAS_COMPAT(compat) DT_NODE_HAS_COMPAT(TEST_ARRAYS, compat)
112
113 #define TO_STRING(x) TO_STRING_(x)
114 #define TO_STRING_(x) #x
115
ZTEST(devicetree_api,test_path_props)116 ZTEST(devicetree_api, test_path_props)
117 {
118 zassert_equal(DT_NUM_REGS(TEST_DEADBEEF), 1, "");
119 zassert_equal(DT_REG_ADDR(TEST_DEADBEEF), 0xdeadbeef, "");
120 zassert_equal(DT_REG_SIZE(TEST_DEADBEEF), 0x1000, "");
121 zassert_equal(DT_PROP(TEST_DEADBEEF, gpio_controller), 1, "");
122 zassert_equal(DT_PROP(TEST_DEADBEEF, ngpios), 100, "");
123 zassert_true(!strcmp(DT_PROP(TEST_DEADBEEF, status), "okay"), "");
124 zassert_equal(DT_PROP_LEN(TEST_DEADBEEF, compatible), 1, "");
125 zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_DEADBEEF, compatible, 0),
126 "vnd,gpio-device"), "");
127 zassert_true(!strcmp(DT_PROP_LAST(TEST_DEADBEEF, compatible), "vnd,gpio-device"), "");
128 zassert_true(DT_NODE_HAS_PROP(TEST_DEADBEEF, status), "");
129 zassert_false(DT_NODE_HAS_PROP(TEST_DEADBEEF, foobar), "");
130
131 zassert_true(DT_SAME_NODE(TEST_ABCD1234, TEST_GPIO_2), "");
132 zassert_equal(DT_NUM_REGS(TEST_ABCD1234), 2, "");
133 zassert_equal(DT_PROP(TEST_ABCD1234, gpio_controller), 1, "");
134 zassert_equal(DT_PROP(TEST_ABCD1234, ngpios), 200, "");
135 zassert_true(!strcmp(DT_PROP(TEST_ABCD1234, status), "okay"), "");
136 zassert_equal(DT_PROP_LEN(TEST_ABCD1234, compatible), 1, "");
137 zassert_equal(DT_PROP_LEN_OR(TEST_ABCD1234, compatible, 4), 1, "");
138 zassert_equal(DT_PROP_LEN_OR(TEST_ABCD1234, invalid_property, 0), 0, "");
139 zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ABCD1234, compatible, 0),
140 "vnd,gpio-device"), "");
141 }
142
ZTEST(devicetree_api,test_alias_props)143 ZTEST(devicetree_api, test_alias_props)
144 {
145 zassert_equal(DT_HAS_ALIAS(test_alias), 1, "");
146 zassert_equal(DT_HAS_ALIAS(test_alias_none), 0, "");
147 zassert_equal(DT_NUM_REGS(TEST_ALIAS), 1, "");
148 zassert_equal(DT_REG_ADDR(TEST_ALIAS), 0xdeadbeef, "");
149 zassert_equal(DT_REG_SIZE(TEST_ALIAS), 0x1000, "");
150 zassert_true(DT_SAME_NODE(TEST_ALIAS, TEST_GPIO_1), "");
151 zassert_equal(DT_PROP(TEST_ALIAS, gpio_controller), 1, "");
152 zassert_equal(DT_PROP(TEST_ALIAS, ngpios), 100, "");
153 zassert_true(!strcmp(DT_PROP(TEST_ALIAS, status), "okay"), "");
154 zassert_equal(DT_PROP_LEN(TEST_ALIAS, compatible), 1, "");
155 zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ALIAS, compatible, 0),
156 "vnd,gpio-device"), "");
157 }
158
ZTEST(devicetree_api,test_nodelabel_props)159 ZTEST(devicetree_api, test_nodelabel_props)
160 {
161 zassert_equal(DT_NUM_REGS(TEST_NODELABEL), 1, "");
162 zassert_equal(DT_REG_ADDR(TEST_NODELABEL), 0xdeadbeef, "");
163 zassert_equal(DT_REG_SIZE(TEST_NODELABEL), 0x1000, "");
164 zassert_equal(DT_PROP(TEST_NODELABEL, gpio_controller), 1, "");
165 zassert_equal(DT_PROP(TEST_NODELABEL, ngpios), 100, "");
166 zassert_true(!strcmp(DT_PROP(TEST_NODELABEL, status), "okay"), "");
167 zassert_equal(DT_PROP_LEN(TEST_NODELABEL, compatible), 1, "");
168 zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_NODELABEL, compatible, 0),
169 "vnd,gpio-device"), "");
170 zassert_equal(DT_PROP_LEN(TEST_ENUM_0, val), 1, "");
171 }
172
173 #undef DT_DRV_COMPAT
174 #define DT_DRV_COMPAT vnd_gpio_device
ZTEST(devicetree_api,test_inst_props)175 ZTEST(devicetree_api, test_inst_props)
176 {
177 /*
178 * Careful:
179 *
180 * We can only test properties that are shared across all
181 * instances of this compatible here. This includes instances
182 * with status "disabled".
183 */
184
185 zassert_equal(DT_PROP(TEST_INST, gpio_controller), 1, "");
186 zassert_true(!strcmp(DT_PROP(TEST_INST, status), "okay") ||
187 !strcmp(DT_PROP(TEST_INST, status), "disabled"), "");
188 zassert_equal(DT_PROP_LEN(TEST_INST, compatible), 1, "");
189 zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_INST, compatible, 0),
190 "vnd,gpio-device"), "");
191
192 zassert_equal(DT_INST_NODE_HAS_PROP(0, gpio_controller), 1, "");
193 zassert_equal(DT_INST_PROP(0, gpio_controller), 1, "");
194 zassert_equal(DT_INST_NODE_HAS_PROP(0, xxxx), 0, "");
195 zassert_true(!strcmp(DT_INST_PROP(0, status), "okay") ||
196 !strcmp(DT_PROP(TEST_INST, status), "disabled"), "");
197 zassert_equal(DT_INST_PROP_LEN(0, compatible), 1, "");
198 zassert_true(!strcmp(DT_INST_PROP_BY_IDX(0, compatible, 0),
199 "vnd,gpio-device"), "");
200 }
201
202 #undef DT_DRV_COMPAT
203 #define DT_DRV_COMPAT vnd_device_with_props
ZTEST(devicetree_api,test_any_inst_prop)204 ZTEST(devicetree_api, test_any_inst_prop)
205 {
206 zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo), 1, "");
207 zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(bar), 1, "");
208 zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz), 0, "");
209 zassert_equal(DT_ANY_INST_HAS_PROP_STATUS_OKAY(does_not_exist), 0, "");
210
211 zassert_equal(COND_CODE_1(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo),
212 (5), (6)),
213 5, "");
214 zassert_equal(COND_CODE_0(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo),
215 (5), (6)),
216 6, "");
217 zassert_equal(COND_CODE_1(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz),
218 (5), (6)),
219 6, "");
220 zassert_equal(COND_CODE_0(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz),
221 (5), (6)),
222 5, "");
223 zassert_true(IS_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo)), "");
224 zassert_true(!IS_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz)), "");
225 zassert_equal(IF_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(foo), (1)) + 1,
226 2, "");
227 zassert_equal(IF_ENABLED(DT_ANY_INST_HAS_PROP_STATUS_OKAY(baz), (1)) + 1,
228 1, "");
229 }
230
231 #undef DT_DRV_COMPAT
ZTEST(devicetree_api,test_any_compat_inst_prop)232 ZTEST(devicetree_api, test_any_compat_inst_prop)
233 {
234 zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, foo), 1, "");
235 zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, bar), 1, "");
236 zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, baz), 0, "");
237 zassert_equal(DT_ANY_COMPAT_HAS_PROP_STATUS_OKAY(vnd_device_with_props, does_not_exist),
238 0, "");
239 }
240
241 #undef DT_DRV_COMPAT
242 #define DT_DRV_COMPAT vnd_device_with_props
ZTEST(devicetree_api,test_any_inst_bool)243 ZTEST(devicetree_api, test_any_inst_bool)
244 {
245 zassert_equal(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_foo), 1, "");
246 zassert_equal(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_bar), 1, "");
247 zassert_equal(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_baz), 0, "");
248 zassert_equal(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(does_not_exist), 0, "");
249
250 zassert_equal(COND_CODE_1(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_foo),
251 (5), (6)),
252 5, "");
253 zassert_equal(COND_CODE_0(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_foo),
254 (5), (6)),
255 6, "");
256 zassert_equal(COND_CODE_1(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_baz),
257 (5), (6)),
258 6, "");
259 zassert_equal(COND_CODE_0(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_baz),
260 (5), (6)),
261 5, "");
262 zassert_true(IS_ENABLED(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_foo)), "");
263 zassert_true(!IS_ENABLED(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_baz)), "");
264 zassert_equal(IF_ENABLED(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_foo), (1)) + 1,
265 2, "");
266 zassert_equal(IF_ENABLED(DT_ANY_INST_HAS_BOOL_STATUS_OKAY(bool_baz), (1)) + 1,
267 1, "");
268 }
269
ZTEST(devicetree_api,test_default_prop_access)270 ZTEST(devicetree_api, test_default_prop_access)
271 {
272 /*
273 * The APIs guarantee that the default_value is not expanded
274 * if the relevant property or cell is defined. This "X" macro
275 * is meant as poison which causes (hopefully) easy to
276 * understand build errors if this guarantee is not met due to
277 * a regression.
278 */
279 #undef X
280 #define X do.not.expand.this.argument
281
282 /* Node identifier variants. */
283 zassert_equal(DT_PROP_OR(TEST_REG, misc_prop, X), 1234, "");
284 zassert_equal(DT_PROP_OR(TEST_REG, not_a_property, -1), -1, "");
285
286 zassert_equal(DT_PHA_BY_IDX_OR(TEST_TEMP, dmas, 1, channel, X), 3, "");
287 zassert_equal(DT_PHA_BY_IDX_OR(TEST_TEMP, dmas, 1, not_a_cell, -1), -1,
288 "");
289
290 zassert_equal(DT_PHA_OR(TEST_TEMP, dmas, channel, X), 1, "");
291 zassert_equal(DT_PHA_OR(TEST_TEMP, dmas, not_a_cell, -1), -1, "");
292
293 zassert_equal(DT_PHA_BY_NAME_OR(TEST_TEMP, dmas, tx, channel, X), 1,
294 "");
295 zassert_equal(DT_PHA_BY_NAME_OR(TEST_TEMP, dmas, tx, not_a_cell, -1),
296 -1, "");
297
298 /* Instance number variants. */
299 #undef DT_DRV_COMPAT
300 #define DT_DRV_COMPAT vnd_reg_holder
301 zassert_equal(DT_INST_PROP_OR(0, misc_prop, X), 1234, "");
302 zassert_equal(DT_INST_PROP_OR(0, not_a_property, -1), -1, "");
303
304 #undef DT_DRV_COMPAT
305 #define DT_DRV_COMPAT vnd_array_holder
306 zassert_equal(DT_INST_PROP_LEN_OR(0, a, X), 3, "");
307 zassert_equal(DT_INST_PROP_LEN_OR(0, not_a_property, -1), -1, "");
308
309 #undef DT_DRV_COMPAT
310 #define DT_DRV_COMPAT vnd_adc_temp_sensor
311 zassert_equal(DT_INST_PHA_BY_IDX_OR(0, dmas, 1, channel, X), 3, "");
312 zassert_equal(DT_INST_PHA_BY_IDX_OR(0, dmas, 1, not_a_cell, -1), -1,
313 "");
314
315 zassert_equal(DT_INST_PHA_OR(0, dmas, channel, X), 1, "");
316 zassert_equal(DT_INST_PHA_OR(0, dmas, not_a_cell, -1), -1, "");
317
318 zassert_equal(DT_INST_PHA_BY_NAME_OR(0, dmas, tx, channel, X), 1,
319 "");
320 zassert_equal(DT_INST_PHA_BY_NAME_OR(0, dmas, tx, not_a_cell, -1), -1,
321 "");
322
323 #undef X
324 }
325
ZTEST(devicetree_api,test_has_path)326 ZTEST(devicetree_api, test_has_path)
327 {
328 zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_0), okay), 0, "");
329 zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_deadbeef), okay), 1,
330 "");
331 zassert_equal(DT_NODE_HAS_STATUS(DT_PATH(test, gpio_abcd1234), okay), 1,
332 "");
333 }
334
ZTEST(devicetree_api,test_has_alias)335 ZTEST(devicetree_api, test_has_alias)
336 {
337 zassert_equal(DT_NODE_HAS_STATUS(DT_ALIAS(test_alias), okay), 1, "");
338 zassert_equal(DT_NODE_HAS_STATUS(DT_ALIAS(test_undef), okay), 0, "");
339 }
340
ZTEST(devicetree_api,test_inst_checks)341 ZTEST(devicetree_api, test_inst_checks)
342 {
343 zassert_equal(DT_NODE_EXISTS(DT_INST(0, vnd_gpio_device)), 1, "");
344 zassert_equal(DT_NODE_EXISTS(DT_INST(1, vnd_gpio_device)), 1, "");
345 zassert_equal(DT_NODE_EXISTS(DT_INST(2, vnd_gpio_device)), 1, "");
346
347 zassert_equal(DT_NUM_INST_STATUS_OKAY(vnd_gpio_device), 2, "");
348 zassert_equal(DT_NUM_INST_STATUS_OKAY(xxxx), 0, "");
349 }
350
ZTEST(devicetree_api,test_has_nodelabel)351 ZTEST(devicetree_api, test_has_nodelabel)
352 {
353 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), okay), 0,
354 "");
355 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_nodelabel), okay), 1,
356 "");
357 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_nodelabel_allcaps),
358 okay),
359 1, "");
360 }
361
ZTEST(devicetree_api,test_has_compat)362 ZTEST(devicetree_api, test_has_compat)
363 {
364 unsigned int compats;
365
366 zassert_true(DT_HAS_COMPAT_STATUS_OKAY(vnd_gpio_device), "");
367 zassert_true(DT_HAS_COMPAT_STATUS_OKAY(vnd_gpio_device), "");
368 zassert_false(DT_HAS_COMPAT_STATUS_OKAY(vnd_disabled_compat), "");
369 zassert_false(DT_HAS_COMPAT_STATUS_OKAY(vnd_reserved_compat), "");
370
371 zassert_equal(TA_HAS_COMPAT(vnd_array_holder), 1, "");
372 zassert_equal(TA_HAS_COMPAT(vnd_undefined_compat), 1, "");
373 zassert_equal(TA_HAS_COMPAT(vnd_not_a_test_array_compat), 0, "");
374 compats = ((TA_HAS_COMPAT(vnd_array_holder) << 0) |
375 (TA_HAS_COMPAT(vnd_undefined_compat) << 1) |
376 (TA_HAS_COMPAT(vnd_not_a_test_array_compat) << 2));
377 zassert_equal(compats, 0x3, "");
378
379 #undef DT_DRV_COMPAT
380 #define DT_DRV_COMPAT vnd_model1
381 zassert_true(DT_INST_NODE_HAS_COMPAT(0, zephyr_model2));
382 }
383
ZTEST(devicetree_api,test_has_status)384 ZTEST(devicetree_api, test_has_status)
385 {
386 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), okay),
387 1, "");
388 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), disabled),
389 0, "");
390 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), reserved),
391 0, "");
392
393 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), okay),
394 1, "");
395 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), disabled),
396 0, "");
397 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), reserved),
398 0, "");
399
400 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), disabled),
401 1, "");
402 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), okay),
403 0, "");
404 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), reserved),
405 0, "");
406
407 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(reserved_gpio), reserved),
408 1, "");
409 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(reserved_gpio), disabled),
410 0, "");
411 zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(reserved_gpio), okay),
412 0, "");
413 }
414
ZTEST(devicetree_api,test_has_status_okay)415 ZTEST(devicetree_api, test_has_status_okay)
416 {
417 zassert_equal(DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(test_gpio_1)), 1);
418
419 zassert_equal(DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(test_no_status)), 1);
420
421 zassert_equal(DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(disabled_gpio)), 0);
422
423 zassert_equal(DT_NODE_HAS_STATUS_OKAY(DT_NODELABEL(reserved_gpio)), 0);
424 }
425
ZTEST(devicetree_api,test_bus)426 ZTEST(devicetree_api, test_bus)
427 {
428 int pin, flags;
429
430 zassert_true(DT_SAME_NODE(TEST_I3C_BUS, TEST_I3C), "");
431 zassert_true(DT_SAME_NODE(TEST_I2C_BUS, TEST_I2C), "");
432 zassert_true(DT_SAME_NODE(TEST_SPI_BUS_0, TEST_SPI), "");
433 zassert_true(DT_SAME_NODE(TEST_SPI_BUS_1, TEST_SPI), "");
434
435 zassert_equal(DT_SPI_DEV_HAS_CS_GPIOS(TEST_SPI_DEV_0), 1, "");
436 zassert_equal(DT_SPI_DEV_HAS_CS_GPIOS(TEST_SPI_DEV_NO_CS), 0, "");
437
438 /* Test a nested I2C bus using vnd,i2c-mux. */
439 zassert_true(DT_SAME_NODE(TEST_I2C_MUX_CTLR_1,
440 DT_BUS(TEST_MUXED_I2C_DEV_1)), "");
441 zassert_true(DT_SAME_NODE(TEST_I2C_MUX_CTLR_2,
442 DT_BUS(TEST_MUXED_I2C_DEV_2)), "");
443
444 #undef DT_DRV_COMPAT
445 #define DT_DRV_COMPAT vnd_spi_device_2
446 /* there is only one instance, and it has no CS */
447 zassert_equal(DT_INST_SPI_DEV_HAS_CS_GPIOS(0), 0, "");
448 /* since there's only one instance, we also know its bus. */
449 zassert_true(DT_SAME_NODE(TEST_SPI_NO_CS, DT_INST_BUS(0)),
450 "expected TEST_SPI_NO_CS as bus for vnd,spi-device-2");
451
452 #undef DT_DRV_COMPAT
453 #define DT_DRV_COMPAT vnd_spi_device
454 /*
455 * DT_INST_SPI_DEV: use with care here. We could be matching
456 * either vnd,spi-device.
457 */
458 zassert_equal(DT_INST_SPI_DEV_HAS_CS_GPIOS(0), 1, "");
459
460 #define CTLR_NODE DT_INST_SPI_DEV_CS_GPIOS_CTLR(0)
461 zassert_true(DT_SAME_NODE(CTLR_NODE, DT_NODELABEL(test_gpio_1)) ||
462 DT_SAME_NODE(CTLR_NODE, DT_NODELABEL(test_gpio_2)), "");
463 #undef CTLR_NODE
464
465 pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(0);
466 zassert_true((pin == 0x10) || (pin == 0x30), "");
467
468 flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(0);
469 zassert_true((flags == 0x20) || (flags == 0x40), "");
470
471 zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, spi), 1, "");
472 zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, i2c), 0, "");
473 zassert_equal(DT_ON_BUS(TEST_SPI_DEV_0, i3c), 0, "");
474
475 zassert_equal(DT_ON_BUS(TEST_I2C_DEV, i2c), 1, "");
476 zassert_equal(DT_ON_BUS(TEST_I2C_DEV, i3c), 0, "");
477 zassert_equal(DT_ON_BUS(TEST_I2C_DEV, spi), 0, "");
478
479 #undef DT_DRV_COMPAT
480 #define DT_DRV_COMPAT vnd_spi_device
481 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 2, "");
482
483 zassert_equal(DT_INST_ON_BUS(0, spi), 1, "");
484 zassert_equal(DT_INST_ON_BUS(0, i2c), 0, "");
485 zassert_equal(DT_INST_ON_BUS(0, i3c), 0, "");
486
487 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 1, "");
488 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 0, "");
489 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 0, "");
490
491 #undef DT_DRV_COMPAT
492 #define DT_DRV_COMPAT vnd_i2c_device
493 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 2, "");
494
495 zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "");
496 zassert_equal(DT_INST_ON_BUS(0, i3c), 0, "");
497 zassert_equal(DT_INST_ON_BUS(0, spi), 0, "");
498
499 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 1, "");
500 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 0, "");
501 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 0, "");
502
503 #undef DT_DRV_COMPAT
504 #define DT_DRV_COMPAT vnd_i3c_device
505 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
506
507 zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "");
508 zassert_equal(DT_INST_ON_BUS(0, i3c), 1, "");
509 zassert_equal(DT_INST_ON_BUS(0, spi), 0, "");
510
511 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 1, "");
512 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 1, "");
513 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 0, "");
514
515 #undef DT_DRV_COMPAT
516 #define DT_DRV_COMPAT vnd_i3c_i2c_device
517 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
518
519 zassert_equal(DT_INST_ON_BUS(0, i2c), 1, "");
520 zassert_equal(DT_INST_ON_BUS(0, i3c), 1, "");
521 zassert_equal(DT_INST_ON_BUS(0, spi), 0, "");
522
523 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c), 1, "");
524 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(i3c), 1, "");
525 zassert_equal(DT_ANY_INST_ON_BUS_STATUS_OKAY(spi), 0, "");
526
527 #undef DT_DRV_COMPAT
528
529 /*
530 * Make sure the underlying DT_HAS_COMPAT_ON_BUS_STATUS_OKAY used by
531 * DT_ANY_INST_ON_BUS works without DT_DRV_COMPAT defined.
532 */
533 zassert_equal(DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(vnd_spi_device, spi), 1);
534 zassert_equal(DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(vnd_spi_device, i2c), 0);
535
536 zassert_equal(DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(vnd_i2c_device, i2c), 1);
537 zassert_equal(DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(vnd_i2c_device, spi), 0);
538
539 zassert_equal(DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(vnd_gpio_expander, i2c), 1,
540 NULL);
541 zassert_equal(DT_HAS_COMPAT_ON_BUS_STATUS_OKAY(vnd_gpio_expander, spi), 1,
542 NULL);
543 }
544
545 #undef DT_DRV_COMPAT
546 #define DT_DRV_COMPAT vnd_vendor
547
548 #define VND_VENDOR "A stand-in for a real vendor which can be used in examples and tests"
549 #define ZEP_VENDOR "Zephyr-specific binding"
550
ZTEST(devicetree_api,test_vendor)551 ZTEST(devicetree_api, test_vendor)
552 {
553 /* DT_NODE_VENDOR_HAS_IDX */
554 zassert_true(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 0), "");
555 zassert_false(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 1), "");
556 zassert_true(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 2), "");
557 zassert_false(DT_NODE_VENDOR_HAS_IDX(TEST_VENDOR, 3), "");
558
559 /* DT_NODE_VENDOR_BY_IDX */
560 zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX(TEST_VENDOR, 0), VND_VENDOR), "");
561 zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX(TEST_VENDOR, 2), ZEP_VENDOR), "");
562
563 /* DT_NODE_VENDOR_BY_IDX_OR */
564 zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 0, NULL), VND_VENDOR), "");
565 zassert_is_null(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 1, NULL), "");
566 zassert_true(!strcmp(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 2, NULL), ZEP_VENDOR), "");
567 zassert_is_null(DT_NODE_VENDOR_BY_IDX_OR(TEST_VENDOR, 3, NULL), "");
568
569 /* DT_NODE_VENDOR_OR */
570 zassert_true(!strcmp(DT_NODE_VENDOR_OR(TEST_VENDOR, NULL), VND_VENDOR), "");
571 }
572
573 #define VND_MODEL "model1"
574 #define ZEP_MODEL "model2"
575
ZTEST(devicetree_api,test_model)576 ZTEST(devicetree_api, test_model)
577 {
578 /* DT_NODE_MODEL_HAS_IDX */
579 zassert_true(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 0), "");
580 zassert_false(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 1), "");
581 zassert_true(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 2), "");
582 zassert_false(DT_NODE_MODEL_HAS_IDX(TEST_MODEL, 3), "");
583
584 /* DT_NODE_MODEL_BY_IDX */
585 zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX(TEST_MODEL, 0), VND_MODEL), "");
586 zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX(TEST_MODEL, 2), ZEP_MODEL), "");
587
588 /* DT_NODE_MODEL_BY_IDX_OR */
589 zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 0, NULL), VND_MODEL), "");
590 zassert_is_null(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 1, NULL), "");
591 zassert_true(!strcmp(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 2, NULL), ZEP_MODEL), "");
592 zassert_is_null(DT_NODE_MODEL_BY_IDX_OR(TEST_MODEL, 3, NULL), "");
593
594 /* DT_NODE_MODEL_OR */
595 zassert_true(!strcmp(DT_NODE_MODEL_OR(TEST_MODEL, NULL), VND_MODEL), "");
596 }
597
598 #undef ZEP_MODEL
599 #undef VND_MODEL
600
601 #undef DT_DRV_COMPAT
602 #define DT_DRV_COMPAT vnd_reg_holder
ZTEST(devicetree_api,test_reg)603 ZTEST(devicetree_api, test_reg)
604 {
605 /* DT_REG_HAS_IDX */
606 zassert_true(DT_REG_HAS_IDX(TEST_ABCD1234, 0), "");
607 zassert_true(DT_REG_HAS_IDX(TEST_ABCD1234, 1), "");
608 zassert_false(DT_REG_HAS_IDX(TEST_ABCD1234, 2), "");
609
610 /* DT_REG_HAS_NAME */
611 zassert_true(DT_REG_HAS_NAME(TEST_ABCD1234, one), "");
612 zassert_true(DT_REG_HAS_NAME(TEST_ABCD1234, two), "");
613 zassert_false(DT_REG_HAS_NAME(TEST_ABCD1234, three), "");
614
615 /* DT_REG_ADDR_BY_IDX */
616 zassert_equal(DT_REG_ADDR_BY_IDX(TEST_ABCD1234, 0), 0xabcd1234, "");
617 zassert_equal(DT_REG_ADDR_BY_IDX(TEST_ABCD1234, 1), 0x98765432, "");
618
619 /* DT_REG_SIZE_BY_IDX */
620 zassert_equal(DT_REG_SIZE_BY_IDX(TEST_ABCD1234, 0), 0x500, "");
621 zassert_equal(DT_REG_SIZE_BY_IDX(TEST_ABCD1234, 1), 0xff, "");
622
623 /* DT_REG_ADDR */
624 zassert_equal(DT_REG_ADDR(TEST_ABCD1234), 0xabcd1234, "");
625
626 /* DT_REG_ADDR_U64 */
627 zassert_equal(DT_REG_ADDR_U64(TEST_ABCD1234), 0xabcd1234, "");
628
629 /* DT_REG_SIZE */
630 zassert_equal(DT_REG_SIZE(TEST_ABCD1234), 0x500, "");
631
632 /* DT_REG_ADDR_BY_NAME */
633 zassert_equal(DT_REG_ADDR_BY_NAME(TEST_ABCD1234, one), 0xabcd1234, "");
634 zassert_equal(DT_REG_ADDR_BY_NAME(TEST_ABCD1234, two), 0x98765432, "");
635
636 /* DT_REG_ADDR_BY_NAME_OR */
637 zassert_equal(DT_REG_ADDR_BY_NAME_OR(TEST_ABCD1234, one, 0x10), 0xabcd1234, "");
638 zassert_equal(DT_REG_ADDR_BY_NAME_OR(TEST_ABCD1234, two, 0x11), 0x98765432, "");
639 zassert_equal(DT_REG_ADDR_BY_NAME_OR(TEST_ABCD1234, three, 0x12), 0x12, "");
640
641 /* DT_REG_ADDR_BY_NAME_U64 */
642 zassert_equal(DT_REG_ADDR_BY_NAME_U64(TEST_ABCD1234, one), 0xabcd1234, "");
643 zassert_equal(DT_REG_ADDR_BY_NAME_U64(TEST_ABCD1234, two), 0x98765432, "");
644
645 /* DT_REG_SIZE_BY_NAME */
646 zassert_equal(DT_REG_SIZE_BY_NAME(TEST_ABCD1234, one), 0x500, "");
647 zassert_equal(DT_REG_SIZE_BY_NAME(TEST_ABCD1234, two), 0xff, "");
648
649 /* DT_REG_SIZE_BY_NAME_OR */
650 zassert_equal(DT_REG_SIZE_BY_NAME_OR(TEST_ABCD1234, one, 0x10), 0x500, "");
651 zassert_equal(DT_REG_SIZE_BY_NAME_OR(TEST_ABCD1234, two, 0x11), 0xff, "");
652 zassert_equal(DT_REG_SIZE_BY_NAME_OR(TEST_ABCD1234, three, 0x12), 0x12, "");
653
654 /* DT_INST */
655 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
656
657 /* DT_INST_REG_HAS_IDX */
658 zassert_true(DT_INST_REG_HAS_IDX(0, 0), "");
659 zassert_true(DT_INST_REG_HAS_IDX(0, 1), "");
660 zassert_false(DT_INST_REG_HAS_IDX(0, 2), "");
661
662 /* DT_INST_REG_HAS_NAME */
663 zassert_true(DT_INST_REG_HAS_NAME(0, first), "");
664 zassert_true(DT_INST_REG_HAS_NAME(0, second), "");
665 zassert_false(DT_INST_REG_HAS_NAME(0, third), "");
666
667 /* DT_INST_REG_ADDR_BY_IDX */
668 zassert_equal(DT_INST_REG_ADDR_BY_IDX(0, 0), 0x9999aaaa, "");
669 zassert_equal(DT_INST_REG_ADDR_BY_IDX(0, 1), 0xbbbbcccc, "");
670
671 /* DT_INST_REG_SIZE_BY_IDX */
672 zassert_equal(DT_INST_REG_SIZE_BY_IDX(0, 0), 0x1000, "");
673 zassert_equal(DT_INST_REG_SIZE_BY_IDX(0, 1), 0x3f, "");
674
675 /* DT_INST_REG_ADDR */
676 zassert_equal(DT_INST_REG_ADDR(0), 0x9999aaaa, "");
677
678 /* DT_INST_REG_ADDR_U64 */
679 zassert_equal(DT_INST_REG_ADDR_U64(0), 0x9999aaaa, "");
680
681 /* DT_INST_REG_SIZE */
682 zassert_equal(DT_INST_REG_SIZE(0), 0x1000, "");
683
684 /* DT_INST_REG_ADDR_BY_NAME */
685 zassert_equal(DT_INST_REG_ADDR_BY_NAME(0, first), 0x9999aaaa, "");
686 zassert_equal(DT_INST_REG_ADDR_BY_NAME(0, second), 0xbbbbcccc, "");
687
688 /* DT_INST_REG_ADDR_BY_NAME_OR */
689 zassert_equal(DT_INST_REG_ADDR_BY_NAME_OR(0, first, 0x10), 0x9999aaaa, "");
690 zassert_equal(DT_INST_REG_ADDR_BY_NAME_OR(0, second, 0x11), 0xbbbbcccc, "");
691 zassert_equal(DT_INST_REG_SIZE_BY_NAME_OR(0, third, 0x12), 0x12, "");
692
693 /* DT_INST_REG_ADDR_BY_NAME_U64 */
694 zassert_equal(DT_INST_REG_ADDR_BY_NAME_U64(0, first), 0x9999aaaa, "");
695 zassert_equal(DT_INST_REG_ADDR_BY_NAME_U64(0, second), 0xbbbbcccc, "");
696
697 /* DT_INST_REG_SIZE_BY_NAME */
698 zassert_equal(DT_INST_REG_SIZE_BY_NAME(0, first), 0x1000, "");
699 zassert_equal(DT_INST_REG_SIZE_BY_NAME(0, second), 0x3f, "");
700
701 /* DT_REG_SIZE_BY_NAME_OR */
702 zassert_equal(DT_INST_REG_SIZE_BY_NAME_OR(0, first, 0x10), 0x1000, "");
703 zassert_equal(DT_INST_REG_SIZE_BY_NAME_OR(0, second, 0x11), 0x3f, "");
704 zassert_equal(DT_INST_REG_SIZE_BY_NAME_OR(0, third, 0x12), 0x12, "");
705 }
706
707 #undef DT_DRV_COMPAT
708 #define DT_DRV_COMPAT vnd_reg_holder_64
ZTEST(devicetree_api,test_reg_64)709 ZTEST(devicetree_api, test_reg_64)
710 {
711 /* DT_REG_ADDR_U64 */
712 zassert_equal(DT_REG_ADDR_U64(TEST_64BIT), 0xffffffff11223344, "");
713
714 /* DT_REG_ADDR_BY_NAME_U64 */
715 zassert_equal(DT_REG_ADDR_BY_NAME_U64(TEST_64BIT, test_name), 0xffffffff11223344, "");
716
717 /* DT_INST_REG_ADDR_U64 */
718 zassert_equal(DT_INST_REG_ADDR_U64(0), 0xffffffff11223344, "");
719
720 /* DT_INST_REG_ADDR_BY_NAME_U64 */
721 zassert_equal(DT_INST_REG_ADDR_BY_NAME_U64(0, test_name), 0xffffffff11223344, "");
722 }
723
724 #undef DT_DRV_COMPAT
725 #define DT_DRV_COMPAT vnd_interrupt_holder
ZTEST(devicetree_api,test_irq)726 ZTEST(devicetree_api, test_irq)
727 {
728 /* DT_NUM_IRQS */
729 zassert_equal(DT_NUM_IRQS(TEST_DEADBEEF), 1, "");
730 zassert_equal(DT_NUM_IRQS(TEST_I2C_BUS), 2, "");
731 zassert_equal(DT_NUM_IRQS(TEST_SPI), 3, "");
732
733 /* DT_IRQ_HAS_IDX */
734 zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 0), "");
735 zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 1), "");
736 zassert_true(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 2), "");
737 zassert_false(DT_IRQ_HAS_IDX(TEST_SPI_BUS_0, 3), "");
738
739 zassert_true(DT_IRQ_HAS_IDX(TEST_DEADBEEF, 0), "");
740 zassert_false(DT_IRQ_HAS_IDX(TEST_DEADBEEF, 1), "");
741
742 zassert_true(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 0), "");
743 zassert_true(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 1), "");
744 zassert_false(DT_IRQ_HAS_IDX(TEST_I2C_BUS, 2), "");
745
746 /* DT_IRQ_BY_IDX */
747 zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 0, irq), 8, "");
748 zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 1, irq), 9, "");
749 zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 2, irq), 10, "");
750 zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 0, priority), 3, "");
751 zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 1, priority), 0, "");
752 zassert_equal(DT_IRQ_BY_IDX(TEST_SPI_BUS_0, 2, priority), 1, "");
753
754 /* DT_IRQ_BY_NAME */
755 zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, status, irq), 6, "");
756 zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, error, irq), 7, "");
757 zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, status, priority), 2, "");
758 zassert_equal(DT_IRQ_BY_NAME(TEST_I2C_BUS, error, priority), 1, "");
759
760 /* DT_IRQ_HAS_CELL_AT_IDX */
761 zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, irq), "");
762 zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, priority), "");
763 zassert_false(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 0, foo), 0, "");
764 zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, irq), "");
765 zassert_true(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, priority), "");
766 zassert_false(DT_IRQ_HAS_CELL_AT_IDX(TEST_IRQ, 2, foo), "");
767
768 /* DT_IRQ_HAS_CELL */
769 zassert_true(DT_IRQ_HAS_CELL(TEST_IRQ, irq), "");
770 zassert_true(DT_IRQ_HAS_CELL(TEST_IRQ, priority), "");
771 zassert_false(DT_IRQ_HAS_CELL(TEST_IRQ, foo), "");
772
773 /* DT_IRQ_HAS_NAME */
774 zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, err), "");
775 zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, stat), "");
776 zassert_true(DT_IRQ_HAS_NAME(TEST_IRQ, done), "");
777 zassert_false(DT_IRQ_HAS_NAME(TEST_IRQ, alpha), "");
778
779 /* DT_IRQ */
780 zassert_equal(DT_IRQ(TEST_I2C_BUS, irq), 6, "");
781 zassert_equal(DT_IRQ(TEST_I2C_BUS, priority), 2, "");
782
783 /* DT_IRQN */
784 #ifndef CONFIG_MULTI_LEVEL_INTERRUPTS
785 zassert_equal(DT_IRQN(TEST_I2C_BUS), 6, "");
786 zassert_equal(DT_IRQN(DT_INST(0, DT_DRV_COMPAT)), 30, "");
787 #else
788 zassert_equal(DT_IRQN(TEST_I2C_BUS),
789 ((6 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
790 zassert_equal(DT_IRQN(DT_INST(0, DT_DRV_COMPAT)),
791 ((30 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
792 #endif
793
794 /* DT_IRQN_BY_IDX */
795 #ifndef CONFIG_MULTI_LEVEL_INTERRUPTS
796 zassert_equal(DT_IRQN_BY_IDX(DT_INST(0, DT_DRV_COMPAT), 0), 30, "");
797 zassert_equal(DT_IRQN_BY_IDX(DT_INST(0, DT_DRV_COMPAT), 1), 40, "");
798 zassert_equal(DT_IRQN_BY_IDX(DT_INST(0, DT_DRV_COMPAT), 2), 60, "");
799 #else
800 zassert_equal(DT_IRQN_BY_IDX(DT_INST(0, DT_DRV_COMPAT), 0),
801 ((30 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
802 zassert_equal(DT_IRQN_BY_IDX(DT_INST(0, DT_DRV_COMPAT), 1),
803 ((40 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
804 zassert_equal(DT_IRQN_BY_IDX(DT_INST(0, DT_DRV_COMPAT), 2),
805 ((60 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
806 #endif
807
808 /* DT_INST */
809 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
810
811 /* DT_INST_NUM_IRQS */
812 zassert_equal(DT_INST_NUM_IRQS(0), 3);
813
814 /* DT_INST_IRQ_HAS_IDX */
815 zassert_equal(DT_INST_IRQ_HAS_IDX(0, 0), 1, "");
816 zassert_equal(DT_INST_IRQ_HAS_IDX(0, 1), 1, "");
817 zassert_equal(DT_INST_IRQ_HAS_IDX(0, 2), 1, "");
818 zassert_equal(DT_INST_IRQ_HAS_IDX(0, 3), 0, "");
819
820 /* DT_INST_IRQ_BY_IDX */
821 zassert_equal(DT_INST_IRQ_BY_IDX(0, 0, irq), 30, "");
822 zassert_equal(DT_INST_IRQ_BY_IDX(0, 1, irq), 40, "");
823 zassert_equal(DT_INST_IRQ_BY_IDX(0, 2, irq), 60, "");
824 zassert_equal(DT_INST_IRQ_BY_IDX(0, 0, priority), 3, "");
825 zassert_equal(DT_INST_IRQ_BY_IDX(0, 1, priority), 5, "");
826 zassert_equal(DT_INST_IRQ_BY_IDX(0, 2, priority), 7, "");
827
828 /* DT_INST_IRQ_BY_NAME */
829 zassert_equal(DT_INST_IRQ_BY_NAME(0, err, irq), 30, "");
830 zassert_equal(DT_INST_IRQ_BY_NAME(0, stat, irq), 40, "");
831 zassert_equal(DT_INST_IRQ_BY_NAME(0, done, irq), 60, "");
832 zassert_equal(DT_INST_IRQ_BY_NAME(0, err, priority), 3, "");
833 zassert_equal(DT_INST_IRQ_BY_NAME(0, stat, priority), 5, "");
834 zassert_equal(DT_INST_IRQ_BY_NAME(0, done, priority), 7, "");
835
836 /* DT_INST_IRQ */
837 zassert_equal(DT_INST_IRQ(0, irq), 30, "");
838 zassert_equal(DT_INST_IRQ(0, priority), 3, "");
839
840 /* DT_INST_IRQN */
841 #ifndef CONFIG_MULTI_LEVEL_INTERRUPTS
842 zassert_equal(DT_INST_IRQN(0), 30, "");
843 #else
844 zassert_equal(DT_INST_IRQN(0), ((30 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
845 #endif
846
847 /* DT_INST_IRQN_BY_IDX */
848 #ifndef CONFIG_MULTI_LEVEL_INTERRUPTS
849 zassert_equal(DT_INST_IRQN_BY_IDX(0, 0), 30, "");
850 zassert_equal(DT_INST_IRQN_BY_IDX(0, 1), 40, "");
851 zassert_equal(DT_INST_IRQN_BY_IDX(0, 2), 60, "");
852 #else
853 zassert_equal(DT_INST_IRQN_BY_IDX(0, 0),
854 ((30 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
855 zassert_equal(DT_INST_IRQN_BY_IDX(0, 1),
856 ((40 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
857 zassert_equal(DT_INST_IRQN_BY_IDX(0, 2),
858 ((60 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
859 #endif
860
861 /* DT_INST_IRQ_HAS_CELL_AT_IDX */
862 zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, irq), "");
863 zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, priority), "");
864 zassert_false(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 0, foo), "");
865 zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, irq), "");
866 zassert_true(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, priority), "");
867 zassert_false(DT_INST_IRQ_HAS_CELL_AT_IDX(0, 2, foo), "");
868
869 /* DT_INST_IRQ_HAS_CELL */
870 zassert_true(DT_INST_IRQ_HAS_CELL(0, irq), "");
871 zassert_true(DT_INST_IRQ_HAS_CELL(0, priority), "");
872 zassert_false(DT_INST_IRQ_HAS_CELL(0, foo), "");
873
874 /* DT_INST_IRQ_HAS_NAME */
875 zassert_true(DT_INST_IRQ_HAS_NAME(0, err), "");
876 zassert_true(DT_INST_IRQ_HAS_NAME(0, stat), "");
877 zassert_true(DT_INST_IRQ_HAS_NAME(0, done), "");
878 zassert_false(DT_INST_IRQ_HAS_NAME(0, alpha), "");
879
880 #ifdef CONFIG_MULTI_LEVEL_INTERRUPTS
881 /* the following asserts check if interrupt IDs are encoded
882 * properly when dealing with a node that consumes interrupts
883 * from L2 aggregators extending different L1 interrupts.
884 */
885 zassert_equal(DT_IRQN_BY_IDX(TEST_IRQ_EXT, 0),
886 ((70 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 11, "");
887 zassert_equal(DT_IRQN_BY_IDX(TEST_IRQ_EXT, 2),
888 ((42 + 1) << CONFIG_1ST_LEVEL_INTERRUPT_BITS) | 12, "");
889 #else
890 zassert_equal(DT_IRQN_BY_IDX(TEST_IRQ_EXT, 0), 70, "");
891 zassert_equal(DT_IRQN_BY_IDX(TEST_IRQ_EXT, 2), 42, "");
892 #endif /* CONFIG_MULTI_LEVEL_INTERRUPTS */
893 }
894
ZTEST(devicetree_api,test_irq_level)895 ZTEST(devicetree_api, test_irq_level)
896 {
897 /* DT_IRQ_LEVEL */
898 zassert_equal(DT_IRQ_LEVEL(TEST_TEMP), 0, "");
899 zassert_equal(DT_IRQ_LEVEL(TEST_INTC), 1, "");
900 zassert_equal(DT_IRQ_LEVEL(TEST_SPI), 2, "");
901
902 /* DT_IRQ_LEVEL */
903 #undef DT_DRV_COMPAT
904 #define DT_DRV_COMPAT vnd_adc_temp_sensor
905 zassert_equal(DT_INST_IRQ_LEVEL(0), 0, "");
906
907 #undef DT_DRV_COMPAT
908 #define DT_DRV_COMPAT vnd_intc
909 zassert_equal(DT_INST_IRQ_LEVEL(1), 1, "");
910
911 #undef DT_DRV_COMPAT
912 #define DT_DRV_COMPAT vnd_spi
913 zassert_equal(DT_INST_IRQ_LEVEL(0), 2, "");
914 }
915
916 struct gpios_struct {
917 gpio_pin_t pin;
918 gpio_flags_t flags;
919 };
920
921 #define CLOCK_FREQUENCY_AND_COMMA(node_id, prop, idx) \
922 DT_PROP_BY_PHANDLE_IDX(node_id, prop, idx, clock_frequency),
923
924 /* Helper macro that UTIL_LISTIFY can use and produces an element with comma */
925 #define DT_GPIO_ELEM(idx, node_id, prop) \
926 { \
927 DT_PHA_BY_IDX(node_id, prop, idx, pin),\
928 DT_PHA_BY_IDX(node_id, prop, idx, flags),\
929 }
930 #define DT_GPIO_LISTIFY(node_id, prop) \
931 { LISTIFY(DT_PROP_LEN(node_id, prop), DT_GPIO_ELEM, (,), \
932 node_id, prop) }
933
934 #undef DT_DRV_COMPAT
935 #define DT_DRV_COMPAT vnd_phandle_holder
ZTEST(devicetree_api,test_phandles)936 ZTEST(devicetree_api, test_phandles)
937 {
938 bool gpio_controller = DT_PROP_BY_PHANDLE(TEST_PH, ph, gpio_controller);
939 size_t phs_freqs[] = { DT_FOREACH_PROP_ELEM(TEST_PH, phs, CLOCK_FREQUENCY_AND_COMMA) };
940 struct gpios_struct gps[] = DT_GPIO_LISTIFY(TEST_PH, gpios);
941
942 /* phandle */
943 zassert_true(DT_NODE_HAS_PROP(TEST_PH, ph), "");
944 zassert_true(DT_SAME_NODE(DT_PROP(TEST_PH, ph),
945 DT_NODELABEL(test_gpio_1)), "");
946 zassert_equal(DT_PROP_LEN(TEST_PH, ph), 1, "");
947 zassert_true(DT_SAME_NODE(DT_PROP_BY_IDX(TEST_PH, ph, 0),
948 DT_NODELABEL(test_gpio_1)), "");
949 /* DT_PROP_BY_PHANDLE */
950 zassert_equal(gpio_controller, true, "");
951
952 /* phandles */
953 zassert_true(DT_NODE_HAS_PROP(TEST_PH, phs), "");
954 zassert_equal(ARRAY_SIZE(phs_freqs), 2, "");
955 zassert_equal(DT_PROP_LEN(TEST_PH, phs), 2, "");
956 zassert_true(DT_SAME_NODE(DT_PROP_BY_IDX(TEST_PH, phs, 1),
957 TEST_SPI), "");
958
959 /* DT_FOREACH_PROP_ELEM on a phandles type property */
960 zassert_equal(phs_freqs[0], 100000, "");
961 zassert_equal(phs_freqs[1], 2000000, "");
962
963 /* DT_PROP_BY_PHANDLE_IDX on a phandles type property */
964 zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 0, clock_frequency),
965 100000, "");
966 zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, phs, 1, clock_frequency),
967 2000000, "");
968
969 /* DT_PROP_BY_PHANDLE_IDX on a phandle-array type property */
970 zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 0,
971 ngpios), 100, "");
972 zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 1,
973 ngpios), 200, "");
974 zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 0, status),
975 "okay"), "");
976 zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX(TEST_PH, gpios, 1, status),
977 "okay"), "");
978
979 /* DT_PROP_BY_PHANDLE_IDX_OR */
980 zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX_OR(TEST_PH, phs_or, 0,
981 val, "zero"), "one"), "");
982 zassert_true(!strcmp(DT_PROP_BY_PHANDLE_IDX_OR(TEST_PH, phs_or, 1,
983 val, "zero"), "zero"), "");
984
985 /* phandle-array */
986 zassert_true(DT_NODE_HAS_PROP(TEST_PH, gpios), "");
987 zassert_equal(ARRAY_SIZE(gps), 2, "");
988 zassert_equal(DT_PROP_LEN(TEST_PH, gpios), 2, "");
989
990 /* DT_PROP_HAS_IDX */
991 zassert_true(DT_PROP_HAS_IDX(TEST_PH, gpios, 0), "");
992 zassert_true(DT_PROP_HAS_IDX(TEST_PH, gpios, 1), "");
993 zassert_false(DT_PROP_HAS_IDX(TEST_PH, gpios, 2), "");
994
995 /* DT_PROP_HAS_NAME */
996 zassert_false(DT_PROP_HAS_NAME(TEST_PH, foos, A), "");
997 zassert_true(DT_PROP_HAS_NAME(TEST_PH, foos, a), "");
998 zassert_false(DT_PROP_HAS_NAME(TEST_PH, foos, b-c), "");
999 zassert_true(DT_PROP_HAS_NAME(TEST_PH, foos, b_c), "");
1000 zassert_false(DT_PROP_HAS_NAME(TEST_PH, bazs, jane), "");
1001
1002 /* DT_PHA_HAS_CELL_AT_IDX */
1003 zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, gpios, 1, pin), "");
1004 zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, gpios, 1, flags), "");
1005 /* pha-gpios index 1 has nothing, not even a phandle */
1006 zassert_false(DT_PROP_HAS_IDX(TEST_PH, pha_gpios, 1), "");
1007 zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 1, pin), "");
1008 zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 1, flags),
1009 "");
1010 /* index 2 only has a pin cell, no flags */
1011 zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 2, pin), "");
1012 zassert_false(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 2, flags),
1013 "");
1014 /* index 3 has both pin and flags cells*/
1015 zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 3, pin), "");
1016 zassert_true(DT_PHA_HAS_CELL_AT_IDX(TEST_PH, pha_gpios, 3, flags), "");
1017 /* even though index 1 has nothing, the length is still 4 */
1018 zassert_equal(DT_PROP_LEN(TEST_PH, pha_gpios), 4, "");
1019
1020 /* DT_PHA_HAS_CELL */
1021 zassert_true(DT_PHA_HAS_CELL(TEST_PH, gpios, flags), "");
1022 zassert_false(DT_PHA_HAS_CELL(TEST_PH, gpios, bar), "");
1023
1024 /* DT_PHANDLE_BY_IDX */
1025 zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_IDX(TEST_PH, gpios, 0), TEST_GPIO_1), "");
1026 zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_IDX(TEST_PH, gpios, 1), TEST_GPIO_2), "");
1027
1028 /* DT_PHANDLE */
1029 zassert_true(DT_SAME_NODE(DT_PHANDLE(TEST_PH, gpios), TEST_GPIO_1), "");
1030
1031 /* DT_PHA */
1032 zassert_equal(DT_PHA(TEST_PH, gpios, pin), 10, "");
1033 zassert_equal(DT_PHA(TEST_PH, gpios, flags), 20, "");
1034
1035 /* DT_PHA_BY_IDX */
1036 zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 0, pin), 10, "");
1037 zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 0, flags), 20, "");
1038
1039 zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 1, pin), 30, "");
1040 zassert_equal(DT_PHA_BY_IDX(TEST_PH, gpios, 1, flags), 40, "");
1041
1042 /* DT_PHA_BY_NAME */
1043 zassert_equal(DT_PHA_BY_NAME(TEST_PH, foos, a, foocell), 100, "");
1044 zassert_equal(DT_PHA_BY_NAME(TEST_PH, foos, b_c, foocell), 110, "");
1045
1046 /* DT_PHANDLE_BY_NAME */
1047 zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_NAME(TEST_PH, foos, a), TEST_GPIO_1), "");
1048 zassert_true(DT_SAME_NODE(DT_PHANDLE_BY_NAME(TEST_PH, foos, b_c), TEST_GPIO_2), "");
1049
1050 /* array initializers */
1051 zassert_equal(gps[0].pin, 10, "");
1052 zassert_equal(gps[0].flags, 20, "");
1053
1054 zassert_equal(gps[1].pin, 30, "");
1055 zassert_equal(gps[1].flags, 40, "");
1056
1057 /* DT_INST */
1058 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1059
1060 /* DT_INST_PROP_BY_PHANDLE */
1061 zassert_equal(DT_INST_PROP_BY_PHANDLE(0, ph, ngpios), 100, "");
1062
1063 /* DT_INST_PROP_BY_PHANDLE_IDX */
1064 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 0, clock_frequency),
1065 100000, "");
1066 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, phs, 1, clock_frequency),
1067 2000000, "");
1068 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 0,
1069 gpio_controller),
1070 1, "");
1071 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 1,
1072 gpio_controller),
1073 1, "");
1074 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 0, ngpios),
1075 100, "");
1076 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, gpios, 1, ngpios),
1077 200, "");
1078
1079 /* DT_INST_PROP_HAS_IDX */
1080 zassert_true(DT_INST_PROP_HAS_IDX(0, gpios, 0), "");
1081 zassert_true(DT_INST_PROP_HAS_IDX(0, gpios, 1), "");
1082 zassert_false(DT_INST_PROP_HAS_IDX(0, gpios, 2), "");
1083
1084 /* DT_INST_PROP_HAS_NAME */
1085 zassert_false(DT_INST_PROP_HAS_NAME(0, foos, A), "");
1086 zassert_true(DT_INST_PROP_HAS_NAME(0, foos, a), "");
1087 zassert_false(DT_INST_PROP_HAS_NAME(0, foos, b-c), "");
1088 zassert_true(DT_INST_PROP_HAS_NAME(0, foos, b_c), "");
1089 zassert_false(DT_INST_PROP_HAS_NAME(0, bazs, jane), "");
1090
1091 /* DT_INST_PHA_HAS_CELL_AT_IDX */
1092 zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, gpios, 1, pin), "");
1093 zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, gpios, 1, flags), "");
1094 /* index 1 has nothing, not even a phandle */
1095 zassert_false(DT_INST_PROP_HAS_IDX(0, pha_gpios, 1), "");
1096 zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 1, pin), "");
1097 zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 1, flags), "");
1098 /* index 2 only has pin, no flags */
1099 zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 2, pin), "");
1100 zassert_false(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 2, flags), "");
1101 /* index 3 has both pin and flags */
1102 zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 3, pin), "");
1103 zassert_true(DT_INST_PHA_HAS_CELL_AT_IDX(0, pha_gpios, 3, flags), "");
1104 /* even though index 1 has nothing, the length is still 4 */
1105 zassert_equal(DT_INST_PROP_LEN(0, pha_gpios), 4, "");
1106
1107 /* DT_INST_PHA_HAS_CELL */
1108 zassert_true(DT_INST_PHA_HAS_CELL(0, gpios, flags), "");
1109 zassert_false(DT_INST_PHA_HAS_CELL(0, gpios, bar), "");
1110
1111 /* DT_INST_PHANDLE_BY_IDX */
1112 zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_IDX(0, gpios, 0), TEST_GPIO_1), "");
1113 zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_IDX(0, gpios, 1), TEST_GPIO_2), "");
1114
1115 /* DT_INST_PHANDLE */
1116 zassert_true(DT_SAME_NODE(DT_INST_PHANDLE(0, gpios), TEST_GPIO_1), "");
1117
1118 /* DT_INST_PHA */
1119 zassert_equal(DT_INST_PHA(0, gpios, pin), 10, "");
1120 zassert_equal(DT_INST_PHA(0, gpios, flags), 20, "");
1121
1122 /* DT_INST_PHA_BY_IDX */
1123 zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 0, pin), 10, "");
1124 zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 0, flags), 20, "");
1125
1126 zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 1, pin), 30, "");
1127 zassert_equal(DT_INST_PHA_BY_IDX(0, gpios, 1, flags), 40, "");
1128
1129 /* DT_INST_PHA_BY_NAME */
1130 zassert_equal(DT_INST_PHA_BY_NAME(0, foos, a, foocell), 100, "");
1131 zassert_equal(DT_INST_PHA_BY_NAME(0, foos, b_c, foocell), 110, "");
1132
1133 /* DT_INST_PHANDLE_BY_NAME */
1134 zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_NAME(0, foos, a), TEST_GPIO_1), "");
1135 zassert_true(DT_SAME_NODE(DT_INST_PHANDLE_BY_NAME(0, foos, b_c), TEST_GPIO_2), "");
1136 }
1137
1138 #undef DT_DRV_COMPAT
1139 #define DT_DRV_COMPAT vnd_phandle_holder
ZTEST(devicetree_api,test_gpio)1140 ZTEST(devicetree_api, test_gpio)
1141 {
1142 /* DT_GPIO_CTLR_BY_IDX */
1143 zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR_BY_IDX(TEST_PH, gpios, 0)),
1144 TO_STRING(DT_NODELABEL(test_gpio_1))), "");
1145 zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR_BY_IDX(TEST_PH, gpios, 1)),
1146 TO_STRING(DT_NODELABEL(test_gpio_2))), "");
1147
1148 /* DT_GPIO_CTLR */
1149 zassert_true(!strcmp(TO_STRING(DT_GPIO_CTLR(TEST_PH, gpios)),
1150 TO_STRING(DT_NODELABEL(test_gpio_1))), "");
1151
1152 /* DT_GPIO_PIN_BY_IDX */
1153 zassert_equal(DT_GPIO_PIN_BY_IDX(TEST_PH, gpios, 0), 10, "");
1154 zassert_equal(DT_GPIO_PIN_BY_IDX(TEST_PH, gpios, 1), 30, "");
1155
1156 /* DT_GPIO_PIN */
1157 zassert_equal(DT_GPIO_PIN(TEST_PH, gpios), 10, "");
1158
1159 /* DT_GPIO_FLAGS_BY_IDX */
1160 zassert_equal(DT_GPIO_FLAGS_BY_IDX(TEST_PH, gpios, 0), 20, "");
1161 zassert_equal(DT_GPIO_FLAGS_BY_IDX(TEST_PH, gpios, 1), 40, "");
1162
1163 /* DT_GPIO_FLAGS */
1164 zassert_equal(DT_GPIO_FLAGS(TEST_PH, gpios), 20, "");
1165
1166 /* DT_NUM_GPIO_HOGS */
1167 zassert_equal(DT_NUM_GPIO_HOGS(TEST_GPIO_HOG_1), 2, "");
1168 zassert_equal(DT_NUM_GPIO_HOGS(TEST_GPIO_HOG_2), 1, "");
1169 zassert_equal(DT_NUM_GPIO_HOGS(TEST_GPIO_HOG_3), 1, "");
1170
1171 /* DT_GPIO_HOG_PIN_BY_IDX */
1172 zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_1, 0), 0, "");
1173 zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_1, 1), 1, "");
1174 zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_2, 0), 3, "");
1175 zassert_equal(DT_GPIO_HOG_PIN_BY_IDX(TEST_GPIO_HOG_3, 0), 4, "");
1176
1177 /* DT_GPIO_HOG_FLAGS_BY_IDX */
1178 zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_1, 0), 0x00, "");
1179 zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_1, 1), 0x10, "");
1180 zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_2, 0), 0x20, "");
1181 zassert_equal(DT_GPIO_HOG_FLAGS_BY_IDX(TEST_GPIO_HOG_3, 0), 0x30, "");
1182
1183 /* DT_INST */
1184 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1185
1186 /* DT_INST_GPIO_PIN_BY_IDX */
1187 zassert_equal(DT_INST_GPIO_PIN_BY_IDX(0, gpios, 0), 10, "");
1188 zassert_equal(DT_INST_GPIO_PIN_BY_IDX(0, gpios, 1), 30, "");
1189
1190 /* DT_INST_GPIO_PIN */
1191 zassert_equal(DT_INST_GPIO_PIN(0, gpios), 10, "");
1192
1193 /* DT_INST_GPIO_FLAGS_BY_IDX */
1194 zassert_equal(DT_INST_GPIO_FLAGS_BY_IDX(0, gpios, 0), 20, "");
1195 zassert_equal(DT_INST_GPIO_FLAGS_BY_IDX(0, gpios, 1), 40, "");
1196
1197 /* DT_INST_GPIO_FLAGS */
1198 zassert_equal(DT_INST_GPIO_FLAGS(0, gpios), 20, "");
1199 }
1200
1201 #undef DT_DRV_COMPAT
1202 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_io_channels)1203 ZTEST(devicetree_api, test_io_channels)
1204 {
1205 /* DT_IO_CHANNELS_CTLR_BY_IDX */
1206 zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_IDX(TEST_TEMP, 0),
1207 TEST_IO_CHANNEL_CTLR_1), "");
1208 zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_IDX(TEST_TEMP, 1),
1209 TEST_IO_CHANNEL_CTLR_2), "");
1210
1211 /* DT_IO_CHANNELS_CTLR_BY_NAME */
1212 zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_NAME(TEST_TEMP, ch1),
1213 TEST_IO_CHANNEL_CTLR_1), "");
1214 zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR_BY_NAME(TEST_TEMP, ch2),
1215 TEST_IO_CHANNEL_CTLR_2), "");
1216
1217 /* DT_IO_CHANNELS_CTLR */
1218 zassert_true(DT_SAME_NODE(DT_IO_CHANNELS_CTLR(TEST_TEMP),
1219 TEST_IO_CHANNEL_CTLR_1), "");
1220
1221 /* DT_INST_IO_CHANNELS_CTLR_BY_IDX */
1222 zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_IDX(0, 0),
1223 TEST_IO_CHANNEL_CTLR_1), "");
1224 zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_IDX(0, 1),
1225 TEST_IO_CHANNEL_CTLR_2), "");
1226
1227 /* DT_INST_IO_CHANNELS_CTLR_BY_NAME */
1228 zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_NAME(0, ch1),
1229 TEST_IO_CHANNEL_CTLR_1), "");
1230 zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR_BY_NAME(0, ch2),
1231 TEST_IO_CHANNEL_CTLR_2), "");
1232
1233 /* DT_INST_IO_CHANNELS_CTLR */
1234 zassert_true(DT_SAME_NODE(DT_INST_IO_CHANNELS_CTLR(0),
1235 TEST_IO_CHANNEL_CTLR_1), "");
1236
1237 zassert_equal(DT_IO_CHANNELS_INPUT_BY_IDX(TEST_TEMP, 0), 10, "");
1238 zassert_equal(DT_IO_CHANNELS_INPUT_BY_IDX(TEST_TEMP, 1), 20, "");
1239 zassert_equal(DT_IO_CHANNELS_INPUT_BY_NAME(TEST_TEMP, ch1), 10, "");
1240 zassert_equal(DT_IO_CHANNELS_INPUT_BY_NAME(TEST_TEMP, ch2), 20, "");
1241 zassert_equal(DT_IO_CHANNELS_INPUT(TEST_TEMP), 10, "");
1242
1243 zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_IDX(0, 0), 10, "");
1244 zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_IDX(0, 1), 20, "");
1245 zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_NAME(0, ch1), 10, "");
1246 zassert_equal(DT_INST_IO_CHANNELS_INPUT_BY_NAME(0, ch2), 20, "");
1247 zassert_equal(DT_INST_IO_CHANNELS_INPUT(0), 10, "");
1248 }
1249
1250 #undef DT_DRV_COMPAT
1251 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_io_channel_names)1252 ZTEST(devicetree_api, test_io_channel_names)
1253 {
1254 struct adc_dt_spec adc_spec;
1255
1256 /* ADC_DT_SPEC_GET_BY_NAME */
1257 adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_GET_BY_NAME(TEST_TEMP, ch1);
1258 zassert_equal(adc_spec.channel_id, 10, "");
1259
1260 adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_GET_BY_NAME(TEST_TEMP, ch2);
1261 zassert_equal(adc_spec.channel_id, 20, "");
1262
1263 /* ADC_DT_SPEC_INST_GET_BY_NAME */
1264 adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_INST_GET_BY_NAME(0, ch1);
1265 zassert_equal(adc_spec.channel_id, 10, "");
1266
1267 adc_spec = (struct adc_dt_spec)ADC_DT_SPEC_INST_GET_BY_NAME(0, ch2);
1268 zassert_equal(adc_spec.channel_id, 20, "");
1269 }
1270
1271 #undef DT_DRV_COMPAT
1272 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_dma)1273 ZTEST(devicetree_api, test_dma)
1274 {
1275 /* DT_DMAS_CTLR_BY_IDX */
1276 zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_IDX(TEST_TEMP, 0),
1277 TEST_DMA_CTLR_1), "");
1278 zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_IDX(TEST_TEMP, 1),
1279 TEST_DMA_CTLR_2), "");
1280
1281 /* DT_DMAS_CTLR_BY_NAME */
1282 zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_NAME(TEST_TEMP, tx),
1283 TEST_DMA_CTLR_1), "");
1284 zassert_true(DT_SAME_NODE(DT_DMAS_CTLR_BY_NAME(TEST_TEMP, rx),
1285 TEST_DMA_CTLR_2), "");
1286
1287 /* DT_DMAS_CTLR */
1288 zassert_true(DT_SAME_NODE(DT_DMAS_CTLR(TEST_TEMP),
1289 TEST_DMA_CTLR_1), "");
1290
1291 /* DT_INST_DMAS_CTLR_BY_IDX */
1292 zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_IDX(0, 0),
1293 TEST_DMA_CTLR_1), "");
1294 zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_IDX(0, 1),
1295 TEST_DMA_CTLR_2), "");
1296
1297 /* DT_INST_DMAS_CTLR_BY_NAME */
1298 zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_NAME(0, tx),
1299 TEST_DMA_CTLR_1), "");
1300 zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR_BY_NAME(0, rx),
1301 TEST_DMA_CTLR_2), "");
1302
1303 /* DT_INST_DMAS_CTLR */
1304 zassert_true(DT_SAME_NODE(DT_INST_DMAS_CTLR(0), TEST_DMA_CTLR_1), "");
1305
1306 zassert_equal(DT_DMAS_CELL_BY_NAME(TEST_TEMP, rx, channel), 3, "");
1307 zassert_equal(DT_INST_DMAS_CELL_BY_NAME(0, rx, channel), 3, "");
1308 zassert_equal(DT_DMAS_CELL_BY_NAME(TEST_TEMP, rx, slot), 4, "");
1309 zassert_equal(DT_INST_DMAS_CELL_BY_NAME(0, rx, slot), 4, "");
1310
1311 zassert_equal(DT_DMAS_CELL_BY_IDX(TEST_TEMP, 1, channel), 3, "");
1312 zassert_equal(DT_INST_DMAS_CELL_BY_IDX(0, 1, channel), 3, "");
1313 zassert_equal(DT_DMAS_CELL_BY_IDX(TEST_TEMP, 1, slot), 4, "");
1314 zassert_equal(DT_INST_DMAS_CELL_BY_IDX(0, 1, slot), 4, "");
1315
1316 zassert_true(DT_DMAS_HAS_NAME(TEST_TEMP, tx), "");
1317 zassert_true(DT_INST_DMAS_HAS_NAME(0, tx), "");
1318 zassert_false(DT_DMAS_HAS_NAME(TEST_TEMP, output), "");
1319 zassert_false(DT_INST_DMAS_HAS_NAME(0, output), "");
1320
1321 zassert_true(DT_DMAS_HAS_IDX(TEST_TEMP, 1), "");
1322 zassert_true(DT_INST_DMAS_HAS_IDX(0, 1), "");
1323 zassert_false(DT_DMAS_HAS_IDX(TEST_TEMP, 2), "");
1324 zassert_false(DT_INST_DMAS_HAS_IDX(0, 2), "");
1325 }
1326
1327 #undef DT_DRV_COMPAT
1328 DT_FOREACH_STATUS_OKAY_VARGS(vnd_video_single_port, DEVICE_DT_DEFINE, NULL, NULL, NULL, NULL,
1329 POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY, NULL);
1330 DT_FOREACH_STATUS_OKAY_VARGS(vnd_video_multi_port, DEVICE_DT_DEFINE, NULL, NULL, NULL, NULL,
1331 POST_KERNEL, CONFIG_APPLICATION_INIT_PRIORITY, NULL);
ZTEST(devicetree_api,test_video)1332 ZTEST(devicetree_api, test_video)
1333 {
1334 /* DT_INST_PORT_BY_ID */
1335 #define DT_DRV_COMPAT vnd_video_single_port
1336 zassert_true(DT_SAME_NODE(DT_INST_PORT_BY_ID(0, 0),
1337 TEST_VIDEO0_PORT), "get port node of video0");
1338 zassert_true(DT_SAME_NODE(DT_INST_PORT_BY_ID(1, 0), TEST_VIDEO1_PORT),
1339 "get port node of video1");
1340 #undef DT_DRV_COMPAT
1341 #define DT_DRV_COMPAT vnd_video_multi_port
1342 zassert_true(DT_SAME_NODE(DT_INST_PORT_BY_ID(0, 0), TEST_VIDEO2_PORT0),
1343 "get port@0 node of video2");
1344 zassert_true(DT_SAME_NODE(DT_INST_PORT_BY_ID(0, 1), TEST_VIDEO2_PORT1),
1345 "get port@1 node of video2");
1346 #undef DT_DRV_COMPAT
1347
1348 /* DT_INST_ENDPOINT_BY_ID */
1349 #define DT_DRV_COMPAT vnd_video_single_port
1350 zassert_true(DT_SAME_NODE(DT_INST_ENDPOINT_BY_ID(0, 0, 0), TEST_VIDEO0_OUT),
1351 "get endpoint node of port node of video0");
1352 zassert_true(DT_SAME_NODE(DT_INST_ENDPOINT_BY_ID(1, 0, 0), TEST_VIDEO1_OUT0),
1353 "get endpoint@0 node of port node of video1");
1354 zassert_true(DT_SAME_NODE(DT_INST_ENDPOINT_BY_ID(1, 0, 1), TEST_VIDEO1_OUT1),
1355 "get endpoint@1 node of port node of video1");
1356 #undef DT_DRV_COMPAT
1357 #define DT_DRV_COMPAT vnd_video_multi_port
1358 zassert_true(DT_SAME_NODE(DT_INST_ENDPOINT_BY_ID(0, 0, 0), TEST_VIDEO2_PORT0_IN0),
1359 "get endpoint@0 node of port@0 node of video2");
1360 zassert_true(DT_SAME_NODE(DT_INST_ENDPOINT_BY_ID(0, 0, 1), TEST_VIDEO2_PORT0_IN1),
1361 "get endpoint@1 node of port@0 node of video2");
1362 zassert_true(DT_SAME_NODE(DT_INST_ENDPOINT_BY_ID(0, 1, 0), TEST_VIDEO2_PORT1_IN),
1363 "get endpoint node of port@1 node of video2");
1364 #undef DT_DRV_COMPAT
1365
1366 /* DT_NODE_BY_ENDPOINT */
1367 zassert_true(DT_SAME_NODE(DT_NODE_BY_ENDPOINT(TEST_VIDEO0_OUT),
1368 TEST_VIDEO0), "get video0 node from its endpoint");
1369 zassert_true(DT_SAME_NODE(DT_NODE_BY_ENDPOINT(TEST_VIDEO1_OUT0), TEST_VIDEO1),
1370 "get video1 node from its endpoint@0");
1371 zassert_true(DT_SAME_NODE(DT_NODE_BY_ENDPOINT(TEST_VIDEO1_OUT1), TEST_VIDEO1),
1372 "get video1 node from its endpoint@1");
1373 zassert_true(DT_SAME_NODE(DT_NODE_BY_ENDPOINT(TEST_VIDEO2_PORT0_IN0), TEST_VIDEO2),
1374 "get video2 node from its endpoint@0 at port@0");
1375 zassert_true(DT_SAME_NODE(DT_NODE_BY_ENDPOINT(TEST_VIDEO2_PORT0_IN1), TEST_VIDEO2),
1376 "get video2 node from its endpoint@1 at port@0");
1377 zassert_true(DT_SAME_NODE(DT_NODE_BY_ENDPOINT(TEST_VIDEO2_PORT1_IN), TEST_VIDEO2),
1378 "get video2 node from its endpoint at port@1");
1379
1380 /* DT_NODE_REMOTE_DEVICE */
1381 #define DT_DRV_COMPAT vnd_video_single_port
1382 zassert_true(DT_SAME_NODE(DT_NODE_REMOTE_DEVICE(TEST_VIDEO0_OUT), TEST_VIDEO2),
1383 "get remote device node of video0's endpoint");
1384 zassert_true(DT_SAME_NODE(DT_NODE_REMOTE_DEVICE(TEST_VIDEO1_OUT0), TEST_VIDEO2),
1385 "get remote device node of video1's endpoint@0");
1386 zassert_true(DT_SAME_NODE(DT_NODE_REMOTE_DEVICE(TEST_VIDEO1_OUT1), TEST_VIDEO2),
1387 "get remote device node of video1's endpoint@1");
1388 #undef DT_DRV_COMPAT
1389 #define DT_DRV_COMPAT vnd_video_multi_port
1390 zassert_true(DT_SAME_NODE(DT_NODE_REMOTE_DEVICE(TEST_VIDEO2_PORT0_IN0), TEST_VIDEO0),
1391 "get remote device node of video2's port@0 endpoint@0");
1392 zassert_true(DT_SAME_NODE(DT_NODE_REMOTE_DEVICE(TEST_VIDEO2_PORT0_IN1), TEST_VIDEO1),
1393 "get remote device node of video2's port@0 endpoint@1");
1394 zassert_true(DT_SAME_NODE(DT_NODE_REMOTE_DEVICE(TEST_VIDEO2_PORT1_IN), TEST_VIDEO1),
1395 "get remote device node of video2's port@1 endpoint");
1396 #undef DT_DRV_COMPAT
1397 }
1398
1399 #undef DT_DRV_COMPAT
1400 #define DT_DRV_COMPAT vnd_phandle_holder
ZTEST(devicetree_api,test_pwms)1401 ZTEST(devicetree_api, test_pwms)
1402 {
1403 /* DT_PWMS_CTLR_BY_IDX */
1404 zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_IDX(TEST_PH, 0),
1405 TEST_PWM_CTLR_1), "");
1406 zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_IDX(TEST_PH, 1),
1407 TEST_PWM_CTLR_2), "");
1408
1409 /* DT_PWMS_CTLR_BY_NAME */
1410 zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_NAME(TEST_PH, red),
1411 TEST_PWM_CTLR_1), "");
1412 zassert_true(DT_SAME_NODE(DT_PWMS_CTLR_BY_NAME(TEST_PH, green),
1413 TEST_PWM_CTLR_2), "");
1414
1415 /* DT_PWMS_CTLR */
1416 zassert_true(DT_SAME_NODE(DT_PWMS_CTLR(TEST_PH),
1417 TEST_PWM_CTLR_1), "");
1418
1419 /* DT_PWMS_CELL_BY_IDX */
1420 zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, channel), 5, "");
1421 zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, period), 100, "");
1422 zassert_equal(DT_PWMS_CELL_BY_IDX(TEST_PH, 1, flags), 1, "");
1423
1424 /* DT_PWMS_CELL_BY_NAME */
1425 zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, channel), 8, "");
1426 zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, period), 200, "");
1427 zassert_equal(DT_PWMS_CELL_BY_NAME(TEST_PH, red, flags), 3, "");
1428
1429 /* DT_PWMS_CELL */
1430 zassert_equal(DT_PWMS_CELL(TEST_PH, channel), 8, "");
1431 zassert_equal(DT_PWMS_CELL(TEST_PH, period), 200, "");
1432 zassert_equal(DT_PWMS_CELL(TEST_PH, flags), 3, "");
1433
1434 /* DT_PWMS_CHANNEL_BY_IDX */
1435 zassert_equal(DT_PWMS_CHANNEL_BY_IDX(TEST_PH, 1), 5, "");
1436
1437 /* DT_PWMS_CHANNEL_BY_NAME */
1438 zassert_equal(DT_PWMS_CHANNEL_BY_NAME(TEST_PH, green), 5, "");
1439
1440 /* DT_PWMS_CHANNEL */
1441 zassert_equal(DT_PWMS_CHANNEL(TEST_PH), 8, "");
1442
1443 /* DT_PWMS_PERIOD_BY_IDX */
1444 zassert_equal(DT_PWMS_PERIOD_BY_IDX(TEST_PH, 1), 100, "");
1445
1446 /* DT_PWMS_PERIOD_BY_NAME */
1447 zassert_equal(DT_PWMS_PERIOD_BY_NAME(TEST_PH, green), 100, "");
1448
1449 /* DT_PWMS_PERIOD */
1450 zassert_equal(DT_PWMS_PERIOD(TEST_PH), 200, "");
1451
1452 /* DT_PWMS_FLAGS_BY_IDX */
1453 zassert_equal(DT_PWMS_FLAGS_BY_IDX(TEST_PH, 1), 1, "");
1454
1455 /* DT_PWMS_FLAGS_BY_NAME */
1456 zassert_equal(DT_PWMS_FLAGS_BY_NAME(TEST_PH, green), 1, "");
1457
1458 /* DT_PWMS_FLAGS */
1459 zassert_equal(DT_PWMS_FLAGS(TEST_PH), 3, "");
1460
1461 /* DT_INST */
1462 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
1463
1464 /* DT_INST_PWMS_CTLR_BY_IDX */
1465 zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_IDX(0, 0),
1466 TEST_PWM_CTLR_1), "");
1467 zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_IDX(0, 1),
1468 TEST_PWM_CTLR_2), "");
1469
1470 /* DT_INST_PWMS_CTLR_BY_NAME */
1471 zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_NAME(0, red),
1472 TEST_PWM_CTLR_1), "");
1473 zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR_BY_NAME(0, green),
1474 TEST_PWM_CTLR_2), "");
1475
1476 /* DT_INST_PWMS_CTLR */
1477 zassert_true(DT_SAME_NODE(DT_INST_PWMS_CTLR(0), TEST_PWM_CTLR_1), "");
1478
1479 /* DT_INST_PWMS_CELL_BY_IDX */
1480 zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, channel), 5, "");
1481 zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, period), 100, "");
1482 zassert_equal(DT_INST_PWMS_CELL_BY_IDX(0, 1, flags), 1, "");
1483
1484 /* DT_INST_PWMS_CELL_BY_NAME */
1485 zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, channel), 5, "");
1486 zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, period), 100, "");
1487 zassert_equal(DT_INST_PWMS_CELL_BY_NAME(0, green, flags), 1, "");
1488
1489 /* DT_INST_PWMS_CELL */
1490 zassert_equal(DT_INST_PWMS_CELL(0, channel), 8, "");
1491 zassert_equal(DT_INST_PWMS_CELL(0, period), 200, "");
1492 zassert_equal(DT_INST_PWMS_CELL(0, flags), 3, "");
1493
1494 /* DT_INST_PWMS_CHANNEL_BY_IDX */
1495 zassert_equal(DT_INST_PWMS_CHANNEL_BY_IDX(0, 1), 5, "");
1496
1497 /* DT_INST_PWMS_CHANNEL_BY_NAME */
1498 zassert_equal(DT_INST_PWMS_CHANNEL_BY_NAME(0, green), 5, "");
1499
1500 /* DT_INST_PWMS_CHANNEL */
1501 zassert_equal(DT_INST_PWMS_CHANNEL(0), 8, "");
1502
1503 /* DT_INST_PWMS_PERIOD_BY_IDX */
1504 zassert_equal(DT_INST_PWMS_PERIOD_BY_IDX(0, 1), 100, "");
1505
1506 /* DT_INST_PWMS_PERIOD_BY_NAME */
1507 zassert_equal(DT_INST_PWMS_PERIOD_BY_NAME(0, red), 200, "");
1508
1509 /* DT_INST_PWMS_PERIOD */
1510 zassert_equal(DT_INST_PWMS_PERIOD(0), 200, "");
1511
1512 /* DT_INST_PWMS_FLAGS_BY_IDX */
1513 zassert_equal(DT_INST_PWMS_FLAGS_BY_IDX(0, 1), 1, "");
1514
1515 /* DT_INST_PWMS_FLAGS_BY_NAME */
1516 zassert_equal(DT_INST_PWMS_FLAGS_BY_NAME(0, red), 3, "");
1517
1518 /* DT_INST_PWMS_FLAGS */
1519 zassert_equal(DT_INST_PWMS_FLAGS(0), 3, "");
1520 }
1521
1522 #undef DT_DRV_COMPAT
1523 #define DT_DRV_COMPAT vnd_can_controller
ZTEST(devicetree_api,test_can)1524 ZTEST(devicetree_api, test_can)
1525 {
1526 /* DT_CAN_TRANSCEIVER_MIN_BITRATE */
1527 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_0, 0), 10000, "");
1528 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_0, 10000), 10000, "");
1529 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_0, 20000), 20000, "");
1530 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_1, 0), 50000, "");
1531 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_1, 50000), 50000, "");
1532 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_1, 100000), 100000, "");
1533 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_2, 0), 0, "");
1534 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_2, 10000), 10000, "");
1535 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_2, 20000), 20000, "");
1536 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_3, 0), 0, "");
1537 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_3, 30000), 30000, "");
1538 zassert_equal(DT_CAN_TRANSCEIVER_MIN_BITRATE(TEST_CAN_CTRL_3, 40000), 40000, "");
1539
1540 /* DT_INST_CAN_TRANSCEIVER_MIN_BITRATE */
1541 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(0, 0), 10000, "");
1542 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(0, 10000), 10000, "");
1543 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(0, 20000), 20000, "");
1544 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(1, 0), 50000, "");
1545 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(1, 50000), 50000, "");
1546 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(1, 100000), 100000, "");
1547 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(2, 0), 0, "");
1548 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(2, 10000), 10000, "");
1549 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(2, 20000), 20000, "");
1550 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(3, 0), 0, "");
1551 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(3, 30000), 30000, "");
1552 zassert_equal(DT_INST_CAN_TRANSCEIVER_MIN_BITRATE(3, 40000), 40000, "");
1553
1554 /* DT_CAN_TRANSCEIVER_MAX_BITRATE */
1555 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_0, 1000000), 1000000, "");
1556 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_0, 5000000), 5000000, "");
1557 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_0, 8000000), 5000000, "");
1558 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_1, 125000), 125000, "");
1559 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_1, 2000000), 2000000, "");
1560 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_1, 5000000), 2000000, "");
1561 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_2, 125000), 125000, "");
1562 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_2, 1000000), 1000000, "");
1563 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_2, 5000000), 1000000, "");
1564 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_3, 125000), 125000, "");
1565 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_3, 1000000), 1000000, "");
1566 zassert_equal(DT_CAN_TRANSCEIVER_MAX_BITRATE(TEST_CAN_CTRL_3, 5000000), 1000000, "");
1567
1568 /* DT_INST_CAN_TRANSCEIVER_MAX_BITRATE */
1569 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(0, 1000000), 1000000, "");
1570 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(0, 5000000), 5000000, "");
1571 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(0, 8000000), 5000000, "");
1572 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(1, 125000), 125000, "");
1573 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(1, 2000000), 2000000, "");
1574 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(1, 5000000), 2000000, "");
1575 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(2, 125000), 125000, "");
1576 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(2, 1000000), 1000000, "");
1577 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(2, 5000000), 1000000, "");
1578 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(3, 125000), 125000, "");
1579 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(3, 1000000), 1000000, "");
1580 zassert_equal(DT_INST_CAN_TRANSCEIVER_MAX_BITRATE(3, 5000000), 1000000, "");
1581 }
1582
ZTEST(devicetree_api,test_macro_names)1583 ZTEST(devicetree_api, test_macro_names)
1584 {
1585 /* white box */
1586 zassert_true(!strcmp(TO_STRING(DT_PATH(test, gpio_deadbeef)),
1587 "DT_N_S_test_S_gpio_deadbeef"), "");
1588 zassert_true(!strcmp(TO_STRING(DT_ALIAS(test_alias)),
1589 "DT_N_S_test_S_gpio_deadbeef"), "");
1590 zassert_true(!strcmp(TO_STRING(DT_NODELABEL(test_nodelabel)),
1591 "DT_N_S_test_S_gpio_deadbeef"), "");
1592 zassert_true(!strcmp(TO_STRING(DT_NODELABEL(test_nodelabel_allcaps)),
1593 "DT_N_S_test_S_gpio_deadbeef"), "");
1594
1595 #define CHILD_NODE_ID DT_CHILD(DT_PATH(test, i2c_11112222), test_i2c_dev_10)
1596 #define FULL_PATH_ID DT_PATH(test, i2c_11112222, test_i2c_dev_10)
1597
1598 zassert_true(!strcmp(TO_STRING(CHILD_NODE_ID),
1599 TO_STRING(FULL_PATH_ID)), "");
1600
1601 #undef CHILD_NODE_ID
1602 #undef FULL_PATH_ID
1603 }
1604
1605 static int a[] = DT_PROP(TEST_ARRAYS, a);
1606 static unsigned char b[] = DT_PROP(TEST_ARRAYS, b);
1607 static char *c[] = DT_PROP(TEST_ARRAYS, c);
1608
ZTEST(devicetree_api,test_arrays)1609 ZTEST(devicetree_api, test_arrays)
1610 {
1611 int ok;
1612
1613 zassert_equal(ARRAY_SIZE(a), 3, "");
1614 zassert_equal(ARRAY_SIZE(b), 4, "");
1615 zassert_equal(ARRAY_SIZE(c), 2, "");
1616
1617 zassert_equal(a[0], 1000, "");
1618 zassert_equal(a[1], 2000, "");
1619 zassert_equal(a[2], 3000, "");
1620
1621 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 0), "");
1622 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 1), "");
1623 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 2), "");
1624 zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 3), "");
1625
1626 /*
1627 * Verify that DT_PROP_HAS_IDX can be used with COND_CODE_1()
1628 * and COND_CODE_0(), i.e. its expansion is a literal 1 or 0,
1629 * not an equivalent expression that evaluates to 1 or 0.
1630 */
1631 ok = 0;
1632 COND_CODE_1(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 0), (ok = 1;), ());
1633 zassert_equal(ok, 1, "");
1634 ok = 0;
1635 COND_CODE_0(DT_PROP_HAS_IDX(TEST_ARRAYS, a, 3), (ok = 1;), ());
1636 zassert_equal(ok, 1, "");
1637
1638 zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 0), a[0], "");
1639 zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 1), a[1], "");
1640 zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, a, 2), a[2], "");
1641 zassert_equal(DT_PROP_LAST(TEST_ARRAYS, a), a[2], "");
1642
1643 zassert_equal(DT_PROP_LEN(TEST_ARRAYS, a), 3, "");
1644
1645 zassert_equal(b[0], 0xaa, "");
1646 zassert_equal(b[1], 0xbb, "");
1647 zassert_equal(b[2], 0xcc, "");
1648 zassert_equal(b[3], 0xdd, "");
1649
1650 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 0), "");
1651 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 1), "");
1652 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 2), "");
1653 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 3), "");
1654 zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, b, 4), "");
1655
1656 zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 0), b[0], "");
1657 zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 1), b[1], "");
1658 zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 2), b[2], "");
1659 zassert_equal(DT_PROP_BY_IDX(TEST_ARRAYS, b, 3), b[3], "");
1660
1661 zassert_equal(DT_PROP_LEN(TEST_ARRAYS, b), 4, "");
1662
1663 zassert_true(!strcmp(c[0], "bar"), "");
1664 zassert_true(!strcmp(c[1], "baz"), "");
1665
1666 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 0), "");
1667 zassert_true(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 1), "");
1668 zassert_false(DT_PROP_HAS_IDX(TEST_ARRAYS, c, 2), "");
1669
1670 zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ARRAYS, c, 0), c[0]), "");
1671 zassert_true(!strcmp(DT_PROP_BY_IDX(TEST_ARRAYS, c, 1), c[1]), "");
1672
1673 zassert_equal(DT_PROP_LEN(TEST_ARRAYS, c), 2, "");
1674 }
1675
ZTEST(devicetree_api,test_foreach)1676 ZTEST(devicetree_api, test_foreach)
1677 {
1678 /*
1679 * We don't know what platform we are running on, so settle for
1680 * some basic checks related to nodes we know are in our overlay.
1681 */
1682 #define IS_ALIASES(node_id) + DT_SAME_NODE(DT_PATH(aliases), node_id)
1683 #define IS_DISABLED_GPIO(node_id) + DT_SAME_NODE(DT_NODELABEL(disabled_gpio), \
1684 node_id)
1685 zassert_equal(1, DT_FOREACH_NODE(IS_ALIASES), "");
1686 zassert_equal(1, DT_FOREACH_NODE(IS_DISABLED_GPIO), "");
1687 zassert_equal(1, DT_FOREACH_STATUS_OKAY_NODE(IS_ALIASES), "");
1688 zassert_equal(0, DT_FOREACH_STATUS_OKAY_NODE(IS_DISABLED_GPIO), "");
1689
1690 #define IS_ALIASES_VARGS(node_id, mul) + ((mul) * DT_SAME_NODE(DT_PATH(aliases), node_id))
1691 #define IS_DISABLED_GPIO_VARGS(node_id, mul) + ((mul) * \
1692 DT_SAME_NODE(DT_NODELABEL(disabled_gpio), node_id))
1693 zassert_equal(2, DT_FOREACH_NODE_VARGS(IS_ALIASES_VARGS, 2), "");
1694 zassert_equal(2, DT_FOREACH_NODE_VARGS(IS_DISABLED_GPIO_VARGS, 2), "");
1695 zassert_equal(2, DT_FOREACH_STATUS_OKAY_NODE_VARGS(IS_ALIASES_VARGS, 2), "");
1696 zassert_equal(0, DT_FOREACH_STATUS_OKAY_NODE_VARGS(IS_DISABLED_GPIO_VARGS, 2), "");
1697
1698
1699 #undef IS_ALIASES
1700 #undef IS_DISABLED_GPIO
1701 #undef IS_ALIASES_VARGS
1702 #undef IS_DISABLED_GPIO_VARGS
1703 }
1704
1705 #undef DT_DRV_COMPAT
1706 #define DT_DRV_COMPAT vnd_gpio_device
ZTEST(devicetree_api,test_foreach_status_okay)1707 ZTEST(devicetree_api, test_foreach_status_okay)
1708 {
1709 /*
1710 * For-each-node type macro tests.
1711 *
1712 * See test_foreach_prop_elem*() for tests of
1713 * for-each-property type macros.
1714 */
1715 unsigned int val;
1716 const char *str;
1717
1718 /* This should expand to something like:
1719 *
1720 * "/test/enum-0" "/test/enum-1"
1721 *
1722 * but there is no guarantee about the order of nodes in the
1723 * expansion, so we test both.
1724 */
1725 str = DT_FOREACH_STATUS_OKAY(vnd_enum_holder, DT_NODE_PATH);
1726 zassert_true(!strcmp(str, "/test/enum-0/test/enum-1") ||
1727 !strcmp(str, "/test/enum-1/test/enum-0"), "");
1728
1729 #undef MY_FN
1730 #define MY_FN(node_id, operator) DT_ENUM_IDX(node_id, val) operator
1731 /* This should expand to something like:
1732 *
1733 * 0 + 2 + 3
1734 *
1735 * and order of expansion doesn't matter, since we're adding
1736 * the values all up.
1737 */
1738 val = DT_FOREACH_STATUS_OKAY_VARGS(vnd_enum_holder, MY_FN, +) 3;
1739 zassert_equal(val, 5, "");
1740
1741 #undef MY_FN
1742 #define MY_FN(inst, compat, operator) DT_ENUM_IDX(DT_INST(inst, compat), val) operator
1743 /* This should expand to something like:
1744 *
1745 * 0 + 2 + 3
1746 *
1747 * and order of expansion doesn't matter, since we're adding
1748 * the values all up.
1749 */
1750 val = DT_COMPAT_FOREACH_STATUS_OKAY_VARGS(vnd_enum_holder, MY_FN, +) 3;
1751 zassert_equal(val, 5, "");
1752
1753 /*
1754 * Make sure DT_INST_FOREACH_STATUS_OKAY can be called from functions
1755 * using macros with side effects in the current scope.
1756 */
1757 val = 0;
1758 #define INC(inst_ignored) do { val++; } while (0);
1759 DT_INST_FOREACH_STATUS_OKAY(INC)
1760 zassert_equal(val, 2, "");
1761 #undef INC
1762
1763 val = 0;
1764 #define INC_ARG(arg) do { val++; val += arg; } while (0)
1765 #define INC(inst_ignored, arg) INC_ARG(arg);
1766 DT_INST_FOREACH_STATUS_OKAY_VARGS(INC, 1)
1767 zassert_equal(val, 4, "");
1768 #undef INC_ARG
1769 #undef INC
1770
1771 /*
1772 * Make sure DT_INST_FOREACH_STATUS_OKAY works with 0 instances, and does
1773 * not expand its argument at all.
1774 */
1775 #undef DT_DRV_COMPAT
1776 #define DT_DRV_COMPAT xxxx
1777 #define BUILD_BUG_ON_EXPANSION (there is a bug in devicetree.h)
1778 DT_INST_FOREACH_STATUS_OKAY(BUILD_BUG_ON_EXPANSION)
1779 #undef BUILD_BUG_ON_EXPANSION
1780
1781 #undef DT_DRV_COMPAT
1782 #define DT_DRV_COMPAT xxxx
1783 #define BUILD_BUG_ON_EXPANSION(arg) (there is a bug in devicetree.h)
1784 DT_INST_FOREACH_STATUS_OKAY_VARGS(BUILD_BUG_ON_EXPANSION, 1)
1785 #undef BUILD_BUG_ON_EXPANSION
1786 }
1787
ZTEST(devicetree_api,test_foreach_nodelabel)1788 ZTEST(devicetree_api, test_foreach_nodelabel)
1789 {
1790 #undef DT_DRV_COMPAT
1791 #define DT_DRV_COMPAT vnd_adc_temp_sensor
1792 #define ENTRY(nodelabel) enum_ ## nodelabel,
1793 #define VAR_PLUS(nodelabel, to_add) int nodelabel ## _added = enum_ ## nodelabel + to_add;
1794
1795 /* DT_FOREACH_NODELABEL */
1796 enum {
1797 DT_FOREACH_NODELABEL(DT_NODELABEL(test_nodelabel), ENTRY)
1798 };
1799 zassert_equal(enum_test_nodelabel, 0, "");
1800 zassert_equal(enum_TEST_NODELABEL_ALLCAPS, 1, "");
1801 zassert_equal(enum_test_gpio_1, 2, "");
1802
1803 /* DT_FOREACH_NODELABEL_VARGS */
1804 DT_FOREACH_NODELABEL_VARGS(DT_NODELABEL(test_nodelabel), VAR_PLUS, 1);
1805 zassert_equal(test_nodelabel_added, 1, "");
1806 zassert_equal(TEST_NODELABEL_ALLCAPS_added, 2, "");
1807 zassert_equal(test_gpio_1_added, 3, "");
1808
1809 /* DT_NODELABEL_STRING_ARRAY is tested here since it's closely related */
1810 const char *nodelabels[] = DT_NODELABEL_STRING_ARRAY(DT_NODELABEL(test_nodelabel));
1811
1812 zassert_equal(ARRAY_SIZE(nodelabels), 3);
1813 zassert_true(!strcmp(nodelabels[0], "test_nodelabel"), "");
1814 zassert_true(!strcmp(nodelabels[1], "TEST_NODELABEL_ALLCAPS"), "");
1815 zassert_true(!strcmp(nodelabels[2], "test_gpio_1"), "");
1816
1817 /* DT_NUM_NODELABELS */
1818 zassert_equal(DT_NUM_NODELABELS(DT_NODELABEL(test_nodelabel)), 3, "");
1819 zassert_equal(DT_NUM_NODELABELS(DT_PATH(chosen)), 0, "");
1820 zassert_equal(DT_NUM_NODELABELS(DT_ROOT), 0, "");
1821
1822 /* DT_INST_FOREACH_NODELABEL */
1823 enum {
1824 DT_INST_FOREACH_NODELABEL(0, ENTRY)
1825 };
1826 zassert_equal(enum_test_temp_sensor, 0, "");
1827
1828 /* DT_INST_FOREACH_NODELABEL_VARGS */
1829 DT_INST_FOREACH_NODELABEL_VARGS(0, VAR_PLUS, 1);
1830 zassert_equal(test_temp_sensor_added, 1, "");
1831
1832 /* DT_INST_NODELABEL_STRING_ARRAY */
1833 const char *inst_nodelabels[] = DT_INST_NODELABEL_STRING_ARRAY(0);
1834
1835 zassert_equal(ARRAY_SIZE(inst_nodelabels), 1);
1836 zassert_true(!strcmp(inst_nodelabels[0], "test_temp_sensor"), "");
1837
1838 /* DT_INST_NUM_NODELABELS */
1839 zassert_equal(DT_INST_NUM_NODELABELS(0), 1, "");
1840
1841 #undef VAR_PLUS
1842 #undef ENTRY
1843 #undef DT_DRV_COMPAT
1844 }
1845
ZTEST(devicetree_api,test_foreach_prop_elem)1846 ZTEST(devicetree_api, test_foreach_prop_elem)
1847 {
1848 #define TIMES_TWO(node_id, prop, idx) \
1849 (2 * DT_PROP_BY_IDX(node_id, prop, idx)),
1850 #define BY_IDX_COMMA(node_id, prop, idx) DT_PROP_BY_IDX(node_id, prop, idx),
1851
1852 int array_a[] = {
1853 DT_FOREACH_PROP_ELEM(TEST_ARRAYS, a, TIMES_TWO)
1854 };
1855
1856 zassert_equal(ARRAY_SIZE(array_a), 3, "");
1857 zassert_equal(array_a[0], 2000, "");
1858 zassert_equal(array_a[1], 4000, "");
1859 zassert_equal(array_a[2], 6000, "");
1860
1861 int array_b[] = {
1862 DT_FOREACH_PROP_ELEM(TEST_ARRAYS, b, TIMES_TWO)
1863 };
1864
1865 zassert_equal(ARRAY_SIZE(array_b), 4, "");
1866 zassert_equal(array_b[0], 2 * 0xAA, "");
1867 zassert_equal(array_b[1], 2 * 0xBB, "");
1868 zassert_equal(array_b[2], 2 * 0xCC, "");
1869 zassert_equal(array_b[3], 2 * 0xDD, "");
1870
1871 static const char * const array_c[] = {
1872 DT_FOREACH_PROP_ELEM(TEST_ARRAYS, c, BY_IDX_COMMA)
1873 };
1874
1875 zassert_equal(ARRAY_SIZE(array_c), 2, "");
1876 zassert_true(!strcmp(array_c[0], "bar"), "");
1877 zassert_true(!strcmp(array_c[1], "baz"), "");
1878
1879 static const char * const array_val[] = {
1880 DT_FOREACH_PROP_ELEM(TEST_ENUM_0, val, BY_IDX_COMMA)
1881 };
1882
1883 zassert_equal(ARRAY_SIZE(array_val), 1, "");
1884 zassert_true(!strcmp(array_val[0], "zero"), "");
1885
1886 static const char * const string_zephyr_user[] = {
1887 DT_FOREACH_PROP_ELEM(ZEPHYR_USER, string, BY_IDX_COMMA)
1888 };
1889
1890 zassert_equal(ARRAY_SIZE(string_zephyr_user), 1, "");
1891 zassert_true(!strcmp(string_zephyr_user[0], "foo"), "");
1892
1893 #undef BY_IDX_COMMA
1894
1895 #define PATH_COMMA(node_id, prop, idx) \
1896 DT_NODE_PATH(DT_PROP_BY_IDX(node_id, prop, idx)),
1897
1898 static const char * const array_ph[] = {
1899 DT_FOREACH_PROP_ELEM(TEST_PH, ph, PATH_COMMA)
1900 };
1901
1902 zassert_equal(ARRAY_SIZE(array_ph), 1, "");
1903 zassert_true(!strcmp(array_ph[0], DT_NODE_PATH(TEST_GPIO_1)), "");
1904
1905 static const char * const array_zephyr_user_ph[] = {
1906 DT_FOREACH_PROP_ELEM(ZEPHYR_USER, ph, PATH_COMMA)
1907 };
1908
1909 zassert_equal(ARRAY_SIZE(array_zephyr_user_ph), 1, "");
1910 zassert_true(!strcmp(array_zephyr_user_ph[0],
1911 DT_NODE_PATH(TEST_GPIO_1)), "");
1912
1913 static const char * const array_phs[] = {
1914 DT_FOREACH_PROP_ELEM(TEST_PH, phs, PATH_COMMA)
1915 };
1916
1917 zassert_equal(ARRAY_SIZE(array_phs), 2, "");
1918 zassert_true(!strcmp(array_phs[0], DT_NODE_PATH(TEST_I2C)), "");
1919 zassert_true(!strcmp(array_phs[1], DT_NODE_PATH(TEST_SPI)), "");
1920
1921 #undef PATH_COMMA
1922
1923 #define PIN_COMMA(node_id, prop, idx) DT_GPIO_PIN_BY_IDX(node_id, prop, idx),
1924
1925 int array_gpios[] = {
1926 DT_FOREACH_PROP_ELEM(TEST_PH, gpios, PIN_COMMA)
1927 };
1928
1929 zassert_equal(ARRAY_SIZE(array_gpios), 2, "");
1930 zassert_equal(array_gpios[0], 10, "");
1931 zassert_equal(array_gpios[1], 30, "");
1932
1933 #undef PATH_COMMA
1934
1935 int array_sep[] = {
1936 DT_FOREACH_PROP_ELEM_SEP(TEST_ARRAYS, a, DT_PROP_BY_IDX, (,))
1937 };
1938
1939 zassert_equal(ARRAY_SIZE(array_sep), 3, "");
1940 zassert_equal(array_sep[0], 1000, "");
1941 zassert_equal(array_sep[1], 2000, "");
1942 zassert_equal(array_sep[2], 3000, "");
1943
1944 #undef DT_DRV_COMPAT
1945 #define DT_DRV_COMPAT vnd_array_holder
1946
1947 int inst_array[] = {
1948 DT_INST_FOREACH_PROP_ELEM(0, a, TIMES_TWO)
1949 };
1950
1951 zassert_equal(ARRAY_SIZE(inst_array), ARRAY_SIZE(array_a), "");
1952 zassert_equal(inst_array[0], array_a[0], "");
1953 zassert_equal(inst_array[1], array_a[1], "");
1954 zassert_equal(inst_array[2], array_a[2], "");
1955
1956 int inst_array_sep[] = {
1957 DT_INST_FOREACH_PROP_ELEM_SEP(0, a, DT_PROP_BY_IDX, (,))
1958 };
1959
1960 zassert_equal(ARRAY_SIZE(inst_array_sep), ARRAY_SIZE(array_sep), "");
1961 zassert_equal(inst_array_sep[0], array_sep[0], "");
1962 zassert_equal(inst_array_sep[1], array_sep[1], "");
1963 zassert_equal(inst_array_sep[2], array_sep[2], "");
1964 #undef TIMES_TWO
1965 }
1966
ZTEST(devicetree_api,test_foreach_prop_elem_varg)1967 ZTEST(devicetree_api, test_foreach_prop_elem_varg)
1968 {
1969 #define TIMES_TWO_ADD(node_id, prop, idx, arg) \
1970 ((2 * DT_PROP_BY_IDX(node_id, prop, idx)) + arg),
1971
1972 int array[] = {
1973 DT_FOREACH_PROP_ELEM_VARGS(TEST_ARRAYS, a, TIMES_TWO_ADD, 3)
1974 };
1975
1976 zassert_equal(ARRAY_SIZE(array), 3, "");
1977 zassert_equal(array[0], 2003, "");
1978 zassert_equal(array[1], 4003, "");
1979 zassert_equal(array[2], 6003, "");
1980
1981 #define PROP_PLUS_ARG(node_id, prop, idx, arg) \
1982 (DT_PROP_BY_IDX(node_id, prop, idx) + arg)
1983
1984 int array_sep[] = {
1985 DT_FOREACH_PROP_ELEM_SEP_VARGS(TEST_ARRAYS, a, PROP_PLUS_ARG,
1986 (,), 3)
1987 };
1988
1989 zassert_equal(ARRAY_SIZE(array_sep), 3, "");
1990 zassert_equal(array_sep[0], 1003, "");
1991 zassert_equal(array_sep[1], 2003, "");
1992 zassert_equal(array_sep[2], 3003, "");
1993
1994 #undef DT_DRV_COMPAT
1995 #define DT_DRV_COMPAT vnd_array_holder
1996
1997 int inst_array[] = {
1998 DT_INST_FOREACH_PROP_ELEM_VARGS(0, a, TIMES_TWO_ADD, 3)
1999 };
2000
2001 zassert_equal(ARRAY_SIZE(inst_array), ARRAY_SIZE(array), "");
2002 zassert_equal(inst_array[0], array[0], "");
2003 zassert_equal(inst_array[1], array[1], "");
2004 zassert_equal(inst_array[2], array[2], "");
2005
2006 int inst_array_sep[] = {
2007 DT_INST_FOREACH_PROP_ELEM_SEP_VARGS(0, a, PROP_PLUS_ARG, (,),
2008 3)
2009 };
2010
2011 zassert_equal(ARRAY_SIZE(inst_array_sep), ARRAY_SIZE(array_sep), "");
2012 zassert_equal(inst_array_sep[0], array_sep[0], "");
2013 zassert_equal(inst_array_sep[1], array_sep[1], "");
2014 zassert_equal(inst_array_sep[2], array_sep[2], "");
2015 #undef TIMES_TWO
2016 }
2017
2018 struct test_gpio_info {
2019 uint32_t reg_addr;
2020 uint32_t reg_len;
2021 };
2022
2023 struct test_gpio_data {
2024 bool init_called;
2025 bool is_gpio_ctlr;
2026 };
2027
test_gpio_init(const struct device * dev)2028 static int test_gpio_init(const struct device *dev)
2029 {
2030 struct test_gpio_data *data = dev->data;
2031
2032 data->init_called = 1;
2033 return 0;
2034 }
2035
2036 #undef DT_DRV_COMPAT
2037 #define DT_DRV_COMPAT vnd_gpio_device
2038
2039 static DEVICE_API(gpio, test_api);
2040
2041 #define TEST_GPIO_INIT(num) \
2042 static struct test_gpio_data gpio_data_##num = { \
2043 .is_gpio_ctlr = DT_INST_PROP(num, \
2044 gpio_controller), \
2045 }; \
2046 static const struct test_gpio_info gpio_info_##num = { \
2047 .reg_addr = DT_INST_REG_ADDR(num), \
2048 .reg_len = DT_INST_REG_SIZE(num), \
2049 }; \
2050 DEVICE_DT_INST_DEFINE(num, \
2051 test_gpio_init, \
2052 NULL, \
2053 &gpio_data_##num, \
2054 &gpio_info_##num, \
2055 POST_KERNEL, \
2056 CONFIG_APPLICATION_INIT_PRIORITY, \
2057 &test_api);
2058
2059 DT_INST_FOREACH_STATUS_OKAY(TEST_GPIO_INIT)
2060
ZTEST(devicetree_api,test_devices)2061 ZTEST(devicetree_api, test_devices)
2062 {
2063 const struct device *devs[2] = { DEVICE_DT_INST_GET(0), DEVICE_DT_INST_GET(1) };
2064 const struct device *dev_abcd;
2065 struct test_gpio_data *data_dev0;
2066 struct test_gpio_data *data_dev1;
2067 const struct test_gpio_info *config_abdc;
2068
2069 zassert_equal(DT_NUM_INST_STATUS_OKAY(vnd_gpio_device), 2, "");
2070
2071 data_dev0 = devs[0]->data;
2072 data_dev1 = devs[1]->data;
2073
2074 zassert_not_null(devs[0], "");
2075 zassert_not_null(devs[1], "");
2076
2077 zassert_true(data_dev0->is_gpio_ctlr, "");
2078 zassert_true(data_dev1->is_gpio_ctlr, "");
2079 zassert_true(data_dev0->init_called, "");
2080 zassert_true(data_dev1->init_called, "");
2081
2082 dev_abcd = DEVICE_DT_GET(TEST_ABCD1234);
2083 config_abdc = dev_abcd->config;
2084 zassert_not_null(dev_abcd, "");
2085 zassert_equal(config_abdc->reg_addr, 0xabcd1234, "");
2086 zassert_equal(config_abdc->reg_len, 0x500, "");
2087 }
2088
ZTEST(devicetree_api,test_cs_gpios)2089 ZTEST(devicetree_api, test_cs_gpios)
2090 {
2091 zassert_equal(DT_SPI_HAS_CS_GPIOS(TEST_SPI_NO_CS), 0, "");
2092 zassert_equal(DT_SPI_NUM_CS_GPIOS(TEST_SPI_NO_CS), 0, "");
2093
2094 zassert_equal(DT_SPI_HAS_CS_GPIOS(TEST_SPI), 1, "");
2095 zassert_equal(DT_SPI_NUM_CS_GPIOS(TEST_SPI), 3, "");
2096
2097 zassert_equal(DT_DEP_ORD(DT_SPI_DEV_CS_GPIOS_CTLR(TEST_SPI_DEV_0)),
2098 DT_DEP_ORD(DT_NODELABEL(test_gpio_1)),
2099 "dev 0 cs gpio controller");
2100 zassert_equal(DT_SPI_DEV_CS_GPIOS_PIN(TEST_SPI_DEV_0), 0x10, "");
2101 zassert_equal(DT_SPI_DEV_CS_GPIOS_FLAGS(TEST_SPI_DEV_0), 0x20, "");
2102 }
2103
ZTEST(devicetree_api,test_chosen)2104 ZTEST(devicetree_api, test_chosen)
2105 {
2106 zassert_equal(DT_HAS_CHOSEN(ztest_xxxx), 0, "");
2107 zassert_equal(DT_HAS_CHOSEN(ztest_gpio), 1, "");
2108 zassert_true(!strcmp(TO_STRING(DT_CHOSEN(ztest_gpio)),
2109 "DT_N_S_test_S_gpio_deadbeef"), "");
2110 }
2111
2112 #define TO_MY_ENUM(token) TO_MY_ENUM_2(token) /* force another expansion */
2113 #define TO_MY_ENUM_2(token) MY_ENUM_ ## token
ZTEST(devicetree_api,test_enums)2114 ZTEST(devicetree_api, test_enums)
2115 {
2116 enum {
2117 MY_ENUM_zero = 0xff,
2118 MY_ENUM_ZERO = 0xaa,
2119 };
2120
2121 /* DT_ENUM_IDX and DT_ENUM_HAS_VALUE on string enum */
2122 zassert_equal(DT_ENUM_IDX(TEST_ENUM_0, val), 0, "0");
2123 zassert_true(DT_ENUM_HAS_VALUE(TEST_ENUM_0, val, zero), "");
2124 zassert_false(DT_ENUM_HAS_VALUE(TEST_ENUM_0, val, one), "");
2125 zassert_false(DT_ENUM_HAS_VALUE(TEST_ENUM_0, val, two), "");
2126
2127 /* DT_ENUM_IDX and DT_ENUM_HAS_VALUE on int enum */
2128 zassert_equal(DT_ENUM_IDX(DT_NODELABEL(test_enum_int_default_0), val), 0, "0");
2129 zassert_true(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 5), "");
2130 zassert_false(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 6), "");
2131 zassert_false(DT_ENUM_HAS_VALUE(DT_NODELABEL(test_enum_int_default_0), val, 7), "");
2132
2133 /* DT_ENUM_IDX_BY_IDX and DT_ENUM_HAS_VALUE_BY_IDX on string-array enum */
2134 zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0), 0);
2135 zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1), 3);
2136 zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2), 0);
2137 zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, foo));
2138 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, bar));
2139 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, baz));
2140 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 0, zoo));
2141 zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, zoo));
2142 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, foo));
2143 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, bar));
2144 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 1, baz));
2145 zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, foo));
2146 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, baz));
2147 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, bar));
2148 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_string_array), val, 2, zoo));
2149
2150 /* DT_ENUM_IDX_BY_IDX and DT_ENUM_HAS_VALUE_BY_IDX on int-array enum */
2151 zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0), 3);
2152 zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1), 4);
2153 zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2), 3);
2154 zassert_equal(DT_ENUM_IDX_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3), 7);
2155 zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0, 4));
2156 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0, 5));
2157 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 0, 6));
2158 zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1, 3));
2159 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1, 0));
2160 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 1, 1));
2161 zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2, 4));
2162 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2, 3));
2163 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 2, 7));
2164 zassert_true(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3, 0));
2165 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3, 2));
2166 zassert_false(DT_ENUM_HAS_VALUE_BY_IDX(DT_NODELABEL(test_enum_int_array), val, 3, 1));
2167 }
2168 #undef TO_MY_ENUM
2169 #undef TO_MY_ENUM_2
2170
ZTEST(devicetree_api,test_enums_required_false)2171 ZTEST(devicetree_api, test_enums_required_false)
2172 {
2173 /* DT_ENUM_IDX_OR on string value */
2174 zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_default_0), val, 2),
2175 1, "");
2176 zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_default_1), val, 2),
2177 2, "");
2178 /* DT_ENUM_IDX_OR on int value */
2179 zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_int_default_0),
2180 val, 4),
2181 0, "");
2182 zassert_equal(DT_ENUM_IDX_OR(DT_NODELABEL(test_enum_int_default_1),
2183 val, 4),
2184 4, "");
2185 /* DT_ENUM_IDX_OR on string-array value */
2186 zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_string_array), val, 0, 2),
2187 0, "");
2188 zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_string_array), val, 5, 2),
2189 2, "");
2190 /* DT_ENUM_IDX_OR on int-array value */
2191 zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_int_array),
2192 val, 0, 7),
2193 3, "");
2194 zassert_equal(DT_ENUM_IDX_BY_IDX_OR(DT_NODELABEL(test_enum_int_array),
2195 val, 4, 7),
2196 7, "");
2197 }
2198
ZTEST(devicetree_api,test_inst_enums)2199 ZTEST(devicetree_api, test_inst_enums)
2200 {
2201 #undef DT_DRV_COMPAT
2202 #define DT_DRV_COMPAT vnd_enum_holder_inst
2203 zassert_equal(DT_INST_ENUM_IDX(0, val), 0, "");
2204 zassert_equal(DT_INST_ENUM_IDX_OR(0, val, 2), 0, "");
2205 zassert_true(DT_INST_ENUM_HAS_VALUE(0, val, zero), "");
2206 zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, one), "");
2207 zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, two), "");
2208
2209 #undef DT_DRV_COMPAT
2210 #define DT_DRV_COMPAT vnd_enum_required_false_holder_inst
2211 zassert_equal(DT_INST_ENUM_IDX_OR(0, val, 2), 2, "");
2212 zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, zero), "");
2213 zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, one), "");
2214 zassert_false(DT_INST_ENUM_HAS_VALUE(0, val, two), "");
2215
2216 /* Also add tests for these:
2217 * DT_INST_ENUM_IDX_BY_IDX
2218 * DT_INST_ENUM_IDX_BY_IDX_OR
2219 * DT_INST_ENUM_HAS_VALUE_BY_IDX
2220 */
2221 #undef DT_DRV_COMPAT
2222 #define DT_DRV_COMPAT vnd_enum_string_array_holder
2223 zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 0), 0, "");
2224 zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 1), 3, "");
2225 zassert_true(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 0, foo), "");
2226 zassert_false(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 0, zoo), "");
2227 zassert_true(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 1, zoo), "");
2228 zassert_false(DT_INST_ENUM_HAS_VALUE_BY_IDX(0, val, 2, baz), "");
2229 zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 0, 10), 0, "");
2230 zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 4, 10), 10, "");
2231
2232 #undef DT_DRV_COMPAT
2233 #define DT_DRV_COMPAT vnd_enum_int_array_holder
2234 zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 0), 3, "");
2235 zassert_equal(DT_INST_ENUM_IDX_BY_IDX(0, val, 3), 7, "");
2236 zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 1, 10), 4, "");
2237 zassert_equal(DT_INST_ENUM_IDX_BY_IDX_OR(0, val, 123654, 10), 10, "");
2238 }
2239
2240 #undef DT_DRV_COMPAT
2241 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_clocks)2242 ZTEST(devicetree_api, test_clocks)
2243 {
2244 /* DT_CLOCKS_CTLR_BY_IDX */
2245 zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR_BY_IDX(TEST_TEMP, 1),
2246 DT_NODELABEL(test_fixed_clk)), "");
2247
2248 /* DT_CLOCKS_CTLR */
2249 zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR(TEST_TEMP),
2250 DT_NODELABEL(test_clk)), "");
2251
2252 /* DT_CLOCKS_CTLR_BY_NAME */
2253 zassert_true(DT_SAME_NODE(DT_CLOCKS_CTLR_BY_NAME(TEST_TEMP, clk_b),
2254 DT_NODELABEL(test_clk)), "");
2255
2256 /* DT_NUM_CLOCKS */
2257 zassert_equal(DT_NUM_CLOCKS(TEST_TEMP), 3, "");
2258
2259 /* DT_CLOCKS_HAS_IDX */
2260 zassert_true(DT_CLOCKS_HAS_IDX(TEST_TEMP, 2), "");
2261 zassert_false(DT_CLOCKS_HAS_IDX(TEST_TEMP, 3), "");
2262
2263 /* DT_CLOCKS_HAS_NAME */
2264 zassert_true(DT_CLOCKS_HAS_NAME(TEST_TEMP, clk_a), "");
2265 zassert_false(DT_CLOCKS_HAS_NAME(TEST_TEMP, clk_z), "");
2266
2267 /* DT_CLOCKS_CELL_BY_IDX */
2268 zassert_equal(DT_CLOCKS_CELL_BY_IDX(TEST_TEMP, 2, bits), 2, "");
2269 zassert_equal(DT_CLOCKS_CELL_BY_IDX(TEST_TEMP, 2, bus), 8, "");
2270
2271 /* DT_CLOCKS_CELL_BY_NAME */
2272 zassert_equal(DT_CLOCKS_CELL_BY_NAME(TEST_TEMP, clk_a, bits), 7, "");
2273 zassert_equal(DT_CLOCKS_CELL_BY_NAME(TEST_TEMP, clk_b, bus), 8, "");
2274
2275 /* DT_CLOCKS_CELL */
2276 zassert_equal(DT_CLOCKS_CELL(TEST_TEMP, bits), 7, "");
2277 zassert_equal(DT_CLOCKS_CELL(TEST_TEMP, bus), 3, "");
2278
2279 /* clock-freq on fixed clock */
2280 zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_TEMP, clocks, 1,
2281 clock_frequency),
2282 25000000, "");
2283
2284 /* DT_INST */
2285 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
2286
2287 /* DT_INST_CLOCKS_CTLR_BY_IDX */
2288 zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR_BY_IDX(0, 1),
2289 DT_NODELABEL(test_fixed_clk)), "");
2290
2291 /* DT_INST_CLOCKS_CTLR */
2292 zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR(0),
2293 DT_NODELABEL(test_clk)), "");
2294
2295 /* DT_INST_CLOCKS_CTLR_BY_NAME */
2296 zassert_true(DT_SAME_NODE(DT_INST_CLOCKS_CTLR_BY_NAME(0, clk_b),
2297 DT_NODELABEL(test_clk)), "");
2298
2299 /* DT_INST_NUM_CLOCKS */
2300 zassert_equal(DT_INST_NUM_CLOCKS(0), 3, "");
2301
2302 /* DT_INST_CLOCKS_HAS_IDX */
2303 zassert_true(DT_INST_CLOCKS_HAS_IDX(0, 2), "");
2304 zassert_false(DT_INST_CLOCKS_HAS_IDX(0, 3), "");
2305
2306 /* DT_INST_CLOCKS_HAS_NAME */
2307 zassert_true(DT_INST_CLOCKS_HAS_NAME(0, clk_a), "");
2308 zassert_false(DT_INST_CLOCKS_HAS_NAME(0, clk_z), "");
2309
2310 /* DT_INST_CLOCKS_CELL_BY_IDX */
2311 zassert_equal(DT_INST_CLOCKS_CELL_BY_IDX(0, 2, bits), 2, "");
2312 zassert_equal(DT_INST_CLOCKS_CELL_BY_IDX(0, 2, bus), 8, "");
2313
2314 /* DT_INST_CLOCKS_CELL_BY_NAME */
2315 zassert_equal(DT_INST_CLOCKS_CELL_BY_NAME(0, clk_a, bits), 7, "");
2316 zassert_equal(DT_INST_CLOCKS_CELL_BY_NAME(0, clk_b, bus), 8, "");
2317
2318 /* DT_INST_CLOCKS_CELL */
2319 zassert_equal(DT_INST_CLOCKS_CELL(0, bits), 7, "");
2320 zassert_equal(DT_INST_CLOCKS_CELL(0, bus), 3, "");
2321
2322 /* clock-freq on fixed clock */
2323 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, clocks, 1,
2324 clock_frequency),
2325 25000000, "");
2326 }
2327
2328 #undef DT_DRV_COMPAT
2329 #define DT_DRV_COMPAT vnd_spi_device
ZTEST(devicetree_api,test_parent)2330 ZTEST(devicetree_api, test_parent)
2331 {
2332 zassert_true(DT_SAME_NODE(DT_PARENT(TEST_SPI_DEV_0), TEST_SPI_BUS_0), "");
2333
2334 /*
2335 * The parent's label for the first instance of vnd,spi-device,
2336 * child of TEST_SPI, is the same as TEST_SPI.
2337 */
2338 zassert_true(DT_SAME_NODE(DT_INST_PARENT(0), TEST_SPI), "");
2339 /*
2340 * We should be able to use DT_PARENT() even with nodes, like /test,
2341 * that have no matching compatible.
2342 */
2343 zassert_true(DT_SAME_NODE(DT_CHILD(DT_PARENT(TEST_SPI_BUS_0), spi_33334444),
2344 TEST_SPI_BUS_0), "");
2345 }
2346
2347 #undef DT_DRV_COMPAT
2348 #define DT_DRV_COMPAT vnd_parent_bindings
ZTEST(devicetree_api,test_parent_nodes_list)2349 ZTEST(devicetree_api, test_parent_nodes_list)
2350 {
2351 /* When traversing upwards, there are no fixed attributes and labels */
2352 #define TEST_FUNC(parent) { /* No operation */ }
2353 #define TEST_FUNC_AND_COMMA(parent) TEST_FUNC(parent),
2354
2355 struct vnd_parent_binding {
2356 int val;
2357 };
2358
2359 struct vnd_parent_binding vals_a[] = {
2360 DT_FOREACH_ANCESTOR(DT_NODELABEL(test_parent_a), TEST_FUNC_AND_COMMA)};
2361
2362 struct vnd_parent_binding vals_b[] = {
2363 DT_FOREACH_ANCESTOR(DT_NODELABEL(test_parent_b), TEST_FUNC_AND_COMMA)};
2364
2365 zassert_equal(ARRAY_SIZE(vals_a), 3, "");
2366 zassert_equal(ARRAY_SIZE(vals_b), 4, "");
2367
2368 #undef TEST_FUNC_AND_COMMA
2369 #undef TEST_FUNC
2370 }
2371
2372 #undef DT_DRV_COMPAT
2373 #define DT_DRV_COMPAT vnd_i2c_mux_controller
ZTEST(devicetree_api,test_gparent)2374 ZTEST(devicetree_api, test_gparent)
2375 {
2376 zassert_true(DT_SAME_NODE(DT_GPARENT(TEST_I2C_MUX_CTLR_1), TEST_I2C), "");
2377 zassert_true(DT_SAME_NODE(DT_INST_GPARENT(0), TEST_I2C), "");
2378 zassert_true(DT_SAME_NODE(DT_INST_GPARENT(1), TEST_I2C), "");
2379 }
2380
2381 #undef DT_DRV_COMPAT
2382 #define DT_DRV_COMPAT vnd_child_bindings
ZTEST(devicetree_api,test_children)2383 ZTEST(devicetree_api, test_children)
2384 {
2385 zassert_equal(DT_PROP(DT_CHILD(DT_NODELABEL(test_children), child_a),
2386 val), 0, "");
2387 zassert_equal(DT_PROP(DT_CHILD(DT_NODELABEL(test_children), child_b),
2388 val), 1, "");
2389 zassert_equal(DT_PROP(DT_CHILD(DT_NODELABEL(test_children), child_c),
2390 val), 2, "");
2391
2392 zassert_equal(DT_PROP(DT_INST_CHILD(0, child_a), val), 0, "");
2393 zassert_equal(DT_PROP(DT_INST_CHILD(0, child_b), val), 1, "");
2394 zassert_equal(DT_PROP(DT_INST_CHILD(0, child_c), val), 2, "");
2395 }
2396
2397 #undef DT_DRV_COMPAT
2398 #define DT_DRV_COMPAT vnd_child_bindings
ZTEST(devicetree_api,test_child_nodes_list)2399 ZTEST(devicetree_api, test_child_nodes_list)
2400 {
2401 #define TEST_FUNC(child) { DT_PROP(child, val) }
2402 #define TEST_FUNC_AND_COMMA(child) TEST_FUNC(child),
2403 #define TEST_PARENT DT_PARENT(DT_NODELABEL(test_child_a))
2404
2405 struct vnd_child_binding {
2406 int val;
2407 };
2408
2409 struct vnd_child_binding vals[] = {
2410 DT_FOREACH_CHILD(TEST_PARENT, TEST_FUNC_AND_COMMA)
2411 };
2412
2413 struct vnd_child_binding vals_sep[] = {
2414 DT_FOREACH_CHILD_SEP(TEST_PARENT, TEST_FUNC, (,))
2415 };
2416
2417 struct vnd_child_binding vals_inst[] = {
2418 DT_INST_FOREACH_CHILD(0, TEST_FUNC_AND_COMMA)
2419 };
2420
2421 struct vnd_child_binding vals_inst_sep[] = {
2422 DT_INST_FOREACH_CHILD_SEP(0, TEST_FUNC, (,))
2423 };
2424
2425 struct vnd_child_binding vals_status_okay[] = {
2426 DT_FOREACH_CHILD_STATUS_OKAY(TEST_PARENT, TEST_FUNC_AND_COMMA)
2427 };
2428
2429 struct vnd_child_binding vals_status_okay_inst[] = {
2430 DT_INST_FOREACH_CHILD_STATUS_OKAY(0, TEST_FUNC_AND_COMMA)
2431 };
2432
2433 struct vnd_child_binding vals_status_okay_sep[] = {
2434 DT_FOREACH_CHILD_STATUS_OKAY_SEP(TEST_PARENT, TEST_FUNC, (,))
2435 };
2436
2437 struct vnd_child_binding vals_status_okay_inst_sep[] = {
2438 DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(0, TEST_FUNC, (,))
2439 };
2440
2441 zassert_equal(ARRAY_SIZE(vals), 3, "");
2442 zassert_equal(ARRAY_SIZE(vals_sep), 3, "");
2443 zassert_equal(ARRAY_SIZE(vals_inst), 3, "");
2444 zassert_equal(ARRAY_SIZE(vals_inst_sep), 3, "");
2445 zassert_equal(ARRAY_SIZE(vals_status_okay), 2, "");
2446 zassert_equal(ARRAY_SIZE(vals_status_okay_inst), 2, "");
2447 zassert_equal(ARRAY_SIZE(vals_status_okay_sep), 2, "");
2448 zassert_equal(ARRAY_SIZE(vals_status_okay_inst_sep), 2, "");
2449
2450 zassert_equal(vals[0].val, 0, "");
2451 zassert_equal(vals[1].val, 1, "");
2452 zassert_equal(vals[2].val, 2, "");
2453 zassert_equal(vals_sep[0].val, 0, "");
2454 zassert_equal(vals_sep[1].val, 1, "");
2455 zassert_equal(vals_sep[2].val, 2, "");
2456 zassert_equal(vals_inst[0].val, 0, "");
2457 zassert_equal(vals_inst[1].val, 1, "");
2458 zassert_equal(vals_inst[2].val, 2, "");
2459 zassert_equal(vals_inst_sep[0].val, 0, "");
2460 zassert_equal(vals_inst_sep[1].val, 1, "");
2461 zassert_equal(vals_inst_sep[2].val, 2, "");
2462 zassert_equal(vals_status_okay[0].val, 0, "");
2463 zassert_equal(vals_status_okay[1].val, 1, "");
2464 zassert_equal(vals_status_okay_inst[0].val, 0, "");
2465 zassert_equal(vals_status_okay_inst[1].val, 1, "");
2466 zassert_equal(vals_status_okay_sep[0].val, 0, "");
2467 zassert_equal(vals_status_okay_sep[1].val, 1, "");
2468 zassert_equal(vals_status_okay_inst_sep[0].val, 0, "");
2469 zassert_equal(vals_status_okay_inst_sep[1].val, 1, "");
2470
2471 #undef TEST_PARENT
2472 #undef TEST_FUNC_AND_COMMA
2473 #undef TEST_FUNC
2474 }
2475
2476 #undef DT_DRV_COMPAT
2477 #define DT_DRV_COMPAT vnd_child_bindings
ZTEST(devicetree_api,test_child_nodes_list_varg)2478 ZTEST(devicetree_api, test_child_nodes_list_varg)
2479 {
2480 #define TEST_FUNC(child, arg) { DT_PROP(child, val) + arg }
2481 #define TEST_FUNC_AND_COMMA(child, arg) TEST_FUNC(child, arg),
2482 #define TEST_PARENT DT_PARENT(DT_NODELABEL(test_child_a))
2483
2484 struct vnd_child_binding {
2485 int val;
2486 };
2487
2488 struct vnd_child_binding vals[] = {
2489 DT_FOREACH_CHILD_VARGS(TEST_PARENT, TEST_FUNC_AND_COMMA, 1)
2490 };
2491
2492 struct vnd_child_binding vals_sep[] = {
2493 DT_FOREACH_CHILD_SEP_VARGS(TEST_PARENT, TEST_FUNC, (,), 1)
2494 };
2495
2496 struct vnd_child_binding vals_inst[] = {
2497 DT_INST_FOREACH_CHILD_VARGS(0, TEST_FUNC_AND_COMMA, 1)
2498 };
2499
2500 struct vnd_child_binding vals_inst_sep[] = {
2501 DT_INST_FOREACH_CHILD_SEP_VARGS(0, TEST_FUNC, (,), 1)
2502 };
2503
2504 struct vnd_child_binding vals_status_okay[] = {
2505 DT_FOREACH_CHILD_STATUS_OKAY_VARGS(TEST_PARENT, TEST_FUNC_AND_COMMA, 1)
2506 };
2507
2508 struct vnd_child_binding vals_status_okay_inst[] = {
2509 DT_INST_FOREACH_CHILD_STATUS_OKAY_VARGS(0, TEST_FUNC_AND_COMMA, 1)
2510 };
2511
2512 struct vnd_child_binding vals_status_okay_sep[] = {
2513 DT_FOREACH_CHILD_STATUS_OKAY_SEP_VARGS(TEST_PARENT, TEST_FUNC, (,), 1)
2514 };
2515
2516 struct vnd_child_binding vals_status_okay_inst_sep[] = {
2517 DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP_VARGS(0, TEST_FUNC, (,), 1)
2518 };
2519
2520 zassert_equal(ARRAY_SIZE(vals), 3, "");
2521 zassert_equal(ARRAY_SIZE(vals_sep), 3, "");
2522 zassert_equal(ARRAY_SIZE(vals_inst), 3, "");
2523 zassert_equal(ARRAY_SIZE(vals_inst_sep), 3, "");
2524 zassert_equal(ARRAY_SIZE(vals_status_okay), 2, "");
2525 zassert_equal(ARRAY_SIZE(vals_status_okay_inst), 2, "");
2526 zassert_equal(ARRAY_SIZE(vals_status_okay_sep), 2, "");
2527 zassert_equal(ARRAY_SIZE(vals_status_okay_inst_sep), 2, "");
2528
2529 zassert_equal(vals[0].val, 1, "");
2530 zassert_equal(vals[1].val, 2, "");
2531 zassert_equal(vals[2].val, 3, "");
2532 zassert_equal(vals_sep[0].val, 1, "");
2533 zassert_equal(vals_sep[1].val, 2, "");
2534 zassert_equal(vals_sep[2].val, 3, "");
2535 zassert_equal(vals_inst[0].val, 1, "");
2536 zassert_equal(vals_inst[1].val, 2, "");
2537 zassert_equal(vals_inst[2].val, 3, "");
2538 zassert_equal(vals_inst_sep[0].val, 1, "");
2539 zassert_equal(vals_inst_sep[1].val, 2, "");
2540 zassert_equal(vals_inst_sep[2].val, 3, "");
2541 zassert_equal(vals_status_okay[0].val, 1, "");
2542 zassert_equal(vals_status_okay[1].val, 2, "");
2543 zassert_equal(vals_status_okay_inst[0].val, 1, "");
2544 zassert_equal(vals_status_okay_inst[1].val, 2, "");
2545 zassert_equal(vals_status_okay_sep[0].val, 1, "");
2546 zassert_equal(vals_status_okay_sep[1].val, 2, "");
2547 zassert_equal(vals_status_okay_inst_sep[0].val, 1, "");
2548 zassert_equal(vals_status_okay_inst_sep[1].val, 2, "");
2549
2550 #undef TEST_PARENT
2551 #undef TEST_FUNC_AND_COMMA
2552 #undef TEST_FUNC
2553 }
2554
2555 #undef DT_DRV_COMPAT
2556 #define DT_DRV_COMPAT vnd_child_bindings
ZTEST(devicetree_api,test_child_nodes_number)2557 ZTEST(devicetree_api, test_child_nodes_number)
2558 {
2559 zassert_equal(DT_CHILD_NUM(TEST_CHILDREN), 3, "");
2560 zassert_equal(DT_INST_CHILD_NUM(0), 3, "");
2561 zassert_equal(DT_CHILD_NUM_STATUS_OKAY(TEST_CHILDREN), 2, "");
2562 zassert_equal(DT_INST_CHILD_NUM_STATUS_OKAY(0), 2, "");
2563 }
2564
ZTEST(devicetree_api,test_great_grandchild)2565 ZTEST(devicetree_api, test_great_grandchild)
2566 {
2567 zassert_equal(DT_PROP(DT_NODELABEL(test_ggc), ggc_prop), 42, "");
2568 }
2569
2570 #undef DT_DRV_COMPAT
2571 #define DT_DRV_COMPAT vnd_test_ranges_pcie
ZTEST(devicetree_api,test_ranges_pcie)2572 ZTEST(devicetree_api, test_ranges_pcie)
2573 {
2574 #define FLAGS(node_id, idx) \
2575 DT_RANGES_CHILD_BUS_FLAGS_BY_IDX(node_id, idx),
2576 #define CHILD_BUS_ADDR(node_id, idx) \
2577 DT_RANGES_CHILD_BUS_ADDRESS_BY_IDX(node_id, idx),
2578 #define PARENT_BUS_ADDR(node_id, idx) \
2579 DT_RANGES_PARENT_BUS_ADDRESS_BY_IDX(node_id, idx),
2580 #define LENGTH(node_id, idx) DT_RANGES_LENGTH_BY_IDX(node_id, idx),
2581
2582 unsigned int count = DT_NUM_RANGES(TEST_RANGES_PCIE);
2583
2584 const uint64_t ranges_pcie_flags[] = {
2585 DT_FOREACH_RANGE(TEST_RANGES_PCIE, FLAGS)
2586 };
2587
2588 const uint64_t ranges_child_bus_addr[] = {
2589 DT_FOREACH_RANGE(TEST_RANGES_PCIE, CHILD_BUS_ADDR)
2590 };
2591
2592 const uint64_t ranges_parent_bus_addr[] = {
2593 DT_FOREACH_RANGE(TEST_RANGES_PCIE, PARENT_BUS_ADDR)
2594 };
2595
2596 const uint64_t ranges_length[] = {
2597 DT_FOREACH_RANGE(TEST_RANGES_PCIE, LENGTH)
2598 };
2599
2600 zassert_equal(count, 3, "");
2601
2602 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 0), 1, "");
2603 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 1), 1, "");
2604 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 2), 1, "");
2605 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_PCIE, 3), 0, "");
2606
2607 zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 0),
2608 1, "");
2609 zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 1),
2610 1, "");
2611 zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 2),
2612 1, "");
2613 zassert_equal(DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(TEST_RANGES_PCIE, 3),
2614 0, "");
2615
2616 zassert_equal(ranges_pcie_flags[0], 0x1000000, "");
2617 zassert_equal(ranges_pcie_flags[1], 0x2000000, "");
2618 zassert_equal(ranges_pcie_flags[2], 0x3000000, "");
2619 zassert_equal(ranges_child_bus_addr[0], 0, "");
2620 zassert_equal(ranges_child_bus_addr[1], 0x10000000, "");
2621 zassert_equal(ranges_child_bus_addr[2], 0x8000000000, "");
2622 zassert_equal(ranges_parent_bus_addr[0], 0x3eff0000, "");
2623 zassert_equal(ranges_parent_bus_addr[1], 0x10000000, "");
2624 zassert_equal(ranges_parent_bus_addr[2], 0x8000000000, "");
2625 zassert_equal(ranges_length[0], 0x10000, "");
2626 zassert_equal(ranges_length[1], 0x2eff0000, "");
2627 zassert_equal(ranges_length[2], 0x8000000000, "");
2628
2629 #undef FLAGS
2630 #undef CHILD_BUS_ADDR
2631 #undef PARENT_BUS_ADDR
2632 #undef LENGTH
2633 }
2634
ZTEST(devicetree_api,test_ranges_other)2635 ZTEST(devicetree_api, test_ranges_other)
2636 {
2637 #define HAS_FLAGS(node_id, idx) \
2638 DT_RANGES_HAS_CHILD_BUS_FLAGS_AT_IDX(node_id, idx)
2639 #define FLAGS(node_id, idx) \
2640 DT_RANGES_CHILD_BUS_FLAGS_BY_IDX(node_id, idx),
2641 #define CHILD_BUS_ADDR(node_id, idx) \
2642 DT_RANGES_CHILD_BUS_ADDRESS_BY_IDX(node_id, idx),
2643 #define PARENT_BUS_ADDR(node_id, idx) \
2644 DT_RANGES_PARENT_BUS_ADDRESS_BY_IDX(node_id, idx),
2645 #define LENGTH(node_id, idx) DT_RANGES_LENGTH_BY_IDX(node_id, idx),
2646
2647 unsigned int count = DT_NUM_RANGES(TEST_RANGES_OTHER);
2648
2649 const uint32_t ranges_child_bus_addr[] = {
2650 DT_FOREACH_RANGE(TEST_RANGES_OTHER, CHILD_BUS_ADDR)
2651 };
2652
2653 const uint32_t ranges_parent_bus_addr[] = {
2654 DT_FOREACH_RANGE(TEST_RANGES_OTHER, PARENT_BUS_ADDR)
2655 };
2656
2657 const uint32_t ranges_length[] = {
2658 DT_FOREACH_RANGE(TEST_RANGES_OTHER, LENGTH)
2659 };
2660
2661 zassert_equal(count, 2, "");
2662
2663 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 0), 1, "");
2664 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 1), 1, "");
2665 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 2), 0, "");
2666 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_OTHER, 3), 0, "");
2667
2668 zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 0), 0, "");
2669 zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 1), 0, "");
2670 zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 2), 0, "");
2671 zassert_equal(HAS_FLAGS(TEST_RANGES_OTHER, 3), 0, "");
2672
2673 zassert_equal(ranges_child_bus_addr[0], 0, "");
2674 zassert_equal(ranges_child_bus_addr[1], 0x10000000, "");
2675 zassert_equal(ranges_parent_bus_addr[0], 0x3eff0000, "");
2676 zassert_equal(ranges_parent_bus_addr[1], 0x10000000, "");
2677 zassert_equal(ranges_length[0], 0x10000, "");
2678 zassert_equal(ranges_length[1], 0x2eff0000, "");
2679
2680 #undef HAS_FLAGS
2681 #undef FLAGS
2682 #undef CHILD_BUS_ADDR
2683 #undef PARENT_BUS_ADDR
2684 #undef LENGTH
2685 }
2686
ZTEST(devicetree_api,test_ranges_empty)2687 ZTEST(devicetree_api, test_ranges_empty)
2688 {
2689 zassert_equal(DT_NODE_HAS_PROP(TEST_RANGES_EMPTY, ranges), 1, "");
2690
2691 zassert_equal(DT_NUM_RANGES(TEST_RANGES_EMPTY), 0, "");
2692
2693 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_EMPTY, 0), 0, "");
2694 zassert_equal(DT_RANGES_HAS_IDX(TEST_RANGES_EMPTY, 1), 0, "");
2695
2696 #define FAIL(node_id, idx) ztest_test_fail();
2697
2698 DT_FOREACH_RANGE(TEST_RANGES_EMPTY, FAIL);
2699
2700 #undef FAIL
2701 }
2702
ZTEST(devicetree_api,test_compat_get_any_status_okay)2703 ZTEST(devicetree_api, test_compat_get_any_status_okay)
2704 {
2705 zassert_true(
2706 DT_SAME_NODE(
2707 DT_COMPAT_GET_ANY_STATUS_OKAY(vnd_reg_holder),
2708 TEST_REG),
2709 "");
2710
2711 /*
2712 * DT_SAME_NODE requires that both its arguments are valid
2713 * node identifiers, so we can't pass it DT_INVALID_NODE,
2714 * which is what this DT_COMPAT_GET_ANY_STATUS_OKAY() expands to.
2715 */
2716 zassert_false(
2717 DT_NODE_EXISTS(
2718 DT_COMPAT_GET_ANY_STATUS_OKAY(this_is_not_a_real_compat)),
2719 "");
2720 }
2721
ord_in_array(unsigned int ord,unsigned int * array,size_t array_size)2722 static bool ord_in_array(unsigned int ord, unsigned int *array,
2723 size_t array_size)
2724 {
2725 size_t i;
2726
2727 for (i = 0; i < array_size; i++) {
2728 if (array[i] == ord) {
2729 return true;
2730 }
2731 }
2732
2733 return false;
2734 }
2735
2736 /* Magic numbers used by COMBINED_ORD_ARRAY. Must be invalid dependency
2737 * ordinals.
2738 */
2739 #define ORD_LIST_SEP 0xFFFF0000
2740 #define ORD_LIST_END 0xFFFF0001
2741 #define INJECTED_DEP_0 0xFFFF0002
2742 #define INJECTED_DEP_1 0xFFFF0003
2743
2744 #define DEP_ORD_AND_COMMA(node_id) DT_DEP_ORD(node_id),
2745 #define CHILD_ORDINALS(node_id) DT_FOREACH_CHILD(node_id, DEP_ORD_AND_COMMA)
2746
2747 #define COMBINED_ORD_ARRAY(node_id) \
2748 { \
2749 DT_DEP_ORD(node_id), \
2750 DT_DEP_ORD(DT_PARENT(node_id)), \
2751 CHILD_ORDINALS(node_id) \
2752 ORD_LIST_SEP, \
2753 DT_REQUIRES_DEP_ORDS(node_id) \
2754 INJECTED_DEP_0, \
2755 INJECTED_DEP_1, \
2756 ORD_LIST_SEP, \
2757 DT_SUPPORTS_DEP_ORDS(node_id) \
2758 ORD_LIST_END \
2759 }
2760
ZTEST(devicetree_api,test_dep_ord)2761 ZTEST(devicetree_api, test_dep_ord)
2762 {
2763 #define ORD_IN_ARRAY(ord, array) ord_in_array(ord, array, ARRAY_SIZE(array))
2764
2765 unsigned int root_ord = DT_DEP_ORD(DT_ROOT),
2766 test_ord = DT_DEP_ORD(DT_PATH(test)),
2767 root_requires[] = { DT_REQUIRES_DEP_ORDS(DT_ROOT) },
2768 test_requires[] = { DT_REQUIRES_DEP_ORDS(DT_PATH(test)) },
2769 root_supports[] = { DT_SUPPORTS_DEP_ORDS(DT_ROOT) },
2770 test_supports[] = { DT_SUPPORTS_DEP_ORDS(DT_PATH(test)) },
2771 children_ords[] = {
2772 DT_FOREACH_CHILD(TEST_CHILDREN, DEP_ORD_AND_COMMA)
2773 },
2774 children_combined_ords[] = COMBINED_ORD_ARRAY(TEST_CHILDREN),
2775 child_a_combined_ords[] =
2776 COMBINED_ORD_ARRAY(DT_NODELABEL(test_child_a));
2777 size_t i;
2778
2779 /* DT_DEP_ORD */
2780 zassert_equal(root_ord, 0, "");
2781 zassert_true(DT_DEP_ORD(DT_NODELABEL(test_child_a)) >
2782 DT_DEP_ORD(DT_NODELABEL(test_children)), "");
2783 zassert_true(DT_DEP_ORD(DT_NODELABEL(test_irq)) >
2784 DT_DEP_ORD(DT_NODELABEL(test_intc)), "");
2785 zassert_true(DT_DEP_ORD(DT_NODELABEL(test_phandles)) >
2786 DT_DEP_ORD(DT_NODELABEL(test_gpio_1)), "");
2787
2788 /* DT_REQUIRES_DEP_ORDS */
2789 zassert_equal(ARRAY_SIZE(root_requires), 0, "");
2790 zassert_true(ORD_IN_ARRAY(root_ord, test_requires), "");
2791
2792 /* DT_SUPPORTS_DEP_ORDS */
2793 zassert_true(ORD_IN_ARRAY(test_ord, root_supports), "");
2794 zassert_false(ORD_IN_ARRAY(root_ord, test_supports), "");
2795
2796 unsigned int children_combined_ords_expected[] = {
2797 /*
2798 * Combined ordinals for /test/test-children are from
2799 * these nodes in this order:
2800 */
2801 DT_DEP_ORD(TEST_CHILDREN), /* node */
2802 DT_DEP_ORD(DT_PATH(test)), /* parent */
2803 DT_DEP_ORD(DT_NODELABEL(test_child_a)), /* children */
2804 DT_DEP_ORD(DT_NODELABEL(test_child_b)),
2805 DT_DEP_ORD(DT_NODELABEL(test_child_c)),
2806 ORD_LIST_SEP, /* separator */
2807 DT_DEP_ORD(DT_PATH(test)), /* requires */
2808 INJECTED_DEP_0, /* injected
2809 * dependencies
2810 */
2811 INJECTED_DEP_1,
2812 ORD_LIST_SEP, /* separator */
2813 DT_DEP_ORD(DT_NODELABEL(test_child_a)), /* supports */
2814 DT_DEP_ORD(DT_NODELABEL(test_child_b)),
2815 DT_DEP_ORD(DT_NODELABEL(test_child_c)),
2816 ORD_LIST_END, /* terminator */
2817 };
2818 zassert_equal(ARRAY_SIZE(children_combined_ords),
2819 ARRAY_SIZE(children_combined_ords_expected),
2820 "%zu", ARRAY_SIZE(children_combined_ords));
2821 for (i = 0; i < ARRAY_SIZE(children_combined_ords); i++) {
2822 zassert_equal(children_combined_ords[i],
2823 children_combined_ords_expected[i],
2824 "test-children at %zu", i);
2825 }
2826
2827 unsigned int child_a_combined_ords_expected[] = {
2828 /*
2829 * Combined ordinals for /test/test-children/child-a
2830 * are from these nodes in this order:
2831 */
2832 DT_DEP_ORD(DT_NODELABEL(test_child_a)), /* node */
2833 DT_DEP_ORD(TEST_CHILDREN), /* parent */
2834 /* children (none) */
2835 ORD_LIST_SEP, /* separator */
2836 DT_DEP_ORD(TEST_CHILDREN), /* requires */
2837 INJECTED_DEP_0, /* injected
2838 * dependencies
2839 */
2840 INJECTED_DEP_1,
2841 ORD_LIST_SEP, /* separator */
2842 /* supports (none) */
2843 ORD_LIST_END, /* terminator */
2844 };
2845 zassert_equal(ARRAY_SIZE(child_a_combined_ords),
2846 ARRAY_SIZE(child_a_combined_ords_expected),
2847 "%zu", ARRAY_SIZE(child_a_combined_ords));
2848 for (i = 0; i < ARRAY_SIZE(child_a_combined_ords); i++) {
2849 zassert_equal(child_a_combined_ords[i],
2850 child_a_combined_ords_expected[i],
2851 "child-a at %zu", i);
2852 }
2853
2854 #undef DT_DRV_COMPAT
2855 #define DT_DRV_COMPAT vnd_child_bindings
2856
2857 /* DT_INST_DEP_ORD */
2858 zassert_equal(DT_INST_DEP_ORD(0),
2859 DT_DEP_ORD(DT_NODELABEL(test_children)), "");
2860
2861 /* DT_INST_REQUIRES_DEP_ORDS */
2862 unsigned int inst_requires[] = { DT_INST_REQUIRES_DEP_ORDS(0) };
2863
2864 zassert_equal(ARRAY_SIZE(inst_requires), 1, "");
2865 zassert_equal(inst_requires[0], test_ord, "");
2866
2867 /* DT_INST_SUPPORTS_DEP_ORDS */
2868 unsigned int inst_supports[] = { DT_INST_SUPPORTS_DEP_ORDS(0) };
2869
2870 zassert_equal(ARRAY_SIZE(inst_supports), 3, "");
2871 for (i = 0; i < ARRAY_SIZE(inst_supports); i++) {
2872 zassert_true(ORD_IN_ARRAY(inst_supports[i], children_ords), "");
2873 }
2874 }
2875
ZTEST(devicetree_api,test_dep_ord_str_sortable)2876 ZTEST(devicetree_api, test_dep_ord_str_sortable)
2877 {
2878 const char *root_ord = STRINGIFY(DT_DEP_ORD_STR_SORTABLE(DT_ROOT));
2879
2880 /* Root ordinal is always 0 */
2881 zassert_mem_equal(root_ord, "00000", 6);
2882
2883 /* Test string sortable versions equal decimal values.
2884 * We go through the STRINGIFY()->atoi conversion cycle to avoid
2885 * the C compiler treating the number as octal due to leading zeros.
2886 * atoi() simply ignores them.
2887 */
2888 zassert_equal(atoi(STRINGIFY(DT_DEP_ORD_STR_SORTABLE(DT_ROOT))),
2889 DT_DEP_ORD(DT_ROOT), "Invalid sortable string");
2890 zassert_equal(atoi(STRINGIFY(DT_DEP_ORD_STR_SORTABLE(TEST_DEADBEEF))),
2891 DT_DEP_ORD(TEST_DEADBEEF), "Invalid sortable string");
2892 zassert_equal(atoi(STRINGIFY(DT_DEP_ORD_STR_SORTABLE(TEST_TEMP))),
2893 DT_DEP_ORD(TEST_TEMP), "Invalid sortable string");
2894 zassert_equal(atoi(STRINGIFY(DT_DEP_ORD_STR_SORTABLE(TEST_REG))),
2895 DT_DEP_ORD(TEST_REG), "Invalid sortable string");
2896 }
2897
ZTEST(devicetree_api,test_path)2898 ZTEST(devicetree_api, test_path)
2899 {
2900 zassert_true(!strcmp(DT_NODE_PATH(DT_ROOT), "/"), "");
2901 zassert_true(!strcmp(DT_NODE_PATH(TEST_DEADBEEF),
2902 "/test/gpio@deadbeef"), "");
2903 }
2904
ZTEST(devicetree_api,test_node_name)2905 ZTEST(devicetree_api, test_node_name)
2906 {
2907 zassert_true(!strcmp(DT_NODE_FULL_NAME(DT_ROOT), "/"), "");
2908 zassert_true(!strcmp(DT_NODE_FULL_NAME(TEST_DEADBEEF),
2909 "gpio@deadbeef"), "");
2910 zassert_true(!strcmp(DT_NODE_FULL_NAME(TEST_TEMP),
2911 "temperature-sensor"), "");
2912 zassert_true(strcmp(DT_NODE_FULL_NAME(TEST_REG),
2913 "reg-holder"), "");
2914 }
2915
ZTEST(devicetree_api,test_node_child_idx)2916 ZTEST(devicetree_api, test_node_child_idx)
2917 {
2918 zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_a)), 0, "");
2919 zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_b)), 1, "");
2920 zassert_equal(DT_NODE_CHILD_IDX(DT_NODELABEL(test_child_c)), 2, "");
2921 }
2922
ZTEST(devicetree_api,test_same_node)2923 ZTEST(devicetree_api, test_same_node)
2924 {
2925 zassert_true(DT_SAME_NODE(TEST_DEADBEEF, TEST_DEADBEEF), "");
2926 zassert_false(DT_SAME_NODE(TEST_DEADBEEF, TEST_ABCD1234), "");
2927 }
2928
ZTEST(devicetree_api,test_pinctrl)2929 ZTEST(devicetree_api, test_pinctrl)
2930 {
2931 #undef DT_DRV_COMPAT
2932 #define DT_DRV_COMPAT vnd_adc_temp_sensor
2933 /*
2934 * Tests when a node does have pinctrl properties.
2935 */
2936
2937 /*
2938 * node_id versions:
2939 */
2940
2941 zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_IDX(TEST_TEMP, 0, 1),
2942 DT_NODELABEL(test_pincfg_b)), "");
2943 zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_IDX(TEST_TEMP, 1, 0),
2944 DT_NODELABEL(test_pincfg_c)), "");
2945
2946 zassert_true(DT_SAME_NODE(DT_PINCTRL_0(TEST_TEMP, 0),
2947 DT_NODELABEL(test_pincfg_a)), "");
2948
2949 zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, default, 1),
2950 DT_NODELABEL(test_pincfg_b)), "");
2951 zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, sleep, 0),
2952 DT_NODELABEL(test_pincfg_c)), "");
2953 zassert_true(DT_SAME_NODE(DT_PINCTRL_BY_NAME(TEST_TEMP, f_o_o2, 0),
2954 DT_NODELABEL(test_pincfg_d)), "");
2955
2956 zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, default), 0, "");
2957 zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, sleep), 1, "");
2958 zassert_equal(DT_PINCTRL_NAME_TO_IDX(TEST_TEMP, f_o_o2), 2, "");
2959
2960 zassert_equal(DT_NUM_PINCTRLS_BY_IDX(TEST_TEMP, 0), 2, "");
2961
2962 zassert_equal(DT_NUM_PINCTRLS_BY_NAME(TEST_TEMP, default), 2, "");
2963 zassert_equal(DT_NUM_PINCTRLS_BY_NAME(TEST_TEMP, f_o_o2), 1, "");
2964
2965 zassert_equal(DT_NUM_PINCTRL_STATES(TEST_TEMP), 3, "");
2966
2967 zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 0), 1, "");
2968 zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 1), 1, "");
2969 zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 2), 1, "");
2970 zassert_equal(DT_PINCTRL_HAS_IDX(TEST_TEMP, 3), 0, "");
2971
2972 zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, default), 1, "");
2973 zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, sleep), 1, "");
2974 zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, f_o_o2), 1, "");
2975 zassert_equal(DT_PINCTRL_HAS_NAME(TEST_TEMP, bar), 0, "");
2976
2977 #undef MAKE_TOKEN
2978 #define MAKE_TOKEN(pc_idx) \
2979 _CONCAT(NODE_ID_ENUM_, \
2980 DT_PINCTRL_IDX_TO_NAME_TOKEN(TEST_TEMP, pc_idx))
2981 #undef MAKE_UPPER_TOKEN
2982 #define MAKE_UPPER_TOKEN(pc_idx) \
2983 _CONCAT(NODE_ID_ENUM_, \
2984 DT_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(TEST_TEMP, pc_idx))
2985 enum {
2986 MAKE_TOKEN(0) = 10,
2987 MAKE_TOKEN(1) = 11,
2988 MAKE_TOKEN(2) = 12,
2989 MAKE_TOKEN(3) = 13,
2990
2991 MAKE_UPPER_TOKEN(0) = 20,
2992 MAKE_UPPER_TOKEN(1) = 21,
2993 MAKE_UPPER_TOKEN(2) = 22,
2994 MAKE_UPPER_TOKEN(3) = 23,
2995 };
2996
2997 zassert_equal(NODE_ID_ENUM_default, 10, "");
2998 zassert_equal(NODE_ID_ENUM_sleep, 11, "");
2999 zassert_equal(NODE_ID_ENUM_f_o_o2, 12, "");
3000
3001 zassert_equal(NODE_ID_ENUM_DEFAULT, 20, "");
3002 zassert_equal(NODE_ID_ENUM_SLEEP, 21, "");
3003 zassert_equal(NODE_ID_ENUM_F_O_O2, 22, "");
3004
3005 /*
3006 * inst versions:
3007 */
3008
3009 zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_IDX(0, 0, 1),
3010 DT_NODELABEL(test_pincfg_b)), "");
3011 zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_IDX(0, 1, 0),
3012 DT_NODELABEL(test_pincfg_c)), "");
3013
3014 zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_0(0, 0),
3015 DT_NODELABEL(test_pincfg_a)), "");
3016
3017 zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, default, 1),
3018 DT_NODELABEL(test_pincfg_b)), "");
3019 zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, sleep, 0),
3020 DT_NODELABEL(test_pincfg_c)), "");
3021 zassert_true(DT_SAME_NODE(DT_INST_PINCTRL_BY_NAME(0, f_o_o2, 0),
3022 DT_NODELABEL(test_pincfg_d)), "");
3023
3024 zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, default), 0, "");
3025 zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, sleep), 1, "");
3026 zassert_equal(DT_INST_PINCTRL_NAME_TO_IDX(0, f_o_o2), 2, "");
3027
3028 zassert_equal(DT_INST_NUM_PINCTRLS_BY_IDX(0, 0), 2, "");
3029
3030 zassert_equal(DT_INST_NUM_PINCTRLS_BY_NAME(0, default), 2, "");
3031 zassert_equal(DT_INST_NUM_PINCTRLS_BY_NAME(0, f_o_o2), 1, "");
3032
3033 zassert_equal(DT_INST_NUM_PINCTRL_STATES(0), 3, "");
3034
3035 zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 0), 1, "");
3036 zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 1), 1, "");
3037 zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 2), 1, "");
3038 zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 3), 0, "");
3039
3040 zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, default), 1, "");
3041 zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, sleep), 1, "");
3042 zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, f_o_o2), 1, "");
3043 zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, bar), 0, "");
3044
3045 #undef MAKE_TOKEN
3046 #define MAKE_TOKEN(pc_idx) \
3047 _CONCAT(INST_ENUM_, \
3048 DT_INST_PINCTRL_IDX_TO_NAME_TOKEN(0, pc_idx))
3049 #undef MAKE_UPPER_TOKEN
3050 #define MAKE_UPPER_TOKEN(pc_idx) \
3051 _CONCAT(INST_ENUM_, \
3052 DT_INST_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(0, pc_idx))
3053 enum {
3054 MAKE_TOKEN(0) = 10,
3055 MAKE_TOKEN(1) = 11,
3056 MAKE_TOKEN(2) = 12,
3057
3058 MAKE_UPPER_TOKEN(0) = 20,
3059 MAKE_UPPER_TOKEN(1) = 21,
3060 MAKE_UPPER_TOKEN(2) = 22,
3061 };
3062
3063 zassert_equal(INST_ENUM_default, 10, "");
3064 zassert_equal(INST_ENUM_sleep, 11, "");
3065 zassert_equal(INST_ENUM_f_o_o2, 12, "");
3066
3067 zassert_equal(INST_ENUM_DEFAULT, 20, "");
3068 zassert_equal(INST_ENUM_SLEEP, 21, "");
3069 zassert_equal(INST_ENUM_F_O_O2, 22, "");
3070
3071 #undef DT_DRV_COMPAT
3072 #define DT_DRV_COMPAT vnd_reg_holder
3073 /*
3074 * Tests when a node does NOT have any pinctrl properties.
3075 */
3076
3077 /* node_id versions */
3078 zassert_equal(DT_NUM_PINCTRL_STATES(TEST_REG), 0, "");
3079 zassert_equal(DT_PINCTRL_HAS_IDX(TEST_REG, 0), 0, "");
3080 zassert_equal(DT_PINCTRL_HAS_NAME(TEST_REG, f_o_o2), 0, "");
3081
3082 /* inst versions */
3083 zassert_equal(DT_INST_NUM_PINCTRL_STATES(0), 0, "");
3084 zassert_equal(DT_INST_PINCTRL_HAS_IDX(0, 0), 0, "");
3085 zassert_equal(DT_INST_PINCTRL_HAS_NAME(0, f_o_o2), 0, "");
3086 }
3087
3088 DEVICE_DT_DEFINE(DT_NODELABEL(test_mbox), NULL, NULL, NULL, NULL, POST_KERNEL,
3089 90, NULL);
3090 DEVICE_DT_DEFINE(DT_NODELABEL(test_mbox_zero_cell), NULL, NULL, NULL, NULL,
3091 POST_KERNEL, 90, NULL);
3092
ZTEST(devicetree_api,test_mbox)3093 ZTEST(devicetree_api, test_mbox)
3094 {
3095 #undef DT_DRV_COMPAT
3096 #define DT_DRV_COMPAT vnd_adc_temp_sensor
3097
3098 const struct mbox_dt_spec channel_tx = MBOX_DT_SPEC_GET(TEST_TEMP, tx);
3099 const struct mbox_dt_spec channel_rx = MBOX_DT_SPEC_GET(TEST_TEMP, rx);
3100
3101 zassert_equal(channel_tx.channel_id, 1, "");
3102 zassert_equal(channel_rx.channel_id, 2, "");
3103
3104 zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, tx), 1, "");
3105 zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, rx), 2, "");
3106
3107 zassert_true(DT_SAME_NODE(DT_MBOX_CTLR_BY_NAME(TEST_TEMP, tx),
3108 DT_NODELABEL(test_mbox)), "");
3109 zassert_true(DT_SAME_NODE(DT_MBOX_CTLR_BY_NAME(TEST_TEMP, rx),
3110 DT_NODELABEL(test_mbox)), "");
3111
3112 zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, tx), 1, "");
3113 zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, rx), 2, "");
3114
3115 const struct mbox_dt_spec channel_zero = MBOX_DT_SPEC_GET(TEST_TEMP, zero);
3116
3117 zassert_equal(channel_zero.channel_id, 0, "");
3118
3119 zassert_equal(DT_MBOX_CHANNEL_BY_NAME(TEST_TEMP, zero), 0, "");
3120
3121 zassert_true(DT_SAME_NODE(DT_MBOX_CTLR_BY_NAME(TEST_TEMP, zero),
3122 DT_NODELABEL(test_mbox_zero_cell)), "");
3123 }
3124
ZTEST(devicetree_api,test_fixed_partitions)3125 ZTEST(devicetree_api, test_fixed_partitions)
3126 {
3127 /* Test finding fixed partitions by the 'label' property. */
3128 zassert_true(DT_HAS_FIXED_PARTITION_LABEL(test_partition_0));
3129 zassert_true(DT_HAS_FIXED_PARTITION_LABEL(test_partition_1));
3130 zassert_true(DT_HAS_FIXED_PARTITION_LABEL(test_partition_2));
3131
3132 zassert_true(DT_SAME_NODE(TEST_PARTITION_0,
3133 DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_0)));
3134 zassert_true(DT_SAME_NODE(TEST_PARTITION_1,
3135 DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_1)));
3136 zassert_true(DT_SAME_NODE(TEST_PARTITION_2,
3137 DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_2)));
3138
3139 zassert_true(DT_FIXED_PARTITION_EXISTS(TEST_PARTITION_0));
3140 zassert_true(DT_FIXED_PARTITION_EXISTS(TEST_PARTITION_1));
3141 zassert_true(DT_FIXED_PARTITION_EXISTS(TEST_PARTITION_2));
3142
3143 /* There should not be a node with `label = "test_partition_3"`. */
3144 zassert_false(DT_HAS_FIXED_PARTITION_LABEL(test_partition_3));
3145 zassert_false(DT_NODE_EXISTS(DT_NODE_BY_FIXED_PARTITION_LABEL(test_partition_3)));
3146
3147 /* There is a node with `label = "FOO"`, but it is not a fixed partition. */
3148 zassert_false(DT_HAS_FIXED_PARTITION_LABEL(FOO));
3149 zassert_false(DT_NODE_EXISTS(DT_NODE_BY_FIXED_PARTITION_LABEL(FOO)));
3150
3151 /* Test DT_MTD_FROM_FIXED_PARTITION. */
3152 zassert_true(DT_NODE_EXISTS(DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
3153 zassert_true(DT_NODE_EXISTS(DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_1)));
3154 zassert_true(DT_NODE_EXISTS(DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_2)));
3155
3156 zassert_true(DT_SAME_NODE(TEST_MTD_0, DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
3157 zassert_true(DT_SAME_NODE(TEST_MTD_0, DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_1)));
3158 zassert_true(DT_SAME_NODE(TEST_MTD_1, DT_MTD_FROM_FIXED_PARTITION(TEST_PARTITION_2)));
3159
3160 /* Test DT_MEM_FROM_FIXED_PARTITION. */
3161 zassert_true(DT_NODE_EXISTS(DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
3162 zassert_true(DT_NODE_EXISTS(DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_1)));
3163 zassert_false(DT_NODE_EXISTS(DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_2)));
3164
3165 zassert_true(DT_SAME_NODE(TEST_MEM_0, DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_0)));
3166 zassert_true(DT_SAME_NODE(TEST_MEM_0, DT_MEM_FROM_FIXED_PARTITION(TEST_PARTITION_1)));
3167
3168 /* Test DT_FIXED_PARTITION_ADDR. */
3169 zassert_equal(DT_FIXED_PARTITION_ADDR(TEST_PARTITION_0), 0x20000000);
3170 zassert_equal(DT_FIXED_PARTITION_ADDR(TEST_PARTITION_1), 0x200000c0);
3171
3172 /* DT_FIXED_PARTITION_ADDR(TEST_PARTITION_2) expands to an invalid expression.
3173 * Test this by way of string comparison.
3174 */
3175 zassert_true(!strcmp(TO_STRING(DT_FIXED_PARTITION_ADDR(TEST_PARTITION_2)),
3176 "(__REG_IDX_0_VAL_ADDRESSU + 458624U)"));
3177 zassert_equal(DT_REG_ADDR(TEST_PARTITION_2), 458624);
3178
3179 /* Test that all DT_FIXED_PARTITION_ID are defined and unique. */
3180 #define FIXED_PARTITION_ID_COMMA(node_id) DT_FIXED_PARTITION_ID(node_id),
3181
3182 static const int ids[] = {
3183 DT_FOREACH_STATUS_OKAY_VARGS(fixed_partitions, DT_FOREACH_CHILD,
3184 FIXED_PARTITION_ID_COMMA)
3185 };
3186 bool found[ARRAY_SIZE(ids)] = { false };
3187
3188 for (int i = 0; i < ARRAY_SIZE(ids); i++) {
3189 zassert_between_inclusive(ids[i], 0, ARRAY_SIZE(ids) - 1, "");
3190 zassert_false(found[ids[i]]);
3191 found[ids[i]] = true;
3192 }
3193
3194 #undef FIXED_PARTITION_ID_COMMA
3195 }
3196
ZTEST(devicetree_api,test_string_token)3197 ZTEST(devicetree_api, test_string_token)
3198 {
3199 #undef DT_DRV_COMPAT
3200 #define DT_DRV_COMPAT vnd_string_token
3201 enum {
3202 token_zero,
3203 token_one,
3204 token_two,
3205 token_no_inst,
3206 };
3207 enum {
3208 TOKEN_ZERO = token_no_inst + 1,
3209 TOKEN_ONE,
3210 TOKEN_TWO,
3211 TOKEN_NO_INST,
3212 };
3213 int i;
3214
3215 /* Test DT_INST_STRING_TOKEN */
3216 #define STRING_TOKEN_TEST_INST_EXPANSION(inst) \
3217 DT_INST_STRING_TOKEN(inst, val),
3218 int array_inst[] = {
3219 DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_TEST_INST_EXPANSION)
3220 };
3221
3222 for (i = 0; i < ARRAY_SIZE(array_inst); i++) {
3223 zassert_between_inclusive(array_inst[i], token_zero, token_two, "");
3224 }
3225
3226 /* Test DT_INST_STRING_UPPER_TOKEN */
3227 #define STRING_UPPER_TOKEN_TEST_INST_EXPANSION(inst) \
3228 DT_INST_STRING_UPPER_TOKEN(inst, val),
3229 int array_inst_upper[] = {
3230 DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_TEST_INST_EXPANSION)
3231 };
3232
3233 for (i = 0; i < ARRAY_SIZE(array_inst_upper); i++) {
3234 zassert_between_inclusive(array_inst_upper[i], TOKEN_ZERO, TOKEN_TWO, "");
3235 }
3236
3237 /* Test DT_INST_STRING_TOKEN_OR when property is found */
3238 #define STRING_TOKEN_OR_TEST_INST_EXPANSION(inst) \
3239 DT_INST_STRING_TOKEN_OR(inst, val, token_no_inst),
3240 int array_inst_or[] = {
3241 DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_OR_TEST_INST_EXPANSION)
3242 };
3243
3244 for (i = 0; i < ARRAY_SIZE(array_inst_or); i++) {
3245 zassert_between_inclusive(array_inst_or[i], token_zero, token_two, "");
3246 }
3247
3248 /* Test DT_INST_STRING_UPPER_TOKEN_OR when property is found */
3249 #define STRING_UPPER_TOKEN_OR_TEST_INST_EXPANSION(inst) \
3250 DT_INST_STRING_UPPER_TOKEN_OR(inst, val, TOKEN_NO_INST),
3251 int array_inst_upper_or[] = {
3252 DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_OR_TEST_INST_EXPANSION)
3253 };
3254
3255 for (i = 0; i < ARRAY_SIZE(array_inst_upper_or); i++) {
3256 zassert_between_inclusive(array_inst_upper_or[i], TOKEN_ZERO, TOKEN_TWO, "");
3257 }
3258
3259 /* Test DT_STRING_TOKEN_OR when property is found */
3260 zassert_equal(DT_STRING_TOKEN_OR(DT_NODELABEL(test_string_token_0),
3261 val, token_one), token_zero, "");
3262 zassert_equal(DT_STRING_TOKEN_OR(DT_NODELABEL(test_string_token_1),
3263 val, token_two), token_one, "");
3264
3265 /* Test DT_STRING_TOKEN_OR is not found */
3266 zassert_equal(DT_STRING_TOKEN_OR(DT_NODELABEL(test_string_token_1),
3267 no_inst, token_zero), token_zero, "");
3268
3269 /* Test DT_STRING_UPPER_TOKEN_OR when property is found */
3270 zassert_equal(DT_STRING_UPPER_TOKEN_OR(DT_NODELABEL(test_string_token_0),
3271 val, TOKEN_ONE), TOKEN_ZERO, "");
3272 zassert_equal(DT_STRING_UPPER_TOKEN_OR(DT_NODELABEL(test_string_token_1),
3273 val, TOKEN_TWO), TOKEN_ONE, "");
3274
3275 /* Test DT_STRING_UPPER_TOKEN_OR is not found */
3276 zassert_equal(DT_STRING_UPPER_TOKEN_OR(DT_NODELABEL(test_string_token_1),
3277 no_inst, TOKEN_ZERO), TOKEN_ZERO, "");
3278
3279 /* Test DT_INST_STRING_TOKEN_OR when property is not found */
3280 #define STRING_TOKEN_TEST_NO_INST_EXPANSION(inst) \
3281 DT_INST_STRING_TOKEN_OR(inst, no_inst, token_no_inst),
3282 int array_no_inst_or[] = {
3283 DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_TEST_NO_INST_EXPANSION)
3284 };
3285 for (i = 0; i < ARRAY_SIZE(array_no_inst_or); i++) {
3286 zassert_equal(array_no_inst_or[i], token_no_inst, "");
3287 }
3288
3289 /* Test DT_INST_STRING_UPPER_TOKEN_OR when property is not found */
3290 #define STRING_UPPER_TOKEN_TEST_NO_INST_EXPANSION(inst) \
3291 DT_INST_STRING_UPPER_TOKEN_OR(inst, no_inst, TOKEN_NO_INST),
3292 int array_no_inst_upper_or[] = {
3293 DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_TEST_NO_INST_EXPANSION)
3294 };
3295 for (i = 0; i < ARRAY_SIZE(array_no_inst_upper_or); i++) {
3296 zassert_equal(array_no_inst_upper_or[i], TOKEN_NO_INST, "");
3297 }
3298 }
3299
3300 #undef DT_DRV_COMPAT
3301 #define DT_DRV_COMPAT vnd_string_array_token
ZTEST(devicetree_api,test_string_idx_token)3302 ZTEST(devicetree_api, test_string_idx_token)
3303 {
3304 enum token_string_idx {
3305 /* Tokens */
3306 token_first_idx_zero,
3307 token_first_idx_one,
3308 token_first_idx_two,
3309 token_second_idx_zero,
3310 token_second_idx_one,
3311 token_second_idx_two,
3312 token_second_idx_three,
3313 /* Upper tokens */
3314 TOKEN_FIRST_IDX_ZERO,
3315 TOKEN_FIRST_IDX_ONE,
3316 TOKEN_FIRST_IDX_TWO,
3317 TOKEN_SECOND_IDX_ZERO,
3318 TOKEN_SECOND_IDX_ONE,
3319 TOKEN_SECOND_IDX_TWO,
3320 TOKEN_SECOND_IDX_THREE
3321 };
3322
3323 /* Test direct idx access */
3324 zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
3325 token_first_idx_zero, "");
3326 zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
3327 token_first_idx_one, "");
3328 zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
3329 token_first_idx_two, "");
3330 zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
3331 token_second_idx_zero, "");
3332 zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
3333 token_second_idx_one, "");
3334 zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
3335 token_second_idx_two, "");
3336 zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
3337 token_second_idx_three, "");
3338
3339 zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
3340 TOKEN_FIRST_IDX_ZERO, "");
3341 zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
3342 TOKEN_FIRST_IDX_ONE, "");
3343 zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
3344 TOKEN_FIRST_IDX_TWO, "");
3345 zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
3346 TOKEN_SECOND_IDX_ZERO, "");
3347 zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
3348 TOKEN_SECOND_IDX_ONE, "");
3349 zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
3350 TOKEN_SECOND_IDX_TWO, "");
3351 zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
3352 TOKEN_SECOND_IDX_THREE, "");
3353
3354 /* Test instances */
3355 #define STRING_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_token_, node_id)
3356 #define STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
3357 enum token_string_idx STRING_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
3358 DT_INST_STRING_TOKEN_BY_IDX(inst, val, 0), \
3359 DT_INST_STRING_TOKEN_BY_IDX(inst, val, 1), \
3360 DT_INST_STRING_TOKEN_BY_IDX(inst, val, 2) \
3361 };
3362 DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION);
3363
3364 zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
3365 token_first_idx_zero, "");
3366 zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
3367 token_first_idx_one, "");
3368 zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
3369 token_first_idx_two, "");
3370 zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
3371 token_second_idx_zero, "");
3372 zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
3373 token_second_idx_one, "");
3374 zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
3375 token_second_idx_two, "");
3376
3377 #define STRING_UPPER_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_upper_token, node_id)
3378 #define STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
3379 enum token_string_idx STRING_UPPER_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
3380 DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 0), \
3381 DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 1), \
3382 DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 2) \
3383 };
3384 DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION);
3385
3386 zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
3387 TOKEN_FIRST_IDX_ZERO, "");
3388 zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
3389 TOKEN_FIRST_IDX_ONE, "");
3390 zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
3391 TOKEN_FIRST_IDX_TWO, "");
3392 zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
3393 TOKEN_SECOND_IDX_ZERO, "");
3394 zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
3395 TOKEN_SECOND_IDX_ONE, "");
3396 zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
3397 TOKEN_SECOND_IDX_TWO, "");
3398 }
3399
3400 #undef DT_DRV_COMPAT
3401 #define DT_DRV_COMPAT vnd_string_unquoted
ZTEST(devicetree_api,test_string_unquoted)3402 ZTEST(devicetree_api, test_string_unquoted)
3403 {
3404 #define XA 12.0
3405 #define XB 34.0
3406 #define XPLUS +
3407 #define XSTR1 "one"
3408 #define XSTR2 "two"
3409 const double f0_expected = 0.1234;
3410 const double f1_expected = 0.9e-3;
3411 const double delta = 0.1e-4;
3412
3413 /* Test DT_STRING_UNQUOTED */
3414 zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_f0), val),
3415 f0_expected, delta, "");
3416 zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_f1), val),
3417 f1_expected, delta, "");
3418 zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_t), val),
3419 XA XPLUS XB, delta, "");
3420 zassert_within(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_esc_t), val), XA XPLUS XB,
3421 delta, "");
3422 zassert_str_equal(DT_STRING_UNQUOTED(DT_NODELABEL(test_str_unquoted_esc_s), val),
3423 "one plus two");
3424 /* Test DT_STRING_UNQUOTED_OR */
3425 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f0), val, (0.0)),
3426 f0_expected, delta, "");
3427 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f1), val, (0.0)),
3428 f1_expected, delta, "");
3429 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_t), val, (0.0)),
3430 XA XPLUS XB, delta, "");
3431 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_t), val, (0.0)),
3432 XA XPLUS XB, delta, "");
3433 zassert_str_equal(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_s), val, "nak"),
3434 "one plus two");
3435 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f0), nak, (0.0)),
3436 0.0, delta, "");
3437 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_f1), nak, (0.0)),
3438 0.0, delta, "");
3439 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_t), nak, (0.0)),
3440 0.0, delta, "");
3441 zassert_within(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_t), nak, (0.0)),
3442 0.0, delta, "");
3443 zassert_str_equal(DT_STRING_UNQUOTED_OR(DT_NODELABEL(test_str_unquoted_esc_s), nak, "nak"),
3444 "nak");
3445 /* Test DT_INST_STRING_UNQUOTED */
3446 #define STRING_UNQUOTED_VAR(node_id) _CONCAT(var_, node_id)
3447 #define STRING_UNQUOTED_TEST_INST_EXPANSION(inst) \
3448 double STRING_UNQUOTED_VAR(DT_DRV_INST(inst)) = DT_INST_STRING_UNQUOTED(inst, val);
3449 DT_INST_FOREACH_STATUS_OKAY(STRING_UNQUOTED_TEST_INST_EXPANSION);
3450
3451 zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_f0)),
3452 f0_expected, delta, "");
3453 zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_f1)),
3454 f1_expected, delta, "");
3455 zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_t)), XA XPLUS XB,
3456 delta, "");
3457 zassert_within(STRING_UNQUOTED_VAR(DT_NODELABEL(test_str_unquoted_esc_t)), XA XPLUS XB,
3458 delta, "");
3459
3460 /* Test DT_INST_STRING_UNQUOTED_OR */
3461 #define STRING_UNQUOTED_OR_VAR(node_id) _CONCAT(var_or_, node_id)
3462 #define STRING_UNQUOTED_OR_TEST_INST_EXPANSION(inst) \
3463 double STRING_UNQUOTED_OR_VAR(DT_DRV_INST(inst))[2] = { \
3464 DT_INST_STRING_UNQUOTED_OR(inst, val, (1.0e10)), \
3465 DT_INST_STRING_UNQUOTED_OR(inst, dummy, (1.0e10)) \
3466 };
3467 DT_INST_FOREACH_STATUS_OKAY(STRING_UNQUOTED_OR_TEST_INST_EXPANSION);
3468
3469 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f0))[0],
3470 f0_expected, delta, "");
3471 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f1))[0],
3472 f1_expected, delta, "");
3473 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_t))[0],
3474 XA XPLUS XB, delta, "");
3475 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_esc_t))[0],
3476 XA XPLUS XB, delta, "");
3477 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f0))[1],
3478 1.0e10, delta, "");
3479 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_f1))[1],
3480 1.0e10, delta, "");
3481 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_t))[1],
3482 1.0e10, delta, "");
3483 zassert_within(STRING_UNQUOTED_OR_VAR(DT_NODELABEL(test_str_unquoted_esc_t))[1], 1.0e10,
3484 delta, "");
3485 #undef XA
3486 #undef XB
3487 #undef XPLUS
3488 #undef XSTR1
3489 #undef XSTR2
3490 }
3491
3492 #undef DT_DRV_COMPAT
3493 #define DT_DRV_COMPAT vnd_string_array_unquoted
ZTEST(devicetree_api,test_string_idx_unquoted)3494 ZTEST(devicetree_api, test_string_idx_unquoted)
3495 {
3496 #define XA 12.0
3497 #define XB 34.0
3498 #define XC 56.0
3499 #define XD 78.0
3500 #define XPLUS +
3501 #define XMINUS -
3502 #define XSTR1 "one"
3503 #define XSTR2 "two"
3504 const double delta = 0.1e-4;
3505
3506 /* DT_STRING_UNQUOTED_BY_IDX */
3507 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 0),
3508 1.0e2, delta, "");
3509 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 1),
3510 2.0e2, delta, "");
3511 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 2),
3512 3.0e2, delta, "");
3513 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f0), val, 3),
3514 4.0e2, delta, "");
3515
3516 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 0),
3517 0.01, delta, "");
3518 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 1),
3519 0.1, delta, "");
3520 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 2),
3521 1.0, delta, "");
3522 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_f1), val, 3),
3523 10.0, delta, "");
3524
3525 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 0),
3526 XA XPLUS XB, delta, "");
3527 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 1),
3528 XC XPLUS XD, delta, "");
3529 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 2),
3530 XA XMINUS XB, delta, "");
3531 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_t), val, 3),
3532 XC XMINUS XD, delta, "");
3533
3534 zassert_within(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_esc), val, 0),
3535 XA XPLUS XB, delta, "");
3536 zassert_str_equal(DT_STRING_UNQUOTED_BY_IDX(DT_NODELABEL(test_stra_unquoted_esc), val, 1),
3537 "one plus two");
3538
3539 #define STRING_UNQUOTED_BY_IDX_VAR(node_id) _CONCAT(var_, node_id)
3540 #define STRING_UNQUOTED_BY_IDX_TEST_INST_EXPANSION(inst) \
3541 double STRING_UNQUOTED_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
3542 DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 0), \
3543 DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 1), \
3544 DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 2), \
3545 DT_INST_STRING_UNQUOTED_BY_IDX(inst, val, 3), \
3546 };
3547 DT_INST_FOREACH_STATUS_OKAY(STRING_UNQUOTED_BY_IDX_TEST_INST_EXPANSION);
3548
3549 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[0],
3550 1.0e2, delta, "");
3551 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[1],
3552 2.0e2, delta, "");
3553 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[2],
3554 3.0e2, delta, "");
3555 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f0))[3],
3556 4.0e2, delta, "");
3557
3558 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[0],
3559 0.01, delta, "");
3560 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[1],
3561 0.1, delta, "");
3562 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[2],
3563 1.0, delta, "");
3564 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_f1))[3],
3565 10.0, delta, "");
3566
3567 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[0],
3568 XA XPLUS XB, delta, "");
3569 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[1],
3570 XC XPLUS XD, delta, "");
3571 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[2],
3572 XA XMINUS XB, delta, "");
3573 zassert_within(STRING_UNQUOTED_BY_IDX_VAR(DT_NODELABEL(test_stra_unquoted_t))[3],
3574 XC XMINUS XD, delta, "");
3575 #undef XA
3576 #undef XB
3577 #undef XC
3578 #undef XD
3579 #undef XPLUS
3580 #undef XMINUS
3581 #undef XSTR1
3582 #undef XSTR2
3583 }
3584
3585 #undef DT_DRV_COMPAT
ZTEST(devicetree_api,test_string_escape)3586 ZTEST(devicetree_api, test_string_escape)
3587 {
3588 zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_0), val), "\a\b\f\n\r\t\v");
3589 zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_1), val), "\'single\' \"double\"");
3590 zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_2), val), "first\nsecond");
3591 zassert_str_equal(DT_PROP(DT_NODELABEL(test_str_escape_3), val), "\x01\x7F");
3592 }
3593
ZTEST(devicetree_api,test_string_array_escape)3594 ZTEST(devicetree_api, test_string_array_escape)
3595 {
3596 zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 0), "\a\b\f\n\r\t\v");
3597 zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 1),
3598 "\'single\' \"double\"");
3599 zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 2), "first\nsecond");
3600 zassert_str_equal(DT_PROP_BY_IDX(DT_NODELABEL(test_stra_escape), val, 3), "\x01\x7F");
3601 }
3602
3603 #undef DT_DRV_COMPAT
3604 #define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api,test_reset)3605 ZTEST(devicetree_api, test_reset)
3606 {
3607 /* DT_RESET_CTLR_BY_IDX */
3608 zassert_true(DT_SAME_NODE(DT_RESET_CTLR_BY_IDX(TEST_TEMP, 1),
3609 DT_NODELABEL(test_reset)), "");
3610
3611 /* DT_RESET_CTLR */
3612 zassert_true(DT_SAME_NODE(DT_RESET_CTLR(TEST_TEMP),
3613 DT_NODELABEL(test_reset)), "");
3614
3615 /* DT_RESET_CTLR_BY_NAME */
3616 zassert_true(DT_SAME_NODE(DT_RESET_CTLR_BY_NAME(TEST_TEMP, reset_b),
3617 DT_NODELABEL(test_reset)), "");
3618
3619 /* DT_RESET_CELL_BY_IDX */
3620 zassert_equal(DT_RESET_CELL_BY_IDX(TEST_TEMP, 1, id), 20, "");
3621 zassert_equal(DT_RESET_CELL_BY_IDX(TEST_TEMP, 0, id), 10, "");
3622
3623 /* DT_RESET_CELL_BY_NAME */
3624 zassert_equal(DT_RESET_CELL_BY_NAME(TEST_TEMP, reset_a, id), 10, "");
3625 zassert_equal(DT_RESET_CELL_BY_NAME(TEST_TEMP, reset_b, id), 20, "");
3626
3627 /* DT_RESET_CELL */
3628 zassert_equal(DT_RESET_CELL(TEST_TEMP, id), 10, "");
3629
3630 /* reg-width on reset */
3631 zassert_equal(DT_PROP_BY_PHANDLE_IDX(TEST_TEMP, resets, 1, reg_width), 4, "");
3632
3633 /* DT_INST */
3634 zassert_equal(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT), 1, "");
3635
3636 /* DT_INST_RESET_CTLR_BY_IDX */
3637 zassert_true(DT_SAME_NODE(DT_INST_RESET_CTLR_BY_IDX(0, 1),
3638 DT_NODELABEL(test_reset)), "");
3639
3640 /* DT_INST_RESET_CTLR */
3641 zassert_true(DT_SAME_NODE(DT_INST_RESET_CTLR(0),
3642 DT_NODELABEL(test_reset)), "");
3643
3644 /* DT_INST_RESET_CTLR_BY_NAME */
3645 zassert_true(DT_SAME_NODE(DT_INST_RESET_CTLR_BY_NAME(0, reset_b),
3646 DT_NODELABEL(test_reset)), "");
3647
3648 /* DT_INST_RESET_CELL_BY_IDX */
3649 zassert_equal(DT_INST_RESET_CELL_BY_IDX(0, 1, id), 20, "");
3650 zassert_equal(DT_INST_RESET_CELL_BY_IDX(0, 0, id), 10, "");
3651
3652 /* DT_INST_RESET_CELL_BY_NAME */
3653 zassert_equal(DT_INST_RESET_CELL_BY_NAME(0, reset_a, id), 10, "");
3654 zassert_equal(DT_INST_RESET_CELL_BY_NAME(0, reset_b, id), 20, "");
3655
3656 /* DT_INST_RESET_CELL */
3657 zassert_equal(DT_INST_RESET_CELL(0, id), 10, "");
3658
3659 /* reg-width on reset */
3660 zassert_equal(DT_INST_PROP_BY_PHANDLE_IDX(0, resets, 1, reg_width), 4, "");
3661
3662 /* DT_RESET_ID_BY_IDX */
3663 zassert_equal(DT_RESET_ID_BY_IDX(TEST_TEMP, 0), 10, "");
3664 zassert_equal(DT_RESET_ID_BY_IDX(TEST_TEMP, 1), 20, "");
3665
3666 /* DT_RESET_ID */
3667 zassert_equal(DT_RESET_ID(TEST_TEMP), 10, "");
3668
3669 /* DT_INST_RESET_ID_BY_IDX */
3670 zassert_equal(DT_INST_RESET_ID_BY_IDX(0, 0), 10, "");
3671 zassert_equal(DT_INST_RESET_ID_BY_IDX(0, 1), 20, "");
3672
3673 /* DT_INST_RESET_ID */
3674 zassert_equal(DT_INST_RESET_ID(0), 10, "");
3675 }
3676
3677 #undef DT_DRV_COMPAT
3678 #define DT_DRV_COMPAT vnd_interrupt_holder_extended
ZTEST(devicetree_api,test_interrupt_controller)3679 ZTEST(devicetree_api, test_interrupt_controller)
3680 {
3681 /* DT_IRQ_INTC_BY_IDX */
3682 zassert_true(DT_SAME_NODE(DT_IRQ_INTC_BY_IDX(TEST_IRQ_EXT, 0), TEST_INTC), "");
3683 zassert_true(DT_SAME_NODE(DT_IRQ_INTC_BY_IDX(TEST_IRQ_EXT, 1), TEST_GPIO_4), "");
3684
3685 /* DT_IRQ_INTC_BY_NAME */
3686 zassert_true(DT_SAME_NODE(DT_IRQ_INTC_BY_NAME(TEST_IRQ_EXT, int1), TEST_INTC), "");
3687 zassert_true(DT_SAME_NODE(DT_IRQ_INTC_BY_NAME(TEST_IRQ_EXT, int2), TEST_GPIO_4), "");
3688
3689 /* DT_IRQ_INTC */
3690 zassert_true(DT_SAME_NODE(DT_IRQ_INTC(TEST_IRQ_EXT), TEST_INTC), "");
3691
3692 /* DT_INST_IRQ_INTC_BY_IDX */
3693 zassert_true(DT_SAME_NODE(DT_INST_IRQ_INTC_BY_IDX(0, 0), TEST_INTC), "");
3694 zassert_true(DT_SAME_NODE(DT_INST_IRQ_INTC_BY_IDX(0, 1), TEST_GPIO_4), "");
3695
3696 /* DT_INST_IRQ_INTC_BY_NAME */
3697 zassert_true(DT_SAME_NODE(DT_INST_IRQ_INTC_BY_NAME(0, int1), TEST_INTC), "");
3698 zassert_true(DT_SAME_NODE(DT_INST_IRQ_INTC_BY_NAME(0, int2), TEST_GPIO_4), "");
3699
3700 /* DT_INST_IRQ_INTC */
3701 zassert_true(DT_SAME_NODE(DT_INST_IRQ_INTC(0), TEST_INTC), "");
3702 }
3703
3704 ZTEST_SUITE(devicetree_api, NULL, NULL, NULL, NULL, NULL);
3705