-
Notifications
You must be signed in to change notification settings - Fork 580
/
devicetree.h
4399 lines (4163 loc) · 141 KB
/
devicetree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) 2020 Nordic Semiconductor
* Copyright (c) 2020, Linaro Ltd.
*
* Not a generated file. Feel free to modify.
*/
/**
* @file
* @brief Devicetree main header
*
* API for accessing the current application's devicetree macros.
*/
#ifndef DEVICETREE_H
#define DEVICETREE_H
#include <devicetree_generated.h>
#include <zephyr/irq_multilevel.h>
#if !defined(_LINKER) && !defined(_ASMLANGUAGE)
#include <stdint.h>
#endif
#include <zephyr/sys/util.h>
/**
* @brief devicetree.h API
* @defgroup devicetree Devicetree
* @{
* @}
*/
/*
* Property suffixes
* -----------------
*
* These are the optional parts that come after the _P_<property>
* part in DT_N_<path-id>_P_<property-id> macros, or the "prop-suf"
* nonterminal in the DT guide's macros.bnf file.
*
* Before adding new ones, check this list to avoid conflicts. If any
* are missing from this list, please add them. It should be complete.
*
* _ENUM_IDX: property's value as an index into bindings enum
* _ENUM_VAL_<val>_EXISTS property's value as a token exists
* _ENUM_TOKEN: property's value as a token into bindings enum (string
* enum values are identifiers) [deprecated, use _STRING_TOKEN]
* _ENUM_UPPER_TOKEN: like _ENUM_TOKEN, but uppercased [deprecated, use
* _STRING_UPPER_TOKEN]
* _EXISTS: property is defined
* _FOREACH_PROP_ELEM: helper for "iterating" over values in the property
* _FOREACH_PROP_ELEM_VARGS: foreach functions with variable number of arguments
* _IDX_<i>: logical index into property
* _IDX_<i>_EXISTS: logical index into property is defined
* _IDX_<i>_PH: phandle array's phandle by index (or phandle, phandles)
* _IDX_<i>_STRING_TOKEN: string array element value as a token
* _IDX_<i>_STRING_UPPER_TOKEN: string array element value as a uppercased token
* _IDX <i>_STRING_UNQUOTED: string array element value as a sequence of tokens, with no quotes
* _IDX_<i>_VAL_<val>: phandle array's specifier value by index
* _IDX_<i>_VAL_<val>_EXISTS: cell value exists, by index
* _LEN: property logical length
* _NAME_<name>_PH: phandle array's phandle by name
* _NAME_<name>_VAL_<val>: phandle array's property specifier by name
* _NAME_<name>_VAL_<val>_EXISTS: cell value exists, by name
* _STRING_TOKEN: string property's value as a token
* _STRING_UPPER_TOKEN: like _STRING_TOKEN, but uppercased
* _STRING_UNQUOTED: string property's value as a sequence of tokens, with no quotes
*/
/**
* @defgroup devicetree-generic-id Node identifiers and helpers
* @ingroup devicetree
* @{
*/
/**
* @brief Name for an invalid node identifier
*
* This supports cases where factored macros can be invoked from paths where
* devicetree data may or may not be available. It is a preprocessor identifier
* that does not match any valid devicetree node identifier.
*/
#define DT_INVALID_NODE _
/**
* @brief Node identifier for the root node in the devicetree
*/
#define DT_ROOT DT_N
/**
* @brief Get a node identifier for a devicetree path
*
* @note This macro returns a node identifier from path components. To get
* a path string from a node identifier, use DT_NODE_PATH() instead.
*
* The arguments to this macro are the names of non-root nodes in the
* tree required to reach the desired node, starting from the root.
* Non-alphanumeric characters in each name must be converted to
* underscores to form valid C tokens, and letters must be lowercased.
*
* Example devicetree fragment:
*
* @code{.dts}
* / {
* soc {
* serial1: serial@40001000 {
* status = "okay";
* current-speed = <115200>;
* ...
* };
* };
* };
* @endcode
*
* You can use `DT_PATH(soc, serial_40001000)` to get a node identifier
* for the `serial@40001000` node. Node labels like `serial1` cannot be
* used as DT_PATH() arguments; use DT_NODELABEL() for those instead.
*
* Example usage with DT_PROP() to get the `current-speed` property:
*
* @code{.c}
* DT_PROP(DT_PATH(soc, serial_40001000), current_speed) // 115200
* @endcode
*
* (The `current-speed` property is also in `lowercase-and-underscores`
* form when used with this API.)
*
* When determining arguments to DT_PATH():
*
* - the first argument corresponds to a child node of the root (`soc` above)
* - a second argument corresponds to a child of the first argument
* (`serial_40001000` above, from the node name `serial@40001000`
* after lowercasing and changing `@` to `_`)
* - and so on for deeper nodes in the desired node's path
*
* @param ... lowercase-and-underscores node names along the node's path,
* with each name given as a separate argument
* @return node identifier for the node with that path
*/
#define DT_PATH(...) DT_PATH_INTERNAL(__VA_ARGS__)
/**
* @brief Get a node identifier for a node label
*
* Convert non-alphanumeric characters in the node label to
* underscores to form valid C tokens, and lowercase all letters. Note
* that node labels are not the same thing as label properties.
*
* Example devicetree fragment:
*
* @code{.dts}
* serial1: serial@40001000 {
* label = "UART_0";
* status = "okay";
* current-speed = <115200>;
* ...
* };
* @endcode
*
* The only node label in this example is `serial1`.
*
* The string `UART_0` is *not* a node label; it's the value of a
* property named label.
*
* You can use `DT_NODELABEL(serial1)` to get a node identifier for the
* `serial@40001000` node. Example usage with DT_PROP() to get the
* current-speed property:
*
* @code{.c}
* DT_PROP(DT_NODELABEL(serial1), current_speed) // 115200
* @endcode
*
* Another example devicetree fragment:
*
* @code{.dts}
* cpu@0 {
* L2_0: l2-cache {
* cache-level = <2>;
* ...
* };
* };
* @endcode
*
* Example usage to get the cache-level property:
*
* @code{.c}
* DT_PROP(DT_NODELABEL(l2_0), cache_level) // 2
* @endcode
*
* Notice how `L2_0` in the devicetree is lowercased to `l2_0` in the
* DT_NODELABEL() argument.
*
* @param label lowercase-and-underscores node label name
* @return node identifier for the node with that label
*/
#define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label)
/**
* @brief Get a node identifier from /aliases
*
* This macro's argument is a property of the `/aliases` node. It
* returns a node identifier for the node which is aliased. Convert
* non-alphanumeric characters in the alias property to underscores to
* form valid C tokens, and lowercase all letters.
*
* Example devicetree fragment:
*
* @code{.dts}
* / {
* aliases {
* my-serial = &serial1;
* };
*
* soc {
* serial1: serial@40001000 {
* status = "okay";
* current-speed = <115200>;
* ...
* };
* };
* };
* @endcode
*
* You can use DT_ALIAS(my_serial) to get a node identifier for the
* `serial@40001000` node. Notice how `my-serial` in the devicetree
* becomes `my_serial` in the DT_ALIAS() argument. Example usage with
* DT_PROP() to get the current-speed property:
*
* @code{.c}
* DT_PROP(DT_ALIAS(my_serial), current_speed) // 115200
* @endcode
*
* @param alias lowercase-and-underscores alias name.
* @return node identifier for the node with that alias
*/
#define DT_ALIAS(alias) DT_CAT(DT_N_ALIAS_, alias)
/**
* @brief Get a node identifier for an instance of a compatible
*
* All nodes with a particular compatible property value are assigned
* instance numbers, which are zero-based indexes specific to that
* compatible. You can get a node identifier for these nodes by
* passing DT_INST() an instance number, @p inst, along with the
* lowercase-and-underscores version of the compatible, @p compat.
*
* Instance numbers have the following properties:
*
* - for each compatible, instance numbers start at 0 and are contiguous
* - exactly one instance number is assigned for each node with a compatible,
* **including disabled nodes**
* - enabled nodes (status property is `okay` or missing) are assigned the
* instance numbers starting from 0, and disabled nodes have instance
* numbers which are greater than those of any enabled node
*
* No other guarantees are made. In particular:
*
* - instance numbers **in no way reflect** any numbering scheme that
* might exist in SoC documentation, node labels or unit addresses,
* or properties of the /aliases node (use DT_NODELABEL() or DT_ALIAS()
* for those)
* - there **is no general guarantee** that the same node will have
* the same instance number between builds, even if you are building
* the same application again in the same build directory
*
* Example devicetree fragment:
*
* @code{.dts}
* serial1: serial@40001000 {
* compatible = "vnd,soc-serial";
* status = "disabled";
* current-speed = <9600>;
* ...
* };
*
* serial2: serial@40002000 {
* compatible = "vnd,soc-serial";
* status = "okay";
* current-speed = <57600>;
* ...
* };
*
* serial3: serial@40003000 {
* compatible = "vnd,soc-serial";
* current-speed = <115200>;
* ...
* };
* @endcode
*
* Assuming no other nodes in the devicetree have compatible
* `"vnd,soc-serial"`, that compatible has nodes with instance numbers
* 0, 1, and 2.
*
* The nodes `serial@40002000` and `serial@40003000` are both enabled, so
* their instance numbers are 0 and 1, but no guarantees are made
* regarding which node has which instance number.
*
* Since `serial@40001000` is the only disabled node, it has instance
* number 2, since disabled nodes are assigned the largest instance
* numbers. Therefore:
*
* @code{.c}
* // Could be 57600 or 115200. There is no way to be sure:
* // either serial@40002000 or serial@40003000 could
* // have instance number 0, so this could be the current-speed
* // property of either of those nodes.
* DT_PROP(DT_INST(0, vnd_soc_serial), current_speed)
*
* // Could be 57600 or 115200, for the same reason.
* // If the above expression expands to 57600, then
* // this expands to 115200, and vice-versa.
* DT_PROP(DT_INST(1, vnd_soc_serial), current_speed)
*
* // 9600, because there is only one disabled node, and
* // disabled nodes are "at the the end" of the instance
* // number "list".
* DT_PROP(DT_INST(2, vnd_soc_serial), current_speed)
* @endcode
*
* Notice how `"vnd,soc-serial"` in the devicetree becomes `vnd_soc_serial`
* (without quotes) in the DT_INST() arguments. (As usual, `current-speed`
* in the devicetree becomes `current_speed` as well.)
*
* Nodes whose `compatible` property has multiple values are assigned
* independent instance numbers for each compatible.
*
* @param inst instance number for compatible @p compat
* @param compat lowercase-and-underscores compatible, without quotes
* @return node identifier for the node with that instance number and
* compatible
*/
#define DT_INST(inst, compat) UTIL_CAT(DT_N_INST, DT_DASH(inst, compat))
/**
* @brief Get a node identifier for a parent node
*
* Example devicetree fragment:
*
* @code{.dts}
* parent: parent-node {
* child: child-node {
* ...
* };
* };
* @endcode
*
* The following are equivalent ways to get the same node identifier:
*
* @code{.c}
* DT_NODELABEL(parent)
* DT_PARENT(DT_NODELABEL(child))
* @endcode
*
* @param node_id node identifier
* @return a node identifier for the node's parent
*/
#define DT_PARENT(node_id) UTIL_CAT(node_id, _PARENT)
/**
* @brief Get a node identifier for a grandparent node
*
* Example devicetree fragment:
*
* @code{.dts}
* gparent: grandparent-node {
* parent: parent-node {
* child: child-node { ... }
* };
* };
* @endcode
*
* The following are equivalent ways to get the same node identifier:
*
* @code{.c}
* DT_GPARENT(DT_NODELABEL(child))
* DT_PARENT(DT_PARENT(DT_NODELABEL(child))
* @endcode
*
* @param node_id node identifier
* @return a node identifier for the node's parent's parent
*/
#define DT_GPARENT(node_id) DT_PARENT(DT_PARENT(node_id))
/**
* @brief Get a node identifier for a child node
*
* Example devicetree fragment:
*
* @code{.dts}
* / {
* soc-label: soc {
* serial1: serial@40001000 {
* status = "okay";
* current-speed = <115200>;
* ...
* };
* };
* };
* @endcode
*
* Example usage with DT_PROP() to get the status of the
* `serial@40001000` node:
*
* @code{.c}
* #define SOC_NODE DT_NODELABEL(soc_label)
* DT_PROP(DT_CHILD(SOC_NODE, serial_40001000), status) // "okay"
* @endcode
*
* Node labels like `serial1` cannot be used as the @p child argument
* to this macro. Use DT_NODELABEL() for that instead.
*
* You can also use DT_FOREACH_CHILD() to iterate over node
* identifiers for all of a node's children.
*
* @param node_id node identifier
* @param child lowercase-and-underscores child node name
* @return node identifier for the node with the name referred to by 'child'
*/
#define DT_CHILD(node_id, child) UTIL_CAT(node_id, DT_S_PREFIX(child))
/**
* @brief Get a node identifier for a status `okay` node with a compatible
*
* Use this if you want to get an arbitrary enabled node with a given
* compatible, and you do not care which one you get. If any enabled
* nodes with the given compatible exist, a node identifier for one
* of them is returned. Otherwise, @ref DT_INVALID_NODE is returned.
*
* Example devicetree fragment:
*
* @code{.dts}
* node-a {
* compatible = "vnd,device";
* status = "okay";
* };
*
* node-b {
* compatible = "vnd,device";
* status = "okay";
* };
*
* node-c {
* compatible = "vnd,device";
* status = "disabled";
* };
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_COMPAT_GET_ANY_STATUS_OKAY(vnd_device)
* @endcode
*
* This expands to a node identifier for either `node-a` or `node-b`.
* It will not expand to a node identifier for `node-c`, because that
* node does not have status `okay`.
*
* @param compat lowercase-and-underscores compatible, without quotes
* @return node identifier for a node with that compatible, or
* @ref DT_INVALID_NODE
*/
#define DT_COMPAT_GET_ANY_STATUS_OKAY(compat) \
COND_CODE_1(DT_HAS_COMPAT_STATUS_OKAY(compat), \
(DT_INST(0, compat)), \
(DT_INVALID_NODE))
/**
* @brief Get a devicetree node's full path as a string literal
*
* This returns the path to a node from a node identifier. To get a
* node identifier from path components instead, use DT_PATH().
*
* Example devicetree fragment:
*
* @code{.dts}
* / {
* soc {
* node: my-node@12345678 { ... };
* };
* };
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_NODE_PATH(DT_NODELABEL(node)) // "/soc/my-node@12345678"
* DT_NODE_PATH(DT_PATH(soc)) // "/soc"
* DT_NODE_PATH(DT_ROOT) // "/"
* @endcode
*
* @param node_id node identifier
* @return the node's full path in the devicetree
*/
#define DT_NODE_PATH(node_id) DT_CAT(node_id, _PATH)
/**
* @brief Get a devicetree node's name with unit-address as a string literal
*
* This returns the node name and unit-address from a node identifier.
*
* Example devicetree fragment:
*
* @code{.dts}
* / {
* soc {
* node: my-node@12345678 { ... };
* };
* };
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_NODE_FULL_NAME(DT_NODELABEL(node)) // "my-node@12345678"
* @endcode
*
* @param node_id node identifier
* @return the node's name with unit-address as a string in the devicetree
*/
#define DT_NODE_FULL_NAME(node_id) DT_CAT(node_id, _FULL_NAME)
/**
* @brief Get a devicetree node's index into its parent's list of children
*
* Indexes are zero-based.
*
* It is an error to use this macro with the root node.
*
* Example devicetree fragment:
*
* @code{.dts}
* parent {
* c1: child-1 {};
* c2: child-2 {};
* };
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_NODE_CHILD_IDX(DT_NODELABEL(c1)) // 0
* DT_NODE_CHILD_IDX(DT_NODELABEL(c2)) // 1
* @endcode
*
* @param node_id node identifier
* @return the node's index in its parent node's list of children
*/
#define DT_NODE_CHILD_IDX(node_id) DT_CAT(node_id, _CHILD_IDX)
/**
* @brief Do @p node_id1 and @p node_id2 refer to the same node?
*
* Both @p node_id1 and @p node_id2 must be node identifiers for nodes
* that exist in the devicetree (if unsure, you can check with
* DT_NODE_EXISTS()).
*
* The expansion evaluates to 0 or 1, but may not be a literal integer
* 0 or 1.
*
* @internal
* Implementation note: distinct nodes have distinct node identifiers.
* See include/zephyr/devicetree/ordinals.h.
* @endinternal
*
* @param node_id1 first node identifier
* @param node_id2 second node identifier
* @return an expression that evaluates to 1 if the node identifiers
* refer to the same node, and evaluates to 0 otherwise
*/
#define DT_SAME_NODE(node_id1, node_id2) \
(DT_DEP_ORD(node_id1) == (DT_DEP_ORD(node_id2)))
/**
* @}
*/
/**
* @defgroup devicetree-generic-prop Property accessors
* @ingroup devicetree
* @{
*/
/**
* @brief Get a devicetree property value
*
* For properties whose bindings have the following types, this macro
* expands to:
*
* - string: a string literal
* - boolean: `0` if the property is false, or `1` if it is true
* - int: the property's value as an integer literal
* - array, uint8-array, string-array: an initializer expression in braces,
* whose elements are integer or string literals (like `{0, 1, 2}`,
* `{"hello", "world"}`, etc.)
* - phandle: a node identifier for the node with that phandle
*
* A property's type is usually defined by its binding. In some
* special cases, it has an assumed type defined by the devicetree
* specification even when no binding is available: `compatible` has
* type string-array, `status` has type string, and
* `interrupt-controller` has type boolean.
*
* For other properties or properties with unknown type due to a
* missing binding, behavior is undefined.
*
* For usage examples, see DT_PATH(), DT_ALIAS(), DT_NODELABEL(),
* and DT_INST() above.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @return a representation of the property's value
*/
#define DT_PROP(node_id, prop) DT_CAT3(node_id, _P_, prop)
/**
* @brief Get a property's logical length
*
* Here, "length" is a number of elements, which may differ from the
* property's size in bytes.
*
* The return value depends on the property's type:
*
* - for types array, string-array, and uint8-array, this expands
* to the number of elements in the array
* - for type phandles, this expands to the number of phandles
* - for type phandle-array, this expands to the number of
* phandle and specifier blocks in the property
* - for type phandle, this expands to 1 (so that a phandle
* can be treated as a degenerate case of phandles with length 1)
* - for type string, this expands to 1 (so that a string can be
* treated as a degenerate case of string-array with length 1)
*
* These properties are handled as special cases:
*
* - reg property: use `DT_NUM_REGS(node_id)` instead
* - interrupts property: use `DT_NUM_IRQS(node_id)` instead
*
* It is an error to use this macro with the `ranges`, `dma-ranges`, `reg`
* or `interrupts` properties.
*
* For other properties, behavior is undefined.
*
* @param node_id node identifier
* @param prop a lowercase-and-underscores property with a logical length
* @return the property's length
*/
#define DT_PROP_LEN(node_id, prop) DT_CAT4(node_id, _P_, prop, _LEN)
/**
* @brief Like DT_PROP_LEN(), but with a fallback to @p default_value
*
* If the property is defined (as determined by DT_NODE_HAS_PROP()),
* this expands to DT_PROP_LEN(node_id, prop). The @p default_value
* parameter is not expanded in this case.
*
* Otherwise, this expands to @p default_value.
*
* @param node_id node identifier
* @param prop a lowercase-and-underscores property with a logical length
* @param default_value a fallback value to expand to
* @return the property's length or the given default value
*/
#define DT_PROP_LEN_OR(node_id, prop, default_value) \
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
(DT_PROP_LEN(node_id, prop)), (default_value))
/**
* @brief Is index @p idx valid for an array type property?
*
* If this returns 1, then DT_PROP_BY_IDX(node_id, prop, idx) or
* DT_PHA_BY_IDX(node_id, prop, idx, ...) are valid at index @p idx.
* If it returns 0, it is an error to use those macros with that index.
*
* These properties are handled as special cases:
*
* - `reg` property: use DT_REG_HAS_IDX(node_id, idx) instead
* - `interrupts` property: use DT_IRQ_HAS_IDX(node_id, idx) instead
*
* It is an error to use this macro with the `reg` or `interrupts` properties.
*
* @param node_id node identifier
* @param prop a lowercase-and-underscores property with a logical length
* @param idx index to check
* @return An expression which evaluates to 1 if @p idx is a valid index
* into the given property, and 0 otherwise.
*/
#define DT_PROP_HAS_IDX(node_id, prop, idx) \
IS_ENABLED(DT_CAT6(node_id, _P_, prop, _IDX_, idx, _EXISTS))
/**
* @brief Is name @p name available in a `foo-names` property?
*
* This property is handled as special case:
*
* - `interrupts` property: use DT_IRQ_HAS_NAME(node_id, idx) instead
*
* It is an error to use this macro with the `interrupts` property.
*
* Example devicetree fragment:
*
* @code{.dts}
* nx: node-x {
* foos = <&bar xx yy>, <&baz xx zz>;
* foo-names = "event", "error";
* status = "okay";
* };
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_PROP_HAS_NAME(DT_NODELABEL(nx), foos, event) // 1
* DT_PROP_HAS_NAME(DT_NODELABEL(nx), foos, failure) // 0
* @endcode
*
* @param node_id node identifier
* @param prop a lowercase-and-underscores `prop-names` type property
* @param name a lowercase-and-underscores name to check
* @return An expression which evaluates to 1 if "name" is an available
* name into the given property, and 0 otherwise.
*/
#define DT_PROP_HAS_NAME(node_id, prop, name) \
IS_ENABLED(DT_CAT6(node_id, _P_, prop, _NAME_, name, _EXISTS))
/**
* @brief Get the value at index @p idx in an array type property
*
* It might help to read the argument order as being similar to
* `node->property[index]`.
*
* The return value depends on the property's type:
*
* - for types array, string-array, uint8-array, and phandles,
* this expands to the idx-th array element as an
* integer, string literal, integer, and node identifier
* respectively
*
* - for type phandle, idx must be 0 and the expansion is a node
* identifier (this treats phandle like a phandles of length 1)
*
* - for type string, idx must be 0 and the expansion is the the
* entire string (this treats string like string-array of length 1)
*
* These properties are handled as special cases:
*
* - `reg`: use DT_REG_ADDR_BY_IDX() or DT_REG_SIZE_BY_IDX() instead
* - `interrupts`: use DT_IRQ_BY_IDX()
* - `ranges`: use DT_NUM_RANGES()
* - `dma-ranges`: it is an error to use this property with
* DT_PROP_BY_IDX()
*
* For properties of other types, behavior is undefined.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param idx the index to get
* @return a representation of the idx-th element of the property
*/
#define DT_PROP_BY_IDX(node_id, prop, idx) \
DT_CAT5(node_id, _P_, prop, _IDX_, idx)
/**
* @brief Like DT_PROP(), but with a fallback to @p default_value
*
* If the value exists, this expands to DT_PROP(node_id, prop).
* The @p default_value parameter is not expanded in this case.
*
* Otherwise, this expands to @p default_value.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param default_value a fallback value to expand to
* @return the property's value or @p default_value
*/
#define DT_PROP_OR(node_id, prop, default_value) \
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
(DT_PROP(node_id, prop)), (default_value))
/**
* @deprecated Use DT_PROP(node_id, label)
* @brief Equivalent to DT_PROP(node_id, label)
*
* This is a convenience for the Zephyr device API, which uses label
* properties as device_get_binding() arguments.
* @param node_id node identifier
* @return node's label property value
*/
#define DT_LABEL(node_id) DT_PROP(node_id, label) __DEPRECATED_MACRO
/**
* @brief Get a property value's index into its enumeration values
*
* The return values start at zero.
*
* Example devicetree fragment:
*
* @code{.dts}
* usb1: usb@12340000 {
* maximum-speed = "full-speed";
* };
* usb2: usb@12341000 {
* maximum-speed = "super-speed";
* };
* @endcode
*
* Example bindings fragment:
*
* @code{.yaml}
* properties:
* maximum-speed:
* type: string
* enum:
* - "low-speed"
* - "full-speed"
* - "high-speed"
* - "super-speed"
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_ENUM_IDX(DT_NODELABEL(usb1), maximum_speed) // 1
* DT_ENUM_IDX(DT_NODELABEL(usb2), maximum_speed) // 3
* @endcode
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @return zero-based index of the property's value in its enum: list
*/
#define DT_ENUM_IDX(node_id, prop) DT_CAT4(node_id, _P_, prop, _ENUM_IDX)
/**
* @brief Like DT_ENUM_IDX(), but with a fallback to a default enum index
*
* If the value exists, this expands to its zero based index value thanks to
* DT_ENUM_IDX(node_id, prop).
*
* Otherwise, this expands to provided default index enum value.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param default_idx_value a fallback index value to expand to
* @return zero-based index of the property's value in its enum if present,
* default_idx_value otherwise
*/
#define DT_ENUM_IDX_OR(node_id, prop, default_idx_value) \
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
(DT_ENUM_IDX(node_id, prop)), (default_idx_value))
/**
* @brief Does a node enumeration property have a given value?
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param value lowercase-and-underscores enumeration value
* @return 1 if the node property has the value @a value, 0 otherwise.
*/
#define DT_ENUM_HAS_VALUE(node_id, prop, value) \
IS_ENABLED(DT_CAT6(node_id, _P_, prop, _ENUM_VAL_, value, _EXISTS))
/**
* @brief Get a string property's value as a token.
*
* This removes "the quotes" from a string property's value,
* converting any non-alphanumeric characters to underscores. This can
* be useful, for example, when programmatically using the value to
* form a C variable or code.
*
* DT_STRING_TOKEN() can only be used for properties with string type.
*
* It is an error to use DT_STRING_TOKEN() in other circumstances.
*
* Example devicetree fragment:
*
* @code{.dts}
* n1: node-1 {
* prop = "foo";
* };
* n2: node-2 {
* prop = "FOO";
* }
* n3: node-3 {
* prop = "123 foo";
* };
* @endcode
*
* Example bindings fragment:
*
* @code{.yaml}
* properties:
* prop:
* type: string
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_STRING_TOKEN(DT_NODELABEL(n1), prop) // foo
* DT_STRING_TOKEN(DT_NODELABEL(n2), prop) // FOO
* DT_STRING_TOKEN(DT_NODELABEL(n3), prop) // 123_foo
* @endcode
*
* Notice how:
*
* - Unlike C identifiers, the property values may begin with a
* number. It's the user's responsibility not to use such values as
* the name of a C identifier.
*
* - The uppercased `"FOO"` in the DTS remains `FOO` as a token. It is
* *not* converted to `foo`.
*
* - The whitespace in the DTS `"123 foo"` string is converted to
* `123_foo` as a token.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @return the value of @p prop as a token, i.e. without any quotes
* and with special characters converted to underscores
*/
#define DT_STRING_TOKEN(node_id, prop) \
DT_CAT4(node_id, _P_, prop, _STRING_TOKEN)
/**
* @brief Like DT_STRING_TOKEN(), but with a fallback to @p default_value
*
* If the value exists, this expands to DT_STRING_TOKEN(node_id, prop).
* The @p default_value parameter is not expanded in this case.
*
* Otherwise, this expands to @p default_value.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param default_value a fallback value to expand to
* @return the property's value as a token, or @p default_value
*/
#define DT_STRING_TOKEN_OR(node_id, prop, default_value) \
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
(DT_STRING_TOKEN(node_id, prop)), (default_value))
/**
* @brief Like DT_STRING_TOKEN(), but uppercased.
*
* This removes "the quotes" from a string property's value,
* converting any non-alphanumeric characters to underscores, and
* capitalizing the result. This can be useful, for example, when
* programmatically using the value to form a C variable or code.
*
* DT_STRING_UPPER_TOKEN() can only be used for properties with string type.
*
* It is an error to use DT_STRING_UPPER_TOKEN() in other circumstances.
*
* Example devicetree fragment:
*
* @code{.dts}
* n1: node-1 {
* prop = "foo";
* };
* n2: node-2 {
* prop = "123 foo";
* };
* @endcode
*
* Example bindings fragment:
*
* @code{.yaml}
* properties:
* prop:
* type: string
*
* @endcode
*
* Example usage:
*
* @code{.c}
* DT_STRING_UPPER_TOKEN(DT_NODELABEL(n1), prop) // FOO
* DT_STRING_UPPER_TOKEN(DT_NODELABEL(n2), prop) // 123_FOO
* @endcode
*
* Notice how:
*
* - Unlike C identifiers, the property values may begin with a
* number. It's the user's responsibility not to use such values as
* the name of a C identifier.
*
* - The lowercased `"foo"` in the DTS becomes `FOO` as a token, i.e.
* it is uppercased.
*
* - The whitespace in the DTS `"123 foo"` string is converted to
* `123_FOO` as a token, i.e. it is uppercased and whitespace becomes
* an underscore.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @return the value of @p prop as an uppercased token, i.e. without
* any quotes and with special characters converted to underscores
*/
#define DT_STRING_UPPER_TOKEN(node_id, prop) \
DT_CAT4(node_id, _P_, prop, _STRING_UPPER_TOKEN)