0

An example of a property I have defined in my bindings.yaml is this:

parity:
  type: string
  required: false
  default: "NONE"
  description: Sets the necessary UART parity required by the sensor.
  enum:
    - "NONE"
    - "ODD"
    - "EVEN"
    - "MARK"
    - "SPACE"

Now, using Zephyr's devicetree API, I can get these string literals back using DT_PROP(device, parity);. However, since Zephyr's UART API defines the following enums

UART_CFG_PARITY_NONE
UART_CFG_PARITY_ODD
UART_CFG_PARITY_EVEN
UART_CFG_PARITY_MARK
UART_CFG_PARITY_SPACE

I would like to convert the string literals to tokens such that I can reconstruct the enums and use them for compile time configuration. I.e. I would like to be able to write code like this:

static const struct uart_config config = {
    .parity = SOME_MACRO(device, parity),
};

and have it expand to

static const struct uart_config config = {
    .parity = UART_CFG_PARITY_<either of NONE, ODD, EVEN, MARK, SPACE>,
};

How can this be done?

1 Answers1

0

After conducting some research, I found that Zephyr has a macro for this exact purpose: https://docs.zephyrproject.org/latest/build/dts/api/api.html#c.DT_STRING_TOKEN.