Skip to content

Commit f4db14f

Browse files
carlocaionegalak
authored andcommitted
dts: Introduce DT_STRING_TOKEN and DT_STRING_UPPER_TOKEN
To be able to get a tokenize DT string without the quotes. Deprecate also DT_ENUM_TOKEN and DT_ENUM_UPPER_TOKEN. Signed-off-by: Carlo Caione <[email protected]>
1 parent f5e5f7d commit f4db14f

File tree

6 files changed

+128
-4
lines changed

6 files changed

+128
-4
lines changed

doc/releases/release-notes-2.7.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ interface and listing all issues with the `bug label
2727
API Changes
2828
***********
2929

30+
Deprecated in this release
31+
32+
* :c:macro:`DT_ENUM_TOKEN` and :c:macro:`DT_ENUM_UPPER_TOKEN`,
33+
were deprecated in favor of utilizing
34+
:c:macro:`DT_STRING_TOKEN` and :c:macro:`DT_STRING_UPPER_TOKEN`
35+
3036
Changes in this release
3137

3238
==========================

drivers/flash/nrf_qspi_nor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ BUILD_ASSERT(INST_0_SCK_FREQUENCY >= (NRF_QSPI_BASE_CLOCK_FREQ / 16),
4646
#define QSPI_PROP_LEN(prop) DT_PROP_LEN(QSPI_NODE, prop)
4747

4848
#define INST_0_QER _CONCAT(JESD216_DW15_QER_, \
49-
DT_ENUM_TOKEN(DT_DRV_INST(0), \
49+
DT_STRING_TOKEN(DT_DRV_INST(0), \
5050
quad_enable_requirements))
5151

5252
BUILD_ASSERT(((INST_0_QER == JESD216_DW15_QER_NONE)

include/devicetree.h

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
*
4242
* _ENUM_IDX: property's value as an index into bindings enum
4343
* _ENUM_TOKEN: property's value as a token into bindings enum (string
44-
* enum values are identifiers)
45-
* _ENUM_UPPER_TOKEN: like _ENUM_TOKEN, but uppercased
44+
* enum values are identifiers) [deprecated, use _STRING_TOKEN]
45+
* _ENUM_UPPER_TOKEN: like _ENUM_TOKEN, but uppercased [deprecated, use
46+
* _STRING_UPPER_TOKEN]
4647
* _EXISTS: property is defined
4748
* _FOREACH_PROP_ELEM: helper for "iterating" over values in the property
4849
* _FOREACH_PROP_ELEM_VARGS: foreach functions with variable number of arguments
@@ -55,6 +56,8 @@
5556
* _NAME_<name>_PH: phandle array's phandle by name
5657
* _NAME_<name>_VAL_<val>: phandle array's property specifier by name
5758
* _NAME_<name>_VAL_<val>_EXISTS: cell value exists, by name
59+
* _STRING_TOKEN: string property's value as a token
60+
* _STRING_UPPER_TOKEN: like _STRING_TOKEN, but uppercased
5861
*/
5962

6063
/**
@@ -702,6 +705,113 @@
702705
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
703706
(DT_ENUM_IDX(node_id, prop)), (default_idx_value))
704707

708+
/**
709+
* @brief Get a string property's value as a token.
710+
*
711+
* This removes "the quotes" from string-valued properties, and converts
712+
* non-alphanumeric characters to underscores. That can be useful, for example,
713+
* when programmatically using the value to form a C variable or code.
714+
*
715+
* DT_STRING_TOKEN() can only be used for properties with string type.
716+
*
717+
* It is an error to use DT_STRING_TOKEN() in other circumstances.
718+
*
719+
* Example devicetree fragment:
720+
*
721+
* n1: node-1 {
722+
* prop = "foo";
723+
* };
724+
* n2: node-2 {
725+
* prop = "FOO";
726+
* }
727+
* n3: node-3 {
728+
* prop = "123 foo";
729+
* };
730+
*
731+
* Example bindings fragment:
732+
*
733+
* properties:
734+
* prop:
735+
* type: string
736+
*
737+
* Example usage:
738+
*
739+
* DT_STRING_TOKEN(DT_NODELABEL(n1), prop) // foo
740+
* DT_STRING_TOKEN(DT_NODELABEL(n2), prop) // FOO
741+
* DT_STRING_TOKEN(DT_NODELABEL(n3), prop) // 123_foo
742+
*
743+
* Notice how:
744+
*
745+
* - Unlike C identifiers, the property values may begin with a
746+
* number. It's the user's responsibility not to use such values as
747+
* the name of a C identifier.
748+
*
749+
* - The uppercased "FOO" in the DTS remains @p FOO as a token. It is
750+
* *not* converted to @p foo.
751+
*
752+
* - The whitespace in the DTS "123 foo" string is converted to @p
753+
* 123_foo as a token.
754+
*
755+
* @param node_id node identifier
756+
* @param prop lowercase-and-underscores property string name
757+
* @return the value of @p prop as a token, i.e. without any quotes
758+
* and with special characters converted to underscores
759+
*/
760+
#define DT_STRING_TOKEN(node_id, prop) \
761+
DT_CAT4(node_id, _P_, prop, _STRING_TOKEN)
762+
763+
/**
764+
* @brief Like DT_STRING_TOKEN(), but uppercased.
765+
*
766+
* This removes "the quotes and capitalize" from string-valued properties, and
767+
* converts non-alphanumeric characters to underscores. That can be useful, for
768+
* example, when programmatically using the value to form a C variable or code.
769+
*
770+
* DT_STRING_UPPER_TOKEN() can only be used for properties with string type.
771+
*
772+
* It is an error to use DT_STRING_UPPER_TOKEN() in other circumstances.
773+
*
774+
* Example devicetree fragment:
775+
*
776+
* n1: node-1 {
777+
* prop = "foo";
778+
* };
779+
* n2: node-2 {
780+
* prop = "123 foo";
781+
* };
782+
*
783+
* Example bindings fragment:
784+
*
785+
* properties:
786+
* prop:
787+
* type: string
788+
*
789+
* Example usage:
790+
*
791+
* DT_STRING_UPPER_TOKEN(DT_NODELABEL(n1), prop) // FOO
792+
* DT_STRING_UPPER_TOKEN(DT_NODELABEL(n2), prop) // 123_FOO
793+
*
794+
* Notice how:
795+
*
796+
* - Unlike C identifiers, the property values may begin with a
797+
* number. It's the user's responsibility not to use such values as
798+
* the name of a C identifier.
799+
*
800+
* - The lowercased "foo" in the DTS becomes @p FOO as a token, i.e.
801+
* it is uppercased.
802+
*
803+
* - The whitespace in the DTS "123 foo" string is converted to @p
804+
* 123_FOO as a token, i.e. it is uppercased and whitespace becomes
805+
* an underscore.
806+
*
807+
* @param node_id node identifier
808+
* @param prop lowercase-and-underscores property string name
809+
* @return the value of @p prop as a token, i.e. without any quotes
810+
* and with special characters converted to underscores
811+
*/
812+
#define DT_STRING_UPPER_TOKEN(node_id, prop) \
813+
DT_CAT4(node_id, _P_, prop, _STRING_UPPER_TOKEN)
814+
705815
/**
706816
* @brief Get an enumeration property's value as a token.
707817
*
@@ -764,6 +874,7 @@
764874
* and with special characters converted to underscores
765875
*/
766876
#define DT_ENUM_TOKEN(node_id, prop) \
877+
__DEPRECATED_MACRO \
767878
DT_CAT4(node_id, _P_, prop, _ENUM_TOKEN)
768879

769880
/**
@@ -823,6 +934,7 @@
823934
* underscores
824935
*/
825936
#define DT_ENUM_UPPER_TOKEN(node_id, prop) \
937+
__DEPRECATED_MACRO \
826938
DT_CAT4(node_id, _P_, prop, _ENUM_UPPER_TOKEN)
827939

828940
/*

scripts/dts/gen_defines.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,10 @@ def write_vanilla_props(node):
524524
# DT_N_<node-id>_P_<prop-id>
525525
macro2val[macro] = val
526526

527+
if prop.spec.type == 'string':
528+
macro2val[macro + "_STRING_TOKEN"] = prop.val_as_token
529+
macro2val[macro + "_STRING_UPPER_TOKEN"] = prop.val_as_token.upper()
530+
527531
if prop.enum_index is not None:
528532
# DT_N_<node-id>_P_<prop-id>_ENUM_IDX
529533
macro2val[macro + "_ENUM_IDX"] = prop.enum_index

soc/arm/nuvoton_npcx/common/soc_dt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
#define NPCX_DT_PROP_ENUM_OR(node_id, prop, default_value) \
2525
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
26-
(DT_ENUM_UPPER_TOKEN(node_id, prop)), (default_value))
26+
(DT_STRING_UPPER_TOKEN(node_id, prop)), (default_value))
2727

2828
/**
2929
* @brief Like DT_INST_PROP_OR(), but expand parameters with

tests/lib/devicetree/api/src/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
* - DT_INST_IO_CHANNELS_LABEL
2929
* - DT_INST_DMAS_LABEL_BY_IDX
3030
* - DT_INST_DMAS_LABEL_BY_NAME
31+
* - DT_ENUM_TOKEN
32+
* - DT_ENUM_UPPER_TOKEN
3133
*/
3234
#define __DEPRECATED_MACRO
3335

0 commit comments

Comments
 (0)