diff --git a/ports/nrf/modules/machine/i2c.c b/ports/nrf/modules/machine/i2c.c index 8b79342a30..1d89716121 100644 --- a/ports/nrf/modules/machine/i2c.c +++ b/ports/nrf/modules/machine/i2c.c @@ -99,12 +99,19 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz return MP_OBJ_FROM_PTR(self); } -int machine_hard_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) { +int machine_hard_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size_t len, uint8_t *buf, unsigned int flags) { machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t *)self_in; nrfx_twi_enable(&self->p_twi); - nrfx_err_t err_code = nrfx_twi_rx(&self->p_twi, addr, dest, len); + nrfx_err_t err_code; + int transfer_ret = 0; + if (flags & MP_MACHINE_I2C_FLAG_READ) { + err_code = nrfx_twi_rx(&self->p_twi, addr, buf, len); + } else { + err_code = nrfx_twi_tx(&self->p_twi, addr, buf, len, (flags & MP_MACHINE_I2C_FLAG_STOP) == 0); + transfer_ret = len; + } if (err_code != NRFX_SUCCESS) { if (err_code == NRFX_ERROR_DRV_TWI_ERR_ANACK) { @@ -118,34 +125,12 @@ int machine_hard_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *de nrfx_twi_disable(&self->p_twi); - return 0; -} - -int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) { - machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t *)self_in; - - nrfx_twi_enable(&self->p_twi); - - nrfx_err_t err_code = nrfx_twi_tx(&self->p_twi, addr, src, len, !stop); - - if (err_code != NRFX_SUCCESS) { - if (err_code == NRFX_ERROR_DRV_TWI_ERR_ANACK) { - return -MP_ENODEV; - } - else if (err_code == NRFX_ERROR_DRV_TWI_ERR_DNACK) { - return -MP_EIO; - } - return -MP_ETIMEDOUT; - } - - nrfx_twi_disable(&self->p_twi); - - return len; + return transfer_ret; } STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { - .readfrom = machine_hard_i2c_readfrom, - .writeto = machine_hard_i2c_writeto, + .transfer = mp_machine_i2c_transfer_adaptor, + .transfer_single = machine_hard_i2c_transfer_single, }; STATIC const mp_obj_type_t machine_hard_i2c_type = {