1"""
2Copyright (c) 2025 Silicon Laboratories Inc.
3
4SPDX-License-Identifier: Apache-2.0
5"""
6import argparse
7import re
8import datetime
9from pathlib import Path
10
11devices = {
12	"xg21": {
13		"bits": "platform/Device/SiliconLabs/EFR32MG21/Include/efr32mg21_acmp.h",
14  },
15	"xg23": {
16		"bits": "platform/Device/SiliconLabs/EFR32FG23/Include/efr32fg23_acmp.h",
17  },
18	"xg24": {
19		"bits": "platform/Device/SiliconLabs/EFR32MG24/Include/efr32mg24_acmp.h",
20  },
21	"xg27": {
22		"bits": "platform/Device/SiliconLabs/EFR32BG27/Include/efr32bg27_acmp.h",
23  },
24	"xg29": {
25		"bits": "platform/Device/SiliconLabs/EFR32MG29/Include/efr32mg29_acmp.h",
26  },
27}
28
29if __name__ == "__main__":
30  parser = argparse.ArgumentParser(description="Generate headers for Comparator for Series 2 "
31                                   "devices. The headers are used from DeviceTree, and represent "
32                                   "every ACMP input selection as a DT compatible macro.")
33  parser.add_argument("--out", "-o", type=Path, default=Path(__file__).parent / "out",
34                      help="Output directory. Defaults to the directory ./out/ relative to the "
35                      "script. Set to $ZEPHYR_BASE/include/zephyr/dt-bindings/comparator/ "
36                      "to directly generate output into the expected location within the Zephyr "
37                      "main tree.")
38  parser.add_argument("--sdk", "-s", type=Path, default=Path(__file__).parent.parent / "simplicity_sdk",
39                      help="Path to Simplicity SDK to extract data from. Defaults to the directory "
40                       "../simplicity_sdk relative to the script.")
41  args = parser.parse_args()
42
43  args.out.mkdir(exist_ok=True)
44
45  defines = {}
46  for device, data_sources in devices.items():
47    bits_file = (args.sdk / data_sources["bits"]).resolve()
48    with bits_file.open() as f:
49      for line in f:
50
51        if m := re.match(r"#define (_ACMP_INPUTCTRL_POSSEL_(?!SHIFT)(?!MASK)(?!DEFAULT).*)\s+(\dx[\dABCDEF]+)", line):
52          input_value = hex(int(m.group(2),16))
53          input_name = f"#define ACMP_INPUT_{m.group(1).split('_')[-1]}"
54          # Detect any input definition collisions
55          if (input_value in defines):
56              if ( input_name == defines[input_value] ):
57                  print(f"Inputs {input_name} and {defines[input_value]} share the same value {input_value}.")
58          defines.update({input_value : f"{input_name} {input_value}"})
59
60        if m := re.match(r"#define (_ACMP_INPUTCTRL_NEGSEL_(?!SHIFT)(?!MASK)(?!DEFAULT).*)\s+(\dx[\dABCDEF]+)", line):
61          input_value = hex(int(m.group(2),16))
62          input_name = f"#define ACMP_INPUT_{m.group(1).split('_')[-1]}"
63          # Detect any input definition collisions
64          if (input_value in defines):
65              if ( input_name == defines[input_value] ):
66                  print(f"Inputs {input_name} and {defines[input_value]} share the same value {input_value}.")
67          defines.update({input_value : f"{input_name} {input_value}"})
68
69  # Sort defines by key
70  defines = dict(sorted(defines.items()))
71
72  file = [
73    "/*",
74    f" * Copyright (c) {datetime.date.today().year} Silicon Laboratories Inc.",
75    " *",
76    " * SPDX-License-Identifier: Apache-2.0",
77    " *",
78    f" * This file was generated by the script {Path(__file__).name} in the hal_silabs module.",
79    " * Do not manually edit.",
80    " */",
81    "",
82    f"#ifndef ZEPHYR_INCLUDE_DT_BINDINGS_COMPARATOR_SILABS_ACMP_H_",
83    f"#define ZEPHYR_INCLUDE_DT_BINDINGS_COMPARATOR_SILABS_ACMP_H_",
84    "",
85    f"/* ACMP Input Aliases */",
86    f"#define ACMP_INPUT_VDACOUT0 ACMP_INPUT_VDAC0OUT0",
87    f"#define ACMP_INPUT_VDACOUT1 ACMP_INPUT_VDAC0OUT1",
88    "",
89    f"/* ACMP Input Definitions */",
90  ] + list(defines.values()) + [
91    "",
92    f"#endif /* ZEPHYR_INCLUDE_DT_BINDINGS_COMPARATOR_SILABS_ACMP_H_ */",
93    "",
94  ]
95
96  outfile = args.out / f"silabs-acmp.h"
97  outfile.write_text("\n".join(file))
98