Skip to content

Commit 7614110

Browse files
rkelmbolivar-nordicgalak
committed
dts: Add _STRING_TOKEN and _STRING_UPPER_TOKEN to string-array
This commit adds string token versions of the values also in items inside string-array. Signed-off-by: Radosław Koppel <[email protected]> Co-authored-by: Marti Bolivar <[email protected]> Co-authored-by: Kumar Gala <[email protected]>
1 parent d493751 commit 7614110

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2022 Intel Corp.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: Test string array token property container
5+
6+
compatible: "vnd,string-array-token"
7+
8+
properties:
9+
val:
10+
type: string-array
11+
required: true

include/zephyr/devicetree.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
* _IDX_<i>: logical index into property
5050
* _IDX_<i>_EXISTS: logical index into property is defined
5151
* _IDX_<i>_PH: phandle array's phandle by index (or phandle, phandles)
52+
* _IDX_<i>_STRING_TOKEN: string array element value as a token
53+
* _IDX_<i>_STRING_UPPER_TOKEN: string array element value as a uppercased token
5254
* _IDX_<i>_VAL_<val>: phandle array's specifier value by index
5355
* _IDX_<i>_VAL_<val>_EXISTS: cell value exists, by index
5456
* _LEN: property logical length
@@ -912,6 +914,94 @@
912914
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
913915
(DT_STRING_UPPER_TOKEN(node_id, prop)), (default_value))
914916

917+
/**
918+
* @brief Get an element out of a string-array property as a token.
919+
*
920+
* This removes "the quotes" from an element in the array, and converts
921+
* non-alphanumeric characters to underscores. That can be useful, for example,
922+
* when programmatically using the value to form a C variable or code.
923+
*
924+
* DT_STRING_TOKEN_BY_IDX() can only be used for properties with
925+
* string-array type.
926+
*
927+
* It is an error to use DT_STRING_TOKEN_BY_IDX() in other circumstances.
928+
*
929+
* Example devicetree fragment:
930+
*
931+
* n1: node-1 {
932+
* prop = "f1", "F2";
933+
* };
934+
* n2: node-2 {
935+
* prop = "123 foo", "456 FOO";
936+
* };
937+
*
938+
* Example bindings fragment:
939+
*
940+
* properties:
941+
* prop:
942+
* type: string-array
943+
*
944+
* Example usage:
945+
*
946+
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 0) // f1
947+
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 1) // F2
948+
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 0) // 123_foo
949+
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 1) // 456_FOO
950+
*
951+
* For more information, see @ref DT_STRING_TOKEN.
952+
*
953+
* @param node_id node identifier
954+
* @param prop lowercase-and-underscores property name
955+
* @param idx the index to get
956+
* @return the element in @p prop at index @p idx as a token
957+
*/
958+
#define DT_STRING_TOKEN_BY_IDX(node_id, prop, idx) \
959+
DT_CAT6(node_id, _P_, prop, _IDX_, idx, _STRING_TOKEN)
960+
961+
/**
962+
* @brief Like DT_STRING_TOKEN_BY_IDX(), but uppercased.
963+
*
964+
* This removes "the quotes" and capitalizes an element in the array, and
965+
* converts non-alphanumeric characters to underscores. That can be useful, for
966+
* example, when programmatically using the value to form a C variable or code.
967+
*
968+
* DT_STRING_UPPER_TOKEN_BY_IDX() can only be used for properties with
969+
* string-array type.
970+
*
971+
* It is an error to use DT_STRING_UPPER_TOKEN_BY_IDX() in other circumstances.
972+
*
973+
* Example devicetree fragment:
974+
*
975+
* n1: node-1 {
976+
* prop = "f1", "F2";
977+
* };
978+
* n2: node-2 {
979+
* prop = "123 foo", "456 FOO";
980+
* };
981+
*
982+
* Example bindings fragment:
983+
*
984+
* properties:
985+
* prop:
986+
* type: string-array
987+
*
988+
* Example usage:
989+
*
990+
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 0) // F1
991+
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 1) // F2
992+
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 0) // 123_FOO
993+
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 1) // 456_FOO
994+
*
995+
* For more information, see @ref DT_STRING_UPPER_TOKEN.
996+
*
997+
* @param node_id node identifier
998+
* @param prop lowercase-and-underscores property name
999+
* @param idx the index to get
1000+
* @return the element in @p prop at index @p idx as an uppercased token
1001+
*/
1002+
#define DT_STRING_UPPER_TOKEN_BY_IDX(node_id, prop, idx) \
1003+
DT_CAT6(node_id, _P_, prop, _IDX_, idx, _STRING_UPPER_TOKEN)
1004+
9151005
/*
9161006
* phandle properties
9171007
*
@@ -2582,6 +2672,26 @@
25822672
#define DT_INST_STRING_UPPER_TOKEN(inst, prop) \
25832673
DT_STRING_UPPER_TOKEN(DT_DRV_INST(inst), prop)
25842674

2675+
/**
2676+
* @brief Get an element out of string-array property as a token.
2677+
* @param inst instance number
2678+
* @param prop lowercase-and-underscores property string name
2679+
* @param idx the index to get
2680+
* @return the element in @p prop at index @p idx as a token
2681+
*/
2682+
#define DT_INST_STRING_TOKEN_BY_IDX(inst, prop, idx) \
2683+
DT_STRING_TOKEN_BY_IDX(DT_DRV_INST(inst), prop, idx)
2684+
2685+
/**
2686+
* @brief Like DT_INST_STRING_TOKEN_BY_IDX(), but uppercased.
2687+
* @param inst instance number
2688+
* @param prop lowercase-and-underscores property name
2689+
* @param idx the index to get
2690+
* @return the element in @p prop at index @p idx as an uppercased token
2691+
*/
2692+
#define DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, prop, idx) \
2693+
DT_STRING_UPPER_TOKEN_BY_IDX(DT_DRV_INST(inst), prop, idx)
2694+
25852695
/**
25862696
* @brief Get a DT_DRV_COMPAT instance's property value from a phandle's node
25872697
* @param inst instance number

scripts/dts/gen_defines.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,8 @@ def write_vanilla_props(node):
625625
subval_as_token = edtlib.str_as_token(subval)
626626
macro2val[macro + f"_IDX_{i}_TOKEN"] = subval_as_token
627627
macro2val[macro + f"_IDX_{i}_UPPER_TOKEN"] = subval_as_token.upper()
628+
macro2val[macro + f"_IDX_{i}_STRING_TOKEN"] = subval_as_token
629+
macro2val[macro + f"_IDX_{i}_STRING_UPPER_TOKEN"] = subval_as_token.upper()
628630
else:
629631
macro2val[macro + f"_IDX_{i}"] = subval
630632
macro2val[macro + f"_IDX_{i}_EXISTS"] = 1

tests/lib/devicetree/api/app.overlay

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,5 +506,20 @@
506506
compatible = "vnd,string-token";
507507
val = "token_two";
508508
};
509+
510+
test_str_array_token_0: string-array-token-0 {
511+
compatible = "vnd,string-array-token";
512+
val = "token_first_idx_zero",
513+
"token_first_idx_one",
514+
"token_first_idx_two";
515+
};
516+
517+
test_str_array_token_1: string-array-token-1 {
518+
compatible = "vnd,string-array-token";
519+
val = "token_second_idx_zero",
520+
"token_second_idx_one",
521+
"token_second_idx_two",
522+
"token_second_idx_three";
523+
};
509524
};
510525
};

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2338,6 +2338,106 @@ ZTEST(devicetree_api, test_string_token)
23382338
}
23392339
}
23402340

2341+
#undef DT_DRV_COMPAT
2342+
#define DT_DRV_COMPAT vnd_string_array_token
2343+
ZTEST(devicetree_api, test_string_idx_token)
2344+
{
2345+
enum token_string_idx {
2346+
/* Tokens */
2347+
token_first_idx_zero,
2348+
token_first_idx_one,
2349+
token_first_idx_two,
2350+
token_second_idx_zero,
2351+
token_second_idx_one,
2352+
token_second_idx_two,
2353+
token_second_idx_three,
2354+
/* Upper tokens */
2355+
TOKEN_FIRST_IDX_ZERO,
2356+
TOKEN_FIRST_IDX_ONE,
2357+
TOKEN_FIRST_IDX_TWO,
2358+
TOKEN_SECOND_IDX_ZERO,
2359+
TOKEN_SECOND_IDX_ONE,
2360+
TOKEN_SECOND_IDX_TWO,
2361+
TOKEN_SECOND_IDX_THREE
2362+
};
2363+
2364+
/* Test direct idx access */
2365+
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
2366+
token_first_idx_zero, "");
2367+
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
2368+
token_first_idx_one, "");
2369+
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
2370+
token_first_idx_two, "");
2371+
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
2372+
token_second_idx_zero, "");
2373+
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
2374+
token_second_idx_one, "");
2375+
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
2376+
token_second_idx_two, "");
2377+
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
2378+
token_second_idx_three, "");
2379+
2380+
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
2381+
TOKEN_FIRST_IDX_ZERO, "");
2382+
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
2383+
TOKEN_FIRST_IDX_ONE, "");
2384+
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
2385+
TOKEN_FIRST_IDX_TWO, "");
2386+
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
2387+
TOKEN_SECOND_IDX_ZERO, "");
2388+
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
2389+
TOKEN_SECOND_IDX_ONE, "");
2390+
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
2391+
TOKEN_SECOND_IDX_TWO, "");
2392+
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
2393+
TOKEN_SECOND_IDX_THREE, "");
2394+
2395+
/* Test instances */
2396+
#define STRING_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_token_, node_id)
2397+
#define STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
2398+
enum token_string_idx STRING_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
2399+
DT_INST_STRING_TOKEN_BY_IDX(inst, val, 0), \
2400+
DT_INST_STRING_TOKEN_BY_IDX(inst, val, 1), \
2401+
DT_INST_STRING_TOKEN_BY_IDX(inst, val, 2) \
2402+
};
2403+
DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION);
2404+
2405+
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
2406+
token_first_idx_zero, "");
2407+
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
2408+
token_first_idx_one, "");
2409+
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
2410+
token_first_idx_two, "");
2411+
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
2412+
token_second_idx_zero, "");
2413+
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
2414+
token_second_idx_one, "");
2415+
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
2416+
token_second_idx_two, "");
2417+
2418+
#define STRING_UPPER_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_upper_token, node_id)
2419+
#define STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
2420+
enum token_string_idx STRING_UPPER_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
2421+
DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 0), \
2422+
DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 1), \
2423+
DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 2) \
2424+
};
2425+
DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION);
2426+
2427+
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
2428+
TOKEN_FIRST_IDX_ZERO, "");
2429+
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
2430+
TOKEN_FIRST_IDX_ONE, "");
2431+
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
2432+
TOKEN_FIRST_IDX_TWO, "");
2433+
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
2434+
TOKEN_SECOND_IDX_ZERO, "");
2435+
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
2436+
TOKEN_SECOND_IDX_ONE, "");
2437+
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
2438+
TOKEN_SECOND_IDX_TWO, "");
2439+
}
2440+
23412441
#undef DT_DRV_COMPAT
23422442
#define DT_DRV_COMPAT vnd_adc_temp_sensor
23432443
ZTEST(devicetree_api, test_reset)

0 commit comments

Comments
 (0)