Skip to content

Commit

Permalink
ports/psoc6: Completing make_pins changes.
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 9, 2023
1 parent 417ca47 commit 3bcfa46
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 266 deletions.
55 changes: 45 additions & 10 deletions ports/psoc6/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,35 @@ ifeq ($(wildcard $(BOARD_DIR)/.),)
$(error Invalid BOARD specified)
endif

# Supporting variables for make-pins.py script execution
MAKE_PINS_CSV = ./boards/make-pins-csv.py
MAKE_PINS = ./boards/make-pins.py

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_01_116_bga_ble.h #ToDo: Check the right file and replace
endif

#ToDo: Post adding af functionality, refactor to minimize dependent variables in py script if possible
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
PREFIX_FILE := ./psoc6_prefix.c

# get path to this file
MPY_PATH_TO_MAIN_MAKEFILE := $(abspath $(lastword $(MAKEFILE_LIST)))
MPY_DIR_OF_MAIN_MAKEFILE := $(dir $(MPY_PATH_TO_MAIN_MAKEFILE))


# Files that are generated and needed before the QSTR build.
QSTR_GENERATED_HEADERS = build/pins_qstr.h
# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h
QSTR_GLOBAL_DEPENDENCIES += $(BOARD_DIR)/mpconfigboard.h
QSTR_DEFS = qstrdefsport.h $(QSTR_GENERATED_HEADERS)
QSTR_GLOBAL_DEPENDENCIES += $(BOARD_DIR)/mpconfigboard.h $(QSTR_GENERATED_HEADERS)

MICROPY_FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py
FROZEN_MANIFEST ?= $(MICROPY_FROZEN_MANIFEST)
Expand Down Expand Up @@ -94,6 +114,22 @@ endif

$(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)
$(MAKE) make_pins

make_pins: $(GEN_PINS_SRC) $(GEN_PINS_HDR) $(GEN_PINS_QSTR)
$(GEN_PINS_SRC) $(GEN_PINS_HDR) $(GEN_PINS_QSTR): $(BOARD_PINS) $(MAKE_PINS) $(BOARD_AF_PINS) | $(HEADER_BUILD)
$(ECHO) "GEN $@"
$(PYTHON) $(MAKE_PINS) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) > $(GEN_PINS_SRC)

clean_csv:
rm -rf $(BOARD_PINS) $(BOARD_AF_PINS)

$(OBJ): | $(GEN_PINS_HDR)


# Flags for optional C++ source code
CXXFLAGS += $(filter-out -std=c99,$(CFLAGS))
Expand Down Expand Up @@ -176,7 +212,7 @@ OBJ += $(addprefix $(BUILD)/, $(MOD_SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_ASM:.s=.o))
OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))

OBJ += $(GEN_PINS_SRC:.c=.o)
# switch for debug mode, also added to mpconfigport.h
# TODO: keep the more suitable one, delete the other
MP_LOGGER_DEBUG ?= 0
Expand All @@ -185,7 +221,6 @@ ifeq ($(MP_LOGGER_DEBUG), 1)
CFLAGS += -DMICROPY_LOGGER_DEBUG=1
endif


$(MPY_MAIN_BUILD_DIR)/firmware.elf: $(OBJ) $(MPY_MTB_LIBRARIES) $(LIBS)
$(info )
$(info Linking $@ $^ $(LIBS) ...)
Expand All @@ -202,12 +237,9 @@ $(MPY_MAIN_BUILD_DIR)/firmware.hex: $(MPY_MAIN_BUILD_DIR)/firmware.elf
# include adapter makefile
include mtb-libs/makefile_mtb.mk

# include py core make definitions
include $(TOP)/py/mkrules.mk

MPY_CROSS_FLAGS += -march=armv7m

build: mtb_get_build_flags $(MPY_PATH_TO_MAIN_MAKEFILE) $(MPY_MAIN_BUILD_DIR)/firmware.hex
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 Expand Up @@ -298,4 +330,7 @@ help:
$(info - mtb_set_bsp Set the board as active in the ModusToolbox project. )


.PHONY: build test test_multi program_multi help
.PHONY: build test test_multi program_multi help make_pins_csv make_pins clean_csv

# include py core make definitions
include $(TOP)/py/mkrules.mk
135 changes: 135 additions & 0 deletions ports/psoc6/boards/make-pins-csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from __future__ import print_function
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")]
)
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_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")


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()
Loading

0 comments on commit 3bcfa46

Please sign in to comment.