Skip to content

Commit

Permalink
ports/psoc6: Combine scripts.
Browse files Browse the repository at this point in the history
Signed-off-by: IFX-Anusha <Anusha.TR@infineon.com>
  • Loading branch information
IFX-Anusha committed Aug 23, 2024
1 parent cb8a144 commit 1c17bda
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 166 deletions.
8 changes: 3 additions & 5 deletions ports/psoc6/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,15 @@ HEADER_BUILD := $(BUILD)/genhdr
GEN_PINS_HDR := $(BUILD)/genhdr/pins.h
GEN_PINS_QSTR := $(BUILD)/pins_qstr.h
BOARD_PINS := pins.csv
BOARD_AF_PINS := pins_af.csv
BOARD_AF_PINS := pins_details.csv

GENERATED_PINS = $(GEN_PINS_SRC) $(GEN_PINS_HDR) $(GEN_PINS_QSTR)

$(GENERATED_PINS):
@echo "Generating $@"
$(PYTHON) boards/make-pins-csv.py --gen-pin-for $(PIN_PACKAGE_FILE) --save-pins-csv-at $(BOARD_PINS) --save-pins-af-csv-at $(BOARD_AF_PINS)
$(MKDIR) -p $(BUILD)/genhdr
$(PYTHON) boards/make-pins.py --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) > $(GEN_PINS_SRC)
rm -rf $(BOARD_PINS) $(BOARD_AF_PINS)

$(PYTHON) boards/make-pins.py --gen-pin-for $(PIN_PACKAGE_FILE) --save-pins-csv-at $(BOARD_PINS) --save-pins-details-csv-at $(BOARD_AF_PINS) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) > $(GEN_PINS_SRC)
#rm -rf $(BOARD_PINS) $(BOARD_AF_PINS)

# Flags for optional C++ source code
CXXFLAGS += $(filter-out -std=c99,$(CFLAGS))
Expand Down
147 changes: 0 additions & 147 deletions ports/psoc6/boards/make-pins-csv.py

This file was deleted.

111 changes: 97 additions & 14 deletions ports/psoc6/boards/make-pins.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
from itertools import islice
import argparse
import sys
import csv
Expand Down Expand Up @@ -42,15 +43,6 @@ def set_is_board_pin(self):
def set_board_index(self, index):
self.board_index = index

def print(self):
"""print(
"const machine_pin_phy_obj_t pin_{:s}_obj = PIN({:s}, {:s});".format(
self._name,
self._name,
self._pin_addr,
)
)"""

def print_header(self, num_pins, hdr_file):
hdr_file.write("#define MAX_IO_PINS {:d} \n".format(num_pins))

Expand Down Expand Up @@ -98,9 +90,8 @@ def print_const_table(self):
pin.print_const_table_entry()
print("};")

# ToDo: Complete for alternate functions
def parse_af_file(self):
with open("./pins_af.csv", "r") as csvfile:
def parse_pin_details_file(self):
with open("./pins_details.csv", "r") as csvfile:
rows = csv.reader(csvfile)
for row in rows:
try:
Expand Down Expand Up @@ -170,12 +161,99 @@ def print_qstr(self, qstr_filename):
# cond_var = None
print("Q({})".format(qstr), file=qstr_file)

def get_pin_addr_helper(self, pin_def):
pattern = r"CYHAL_PORT_(\d+),\s*(\d+)"
match = re.search(pattern, pin_def)
port_number = match.group(1)
pin_number = match.group(2)
return (int(port_number) << 3) + int(pin_number)

def get_pin_package_path(self, filename):
root_dir = "./mtb_shared/mtb-hal-cat1"
mid_dir = "COMPONENT_CAT1A/include/pin_packages"
for dirpath, dirnames, filenames in os.walk(root_dir):
for dirname in dirnames:
if dirname.startswith("release-"):
release_version = dirname
file_path = os.path.join(root_dir, release_version, mid_dir, filename)
if os.path.isfile(file_path):
return file_path
return None

def generate_pins_csv(
self, pin_package_filename, pins_csv_filename, pins_details_csv_filename
):
file_path = self.get_pin_package_path(pin_package_filename)

if file_path is None:
sys.exit(1)

# Read the header file
with open(file_path, "r") as file:
content = file.read()

# Find the starting and ending points of the enum declaration
enum_start = content.find("typedef enum {")
enum_end = content.find("}")

if enum_start != -1 and enum_end != -1:
enum_content = content[enum_start:enum_end]

# Extract enum values using regex
enum_values = re.findall(r"\b(\w+)\s*=", enum_content)
pin_name = re.findall(r"\b(?!NC\b)(\w+)\s*=", enum_content)
pin_defs = re.findall(r"=\s*(.*?\))", enum_content)

# Write enum values to a CSV file
with open("./" + pins_csv_filename, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerows(
[
[value, value]
for value in enum_values
if value.startswith("P") or value.startswith("N")
]
)

with open("./" + pins_details_csv_filename, "w", newline="") as csv_file:
NC_pin = ["NC", 255, "CYHAL_GET_GPIO(CYHAL_PORT_31, 7)"] # non-existent port
csv_writer = csv.writer(csv_file)
csv_writer.writerow(NC_pin)
for pname, pdef in zip(pin_name, pin_defs):
val = self.get_pin_addr_helper(pdef)
csv_writer.writerow([pname, val, pdef.strip('"')])

print("// pins.csv and pins_details.csv generated successfully")
else:
print("// Error: pins.csv and pins_details.csv generation failed")


def main():
parser = argparse.ArgumentParser(
prog="make-pins.py",
usage="%(prog)s [options] [command]",
description="Generate board specific pin file",
description="Generate intermediate board specific pin csv files to be used by make-pins script",
)

parser.add_argument(
"-g",
"--gen-pin-for",
dest="pin_package_filename",
help="Specifies the pin package file from mtb assets to generate pins.csv",
)

parser.add_argument(
"-p",
"--save-pins-csv-at",
dest="pins_csv",
help="Specifies name of generated pins csv file",
)

parser.add_argument(
"-gaf",
"--save-pins-details-csv-at",
dest="pins_details_csv",
help="Specifies name of generated pins details csv file",
)

parser.add_argument(
Expand All @@ -196,7 +274,12 @@ def main():

pins = Pins()

pins.parse_af_file()
if args.pin_package_filename and args.pins_csv and args.pins_details_csv:
print("// Generating pins csv file")
print("// - --gen-pin-for {:s}".format(args.pin_package_filename))
pins.generate_pins_csv(args.pin_package_filename, args.pins_csv, args.pins_details_csv)

pins.parse_pin_details_file()

if args.hdr_filename and args.qstr_filename:
pins.parse_board_file()
Expand Down

0 comments on commit 1c17bda

Please sign in to comment.