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 <r.koppel@k-el.com>
Co-authored-by: Marti Bolivar <marti.bolivar@nordicsemi.no>
Co-authored-by: Kumar Gala <galak@kernel.org>
This commit is contained in:
Radosław Koppel 2022-05-07 20:27:49 +02:00 committed by Kumar Gala
parent d493751fa1
commit 7614110a20
5 changed files with 238 additions and 0 deletions

View file

@ -0,0 +1,11 @@
# Copyright (c) 2022 Intel Corp.
# SPDX-License-Identifier: Apache-2.0
description: Test string array token property container
compatible: "vnd,string-array-token"
properties:
val:
type: string-array
required: true

View file

@ -49,6 +49,8 @@
* _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>_VAL_<val>: phandle array's specifier value by index
* _IDX_<i>_VAL_<val>_EXISTS: cell value exists, by index
* _LEN: property logical length
@ -912,6 +914,94 @@
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
(DT_STRING_UPPER_TOKEN(node_id, prop)), (default_value))
/**
* @brief Get an element out of a string-array property as a token.
*
* This removes "the quotes" from an element in the array, and converts
* non-alphanumeric characters to underscores. That can be useful, for example,
* when programmatically using the value to form a C variable or code.
*
* DT_STRING_TOKEN_BY_IDX() can only be used for properties with
* string-array type.
*
* It is an error to use DT_STRING_TOKEN_BY_IDX() in other circumstances.
*
* Example devicetree fragment:
*
* n1: node-1 {
* prop = "f1", "F2";
* };
* n2: node-2 {
* prop = "123 foo", "456 FOO";
* };
*
* Example bindings fragment:
*
* properties:
* prop:
* type: string-array
*
* Example usage:
*
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 0) // f1
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 1) // F2
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 0) // 123_foo
* DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 1) // 456_FOO
*
* For more information, see @ref DT_STRING_TOKEN.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param idx the index to get
* @return the element in @p prop at index @p idx as a token
*/
#define DT_STRING_TOKEN_BY_IDX(node_id, prop, idx) \
DT_CAT6(node_id, _P_, prop, _IDX_, idx, _STRING_TOKEN)
/**
* @brief Like DT_STRING_TOKEN_BY_IDX(), but uppercased.
*
* This removes "the quotes" and capitalizes an element in the array, and
* converts non-alphanumeric characters to underscores. That can be useful, for
* example, when programmatically using the value to form a C variable or code.
*
* DT_STRING_UPPER_TOKEN_BY_IDX() can only be used for properties with
* string-array type.
*
* It is an error to use DT_STRING_UPPER_TOKEN_BY_IDX() in other circumstances.
*
* Example devicetree fragment:
*
* n1: node-1 {
* prop = "f1", "F2";
* };
* n2: node-2 {
* prop = "123 foo", "456 FOO";
* };
*
* Example bindings fragment:
*
* properties:
* prop:
* type: string-array
*
* Example usage:
*
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 0) // F1
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n1), prop, 1) // F2
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 0) // 123_FOO
* DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(n2), prop, 1) // 456_FOO
*
* For more information, see @ref DT_STRING_UPPER_TOKEN.
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param idx the index to get
* @return the element in @p prop at index @p idx as an uppercased token
*/
#define DT_STRING_UPPER_TOKEN_BY_IDX(node_id, prop, idx) \
DT_CAT6(node_id, _P_, prop, _IDX_, idx, _STRING_UPPER_TOKEN)
/*
* phandle properties
*
@ -2582,6 +2672,26 @@
#define DT_INST_STRING_UPPER_TOKEN(inst, prop) \
DT_STRING_UPPER_TOKEN(DT_DRV_INST(inst), prop)
/**
* @brief Get an element out of string-array property as a token.
* @param inst instance number
* @param prop lowercase-and-underscores property string name
* @param idx the index to get
* @return the element in @p prop at index @p idx as a token
*/
#define DT_INST_STRING_TOKEN_BY_IDX(inst, prop, idx) \
DT_STRING_TOKEN_BY_IDX(DT_DRV_INST(inst), prop, idx)
/**
* @brief Like DT_INST_STRING_TOKEN_BY_IDX(), but uppercased.
* @param inst instance number
* @param prop lowercase-and-underscores property name
* @param idx the index to get
* @return the element in @p prop at index @p idx as an uppercased token
*/
#define DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, prop, idx) \
DT_STRING_UPPER_TOKEN_BY_IDX(DT_DRV_INST(inst), prop, idx)
/**
* @brief Get a DT_DRV_COMPAT instance's property value from a phandle's node
* @param inst instance number

View file

@ -625,6 +625,8 @@ def write_vanilla_props(node):
subval_as_token = edtlib.str_as_token(subval)
macro2val[macro + f"_IDX_{i}_TOKEN"] = subval_as_token
macro2val[macro + f"_IDX_{i}_UPPER_TOKEN"] = subval_as_token.upper()
macro2val[macro + f"_IDX_{i}_STRING_TOKEN"] = subval_as_token
macro2val[macro + f"_IDX_{i}_STRING_UPPER_TOKEN"] = subval_as_token.upper()
else:
macro2val[macro + f"_IDX_{i}"] = subval
macro2val[macro + f"_IDX_{i}_EXISTS"] = 1

View file

@ -506,5 +506,20 @@
compatible = "vnd,string-token";
val = "token_two";
};
test_str_array_token_0: string-array-token-0 {
compatible = "vnd,string-array-token";
val = "token_first_idx_zero",
"token_first_idx_one",
"token_first_idx_two";
};
test_str_array_token_1: string-array-token-1 {
compatible = "vnd,string-array-token";
val = "token_second_idx_zero",
"token_second_idx_one",
"token_second_idx_two",
"token_second_idx_three";
};
};
};

View file

@ -2338,6 +2338,106 @@ ZTEST(devicetree_api, test_string_token)
}
}
#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_string_array_token
ZTEST(devicetree_api, test_string_idx_token)
{
enum token_string_idx {
/* Tokens */
token_first_idx_zero,
token_first_idx_one,
token_first_idx_two,
token_second_idx_zero,
token_second_idx_one,
token_second_idx_two,
token_second_idx_three,
/* Upper tokens */
TOKEN_FIRST_IDX_ZERO,
TOKEN_FIRST_IDX_ONE,
TOKEN_FIRST_IDX_TWO,
TOKEN_SECOND_IDX_ZERO,
TOKEN_SECOND_IDX_ONE,
TOKEN_SECOND_IDX_TWO,
TOKEN_SECOND_IDX_THREE
};
/* Test direct idx access */
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
token_first_idx_zero, "");
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
token_first_idx_one, "");
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
token_first_idx_two, "");
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
token_second_idx_zero, "");
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
token_second_idx_one, "");
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
token_second_idx_two, "");
zassert_equal(DT_STRING_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
token_second_idx_three, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 0),
TOKEN_FIRST_IDX_ZERO, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 1),
TOKEN_FIRST_IDX_ONE, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_0), val, 2),
TOKEN_FIRST_IDX_TWO, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 0),
TOKEN_SECOND_IDX_ZERO, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 1),
TOKEN_SECOND_IDX_ONE, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 2),
TOKEN_SECOND_IDX_TWO, "");
zassert_equal(DT_STRING_UPPER_TOKEN_BY_IDX(DT_NODELABEL(test_str_array_token_1), val, 3),
TOKEN_SECOND_IDX_THREE, "");
/* Test instances */
#define STRING_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_token_, node_id)
#define STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
enum token_string_idx STRING_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
DT_INST_STRING_TOKEN_BY_IDX(inst, val, 0), \
DT_INST_STRING_TOKEN_BY_IDX(inst, val, 1), \
DT_INST_STRING_TOKEN_BY_IDX(inst, val, 2) \
};
DT_INST_FOREACH_STATUS_OKAY(STRING_TOKEN_BY_IDX_TEST_INST_EXPANSION);
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
token_first_idx_zero, "");
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
token_first_idx_one, "");
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
token_first_idx_two, "");
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
token_second_idx_zero, "");
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
token_second_idx_one, "");
zassert_equal(STRING_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
token_second_idx_two, "");
#define STRING_UPPER_TOKEN_BY_IDX_VAR(node_id) _CONCAT(var_upper_token, node_id)
#define STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION(inst) \
enum token_string_idx STRING_UPPER_TOKEN_BY_IDX_VAR(DT_DRV_INST(inst))[] = { \
DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 0), \
DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 1), \
DT_INST_STRING_UPPER_TOKEN_BY_IDX(inst, val, 2) \
};
DT_INST_FOREACH_STATUS_OKAY(STRING_UPPER_TOKEN_BY_IDX_TEST_INST_EXPANSION);
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[0],
TOKEN_FIRST_IDX_ZERO, "");
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[1],
TOKEN_FIRST_IDX_ONE, "");
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_0))[2],
TOKEN_FIRST_IDX_TWO, "");
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[0],
TOKEN_SECOND_IDX_ZERO, "");
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[1],
TOKEN_SECOND_IDX_ONE, "");
zassert_equal(STRING_UPPER_TOKEN_BY_IDX_VAR(DT_NODELABEL(test_str_array_token_1))[2],
TOKEN_SECOND_IDX_TWO, "");
}
#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_adc_temp_sensor
ZTEST(devicetree_api, test_reset)