Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

devicetree: Prefix and Postfix helpers for enums #59786

Closed
wants to merge 1 commit into from

Conversation

DaGuich
Copy link

@DaGuich DaGuich commented Jun 27, 2023

Added helper macros to simplify handling of enum-strings defined in the dt-bindings. With these new macros you can define C-enums and generate by parsing the devicetree the enum values.

This makes the devicetree simpler to read and the implementation of device drivers more straight-forward.

Enumerations with possible (device) register values are neat because the author or reviewers of a driver can easily get the meaning of different values. But this has the drawback that you're not able to directly bring these (C-enums) into the devictree. One alternative solution would be to use defines. But then you'd need to maintain (bugs?) the same value in two different locations. The solution I propose is using string-enums in the dt-bindings with a meaningful name. You could document these values in the dt-bindings.

If you implement a driver you could then parse the string as a token. But usually we are using prefixes, so we have unique enum members. Here comes the new macros of this PR into play. These macros are helping users to prefix the string tokens with the corresponding prefixes and automagically use the enum values while parsing the devicetree.

Related to: #59256

Edit: Added some background on why I've implemented the change

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @DaGuich, and thank you very much for your first pull request to the Zephyr project!

A project maintainer just triggered our CI pipeline to run it against your PR and ensure it's compliant and doesn't cause any issues. You might want to take this opportunity to review the project's Contributor Expectations and make any updates to your pull request if necessary. 😊

Added helper macros to simplify handling of enum-strings defined in the
dt-bindings. With these new macros you can define C-enums and generate
by parsing the devicetree the enum values.

This makes the devicetree simpler to read and the implementation of
device drivers more straight-forward.

Signed-off-by: Matthias Gilch <[email protected]>
@MaureenHelm
Copy link
Member

@mbolivar-ampere can you take a look and update the MAINTAINER file with your new handle?

Copy link
Contributor

@mbolivar-ampere mbolivar-ampere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patch! I'm OK with the API changes, but there are some issues with the implementation.

Comment on lines +921 to +926
* @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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revisit the docstring. It looks like things were copy/pasted from elsewhere that don't accurately reflect the behavior of these macros.

* and with special characters converted to underscores
*/
#define DT_STRING_TOKEN_PREFIX(node_id, prop, prefix) \
UTIL_CAT(prefix, DT_STRING_TOKEN(node_id, prop))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use DT_CAT, not UTIL_CAT, for reasons documented at the end of the file where the DT_CAT macros are defined.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. If I were using DT_CAT I'd need to wrap these macros, because DT_STRING_TOKEN needs to expand first, which would not happen if I'd simply replaced UTIL_CAT with DT_CAT.

UTIL_CAT is already used on other occasions in this file, so I was thinking it's better to use it, instead of wrapping it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Can you address the other comment?

@kartben
Copy link
Collaborator

kartben commented Sep 28, 2023

@DaGuich will you have time to look at incorporating feedback from @mbolivar-ampere ?

Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Dec 26, 2023
@github-actions github-actions bot closed this Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants