zephyr/machine_i2c: Rework I2C driver to make continuous transactions.
Check code formatting / code-formatting (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c5_c6, v5.5.1) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2, v5.3) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2, v5.5.1) (push) Has been cancelled
esp32 port / build_idf (esp32_build_p4, v5.5.1) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3, v5.3) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3, v5.5.1) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, x64, v143, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, v143, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, v142, dev, 2019, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, v142, standard, 2019, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, v143, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, v143, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, v142, dev, 2019, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, v142, standard, 2019, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, v143, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, v143, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled

Stop using `mp_machine_i2c_transfer_adaptor` and instead implement support
for `MP_MACHINE_I2C_FLAG_WRITE1`.  That allows combined write-read
transactions like `I2C.readfrom_mem()` to work properly with a RESTART,
instead of being split into a separate write-with-STOP followed by a
read-with-STOP (many I2C controllers in zephyr don't support incomplete
transactions).

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
This commit is contained in:
Fin Maaß
2026-01-13 14:02:57 +01:00
committed by Damien George
parent 00c5968b35
commit 56b168b086
2 changed files with 63 additions and 21 deletions
+62 -21
View File
@@ -5,6 +5,7 @@
*
* Copyright (c) 2013, 2014, 2015 Damien P. George
* Copyright (c) 2019, NXP
* Copyright (c) 2026 Fin Maaß
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -44,7 +45,6 @@
typedef struct _machine_hard_i2c_obj_t {
mp_obj_base_t base;
const struct device *dev;
bool restart;
} machine_hard_i2c_obj_t;
static void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
@@ -82,44 +82,85 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz
machine_hard_i2c_obj_t *self = mp_obj_malloc(machine_hard_i2c_obj_t, &machine_i2c_type);
self->dev = dev;
self->restart = false;
return MP_OBJ_FROM_PTR(self);
}
static int machine_hard_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, size_t len, uint8_t *buf, unsigned int flags) {
static int machine_hard_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags) {
machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t *)self_in;
struct i2c_msg msg;
struct i2c_msg msg[2] = {0};
int ret;
size_t len;
size_t i;
uint8_t start_idx = 0;
msg.buf = (uint8_t *)buf;
msg.len = len;
msg.flags = 0;
if (flags & MP_MACHINE_I2C_FLAG_WRITE1) {
msg[0].buf = bufs[0].buf;
msg[0].len = bufs[0].len;
msg[0].flags = I2C_MSG_WRITE;
msg[1].flags = I2C_MSG_RESTART;
start_idx = 1;
n--;
bufs++;
}
if (n == 0) {
return -MP_EINVAL;
}
if (!(flags & MP_MACHINE_I2C_FLAG_STOP)) {
return -MP_EINVAL;
}
if (flags & MP_MACHINE_I2C_FLAG_READ) {
msg.flags |= I2C_MSG_READ;
msg[start_idx].flags |= I2C_MSG_READ | I2C_MSG_STOP;
} else {
msg.flags |= I2C_MSG_WRITE;
msg[start_idx].flags |= I2C_MSG_WRITE | I2C_MSG_STOP;
}
if (self->restart) {
msg.flags |= I2C_MSG_RESTART;
}
if (flags & MP_MACHINE_I2C_FLAG_STOP) {
msg.flags |= I2C_MSG_STOP;
self->restart = false;
if (n == 1) {
// Use given single buffer
msg[start_idx].buf = bufs[0].buf;
msg[start_idx].len = bufs[0].len;
} else {
self->restart = true;
// Combine buffers into a single one
msg[start_idx].len = 0;
for (i = 0; i < n; ++i) {
msg[start_idx].len += bufs[i].len;
}
msg[start_idx].buf = m_new(uint8_t, msg[start_idx].len);
if (!(flags & MP_MACHINE_I2C_FLAG_READ)) {
len = 0;
for (i = 0; i < n; ++i) {
memcpy(&msg[start_idx].buf[len], bufs[i].buf, bufs[i].len);
len += bufs[i].len;
}
}
}
ret = i2c_transfer(self->dev, &msg, 1, addr);
return (ret < 0) ? -MP_EIO : len;
ret = i2c_transfer(self->dev, msg, start_idx + 1, addr);
if (ret < 0) {
return -MP_EIO;
}
if (n > 1) {
if (flags & MP_MACHINE_I2C_FLAG_READ) {
// Copy data from single buffer to individual ones
len = 0;
for (i = 0; i < n; ++i) {
memcpy(bufs[i].buf, &msg[start_idx].buf[len], bufs[i].len);
len += bufs[i].len;
}
}
m_del(uint8_t, msg[start_idx].buf, msg[start_idx].len);
}
return msg[0].len + msg[1].len;
}
static const mp_machine_i2c_p_t machine_hard_i2c_p = {
.transfer = mp_machine_i2c_transfer_adaptor,
.transfer_single = machine_hard_i2c_transfer_single,
.transfer_supports_write1 = true,
.transfer = machine_hard_i2c_transfer,
};
MP_DEFINE_CONST_OBJ_TYPE(
+1
View File
@@ -75,6 +75,7 @@
#define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_INCLUDEFILE "ports/zephyr/modmachine.c"
#define MICROPY_PY_MACHINE_I2C (1)
#define MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 (1)
#ifdef CONFIG_I2C_TARGET
#define MICROPY_PY_MACHINE_I2C_TARGET (1)
#define MICROPY_PY_MACHINE_I2C_TARGET_INCLUDEFILE "ports/zephyr/machine_i2c_target.c"