Skip to content

Commit

Permalink
ports/psoc6: Complete generation of pins_af.csv.
Browse files Browse the repository at this point in the history
Signed-off-by: NikhitaR-IFX <Nikhita.Rajasekhar@infineon.com>
  • Loading branch information
NikhitaR-IFX committed Oct 6, 2023
1 parent 4997bdf commit 73fe6db
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 54 deletions.
5 changes: 3 additions & 2 deletions ports/psoc6/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ $(info Compiling in $(CONFIG) mode !)
# Rule to generate pins.csv
make_pins_csv:
@echo "Generating $@"
$(PYTHON) $(MAKE_PINS_CSV) --gen-pin-for $(PIN_PACKAGE_FILE) --save-pins-csv-at $(BOARD_PINS) --save-pins-af-csv-at $(BOARD_AF_PINS)
$(PYTHON) $(MAKE_PINS_CSV) --gen-pin-for $(PIN_PACKAGE_FILE) --gpio-file $(GPIO_MTB_HEADER_FILE) --save-pins-csv-at $(BOARD_PINS) --save-pins-af-csv-at $(BOARD_AF_PINS)
$(MAKE) make_pins

make_pins: $(GEN_PINS_SRC) $(GEN_PINS_HDR) $(GEN_PINS_QSTR)
Expand Down Expand Up @@ -232,7 +232,8 @@ include mtb-libs/makefile_mtb.mk

MPY_CROSS_FLAGS += -march=armv7m

build: mtb_get_build_flags make_pins_csv $(MPY_PATH_TO_MAIN_MAKEFILE) $(MPY_MAIN_BUILD_DIR)/firmware.hex clean_csv
build: mtb_get_build_flags make_pins_csv $(MPY_PATH_TO_MAIN_MAKEFILE) $(MPY_MAIN_BUILD_DIR)/firmware.hex
#clean_csv

all: build

Expand Down
1 change: 1 addition & 0 deletions ports/psoc6/boards/CY8CPROTO-062-4343W/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ BOARD_VERSION=release-v4.0.0

# Variables to support make-pins
PIN_PACKAGE_FILE = cyhal_psoc6_02_124_bga.h
GPIO_MTB_HEADER_FILE = gpio_psoc6_02_124_bga.h #ToDo: Remove when extraction from bsp.mk is added
3 changes: 2 additions & 1 deletion ports/psoc6/boards/CY8CPROTO-063-BLE/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ MICROPY_PSOC6_SSL_MBEDTLS = 0
BOARD_VERSION=release-v4.2.0

# Variables to support make-pins
PIN_PACKAGE_FILE = cyhal_psoc6_01_116_bga_ble.h
PIN_PACKAGE_FILE = cyhal_psoc6_01_116_bga_ble.h
GPIO_MTB_HEADER_FILE = gpio_psoc6_02_124_bga.h #ToDo: Get the right file name
174 changes: 124 additions & 50 deletions ports/psoc6/boards/make-pins-csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
import os


mtb_root_dir = "./mtb_shared"
mtb_device_header = "./mtb_shared/mtb-pdl-cat1/release-v3.6.0/devices/COMPONENT_CAT1A/include/"
mtb_lib_log_file = "./mtb-libs/deps/locking_commit.log"


def get_mtb_hal_release_version():
with open(mtb_lib_log_file, "r") as f:
log_file = f.read()
mtb_hal_cat1_version = log_file.split("mtb-hal-cat1 ")[1].split("\n")[0]
return mtb_hal_cat1_version


def get_mtb_pdl_release_version():
with open(mtb_lib_log_file, "r") as f:
log_file = f.read()
mtb_pdl_cat1_version = log_file.split("mtb-pdl-cat1 ")[1].split("\n")[0]
return mtb_pdl_cat1_version


def get_pin_addr_helper(pin_def):
pattern = r"CYHAL_PORT_(\d+),\s*(\d+)"
match = re.search(pattern, pin_def)
Expand All @@ -14,26 +33,29 @@ def get_pin_addr_helper(pin_def):
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
def get_gpio_hdr_path(filename):
file_path = (
f"./mtb_shared/mtb-pdl-cat1/{get_mtb_pdl_release_version()}/devices/COMPONENT_CAT1A/include/{filename}"
if get_mtb_pdl_release_version()
else None
)
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)
def get_pin_package_path(filename):
file_path = (
f"./mtb_shared/mtb-hal-cat1/{get_mtb_hal_release_version()}/COMPONENT_CAT1A/include/pin_packages/{filename}"
if get_mtb_hal_release_version()
else None
)
if os.path.isfile(file_path):
return file_path
return None

if file_path is None:
sys.exit(1)

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

Expand All @@ -45,51 +67,94 @@ def generate_pins_csv(pin_package_filename, pins_csv_filename):
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")]
)
print("// pins.csv generated successfully")
pin_name = re.findall(r"\b(?!NC\b)(P\w+)\s*=", enum_content)
pin_def = re.findall(r"=\s*(.*?\))", enum_content)
pin_addr = [get_pin_addr_helper(pin) for pin in pin_def]

return pin_name, pin_addr, pin_def
else:
print("// Error: pins.csv generation failed")
return []


def generate_af_pins_csv(pin_package_filename, pins_af_csv_filename):
file_path = get_pin_package_path(pin_package_filename)
def _get_pin_af_details(gpio_hdr_file_path):
with open(gpio_hdr_file_path, "r") as file:
content = file.read()

enum_start = content.index("/* HSIOM Connections */\ntypedef enum\n{\n")
enum_end = content.index("} en_hsiom_sel_t;") + len("} en_hsiom_sel_t;")
enum_content = content[enum_start:enum_end]

# extract the enum values
enum_values = re.findall(r"(\w+)\s*=\s*([\-\d]+)", enum_content)

## create dictionary from enum values
enum_content1 = {name: int(value) for name, value in enum_values}

hsiom_name = [name for name in enum_content1.keys() if name.startswith("HSIOM_SEL_ACT_")]

## Create a list of tuples storing the name and number for each "HSIOM_SEL_ACT_" value
hsiom_defs = [(name, enum_content1[name]) for name in hsiom_name]

pins = {}
for key in enum_content1:
value = enum_content1[key]
for i in range(len(hsiom_defs)):
if hsiom_defs[i][1] == value:
pin = key.split("_")[0] + "_" + key.split("_")[1]
if pin not in pins:
pins[pin] = [" "] * len(hsiom_defs)
if i < len(pins[pin]):
pins[pin][i] = key
break
pins = {key: value for key, value in pins.items() if key.startswith("P")}

return pins


def _write_to_csv(data, csv_filename):
with open("./" + csv_filename, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerows(data)


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("}")
pin_details = _get_pin_def(file_path)

if enum_start != -1 and enum_end != -1:
enum_content = content[enum_start:enum_end]
csv_data = [[pin_name, pin_name] for pin_name in pin_details[0]]

# 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("./" + pins_af_csv_filename, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
for pname, pdef in zip(pin_name, 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")
_write_to_csv(csv_data, pins_csv_filename)


def generate_af_pins_csv(pin_package_filename, gpio_mtb_header_filename, pins_af_csv_filename):
pin_package_file_path = get_pin_package_path(pin_package_filename)
gpio_hdr_file_path = get_gpio_hdr_path(gpio_mtb_header_filename)

if pin_package_file_path is None or pin_package_file_path is None:
sys.exit(1)

pin_details = _get_pin_def(pin_package_file_path)

pin_af_details = _get_pin_af_details(gpio_hdr_file_path)

csv_data = [
(
pin_details[0][i],
pin_details[1][i],
pin_details[2][i],
pin_af_details[pin_details[0][i]],
)
for i in range(len(pin_af_details))
]

_write_to_csv(csv_data, pins_af_csv_filename)


def main():

parser = argparse.ArgumentParser(
prog="make-pins-csv.py",
usage="%(prog)s [options] [command]",
Expand All @@ -103,6 +168,13 @@ def main():
help="Specifies the pin package file from mtb assets to generate pins.csv",
)

parser.add_argument(
"-gp",
"--gpio-file",
dest="gpio_mtb_header_filename",
help="Specifies the gpio header file from mtb assets to generate pins_af.csv",
)

parser.add_argument(
"-p",
"--save-pins-csv-at",
Expand All @@ -125,10 +197,12 @@ def main():
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:
if args.pin_package_filename and args.gpio_mtb_header_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)
generate_af_pins_csv(
args.pin_package_filename, args.gpio_mtb_header_filename, args.pins_af_csv
)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion ports/psoc6/boards/make-pins.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def qstr_list(self):
return [self._name]

def print_const_table_entry(self):
print('{{{{ &machine_pin_type}},{:s}, "{:s}"}},'.format(self._pin_exp, self._name))
print('{{{{ &machine_pin_type}},{:s}, "{:s}"}},'.format(self._pin_addr, self._name))


class Pins(object):
Expand Down

0 comments on commit 73fe6db

Please sign in to comment.