Skip to content

Commit

Permalink
Hopefully fixes I2C.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdschlosser committed Jan 7, 2025
1 parent 7a50adb commit 263c064
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions micropy_updates/esp32/machine_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
#include "../../../../micropy_updates/common/mp_i2c_common.h"
#include "sdkconfig.h"

#define MP_MACHINE_I2C_FLAG_WRITE2 (0x08)

#if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32S3
#define SCLK_I2C_FREQ XTAL_CLK_FREQ
Expand All @@ -65,6 +68,11 @@
xSemaphoreGive(self->lock.handle); \
}

#if CONFIG_LCD_ENABLE_DEBUG_LOG
#define I2C_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__);
#else
#define I2C_DEBUG_PRINT(...)
#endif

// ********************** machine.I2C.Bus ************************

Expand Down Expand Up @@ -150,6 +158,15 @@
++bufs;
}

if (flags & MP_MACHINE_I2C_FLAG_WRITE2) {
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);

Expand All @@ -168,7 +185,6 @@
i2c_master_stop(cmd);
}

// TODO proper timeout
esp_err_t err = i2c_master_cmd_begin(self->port, cmd, 100 * (1 + data_len) / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);

Expand Down Expand Up @@ -408,12 +424,43 @@
mp_buffer_info_t read_bufinfo;
mp_get_buffer_raise(args[ARG_write_buf].u_obj, &read_bufinfo, MP_BUFFER_WRITE);

int ret = device_writeto(self, self->device_id, (uint8_t *)write_bufinfo.buf, write_bufinfo.len, false);
if (ret < 0) {
mp_raise_OSError(-ret);
uint8_t * write_buf = (uint8_t *)write_bufinfo.buf;
uint32_t memaddr = 0;

if ((self->reg_bits & 7) != 0 || self->reg_bits > 32) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid mem address size (%u)"), self->reg_bits);
}

ret = device_readfrom(self, self->device_id, (uint8_t *)read_bufinfo.buf, read_bufinfo.len, true);
for (int i=(int)(self->reg_bits / 8);i>-1;i--) {
memaddr |= (uint32_t)(write_buf[i] << ((~i + (int)(self->reg_bits / 8)) * 8));
}

uint8_t memaddr_buf[4];
size_t memaddr_len = get_memaddr_buf(&memaddr_buf[0], memaddr, self->reg_bits);

size_t num_bufs = 2;

unsigned int flags = MP_MACHINE_I2C_FLAG_WRITE1 | MP_MACHINE_I2C_FLAG_READ | MP_MACHINE_I2C_FLAG_STOP;

mp_machine_i2c_buf_t bufs[3] = {
{.len = memaddr_len, .buf = memaddr_buf},
{.len = 0, .buf = NULL},
{.len = 0, .buf = NULL}
};

if ((size_t)(self->reg_bits / 8) < write_bufinfo.len) {
bufs[1].buf = write_buf + (self->reg_bits / 8);
bufs[1].len = write_bufinfo.len - (size_t)(self->reg_bits / 8);
num_bufs += 1;
flags |= MP_MACHINE_I2C_FLAG_WRITE2;
}

bufs[num_bufs - 1].buf = read_bufinfo.buf;
bufs[num_bufs - 1].len = read_bufinfo.len;

if (self->bus == NULL) mp_raise_OSError(1);

int ret = i2c_bus_transfer((mp_obj_base_t *)self->bus, self->device_id, num_bufs, bufs, flags);
if (ret < 0) {
mp_raise_OSError(-ret);
}
Expand Down

0 comments on commit 263c064

Please sign in to comment.