Skip to content

Commit 5233fb3

Browse files
committed
extmod/machine_i2c: Only use WRITE1 option if transfer supports it.
When MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1 is enabled the port's hardware I2C transfer functions should support the MP_MACHINE_I2C_FLAG_WRITE1 option, but software I2C will not. So add a flag to the I2C protocol struct so each individual protocol can indicate whether it supports this option or not. Fixes issue #8765. Signed-off-by: Damien George <damien@micropython.org>
1 parent d7919ea commit 5233fb3

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

extmod/machine_i2c.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -515,19 +515,20 @@ STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t a
515515
size_t memaddr_len = fill_memaddr_buf(&memaddr_buf[0], memaddr, addrsize);
516516

517517
#if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1
518-
519-
// Create partial write and read buffers
520-
mp_machine_i2c_buf_t bufs[2] = {
521-
{.len = memaddr_len, .buf = memaddr_buf},
522-
{.len = len, .buf = buf},
523-
};
524-
525-
// Do write+read I2C transfer
518+
// The I2C transfer function may support the MP_MACHINE_I2C_FLAG_WRITE1 option
526519
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t *)self->type->protocol;
527-
return i2c_p->transfer(self, addr, 2, bufs,
528-
MP_MACHINE_I2C_FLAG_WRITE1 | MP_MACHINE_I2C_FLAG_READ | MP_MACHINE_I2C_FLAG_STOP);
520+
if (i2c_p->transfer_supports_write1) {
521+
// Create partial write and read buffers
522+
mp_machine_i2c_buf_t bufs[2] = {
523+
{.len = memaddr_len, .buf = memaddr_buf},
524+
{.len = len, .buf = buf},
525+
};
529526

530-
#else
527+
// Do write+read I2C transfer
528+
return i2c_p->transfer(self, addr, 2, bufs,
529+
MP_MACHINE_I2C_FLAG_WRITE1 | MP_MACHINE_I2C_FLAG_READ | MP_MACHINE_I2C_FLAG_STOP);
530+
}
531+
#endif
531532

532533
int ret = mp_machine_i2c_writeto(self, addr, memaddr_buf, memaddr_len, false);
533534
if (ret != memaddr_len) {
@@ -536,8 +537,6 @@ STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t a
536537
return ret;
537538
}
538539
return mp_machine_i2c_readfrom(self, addr, buf, len, true);
539-
540-
#endif
541540
}
542541

543542
STATIC int write_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t addrsize, const uint8_t *buf, size_t len) {

extmod/machine_i2c.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ typedef struct _mp_machine_i2c_buf_t {
6161
// - transfer must be non-NULL
6262
// - transfer_single only needs to be set if transfer=mp_machine_i2c_transfer_adaptor
6363
typedef struct _mp_machine_i2c_p_t {
64+
#if MICROPY_PY_MACHINE_I2C_TRANSFER_WRITE1
65+
bool transfer_supports_write1;
66+
#endif
6467
void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
6568
int (*start)(mp_obj_base_t *obj);
6669
int (*stop)(mp_obj_base_t *obj);

ports/esp32/machine_i2c.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_
188188
}
189189

190190
STATIC const mp_machine_i2c_p_t machine_hw_i2c_p = {
191+
.transfer_supports_write1 = true,
191192
.transfer = machine_hw_i2c_transfer,
192193
};
193194

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy