Commit bb29bde1 authored by Damien George's avatar Damien George

nrf/machine/i2c: Update to support new C-level I2C API.

parent 38ac697b
...@@ -99,35 +99,20 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz ...@@ -99,35 +99,20 @@ 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); 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; machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t *)self_in;
nrfx_twi_enable(&self->p_twi); 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 (err_code != NRFX_SUCCESS) { if (flags & MP_MACHINE_I2C_FLAG_READ) {
if (err_code == NRFX_ERROR_DRV_TWI_ERR_ANACK) { err_code = nrfx_twi_rx(&self->p_twi, addr, buf, len);
return -MP_ENODEV; } else {
} err_code = nrfx_twi_tx(&self->p_twi, addr, buf, len, (flags & MP_MACHINE_I2C_FLAG_STOP) == 0);
else if (err_code == NRFX_ERROR_DRV_TWI_ERR_DNACK) { transfer_ret = len;
return -MP_EIO;
}
return -MP_ETIMEDOUT;
} }
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_SUCCESS) {
if (err_code == NRFX_ERROR_DRV_TWI_ERR_ANACK) { if (err_code == NRFX_ERROR_DRV_TWI_ERR_ANACK) {
return -MP_ENODEV; return -MP_ENODEV;
...@@ -140,12 +125,12 @@ int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_ ...@@ -140,12 +125,12 @@ int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_
nrfx_twi_disable(&self->p_twi); nrfx_twi_disable(&self->p_twi);
return len; return transfer_ret;
} }
STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
.readfrom = machine_hard_i2c_readfrom, .transfer = mp_machine_i2c_transfer_adaptor,
.writeto = machine_hard_i2c_writeto, .transfer_single = machine_hard_i2c_transfer_single,
}; };
STATIC const mp_obj_type_t machine_hard_i2c_type = { STATIC const mp_obj_type_t machine_hard_i2c_type = {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment