diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index 652234508a9a4..fafa2ac6dae48 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -100,18 +100,13 @@ GEN_PINS_SRC := $(BUILD)/pins_$(BOARD).c 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 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) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) > $(GEN_PINS_SRC) # Flags for optional C++ source code CXXFLAGS += $(filter-out -std=c99,$(CFLAGS)) diff --git a/ports/psoc6/boards/make-pins-csv.py b/ports/psoc6/boards/make-pins-csv.py deleted file mode 100644 index 7f4d720eaf6ae..0000000000000 --- a/ports/psoc6/boards/make-pins-csv.py +++ /dev/null @@ -1,147 +0,0 @@ -from __future__ import print_function -from itertools import islice -import argparse -import sys -import csv -import re -import os - - -def get_pin_addr_helper(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(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(pin_package_filename, pins_csv_filename): - file_path = 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) - # 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") - ] - ) - print("// pins.csv generated successfully") - else: - print("// Error: pins.csv generation failed") - - -def generate_af_pins_csv(pin_package_filename, pins_af_csv_filename): - file_path = 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 - pin_name = re.findall(r"\b(?!NC\b)(\w+)\s*=", enum_content) - # pin_name = re.findall(r"\b(\w+)\s*=", enum_content) - pin_def = re.findall(r"=\s*(.*?\))", enum_content) - # pin_def.insert(0, "255") - # print(pin_def) - - # Write enum values to a CSV file - with open("./" + pins_af_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_def): - # for pname, pdef in zip(islice(pin_name, 1, None), pin_def): - # if pin_name[pname].startswith('P'): - val = get_pin_addr_helper(pdef) - csv_writer.writerow([pname, val, pdef.strip('"')]) - - print("// pins_af.csv generated successfully") - else: - print("Error: pins_af.csv generation failed") - - -def main(): - parser = argparse.ArgumentParser( - prog="make-pins-csv.py", - usage="%(prog)s [options] [command]", - 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-af-csv-at", - dest="pins_af_csv", - help="Specifies name of generated alternate functions pins csv file", - ) - - args = parser.parse_args(sys.argv[1:]) - - if args.pin_package_filename and args.pins_csv: - print("// Generating pins csv file") - print("// - --gen-pin-for {:s}".format(args.pin_package_filename)) - print("// - --save-pins-csv-at {:s}".format(args.pins_csv)) - generate_pins_csv(args.pin_package_filename, args.pins_csv) - - if args.pin_package_filename and args.pins_af_csv: - print("// Generating alternate functions pins csv file") - print("// - --save-pins-csv-at {:s}".format(args.pins_af_csv)) - generate_af_pins_csv(args.pin_package_filename, args.pins_af_csv) - - -if __name__ == "__main__": - main() diff --git a/ports/psoc6/boards/make-pins.py b/ports/psoc6/boards/make-pins.py index c6863948bf9b2..7031df29c3463 100755 --- a/ports/psoc6/boards/make-pins.py +++ b/ports/psoc6/boards/make-pins.py @@ -1,7 +1,7 @@ from __future__ import print_function +from itertools import islice import argparse import sys -import csv import re import os @@ -23,6 +23,28 @@ def name(self): return self._name +class PinsTable(object): + def __init__(self): + self.pins = [] + + def add_pin(self, pin_name, pin_addr): + self.pins.append([pin_name, pin_addr]) + + def get_pins(self): + return self.pins + + +class PinsDetailsTable(object): + def __init__(self): + self.pins_details = [] + + def add_pin_details(self, pin_name, pin_addr, pin_exp): + self.pins_details.append([pin_name, pin_addr, pin_exp]) + + def get_pins_details(self): + return self.pins_details + + class Pin(object): def __init__(self, name, pin_addr, pin_exp): self._name = name @@ -42,15 +64,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)) @@ -98,34 +111,22 @@ 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: - rows = csv.reader(csvfile) - for row in rows: - try: - pin_name = row[0] - pin_addr = row[1] - pin_exp = row[2].strip('"') - except: - continue - pin = Pin(pin_name, pin_addr, pin_exp) - self.cpu_pins.append(NamedPin(pin_name, pin)) - - def parse_board_file(self): - # Assuming the same path has required board files - with open("./pins.csv", "r") as csvfile: - rows = csv.reader(csvfile) - for row in rows: - try: - board_pin_name = row[0] - cpu_pin_name = row[1] - except: - continue - pin = self.find_pin(cpu_pin_name) - if pin: - pin.set_is_board_pin() - self.board_pins.append(NamedPin(board_pin_name, pin)) + def parse_pin_details_table(self, pins_details_table): + for pin in pins_details_table.get_pins_details(): + pin_name = pin[0] + pin_addr = pin[1] + pin_exp = pin[2].strip('"') + pin = Pin(pin_name, pin_addr, pin_exp) + self.cpu_pins.append(NamedPin(pin_name, pin)) + + def parse_board_table(self, pins_table): + for pin in pins_table.get_pins(): + board_pin_name = pin[0] + cpu_pin_name = pin[1] + pin = self.find_pin(cpu_pin_name) + if pin: + pin.set_is_board_pin() + self.board_pins.append(NamedPin(board_pin_name, pin)) def print_named(self, label, named_pins): print( @@ -167,15 +168,73 @@ def print_qstr(self, qstr_filename): for named_pin in self.board_pins: qstr_set |= set([named_pin.name()]) for qstr in sorted(qstr_set): - # 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_table(self, pin_pacakge_file_name): + file_path = self.get_pin_package_path(pin_pacakge_file_name) + + if file_path is None: + sys.exit(1) + + with open(file_path, "r") as file: + content = file.read() + + 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] + 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) + + pins_table = PinsTable() + pins_details_table = PinsDetailsTable() + + for value in enum_values: + if value.startswith("P") or value.startswith("N"): + pins_table.add_pin(value, value) + + pins_details_table.add_pin_details("NC", 255, "CYHAL_GET_GPIO(CYHAL_PORT_31, 7)") + for pname, pdef in zip(pin_name, pin_defs): + val = self.get_pin_addr_helper(pdef) + pins_details_table.add_pin_details(pname, val, pdef.strip('"')) + return pins_table, pins_details_table + else: + print("// Error: pins table and pins details table generation failed") + def main(): parser = argparse.ArgumentParser( prog="make-pins.py", usage="%(prog)s [options] [command]", - description="Generate board specific pin file", + description="Generate board specific pin details", + ) + + parser.add_argument( + "-g", + "--gen-pin-for", + dest="pin_package_filename", + help="Specifies the pin package file from mtb assets to generate pin details", ) parser.add_argument( @@ -196,10 +255,15 @@ def main(): pins = Pins() - pins.parse_af_file() + if args.pin_package_filename: + print("// Generating pins table") + print("// - --gen-pin-for {:s}".format(args.pin_package_filename)) + pins_table, pins_details_table = pins.generate_pins_table(args.pin_package_filename) + + pins.parse_pin_details_table(pins_details_table) if args.hdr_filename and args.qstr_filename: - pins.parse_board_file() + pins.parse_board_table(pins_table) pins.update_num_cpu_pins() pins.print_const_table() pins.print()