From bfa7ad26704c0676fdee253350df0717dccb8506 Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Fri, 6 Oct 2023 16:43:31 +0200 Subject: [PATCH] ports/psoc6/machine: Moving pin_phy functions to its module. Signed-off-by: enriquezgarc --- ports/psoc6/modules/machine/machine_pin.c | 214 +++--------------- ports/psoc6/modules/machine/machine_pin.h | 3 - ports/psoc6/modules/machine/machine_pin_phy.c | 35 +++ ports/psoc6/modules/machine/machine_pin_phy.h | 4 + 4 files changed, 72 insertions(+), 184 deletions(-) diff --git a/ports/psoc6/modules/machine/machine_pin.c b/ports/psoc6/modules/machine/machine_pin.c index 5a1fdc18e343..d9c55a464769 100644 --- a/ports/psoc6/modules/machine/machine_pin.c +++ b/ports/psoc6/modules/machine/machine_pin.c @@ -2,7 +2,6 @@ #include "py/mphal.h" #include "py/runtime.h" - // port-specific includes #include "modmachine.h" #include "machine_pin.h" @@ -16,168 +15,12 @@ enum {GPIO_PULL_NONE = 0, GPIO_PULL_UP, GPIO_PULL_DOWN}; typedef struct _machine_pin_io_obj_t { mp_obj_base_t base; - uint32_t pin_addr; - char *pin_name; machine_pin_phy_obj_t *pin_phy; uint8_t mode; uint8_t drive; uint8_t pull; } machine_pin_io_obj_t; -machine_pin_io_obj_t machine_pin_obj[] = { - {{&machine_pin_type}, PIN_P0_0, "P0_0"}, - {{&machine_pin_type}, PIN_P0_1, "P0_1"}, - {{&machine_pin_type}, PIN_P0_2, "P0_2"}, - {{&machine_pin_type}, PIN_P0_3, "P0_3"}, - {{&machine_pin_type}, PIN_P0_4, "P0_4"}, - {{&machine_pin_type}, PIN_P0_5, "P0_5"}, - - {{&machine_pin_type}, PIN_P1_0, "P1_0"}, - {{&machine_pin_type}, PIN_P1_1, "P1_1"}, - {{&machine_pin_type}, PIN_P1_2, "P1_2"}, - {{&machine_pin_type}, PIN_P1_3, "P1_3"}, - {{&machine_pin_type}, PIN_P1_4, "P1_4"}, - {{&machine_pin_type}, PIN_P1_5, "P1_5"}, - - {{&machine_pin_type}, PIN_P2_0, "P2_0"}, - {{&machine_pin_type}, PIN_P2_1, "P2_1"}, - {{&machine_pin_type}, PIN_P2_2, "P2_2"}, - {{&machine_pin_type}, PIN_P2_3, "P2_3"}, - {{&machine_pin_type}, PIN_P2_4, "P2_4"}, - {{&machine_pin_type}, PIN_P2_5, "P2_5"}, - {{&machine_pin_type}, PIN_P2_6, "P2_6"}, - {{&machine_pin_type}, PIN_P2_7, "P2_7"}, - - {{&machine_pin_type}, PIN_P3_0, "P3_0"}, - {{&machine_pin_type}, PIN_P3_1, "P3_1"}, - {{&machine_pin_type}, PIN_P3_2, "P3_2"}, - {{&machine_pin_type}, PIN_P3_3, "P3_3"}, - {{&machine_pin_type}, PIN_P3_4, "P3_4"}, - {{&machine_pin_type}, PIN_P3_5, "P3_5"}, - - {{&machine_pin_type}, PIN_P4_0, "P4_0"}, - {{&machine_pin_type}, PIN_P4_1, "P4_1"}, - - {{&machine_pin_type}, PIN_P5_0, "P5_0"}, - {{&machine_pin_type}, PIN_P5_1, "P5_1"}, - {{&machine_pin_type}, PIN_P5_2, "P5_2"}, - {{&machine_pin_type}, PIN_P5_3, "P5_3"}, - {{&machine_pin_type}, PIN_P5_4, "P5_4"}, - {{&machine_pin_type}, PIN_P5_5, "P5_5"}, - {{&machine_pin_type}, PIN_P5_6, "P5_6"}, - {{&machine_pin_type}, PIN_P5_7, "P5_7"}, - - {{&machine_pin_type}, PIN_P6_0, "P6_0"}, - {{&machine_pin_type}, PIN_P6_1, "P6_1"}, - {{&machine_pin_type}, PIN_P6_2, "P6_2"}, - {{&machine_pin_type}, PIN_P6_3, "P6_3"}, - {{&machine_pin_type}, PIN_P6_4, "P6_4"}, - {{&machine_pin_type}, PIN_P6_5, "P6_5"}, - {{&machine_pin_type}, PIN_P6_6, "P6_6"}, - {{&machine_pin_type}, PIN_P6_7, "P6_7"}, - - {{&machine_pin_type}, PIN_P7_0, "P7_0"}, - {{&machine_pin_type}, PIN_P7_1, "P7_1"}, - {{&machine_pin_type}, PIN_P7_2, "P7_2"}, - {{&machine_pin_type}, PIN_P7_3, "P7_3"}, - {{&machine_pin_type}, PIN_P7_4, "P7_4"}, - {{&machine_pin_type}, PIN_P7_5, "P7_5"}, - {{&machine_pin_type}, PIN_P7_6, "P7_6"}, - {{&machine_pin_type}, PIN_P7_7, "P7_7"}, - - {{&machine_pin_type}, PIN_P8_0, "P8_0"}, - {{&machine_pin_type}, PIN_P8_1, "P8_1"}, - {{&machine_pin_type}, PIN_P8_2, "P8_2"}, - {{&machine_pin_type}, PIN_P8_3, "P8_3"}, - {{&machine_pin_type}, PIN_P8_4, "P8_4"}, - {{&machine_pin_type}, PIN_P8_5, "P8_5"}, - {{&machine_pin_type}, PIN_P8_6, "P8_6"}, - {{&machine_pin_type}, PIN_P8_7, "P8_7"}, - - {{&machine_pin_type}, PIN_P9_0, "P9_0"}, - {{&machine_pin_type}, PIN_P9_1, "P9_1"}, - {{&machine_pin_type}, PIN_P9_2, "P9_2"}, - {{&machine_pin_type}, PIN_P9_3, "P9_3"}, - {{&machine_pin_type}, PIN_P9_4, "P9_4"}, - {{&machine_pin_type}, PIN_P9_5, "P9_5"}, - {{&machine_pin_type}, PIN_P9_6, "P9_6"}, - {{&machine_pin_type}, PIN_P9_7, "P9_7"}, - - {{&machine_pin_type}, PIN_P10_0, "P10_0"}, - {{&machine_pin_type}, PIN_P10_1, "P10_1"}, - {{&machine_pin_type}, PIN_P10_2, "P10_2"}, - {{&machine_pin_type}, PIN_P10_3, "P10_3"}, - {{&machine_pin_type}, PIN_P10_4, "P10_4"}, - {{&machine_pin_type}, PIN_P10_5, "P10_5"}, - {{&machine_pin_type}, PIN_P10_6, "P10_6"}, - {{&machine_pin_type}, PIN_P10_7, "P10_7"}, - - {{&machine_pin_type}, PIN_P11_0, "P11_0"}, - {{&machine_pin_type}, PIN_P11_1, "P11_1"}, - {{&machine_pin_type}, PIN_P11_2, "P11_2"}, - {{&machine_pin_type}, PIN_P11_3, "P11_3"}, - {{&machine_pin_type}, PIN_P11_4, "P11_4"}, - {{&machine_pin_type}, PIN_P11_5, "P11_5"}, - {{&machine_pin_type}, PIN_P11_6, "P11_6"}, - {{&machine_pin_type}, PIN_P11_7, "P11_7"}, - - {{&machine_pin_type}, PIN_P12_0, "P12_0"}, - {{&machine_pin_type}, PIN_P12_1, "P12_1"}, - {{&machine_pin_type}, PIN_P12_2, "P12_2"}, - {{&machine_pin_type}, PIN_P12_3, "P12_3"}, - {{&machine_pin_type}, PIN_P12_4, "P12_4"}, - {{&machine_pin_type}, PIN_P12_5, "P12_5"}, - {{&machine_pin_type}, PIN_P12_6, "P12_6"}, - {{&machine_pin_type}, PIN_P12_7, "P12_7"}, - - {{&machine_pin_type}, PIN_P13_0, "P13_0"}, - {{&machine_pin_type}, PIN_P13_1, "P13_1"}, - {{&machine_pin_type}, PIN_P13_2, "P13_2"}, - {{&machine_pin_type}, PIN_P13_3, "P13_3"}, - {{&machine_pin_type}, PIN_P13_4, "P13_4"}, - {{&machine_pin_type}, PIN_P13_5, "P13_5"}, - {{&machine_pin_type}, PIN_P13_6, "P13_6"}, - {{&machine_pin_type}, PIN_P13_7, "P13_7"}, - - {{&machine_pin_type}, PIN_USBDP, "USBDP"}, - {{&machine_pin_type}, PIN_USBDM, "USBDM"}, -}; - -// Function definitions -// helper function to translate pin_name(string) into machine_pin_io_obj_t index. -int pin_find(mp_obj_t pin) { - int wanted_pin = -1; - if (mp_obj_is_small_int(pin)) { - // Pin defined by the index of pin table - wanted_pin = mp_obj_get_int(pin); - } else if (mp_obj_is_str(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++) { - if (slen == strlen(machine_pin_obj[i].pin_name) && strncmp(s, machine_pin_obj[i].pin_name, slen) == 0) { - wanted_pin = i; - break; - } - } - } - - if (!(0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj))) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid pin: Pin not defined!")); - } - - return wanted_pin; -} - -// helper function to translate pin_name(string) into machine_pin_io_obj_t->pin_addr -int pin_addr_by_name(mp_obj_t pin) { - if (mp_obj_is_str(pin)) { - return machine_pin_obj[pin_find(pin)].pin_addr; - } else { - return -1; // expecting a str as input - } -} - #define MAX_PIN_IO MP_ARRAY_SIZE(machine_pin_phy_obj) machine_pin_io_obj_t *pin_io[MAX_PIN_IO] = {NULL}; @@ -192,27 +35,6 @@ static inline machine_pin_io_obj_t *pin_io_allocate(void) { return pin_io[i]; } -STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { - - mp_arg_check_num(n_args, n_kw, 0, 1, false); - machine_pin_io_obj_t *self = self_in; - - if (n_args == 0) { - if (self->mode == GPIO_MODE_IN) { - return MP_OBJ_NEW_SMALL_INT(cyhal_gpio_read(self->pin_phy->addr)); - } else { - return mp_const_none; - } - } else { - if (self->mode != GPIO_MODE_IN) { - bool value = mp_obj_is_true(args[0]); - cyhal_gpio_write(self->pin_phy->addr, value); - } - } - - return mp_const_none; -} - // Pin.print() STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { mplogger_print("machine pin print\n"); @@ -378,7 +200,6 @@ STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_io_obj_t *self, size_t n if (pin_already_inited) { result = cyhal_gpio_configure(self->pin_phy->addr, direction, drive); } else { - cyhal_gpio_free(self->pin_addr); // TODO: remove result = cyhal_gpio_init(self->pin_phy->addr, direction, drive, value); } mplogger_print("Direction: %d, Drive:%d, Value:%d\n", direction, drive, value); @@ -415,6 +236,27 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, return MP_OBJ_FROM_PTR(self); } +STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { + + mp_arg_check_num(n_args, n_kw, 0, 1, false); + machine_pin_io_obj_t *self = self_in; + + if (n_args == 0) { + if (self->mode == GPIO_MODE_IN) { + return MP_OBJ_NEW_SMALL_INT(cyhal_gpio_read(self->pin_phy->addr)); + } else { + return mp_const_none; + } + } else { + if (self->mode != GPIO_MODE_IN) { + bool value = mp_obj_is_true(args[0]); + cyhal_gpio_write(self->pin_phy->addr, value); + } + } + + return mp_const_none; +} + // pin.value([value]) STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { return machine_pin_call(args[0], n_args - 1, 0, args + 1); @@ -428,6 +270,15 @@ STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map } MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_obj_init_obj, 1, machine_pin_obj_init); +// Pin.deinit() +STATIC mp_obj_t machine_pin_obj_deinit(mp_obj_t self_in) { + machine_pin_io_obj_t *self = MP_OBJ_TO_PTR(self_in); + cyhal_gpio_free(self->pin_phy->addr); + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_deinit_obj, machine_pin_obj_deinit); + // Pin.toggle() STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) { @@ -466,6 +317,7 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { // Instance methods { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_machine) }, { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_obj_init_obj) }, + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_pin_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_toggle), MP_ROM_PTR(&machine_pin_toggle_obj) }, { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) }, { MP_ROM_QSTR(MP_QSTR_low), MP_ROM_PTR(&machine_pin_low_obj) }, @@ -488,10 +340,10 @@ STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, i machine_pin_io_obj_t *self = self_in; switch (request) { case MP_PIN_READ: { - return cyhal_gpio_read(self->pin_addr); + return cyhal_gpio_read(self->pin_phy->addr); } case MP_PIN_WRITE: { - cyhal_gpio_write(self->pin_addr, arg); + cyhal_gpio_write(self->pin_phy->addr, arg); return 0; } } diff --git a/ports/psoc6/modules/machine/machine_pin.h b/ports/psoc6/modules/machine/machine_pin.h index 871baefe1779..ca9db8a3f4f1 100644 --- a/ports/psoc6/modules/machine/machine_pin.h +++ b/ports/psoc6/modules/machine/machine_pin.h @@ -4,8 +4,5 @@ #include #include "machine_pin_phy.h" -// Function Prototypes to support interaction between c<->py -int pin_find(mp_obj_t obj); -int pin_addr_by_name(mp_obj_t obj); #endif // MICROPY_INCLUDED_MACHINE_PIN_H diff --git a/ports/psoc6/modules/machine/machine_pin_phy.c b/ports/psoc6/modules/machine/machine_pin_phy.c index 003258cfa192..11ab84a4e797 100644 --- a/ports/psoc6/modules/machine/machine_pin_phy.c +++ b/ports/psoc6/modules/machine/machine_pin_phy.c @@ -121,6 +121,41 @@ machine_pin_phy_obj_t machine_pin_phy_obj[] = { {PIN_USBDM, "USBDM", PIN_PHY_FUNC_NONE} }; +// Function definitions +// helper function to translate pin_name(string) into machine_pin_io_obj_t index. +int pin_find(mp_obj_t pin) { + int wanted_pin = -1; + if (mp_obj_is_small_int(pin)) { + // Pin defined by the index of pin table + wanted_pin = mp_obj_get_int(pin); + } else if (mp_obj_is_str(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_phy_obj); i++) { + if (slen == strlen(machine_pin_phy_obj[i].name) && strncmp(s, machine_pin_phy_obj[i].name, slen) == 0) { + wanted_pin = i; + break; + } + } + } + + if (!(0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_phy_obj))) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid pin: Pin not defined!")); + } + + return wanted_pin; +} + +// helper function to translate pin_name(string) into machine_pin_io_obj_t->pin_addr +int pin_addr_by_name(mp_obj_t pin) { + if (mp_obj_is_str(pin)) { + return machine_pin_phy_obj[pin_find(pin)].addr; + } else { + return -1; // expecting a str as input + } +} + #define pin_phy_assert_null(x) { if (x == NULL) { return NULL; } } machine_pin_phy_obj_t *pin_phy_find_by_name(mp_obj_t pin_name) { diff --git a/ports/psoc6/modules/machine/machine_pin_phy.h b/ports/psoc6/modules/machine/machine_pin_phy.h index a77b4ed1317e..ef9215f8e772 100644 --- a/ports/psoc6/modules/machine/machine_pin_phy.h +++ b/ports/psoc6/modules/machine/machine_pin_phy.h @@ -26,6 +26,10 @@ machine_pin_phy_obj_t *pin_phy_alloc(mp_obj_t addr, machine_pin_phy_func_t func) machine_pin_phy_obj_t *pin_phy_realloc(mp_obj_t addr, machine_pin_phy_func_t func); void pin_phy_free(machine_pin_phy_obj_t *obj); +// Function Prototypes to support interaction between c<->py +int pin_find(mp_obj_t obj); +int pin_addr_by_name(mp_obj_t obj); + #define PIN_P0_0 CYHAL_GET_GPIO(CYHAL_PORT_0, 0)// !< Port 0 Pin 0 #define PIN_P0_1 CYHAL_GET_GPIO(CYHAL_PORT_0, 1)// !< Port 0 Pin 1 #define PIN_P0_2 CYHAL_GET_GPIO(CYHAL_PORT_0, 2)// !< Port 0 Pin 2