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

Expand Device Tree enums to C-enum names #59256

Open
DaGuich opened this issue Jun 15, 2023 · 2 comments
Open

Expand Device Tree enums to C-enum names #59256

DaGuich opened this issue Jun 15, 2023 · 2 comments
Labels
area: Devicetree Tooling PR modifies or adds a Device Tree tooling Feature Request A request for a new feature Needs review This PR needs attention from Zephyr's maintainers

Comments

@DaGuich
Copy link

DaGuich commented Jun 15, 2023

Is your feature request related to a problem? Please describe.
While creating the device tree bindings and implementing the parsing of the said bindings, we came across some issues.
In the pre-zephyr times, we had the configuration of these devices in code, with an enum, where every enum value was associated with either direct register values or bit masks to work on the registers.
At the moment we are using included defines inside of the device tree in combination with int-enums in the device tree bindings to reflect this. But from our point of view this has some disadvantages:

  • The device tree binding (yaml) gets cluttered with lots of hard-to-understand integer values
  • The value/bit mask is in distributed at least 2 times in the project (dt-bindings and header file)
  • The generated device tree file gets harder to read and "debug"

Instead of using in the dt-bindings an integer enum, we could use a string enum. Then we are able to access the enum value by using the props ..._ENUM_TOKEN macro.

The string enum will be easier to read in the generated device tree. It will also not generate any overhead in the binaries as these are behind macros in devicetree_generated.h. But this also has some drawbacks. First and foremost the configuration will have the full code-enum value which (again) makes the device tree harder to read.

Example:
Configuration of a multiplier:
Enumeration:

enum devname_pwm_multiplier_t {
    DEVNAME_MULT_8 = 0x00,
    DEVNAME_MULT_10 = 0x10,
    DEVNAME_MULT_38 = 0x50,
    DEVNAME_MULT_44 = 0x60,
    DEVNAME_MULT_48 = 0x70,
};

dt-bindings:

properties:
  pwm-multiplier:
    type: int
    enum:
      - 0
      - 16
      - 80
      - 96
      - 112
    description: useful description

Describe the solution you'd like

The addition necessary for this is a macro to prefix (and maybe postfix) the string enum with the actual code-enum value.

Using the example above
dt-bindings

properties:
  pwm-multiplier:
    type: string
    enum:
      - "MULT_8"
      - "MULT_10"
      - "MULT_38"
      - "MULT_44"
      - "MULT_48"
    description: useful description

In code, we would have a macro to access it

  .pwm_multiplier = DT_INST_ENUM_PREFIX(n, pwm_multiplier, DEVNAME_),

The macro DT_INST_ENUM_PREFIX will expand then to DEVNAME_MULT_38

Describe alternatives you've considered
An alternative solution would have been to add an additional field(s) in the device-tree syntax which would contain the code-enum prefix and postfix. For this, we would also need to adapt the scripts to generate additional macros into devicetree_generated.h.

@DaGuich DaGuich added the Feature Request A request for a new feature label Jun 15, 2023
@github-actions
Copy link

Hi @DaGuich! We appreciate you submitting your first issue for our open-source project. 🌟

Even though I'm a bot, I can assure you that the whole community is genuinely grateful for your time and effort. 🤖💙

@DaGuich
Copy link
Author

DaGuich commented Jun 27, 2023

I have implemented the functionality for prefixes and postfixes in the PR #59786 .

@zephyrbot zephyrbot added the Needs review This PR needs attention from Zephyr's maintainers label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree Tooling PR modifies or adds a Device Tree tooling Feature Request A request for a new feature Needs review This PR needs attention from Zephyr's maintainers
Projects
None yet
Development

No branches or pull requests

3 participants