esp32/machine_i2c: Implement write-then-read I2C transfers.

I2C transfers are much more efficient if they are combined, instead of
doing separate writes and reads.

Fixes issue #7134.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2022-06-01 11:58:30 +10:00
parent 4a1ae99ac3
commit 6bda80d811
2 changed files with 12 additions and 1 deletions

View File

@ -79,10 +79,20 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_
machine_hw_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
int data_len = 0;
if (flags & MP_MACHINE_I2C_FLAG_WRITE1) {
i2c_master_start(cmd);
i2c_master_write_byte(cmd, addr << 1, true);
i2c_master_write(cmd, bufs->buf, bufs->len, true);
data_len += bufs->len;
--n;
++bufs;
}
i2c_master_start(cmd);
i2c_master_write_byte(cmd, addr << 1 | (flags & MP_MACHINE_I2C_FLAG_READ), true);
int data_len = 0;
for (; n--; ++bufs) {
if (flags & MP_MACHINE_I2C_FLAG_READ) {
i2c_master_read(cmd, bufs->buf, bufs->len, n == 0 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK);

View File

@ -87,6 +87,7 @@
#define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1)
#define MICROPY_PY_MACHINE_PWM_INCLUDEFILE "ports/esp32/machine_pwm.c"
#define MICROPY_PY_MACHINE_I2C (1)
#define MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 (1)
#define MICROPY_PY_MACHINE_SOFTI2C (1)
#define MICROPY_PY_MACHINE_SPI (1)
#define MICROPY_PY_MACHINE_SPI_MSB (0)