diff --git a/zephyr/machine_pin.c b/zephyr/machine_pin.c index 049255e9f6..5bf2739092 100644 --- a/zephyr/machine_pin.c +++ b/zephyr/machine_pin.c @@ -81,7 +81,7 @@ STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, mp_uint_t n } // constructor(drv_name, pin, ...) -STATIC mp_obj_t machine_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); // get the wanted port @@ -154,6 +154,24 @@ STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); +STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { + (void)errcode; + machine_pin_obj_t *self = self_in; + + switch (request) { + case MP_PIN_READ: { + uint32_t pin_val; + gpio_pin_read(self->port, self->pin, &pin_val); + return pin_val; + } + case MP_PIN_WRITE: { + gpio_pin_write(self->port, self->pin, arg); + return 0; + } + } + return -1; +} + STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = { // instance methods { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&machine_pin_init_obj }, @@ -170,11 +188,16 @@ STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); +STATIC const mp_pin_p_t machine_pin_pin_p = { + .ioctl = machine_pin_ioctl, +}; + const mp_obj_type_t machine_pin_type = { { &mp_type_type }, .name = MP_QSTR_Pin, .print = machine_pin_print, - .make_new = machine_pin_make_new, + .make_new = mp_pin_make_new, .call = machine_pin_call, + .protocol = &machine_pin_pin_p, .locals_dict = (mp_obj_t)&machine_pin_locals_dict, }; diff --git a/zephyr/mpconfigport.h b/zephyr/mpconfigport.h index 9367834510..2c57ede4d5 100644 --- a/zephyr/mpconfigport.h +++ b/zephyr/mpconfigport.h @@ -58,6 +58,7 @@ #define MICROPY_PY_IO (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (1) #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_MODULE_WEAK_LINKS (1) #define MICROPY_PY_STRUCT (0) #ifdef CONFIG_NETWORKING