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