diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index 046e2762495e5..f6becb3f6a4de 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -267,8 +267,8 @@ test_multi: $(Q) cd ../../tests ; ./run-multitests.py -i pyb:$(DEV0) -i pyb:$(DEV1) $(MULTI_TESTS) -$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) - $(call compile_c) +#$(GEN_PINS_SRC:.c=.o): $(GEN_PINS_SRC) +# $(call compile_c) #make_pins: # @echo "HERE!!!" diff --git a/ports/psoc6/boards/CY8CPROTO-062-4343W/mpconfigboard.h b/ports/psoc6/boards/CY8CPROTO-062-4343W/mpconfigboard.h index 68266f143618b..91827be8d2a4f 100644 --- a/ports/psoc6/boards/CY8CPROTO-062-4343W/mpconfigboard.h +++ b/ports/psoc6/boards/CY8CPROTO-062-4343W/mpconfigboard.h @@ -1,5 +1,5 @@ // Board and hardware specific configuration -#define MICROPY_HW_MCU_NAME "PSoC62" +#define MICROPY_HW_MCU_NAME "PSoC62" #define MICROPY_HW_BOARD_NAME "CY8CPROTO-062-4343W" #define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "CY8C-062-4343W" diff --git a/ports/psoc6/boards/make-pins-csv.py b/ports/psoc6/boards/make-pins-csv.py index d94ca493614d9..23b27ccf55f0e 100644 --- a/ports/psoc6/boards/make-pins-csv.py +++ b/ports/psoc6/boards/make-pins-csv.py @@ -47,11 +47,12 @@ def generate_pins_csv(pin_package_filename, pins_csv_filename): # 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]) + 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") @@ -80,6 +81,8 @@ def generate_af_pins_csv(pin_package_filename, pins_af_csv_filename): 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): + print(pname, pdef) + # if pin_name[pname].startswith('P'): val = get_pin_addr_helper(pdef) csv_writer.writerow([pname, val]) diff --git a/ports/psoc6/boards/make-pins.py b/ports/psoc6/boards/make-pins.py index 0e04aab9d0b55..c0993d9a77547 100755 --- a/ports/psoc6/boards/make-pins.py +++ b/ports/psoc6/boards/make-pins.py @@ -22,7 +22,7 @@ class Pin(object): def __init__(self, name, pin_addr): self._name = name self._pin_addr = pin_addr - # self._board_pin = False + self._board_pin = False def cpu_pin_name(self): return self._name @@ -33,24 +33,35 @@ def is_board_pin(self): def set_is_board_pin(self): self._board_pin = True + def set_board_index(self, index): + self.board_index = index + def print(self): - print( + """print( "const machine_pin_obj_t pin_{:s}_obj = PIN({:s}, {:s});".format( self._name, self._name, self._pin_addr, ) - ) - print("") + )""" + # 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)) + # hdr_file.write("#define pin_{:s} (&pin_{:s}_obj)\n".format(n, n)) def qstr_list(self): return [self._name] + def print_const_table_entry(self): + print( + # " {{{{&machine_pin_type}}, PIN({:s}, {:s}), \"{:s}\" }},".format( + # self._name, self._pin_addr, self._name + # ) + '{{{{ &machine_pin_type}}, PIN_{:s}, "{:s}"}},'.format(self._name, self._name) + ) + class Pins(object): def __init__(self): @@ -64,6 +75,23 @@ def find_pin(self, cpu_pin_name): if pin.cpu_pin_name() == cpu_pin_name: return pin + def print_const_table(self): + num_cpu_pins = 0 + for named_pin in self.cpu_pins: + pin = named_pin.pin() + if pin.is_board_pin(): + pin.set_board_index(num_cpu_pins) + num_cpu_pins += 1 + print("") + print("const uint8_t machine_pin_num_of_cpu_pins = {:d};".format(num_cpu_pins)) + print("") + print("const machine_pin_obj_t machine_pin_obj[{:d}] = {{".format(num_cpu_pins)) + for named_pin in self.cpu_pins: + pin = named_pin.pin() + if pin.is_board_pin(): + pin.print_const_table_entry() + print("};") + # ToDo: Complete for alternate functions def parse_af_file(self): with open("./pins_af.csv", "r") as csvfile: @@ -97,14 +125,16 @@ def print_named(self, label, named_pins): print( "STATIC const mp_rom_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{".format(label) ) + num_cpu_pins = 0 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() + " {{ MP_ROM_QSTR(MP_QSTR_{:s}), MP_ROM_PTR(&machine_pin_obj[{:d}]) }},".format( + named_pin.name(), num_cpu_pins ) ) + num_cpu_pins += 1 print("};") print( "MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);".format( @@ -191,6 +221,7 @@ def main(): if args.hdr_filename and args.qstr_filename: pins.parse_board_file() + pins.print_const_table() pins.print() pins.print_header(args.hdr_filename) pins.print_qstr(args.qstr_filename) diff --git a/ports/psoc6/modules/machine/pins.c b/ports/psoc6/modules/machine/pins.c index 8f9a7e14cf070..4d108465e1a01 100644 --- a/ports/psoc6/modules/machine/pins.c +++ b/ports/psoc6/modules/machine/pins.c @@ -6,6 +6,36 @@ #include "pins.h" +extern const uint8_t machine_pin_num_of_cpu_pins; + +const machine_pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { + const mp_map_t *named_map = &named_pins->map; + mp_map_elem_t *named_elem = mp_map_lookup((mp_map_t *)named_map, name, MP_MAP_LOOKUP); + if (named_elem != NULL && named_elem->value != MP_OBJ_NULL) { + return MP_OBJ_TO_PTR(named_elem->value); + } + return NULL; +} + +// C API used to convert a user-supplied pin name into an ordinal pin number. +/*const machine_pin_obj_t *pin_find(mp_obj_t user_obj) { + const machine_pin_obj_t *pin_obj; + + // If a pin was provided, then use it + if (mp_obj_is_type(user_obj, &machine_pin_type)) { + pin_obj = MP_OBJ_TO_PTR(user_obj); + return pin_obj; + } + + // See if the pin name matches a cpu pin + pin_obj = pin_find_named_pin(&pin_cpu_pins_locals_dict, user_obj); + if (pin_obj) { + return pin_obj; + } + + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Pin(%s) doesn't exist"), mp_obj_str_get_str(user_obj)); +}*/ + // Function definitions // helper function to translate pin_name(string) into machine_pin_obj_t index. int pin_find(mp_obj_t pin) { @@ -17,7 +47,7 @@ int pin_find(mp_obj_t pin) { // Search by name size_t slen; const char *s = mp_obj_str_get_data(pin, &slen); - for (int i = 0; i < MP_ARRAY_SIZE(machine_pin_obj); i++) { + for (int i = 0; i < machine_pin_num_of_cpu_pins; i++) { if (slen == strlen(machine_pin_obj[i].pin_name) && strncmp(s, machine_pin_obj[i].pin_name, slen) == 0) { wanted_pin = i; break; @@ -25,7 +55,7 @@ int pin_find(mp_obj_t pin) { } } - if (!(0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj))) { + if (!(0 <= wanted_pin && wanted_pin < machine_pin_num_of_cpu_pins)) { mp_raise_ValueError(MP_ERROR_TEXT("invalid pin: Pin not defined!")); } @@ -163,3 +193,10 @@ int pin_addr_by_name(mp_obj_t pin) { {{&machine_pin_type}, PIN_USBDP, "USBDP"}, {{&machine_pin_type}, PIN_USBDM, "USBDM"}, };*/ + +MP_DEFINE_CONST_OBJ_TYPE( + pin_cpu_pins_obj_type, + MP_QSTR_cpu, + MP_TYPE_FLAG_NONE, + locals_dict, &pin_cpu_pins_locals_dict + ); diff --git a/ports/psoc6/modules/machine/pins.h b/ports/psoc6/modules/machine/pins.h index 3d3423b69d22e..75a32ff19ddf1 100644 --- a/ports/psoc6/modules/machine/pins.h +++ b/ports/psoc6/modules/machine/pins.h @@ -129,29 +129,34 @@ #define PIN_P13_5 CYHAL_GET_GPIO(CYHAL_PORT_13, 5)// !< Port 13 Pin 5 #define PIN_P13_6 CYHAL_GET_GPIO(CYHAL_PORT_13, 6)// !< Port 13 Pin 6 #define PIN_P13_7 CYHAL_GET_GPIO(CYHAL_PORT_13, 7)// !< Port 13 Pin 7 - #define PIN_USBDP CYHAL_GET_GPIO(CYHAL_PORT_14, 0)// !< Port 14 Pin 0 +#define PIN_P14_0 CYHAL_GET_GPIO(CYHAL_PORT_14, 0) // !< Port 14 Pin 0 #define PIN_USBDM CYHAL_GET_GPIO(CYHAL_PORT_14, 1)// !< Port 14 Pin 1 +#define PIN_P14_1 CYHAL_GET_GPIO(CYHAL_PORT_14, 1) // !< Port 14 Pin 1 // Add all machine pin objects - GPIO , I2C, ADC etc. typedef struct _machine_pin_obj_t { mp_obj_base_t base; - qstr pin_name; - // char *pin_name; + qstr pname; + char *pin_name; uint32_t pin_addr; } machine_pin_obj_t; #include "genhdr/pins.h" // Function Prototypes to support interaction between c<->py int pin_find(mp_obj_t obj); +// const machine_pin_obj_t *pin_find(mp_obj_t user_obj); int pin_addr_by_name(mp_obj_t obj); +extern const mp_obj_type_t pin_cpu_pins_obj_type; +extern const mp_obj_dict_t pin_cpu_pins_locals_dict; + /* TODO: to auto-generate the pin instances in the pin_obj array and thereby capture the obj count. It cannot be declared without a defined size, since there are sizeof() and other functions in machine_pin which act on this array and need a deterministic size of the array to compile. */ -extern const machine_pin_obj_t machine_pin_obj[102]; +extern const machine_pin_obj_t machine_pin_obj[]; #endif // MICROPY_INCLUDED_PSOC6_PINS_H