From ed3168c68d991ecb96403b8cfda4718fa59faccd Mon Sep 17 00:00:00 2001 From: NikhitaR-IFX Date: Wed, 6 Sep 2023 18:00:05 +0530 Subject: [PATCH] ports/psoc6: Completed make-pins file and Makefile changes. Signed-off-by: NikhitaR-IFX --- ports/psoc6/Makefile | 45 +++- .../boards/CY8CPROTO-062-4343W/pins_af.csv | 104 ++++++++ ports/psoc6/boards/make-pins.py | 238 ++++++++++++++++++ ports/psoc6/{boards => }/psoc6_prefix.c | 0 4 files changed, 382 insertions(+), 5 deletions(-) create mode 100644 ports/psoc6/boards/CY8CPROTO-062-4343W/pins_af.csv rename ports/psoc6/{boards => }/psoc6_prefix.c (100%) mode change 100644 => 100755 diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index 98254878c9c2d..f30123c1e358c 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -233,11 +233,46 @@ export MTB_PIN_PACKAGES_PATH = ./mtb_shared/mtb-hal-cat1/release-v2.4.1/COMPONEN export PINS_CSV_PATH = $(BOARD_DIR)/ MAKE_PINS = boards/make-pins.py -make_pins: - $(info ) - $(info Executing make-pins.py to generate pin structure automatically) - $(Q)$(PYTHON) $(MAKE_PINS) --gen-pin-for cyhal_psoc6_02_124_bga.h --save-pins-csv-at pins.csv - +ifeq ($(BOARD), CY8CPROTO-062-4343W) +PIN_PACKAGE_FILE = cyhal_psoc6_02_124_bga.h +endif +ifeq ($(BOARD), CY8CPROTO-063-BLE) +PIN_PACKAGE_FILE = cyhal_psoc6_03_68_qfn.h #ToDo: Check the right file and replace +endif + +GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c +GEN_PINS_HDR = $(HEADER_BUILD)/pins.h +GEN_PINS_QSTR = $(BUILD)/pins_qstr.h +BOARD_PINS = pins.csv +BOARD_AF_PINS = pins_af.csv +PREFIX_FILE = psoc6_prefix.c +HEADER_BUILD = $(BUILD)/genhdr +AF_FILE = boards/$(BOARD)/pins_af.csv + +# Making OBJ use an order-only depenedency on the generated pins.h file +# has the side effect of making the pins.h file before we actually compile +# any of the objects. The normal dependency generation will deal with the +# case when pins.h is modified. But when it doesn't exist, we don't know +# which source files might need it. +$(OBJ): | $(GEN_PINS_HDR) +# Rule to generate pins.csv +$(BOARD_PINS) $(BOARD_AF_PINS): + @echo "Generating $@" + @$(PYTHON) $(MAKE_PINS) --gen-pin-for $(PIN_PACKAGE_FILE) --save-pins-csv-at $(BOARD_PINS) --save-pins-af-csv-at $(BOARD_AF_PINS) + +make_pins: $(GEN_PINS_SRC) $(GEN_PINS_HDR) $(GEN_PINS_QSTR) + +$(GEN_PINS_SRC) $(GEN_PINS_HDR) $(GEN_PINS_QSTR): $(BOARD_PINS) $(BOARD_AF_PINS) $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE) | $(HEADER_BUILD) + $(ECHO) "GEN $@" + $(Q) $(PYTHON) $(MAKE_PINS) --prefix $(PREFIX_FILE) --board $(BOARD_PINS) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) > $(GEN_PINS_SRC) + +$(HEADER_BUILD): + mkdir -p $(HEADER_BUILD) + +#make_pins: +# $(info ) +# $(info Executing make-pins.py to generate pin structure automatically) +# $(Q)$(PYTHON) $(MAKE_PINS) --gen-pin-for cyhal_psoc6_02_124_bga.h --save-pins-csv-at pins.csv --save-pins-af-csv-at pins_af.csv --board pins.csv --prefix psoc6_prefix.c --hdr $(BUILD)pins.h > $(GEN_PINS_SRC) help: @: diff --git a/ports/psoc6/boards/CY8CPROTO-062-4343W/pins_af.csv b/ports/psoc6/boards/CY8CPROTO-062-4343W/pins_af.csv new file mode 100644 index 0000000000000..def237a4a8c31 --- /dev/null +++ b/ports/psoc6/boards/CY8CPROTO-062-4343W/pins_af.csv @@ -0,0 +1,104 @@ +P0_0,0 +P0_1,1 +P0_2,2 +P0_3,3 +P0_4,4 +P0_5,5 +P1_0,8 +P1_1,9 +P1_2,10 +P1_3,11 +P1_4,12 +P1_5,13 +P2_0,16 +P2_1,17 +P2_2,18 +P2_3,19 +P2_4,20 +P2_5,21 +P2_6,22 +P2_7,23 +P3_0,24 +P3_1,25 +P3_2,26 +P3_3,27 +P3_4,28 +P3_5,29 +P4_0,32 +P4_1,33 +P5_0,40 +P5_1,41 +P5_2,42 +P5_3,43 +P5_4,44 +P5_5,45 +P5_6,46 +P5_7,47 +P6_0,48 +P6_1,49 +P6_2,50 +P6_3,51 +P6_4,52 +P6_5,53 +P6_6,54 +P6_7,55 +P7_0,56 +P7_1,57 +P7_2,58 +P7_3,59 +P7_4,60 +P7_5,61 +P7_6,62 +P7_7,63 +P8_0,64 +P8_1,65 +P8_2,66 +P8_3,67 +P8_4,68 +P8_5,69 +P8_6,70 +P8_7,71 +P9_0,72 +P9_1,73 +P9_2,74 +P9_3,75 +P9_4,76 +P9_5,77 +P9_6,78 +P9_7,79 +P10_0,80 +P10_1,81 +P10_2,82 +P10_3,83 +P10_4,84 +P10_5,85 +P10_6,86 +P10_7,87 +P11_0,88 +P11_1,89 +P11_2,90 +P11_3,91 +P11_4,92 +P11_5,93 +P11_6,94 +P11_7,95 +P12_0,96 +P12_1,97 +P12_2,98 +P12_3,99 +P12_4,100 +P12_5,101 +P12_6,102 +P12_7,103 +P13_0,104 +P13_1,105 +P13_2,106 +P13_3,107 +P13_4,108 +P13_5,109 +P13_6,110 +P13_7,111 +USBDP,112 +P14_0,112 +USBDM,113 +P14_1,113 diff --git a/ports/psoc6/boards/make-pins.py b/ports/psoc6/boards/make-pins.py index 71788aa5e2d11..830953f058f54 100755 --- a/ports/psoc6/boards/make-pins.py +++ b/ports/psoc6/boards/make-pins.py @@ -6,8 +6,73 @@ import re import os +# Helper function to get the deduce the port_pin address +def get_pin_addr_helper(pin_def): + # Regular expression pattern to extract port and pin numbers + 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) + + +class NamedPin(object): + def __init__(self, name, pin): + self._name = name + self._pin = pin + + def pin(self): + return self._pin + + def name(self): + return self._name + + +class Pin(object): + def __init__(self, name, pin_addr): + self._name = name + self._pin_addr = pin_addr + self._board_pin = False + + def cpu_pin_name(self): + return self._name + + def is_board_pin(self): + return self._board_pin + + def set_is_board_pin(self): + self._board_pin = True + + def print(self): + print( + "const machine_pin_obj_t pin_{:s}_obj = PIN({:s}, {:s});".format( + self._name, + self._name, + self._pin_addr, + ) + ) + print("") + + def print_header(self, hdr_file): + n = self.cpu_pin_name() + hdr_file.write("extern const machine_pin_obj_t pin_{:s}_obj;\n".format(n)) + hdr_file.write("#define pin_{:s} (&pin_{:s}_obj)\n".format(n, n)) + + def qstr_list(self): + return [self._name] + class Pins(object): + def __init__(self): + self.cpu_pins = [] # list of NamedPin objects + self.board_pins = [] # list of NamedPin objects + + def find_pin(self, cpu_pin_name): + for named_pin in self.cpu_pins: + pin = named_pin.pin() + if pin.cpu_pin_name() == cpu_pin_name: + return pin + def generate_pins_csv(self, pin_package_filename, pins_csv_filename): # Read the header file with open(os.environ.get("MTB_PIN_PACKAGES_PATH") + pin_package_filename, "r") as file: @@ -34,6 +99,116 @@ def generate_pins_csv(self, pin_package_filename, pins_csv_filename): else: print("Error: pins.csv generation failed") + def generate_af_pins_csv(self, pin_package_filename, pins_af_csv_filename): + # Read the header file + with open(os.environ.get("MTB_PIN_PACKAGES_PATH") + pin_package_filename, "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_def = re.findall(r"=\s*(.*?\))", enum_content) + # Write enum values to a CSV file + with open( + os.environ.get("PINS_CSV_PATH") + pins_af_csv_filename, "w", newline="" + ) as csv_file: + csv_writer = csv.writer(csv_file) + for pname, pdef in zip(pin_name, pin_def): + val = get_pin_addr_helper(pdef) + csv_writer.writerow([pname, val]) + + print("// pins_af.csv generated successfully") + else: + print("Error: pins_af.csv generation failed") + + # ToDo: Complete for alternate functions + def parse_af_file(self, filename): + with open(os.environ.get("PINS_CSV_PATH") + filename, "r") as csvfile: + rows = csv.reader(csvfile) + for row in rows: + try: + pin_name = row[0] + pin_addr = row[1] + except: + continue + pin = Pin(pin_name, pin_addr) + self.cpu_pins.append(NamedPin(pin_name, pin)) + + def parse_board_file(self, filename): + with open(os.environ.get("PINS_CSV_PATH") + filename, "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 print_named(self, label, named_pins): + print( + "STATIC const mp_rom_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{".format(label) + ) + for named_pin in named_pins: + pin = named_pin.pin() + if pin.is_board_pin(): + print( + " {{ MP_ROM_QSTR(MP_QSTR_{:s}), MP_ROM_PTR(&pin_{:s}_obj) }},".format( + named_pin.name(), pin.cpu_pin_name() + ) + ) + print("};") + print( + "MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);".format( + label, label + ) + ) + + def print(self): + for named_pin in self.cpu_pins: + pin = named_pin.pin() + if pin.is_board_pin(): + pin.print() + self.print_named("cpu", self.cpu_pins) + print("") + self.print_named("board", self.board_pins) + + def print_header(self, hdr_filename): + with open(hdr_filename, "wt") as hdr_file: + for named_pin in self.cpu_pins: + pin = named_pin.pin() + if pin.is_board_pin(): + pin.print_header(hdr_file) + + def print_qstr(self, qstr_filename): + with open(qstr_filename, "wt") as qstr_file: + qstr_set = set([]) + for named_pin in self.cpu_pins: + pin = named_pin.pin() + if pin.is_board_pin(): + qstr_set |= set(pin.qstr_list()) + qstr_set |= set([named_pin.name()]) + for named_pin in self.board_pins: + qstr_set |= set([named_pin.name()]) + for qstr in sorted(qstr_set): + # cond_var = None + + # ToDO: For next iteration + """if qstr.startswith("AF"): + af_words = qstr.split("_") + cond_var = conditional_var(af_words[1]) + print_conditional_if(cond_var, file=qstr_file)""" + print("Q({})".format(qstr), file=qstr_file) + def main(): parser = argparse.ArgumentParser( @@ -57,6 +232,46 @@ def main(): default="pins.csv", ) + parser.add_argument( + "-gaf", + "--save-pins-af-csv-at", + dest="pins_af_csv", + help="Specifies name of generated alternate functions pins csv file", + default="pins_af.csv", + ) + + parser.add_argument( + "-b", + "--board", + dest="board_filename", + help="Specifies the generated board pins csv file", + default="pins.csv", + ) + + parser.add_argument( + "-pr", + "--prefix", + dest="prefix_filename", + help="Specifies beginning portion of generated pins file", + default="psoc6_prefix.c", + ) + + parser.add_argument( + "-q", + "--qstr", + dest="qstr_filename", + help="Specifies name of generated qstr header file", + default="build/pins_qstr.h", + ) + + parser.add_argument( + "-r", + "--hdr", + dest="hdr_filename", + help="Specifies name of generated pin header file", + default="build/pins.h", + ) + args = parser.parse_args(sys.argv[1:]) pins = Pins() @@ -67,6 +282,29 @@ def main(): print("// - --save-pins-csv-at {:s}".format(args.pins_csv)) pins.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 with following parsed arguments") + print("// - --save-pins-csv-at {:s}".format(args.pins_af_csv)) + pins.generate_af_pins_csv(args.pin_package_filename, args.pins_af_csv) + + if args.pins_af_csv: + print("// --save-pins-af-csv-at {:s}".format(args.pins_af_csv)) + pins.parse_af_file(args.pins_af_csv) + + if args.board_filename: + print("// --board {:s}".format(args.board_filename)) + pins.parse_board_file(args.board_filename) + + if args.prefix_filename: + print("// --prefix {:s}".format(args.prefix_filename)) + print("") + with open(args.prefix_filename, "r") as prefix_file: + print(prefix_file.read()) + + pins.print() + pins.print_header(args.hdr_filename) + pins.print_qstr(args.qstr_filename) + if __name__ == "__main__": main() diff --git a/ports/psoc6/boards/psoc6_prefix.c b/ports/psoc6/psoc6_prefix.c old mode 100644 new mode 100755 similarity index 100% rename from ports/psoc6/boards/psoc6_prefix.c rename to ports/psoc6/psoc6_prefix.c