76 uint8_t *read_buffer,
size_t read_count) {
78 ESP_LOGE(TAG,
"I2C bus not initialized");
82 ESP_LOGVV(TAG,
"I2C write_readv addr=0x%02X write=%zu read=%zu",
address, write_count, read_count);
86 if (write_count == 0 && read_count == 0) {
93 struct i2c_rdwr_ioctl_data rdwr_data;
94 rdwr_data.msgs = &msg;
101 if (err == EOPNOTSUPP || err == ENOSYS) {
102 ESP_LOGVV(TAG,
"I2C_RDWR probe failed, trying SMBus Quick for addr=0x%02X",
address);
107 union i2c_smbus_data data;
108 struct i2c_smbus_ioctl_data
args;
109 args.read_write = I2C_SMBUS_WRITE;
111 args.size = I2C_SMBUS_QUICK;
125 if (write_count > UINT16_MAX || read_count > UINT16_MAX) {
126 ESP_LOGE(TAG,
"I2C transfer too large: write=%zu read=%zu (max %u)", write_count, read_count,
127 (
unsigned) UINT16_MAX);
132 struct i2c_msg msgs[2];
136 if (write_count > 0) {
138 msgs[num_msgs].flags = 0;
139 msgs[num_msgs].len = write_count;
140 msgs[num_msgs].buf =
const_cast<uint8_t *
>(write_buffer);
145 if (read_count > 0) {
147 msgs[num_msgs].flags = I2C_M_RD;
148 msgs[num_msgs].len = read_count;
149 msgs[num_msgs].buf = read_buffer;
154 struct i2c_rdwr_ioctl_data rdwr_data;
155 rdwr_data.msgs = msgs;
156 rdwr_data.nmsgs = num_msgs;
161 if (err == EOPNOTSUPP || err == ENOSYS) {
162 ESP_LOGV(TAG,
"I2C_RDWR not supported, using I2C_SLAVE fallback for addr=0x%02X",
address);
164 ESP_LOGV(TAG,
"I2C_SLAVE ioctl failed: %s", strerror(errno));
168 if (write_count > 0) {
171 int write_err = errno;
173 if (write_err == EOPNOTSUPP || write_err == ENOSYS) {
174 ESP_LOGV(TAG,
"I2C_SLAVE write not supported, trying I2C_SMBUS for addr=0x%02X",
address);
177 if (write_count < 1) {
178 ESP_LOGE(TAG,
"Write size too small for I2C_SMBUS");
181 if (write_count > I2C_SMBUS_BLOCK_MAX + 1) {
182 ESP_LOGE(TAG,
"Write size %zu exceeds I2C_SMBUS_BLOCK_MAX+1 (%d)", write_count, I2C_SMBUS_BLOCK_MAX + 1);
185 union i2c_smbus_data data;
187 uint8_t command = write_buffer[0];
188 size_t data_len = write_count - 1;
189 data.block[0] = data_len;
191 memcpy(&data.block[1], write_buffer + 1, data_len);
194 struct i2c_smbus_ioctl_data
args;
195 args.read_write = I2C_SMBUS_WRITE;
196 args.command = command;
197 args.size = I2C_SMBUS_I2C_BLOCK_DATA;
202 ESP_LOGV(TAG,
"I2C_SMBUS write failed: %s", strerror(errno));
206 ESP_LOGV(TAG,
"I2C write failed: %s", strerror(write_err));
212 if (read_count > 0) {
214 if (bytes_read != (
ssize_t) read_count) {
215 int read_err = errno;
217 if (read_err == EOPNOTSUPP || read_err == ENOSYS) {
218 ESP_LOGV(TAG,
"I2C_SLAVE read not supported, trying I2C_SMBUS for addr=0x%02X",
address);
220 if (read_count > I2C_SMBUS_BLOCK_MAX) {
221 ESP_LOGE(TAG,
"Read size %zu exceeds I2C_SMBUS_BLOCK_MAX (%d)", read_count, I2C_SMBUS_BLOCK_MAX);
224 union i2c_smbus_data data;
225 data.block[0] = read_count;
227 struct i2c_smbus_ioctl_data
args;
228 args.read_write = I2C_SMBUS_READ;
230 args.size = I2C_SMBUS_I2C_BLOCK_DATA;
235 ESP_LOGV(TAG,
"I2C_SMBUS read failed: %s", strerror(errno));
240 if (data.block[0] < read_count) {
241 ESP_LOGV(TAG,
"I2C_SMBUS short read: got %u, expected %zu", data.block[0], read_count);
245 memcpy(read_buffer, &data.block[1], read_count);
247 ESP_LOGV(TAG,
"I2C read failed: %s", strerror(read_err));
252 ESP_LOGVV(TAG,
"I2C transaction successful (I2C_SLAVE method)");
255 ESP_LOGV(TAG,
"I2C transaction failed: %s", strerror(err));
259 ESP_LOGVV(TAG,
"I2C transaction successful");