diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index a9d570e4962f..1e2b1021ac9b 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -128,7 +128,6 @@ DRIVERS_SRC_C += $(addprefix drivers/,\ ) DRIVERS_SRC_C += $(addprefix drivers/,\ - machine/psoc6_i2c.c \ machine/psoc6_pwm.c \ machine/psoc6_system.c \ ) diff --git a/ports/psoc6/drivers/machine/psoc6_i2c.c b/ports/psoc6/drivers/machine/psoc6_i2c.c deleted file mode 100644 index f8b3268f14e7..000000000000 --- a/ports/psoc6/drivers/machine/psoc6_i2c.c +++ /dev/null @@ -1,43 +0,0 @@ - -// std includes - - -// mpy includes - - -// MTB includes -#include "cyhal.h" - - -// port-specific includes -#include "psoc6_i2c.h" - - -cy_rslt_t i2c_init(machine_i2c_obj_t *machine_i2c_obj) { - // free first in case object has been initialized before - cyhal_i2c_free(&machine_i2c_obj->i2c_obj); - - // Define the I2C master configuration structure - cyhal_i2c_cfg_t i2c_master_config = - { - CYHAL_I2C_MODE_MASTER, - 0, // address is not used for master mode - machine_i2c_obj->freq - }; - - // Initialize I2C master, set the SDA and SCL pins and assign a new clock - cy_rslt_t result = cyhal_i2c_init(&machine_i2c_obj->i2c_obj, machine_i2c_obj->sda, machine_i2c_obj->scl, NULL); - - return result == CY_RSLT_SUCCESS ? cyhal_i2c_configure(&machine_i2c_obj->i2c_obj, &i2c_master_config) - : result; -} - - -cy_rslt_t i2c_read(cyhal_i2c_t *i2c_obj, uint16_t addr, uint8_t *buf, size_t len, uint32_t timeout, bool send_stop) { - return cyhal_i2c_master_read(i2c_obj, addr, buf, len, timeout, send_stop); -} - - -cy_rslt_t i2c_write(cyhal_i2c_t *i2c_obj, uint16_t addr, uint8_t *buf, size_t len, uint32_t timeout, bool send_stop) { - return cyhal_i2c_master_write(i2c_obj, addr, buf, len, timeout, send_stop); -} diff --git a/ports/psoc6/drivers/machine/psoc6_i2c.h b/ports/psoc6/drivers/machine/psoc6_i2c.h deleted file mode 100644 index 481e80c9bd51..000000000000 --- a/ports/psoc6/drivers/machine/psoc6_i2c.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef MICROPY_INCLUDED_PSOC6_I2C_H -#define MICROPY_INCLUDED_PSOC6_I2C_H - - -// mpy includes -#include "py/runtime.h" - - -// MTB includes -#include "cyhal.h" - - -typedef struct _machine_i2c_obj_t { - mp_obj_base_t base; - cyhal_i2c_t i2c_obj; - uint8_t i2c_id; - uint8_t scl; - uint8_t sda; - uint32_t freq; -} machine_i2c_obj_t; - - -cy_rslt_t i2c_init(machine_i2c_obj_t *machine_i2c_obj); - -cy_rslt_t i2c_read(cyhal_i2c_t *i2c_obj, uint16_t addr, uint8_t *buf, size_t len, uint32_t timeout, bool send_stop); -cy_rslt_t i2c_write(cyhal_i2c_t *i2c_obj, uint16_t addr, uint8_t *buf, size_t len, uint32_t timeout, bool send_stop); - - -#endif // MICROPY_INCLUDED_PSOC6_I2C_H diff --git a/ports/psoc6/modules/machine/machine_i2c.c b/ports/psoc6/modules/machine/machine_i2c.c index 7db6d27d8caa..c58a0785b979 100644 --- a/ports/psoc6/modules/machine/machine_i2c.c +++ b/ports/psoc6/modules/machine/machine_i2c.c @@ -14,17 +14,50 @@ // port-specific includes -#include "drivers/machine/psoc6_i2c.h" #include "modmachine.h" #include "mplogger.h" #include "pins.h" - #define DEFAULT_I2C_FREQ (400000) #define MICROPY_HW_I2C_SCL (CYBSP_I2C_SCL) #define MICROPY_HW_I2C_SDA (CYBSP_I2C_SDA) #define PSOC_I2C_MASTER_MODE (CYHAL_I2C_MODE_MASTER) +typedef struct _machine_i2c_obj_t { + mp_obj_base_t base; + cyhal_i2c_t i2c_obj; + uint8_t i2c_id; + uint8_t scl; + uint8_t sda; + uint32_t freq; +} machine_i2c_obj_t; + +STATIC cy_rslt_t i2c_init(machine_i2c_obj_t *machine_i2c_obj) { + // free first in case object has been initialized before + cyhal_i2c_free(&machine_i2c_obj->i2c_obj); + + // Define the I2C master configuration structure + cyhal_i2c_cfg_t i2c_master_config = + { + CYHAL_I2C_MODE_MASTER, + 0, // address is not used for master mode + machine_i2c_obj->freq + }; + + // Initialize I2C master, set the SDA and SCL pins and assign a new clock + cy_rslt_t result = cyhal_i2c_init(&machine_i2c_obj->i2c_obj, machine_i2c_obj->sda, machine_i2c_obj->scl, NULL); + + return result == CY_RSLT_SUCCESS ? cyhal_i2c_configure(&machine_i2c_obj->i2c_obj, &i2c_master_config) + : result; +} + +STATIC cy_rslt_t i2c_read(cyhal_i2c_t *i2c_obj, uint16_t addr, uint8_t *buf, size_t len, uint32_t timeout, bool send_stop) { + return cyhal_i2c_master_read(i2c_obj, addr, buf, len, timeout, send_stop); +} + +STATIC cy_rslt_t i2c_write(cyhal_i2c_t *i2c_obj, uint16_t addr, uint8_t *buf, size_t len, uint32_t timeout, bool send_stop) { + return cyhal_i2c_master_write(i2c_obj, addr, buf, len, timeout, send_stop); +} extern mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj);