From 50c6efcdf241fb8b3e48ea6ee6e3b740d916807c Mon Sep 17 00:00:00 2001 From: NikhitaR-IFX Date: Wed, 9 Aug 2023 13:18:38 +0530 Subject: [PATCH] ports/psoc6: Adding code for machine.Signal functionality. Signed-off-by: NikhitaR-IFX --- docs/psoc6/quickref.rst | 3 +++ ports/psoc6/modules/machine/machine_pin.c | 21 +++++++++++++++++++++ ports/psoc6/modules/machine/modmachine.c | 1 + 3 files changed, 25 insertions(+) diff --git a/docs/psoc6/quickref.rst b/docs/psoc6/quickref.rst index ff79d5a20d3f9..f0a1662e8b5d2 100644 --- a/docs/psoc6/quickref.rst +++ b/docs/psoc6/quickref.rst @@ -146,6 +146,9 @@ The following constants are used to configure the pin objects in addition to the Selects the pin value. +There's a higher-level abstraction :ref:`machine.Signal ` +which can be used to invert a pin. Useful for illuminating active-low LEDs +using ``on()`` or ``value(1)``. Software I2C bus ---------------- diff --git a/ports/psoc6/modules/machine/machine_pin.c b/ports/psoc6/modules/machine/machine_pin.c index 5ddf0fea2980e..7f557b40a5b30 100644 --- a/ports/psoc6/modules/machine/machine_pin.c +++ b/ports/psoc6/modules/machine/machine_pin.c @@ -7,6 +7,7 @@ #include "modmachine.h" #include "drivers/machine/psoc6_gpio.h" #include "pins.h" +#include "extmod/virtpin.h" #include "mplogger.h" @@ -326,6 +327,25 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); +STATIC mp_uint_t 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: { + return cyhal_gpio_read(self->pin_addr); + } + case MP_PIN_WRITE: { + cyhal_gpio_write(self->pin_addr, arg); + return 0; + } + } + return -1; +} + +STATIC const mp_pin_p_t pin_pin_p = { + .ioctl = pin_ioctl, +}; + MP_DEFINE_CONST_OBJ_TYPE( machine_pin_type, MP_QSTR_Pin, @@ -333,5 +353,6 @@ MP_DEFINE_CONST_OBJ_TYPE( make_new, mp_pin_make_new, print, machine_pin_print, call, machine_pin_call, + protocol, &pin_pin_p, locals_dict, &machine_pin_locals_dict ); diff --git a/ports/psoc6/modules/machine/modmachine.c b/ports/psoc6/modules/machine/modmachine.c index 597fff840ae3d..72bb69040495a 100644 --- a/ports/psoc6/modules/machine/modmachine.c +++ b/ports/psoc6/modules/machine/modmachine.c @@ -258,6 +258,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, + { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_spi_type) },