Skip to content

Commit

Permalink
soc/integration: Add mixed lists to dts_property()
Browse files Browse the repository at this point in the history
Allows a mix of integers and phandles in the list to support
generating gpio-controller references, interrupt-map
references and other more expressive DTS features.
  • Loading branch information
Andrew Dennison committed Jun 17, 2024
1 parent aa3742a commit eb0358e
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions litex/gen/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,25 @@ def reverse_bytes(s):
# DTS ---------------------------------------------------------------------------------------------


def dts_property(name: str, value: Union[int, str, List[int], List[str]] = None) -> str:
def dts_property(name: str, value: Union[int, str, List[Union[int, str]], List[str]] = None) -> str:
"""Returns property 'name' formatted in Devicetree syntax
value can be None (default), int, str or a list of int or str.
value can be:
None (default)
int
str
list of str
list of int and str: in this case the str is expected to be a phandle.
"""
if value is None:
return f"{name};\n"
elif isinstance(value, list):
if all(isinstance(v, int) for v in value):
if all(isinstance(v, str) for v in value):
of_value = ", ".join(f'"{v}"' for v in value)
elif all(isinstance(v, int) or (isinstance(v, str) and v.startswith("&")) for v in value):
of_value = "<" + " ".join(f"{v}" for v in value) + ">"
elif all(isinstance(v, str) for v in value):
of_value = ", ".join(f'"{v}"' for v in value)
else:
raise ValueError("All elements in the list must be of the same type (either all int or all str)")
raise ValueError(f"unsupported elements in {value}")
return f"{name} = {of_value};\n"
else:
of_value = f'"{value}"' if isinstance(value, str) else f"<{value}>"
Expand Down

0 comments on commit eb0358e

Please sign in to comment.