The i2c_rdwr method allows you to execute one or more i2c_msg objects in a single kernel ioctl call using repeated-start semantics. This is essential for:
- Transfers > 32 bytes: Bypasses the SMBus block limit.
- Atomic Write-then-Read: Ensures the slave address is not released between the write and read phases.
Important: i2c_rdwr does not return data. You must access the data stored within the i2c_msg object using list(msg), iteration, or msg.buf.
from smbus2 import SMBus, i2c_msg
with SMBus(1) as bus:
# Single write
write_msg = i2c_msg.write(0x50, [0x00, 0x01, 0x02])
bus.i2c_rdwr(write_msg)
# Single read (e.g., 64 bytes)
read_msg = i2c_msg.read(0x50, 64)
bus.i2c_rdwr(read_msg)
data = list(read_msg)
# Dual message: Write register then read 2 bytes (Repeated Start)
write = i2c_msg.write(0x50, [0x00])
read = i2c_msg.read(0x50, 2)
bus.i2c_rdwr(write, read)
result = list(read)