diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index afc9aaf537a6e..ebaae573938d2 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -192,6 +192,9 @@ $(MPY_MAIN_BUILD_DIR)/firmware.elf: $(OBJ) $(MPY_MTB_LIBRARIES) $(LIBS) $(MPY_MAIN_BUILD_DIR)/firmware.hex: $(MPY_MAIN_BUILD_DIR)/firmware.elf $(Q) $(OBJCOPY) -O ihex $^ $@ +MAKE_PINS = boards/make-pins.py +BOARD_PINS = $(BOARD_DIR)/pins.csv +PREFIX_FILE = boards/ra_pin_prefix.c # include adapter makefile include mtb-libs/makefile_mtb.mk diff --git a/ports/psoc6/boards/cy8c62xa_af.csv b/ports/psoc6/boards/cy8c62xa_af.csv new file mode 100644 index 0000000000000..b96be9ba6bc86 --- /dev/null +++ b/ports/psoc6/boards/cy8c62xa_af.csv @@ -0,0 +1,3 @@ +TESTPort,,AF0,AF1,AF2,AF3,AF4,AF5,AF6,AF7,AF8,AF9,AF10,AF11,AF12,AF13,AF14,AF15, +,,SYS,TIM1/2,TIM3/4/5,TIM8/9/10/11,I2C1/2/3,SPI1/SPI2/I2S2/I2S2ext,SPI3/I2Sext/I2S3,USART1/2/3/I2S3ext,UART4/5/USART6,CAN1/CAN2/TIM12/13/14,OTG_FS/OTG_HS,ETH,FSMC/SDIO/OTG_FS,DCMI,,,ADC +PortA,PA0,,TIM2_CH1/TIM2_ETR,TIM5_CH1,TIM8_ETR,,,,USART2_CTS,UART4_TX,,,ETH_MII_CRS,,,,EVENTOUT,ADC123_IN0 diff --git a/ports/psoc6/boards/make_pins.py b/ports/psoc6/boards/make_pins.py new file mode 100644 index 0000000000000..d2713488e97a3 --- /dev/null +++ b/ports/psoc6/boards/make_pins.py @@ -0,0 +1,151 @@ +from __future__ import print_function + +import argparse +import sys +import csv + +class Pin: + """Holds the information associated with a pin.""" + + def __init__(self): + def __init__(self, pin): + self.port = port + self.pin = pin + self.alt_fn = [] + self.alt_fn_count = 0 + +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 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() and not named_pin.is_hidden(): + 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 parse_af_file(self, filename, pinname_col, af_col): + with open(filename, "r") as csvfile: + rows = csv.reader(csvfile) + for row in rows: + try: + (port_num, pin_num) = parse_port_pin(row[pinname_col]) + except: + continue + pin = Pin(port_num, pin_num) + for af_idx in range(af_col, len(row)): + if af_idx < af_col + 16: + pin.parse_af(af_idx - af_col, row[af_idx]) + elif af_idx == af_col + 16: + pin.parse_adc(row[af_idx]) + self.cpu_pins.append(NamedPin(pin.cpu_pin_name(), pin)) + + + def parse_board_file(self, filename): + with open(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 main(): + parser = argparse.ArgumentParser( + prog="make-pins.py", + usage="%(prog)s [options] [command]", + description="Generate board specific pin file", + ) + + parser.add_argument( + "-a", + "--af", + dest="af_filename", + help="Specifies the alternate function file for the chip", + default="cy8c62xa_af.csv", + ) + + parser.add_argument( + "-b", "--board", dest="board_filename", help="Specifies the board file", default="pins.csv" + ) + + 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() + + print("// This file was automatically generated by make-pins.py") + print("//") + + if args.af_filename: + print("// --af {:s}".format(args.af_filename)) + pins.parse_af_file(args.af_filename, 1, 2) + + '''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() + + \ No newline at end of file diff --git a/ports/psoc6/boards/pins.csv b/ports/psoc6/boards/pins.csv new file mode 100644 index 0000000000000..aec2dc1d7e605 --- /dev/null +++ b/ports/psoc6/boards/pins.csv @@ -0,0 +1 @@ +P0_0, P0_0 \ No newline at end of file diff --git a/ports/psoc6/boards/psoc6_prefix.c b/ports/psoc6/boards/psoc6_prefix.c new file mode 100644 index 0000000000000..50be0d050b13b --- /dev/null +++ b/ports/psoc6/boards/psoc6_prefix.c @@ -0,0 +1,15 @@ +// ra_pin_prefix.c becomes the initial portion of the generated pins file. + +#include + +#include "py/obj.h" +#include "py/mphal.h" +#include "pins.h" + +#define PIN(p_name, p_pin, p_ad) \ + { \ + { &machine_pin_type }, \ + .name = MP_QSTR_##p_name, \ + .pin = p_pin, \ + .ad = p_ad, \ + } diff --git a/ports/psoc6/modules/machine/pins.h b/ports/psoc6/modules/machine/pins.h index 1367a325872e3..b4bd84ffa7d4c 100644 --- a/ports/psoc6/modules/machine/pins.h +++ b/ports/psoc6/modules/machine/pins.h @@ -135,10 +135,19 @@ // Add all machine pin objects - GPIO , I2C, ADC etc. typedef struct _machine_pin_obj_t { + mp_obj_base_t base; + qstr name; + uint32_t pin; + uint8_t num_af; + //const pin_af_obj_t *af; + //const pin_ad_obj_t *ad; +} machine_pin_obj_t; + +/*typedef struct _machine_pin_obj_t { mp_obj_base_t base; uint32_t pin_addr; char *pin_name; -} machine_pin_obj_t; +} machine_pin_obj_t;*/ // Function Prototypes to support interaction between c<->py int pin_find(mp_obj_t obj);