69 uint8_t *read_buffer,
size_t read_count) {
71 ESP_LOGE(TAG,
"I2C bus not initialized");
75 ESP_LOGVV(TAG,
"I2C write_readv addr=0x%02X write=%zu read=%zu",
address, write_count, read_count);
79 if (write_count == 0 && read_count == 0) {
86 struct i2c_rdwr_ioctl_data rdwr_data;
87 rdwr_data.msgs = &msg;
94 if (err == EOPNOTSUPP || err == ENOSYS) {
95 ESP_LOGVV(TAG,
"I2C_RDWR probe failed, trying SMBus Quick for addr=0x%02X",
address);
100 union i2c_smbus_data data;
101 struct i2c_smbus_ioctl_data args;
102 args.read_write = I2C_SMBUS_WRITE;
104 args.size = I2C_SMBUS_QUICK;
118 if (write_count > UINT16_MAX || read_count > UINT16_MAX) {
119 ESP_LOGE(TAG,
"I2C transfer too large: write=%zu read=%zu (max %u)", write_count, read_count,
120 (
unsigned) UINT16_MAX);
125 struct i2c_msg msgs[2];
129 if (write_count > 0) {
131 msgs[num_msgs].flags = 0;
132 msgs[num_msgs].len = write_count;
133 msgs[num_msgs].buf =
const_cast<uint8_t *
>(write_buffer);
138 if (read_count > 0) {
140 msgs[num_msgs].flags = I2C_M_RD;
141 msgs[num_msgs].len = read_count;
142 msgs[num_msgs].buf = read_buffer;
147 struct i2c_rdwr_ioctl_data rdwr_data;
148 rdwr_data.msgs = msgs;
149 rdwr_data.nmsgs = num_msgs;
154 if (err == EOPNOTSUPP || err == ENOSYS) {
155 ESP_LOGV(TAG,
"I2C_RDWR not supported, using I2C_SLAVE fallback for addr=0x%02X",
address);
157 ESP_LOGV(TAG,
"I2C_SLAVE ioctl failed: %s", strerror(errno));
161 if (write_count > 0) {
164 int write_err = errno;
166 if (write_err == EOPNOTSUPP || write_err == ENOSYS) {
167 ESP_LOGV(TAG,
"I2C_SLAVE write not supported, trying I2C_SMBUS for addr=0x%02X",
address);
170 if (write_count < 1) {
171 ESP_LOGE(TAG,
"Write size too small for I2C_SMBUS");
174 if (write_count > I2C_SMBUS_BLOCK_MAX + 1) {
175 ESP_LOGE(TAG,
"Write size %zu exceeds I2C_SMBUS_BLOCK_MAX+1 (%d)", write_count, I2C_SMBUS_BLOCK_MAX + 1);
178 union i2c_smbus_data data;
180 uint8_t command = write_buffer[0];
181 size_t data_len = write_count - 1;
182 data.block[0] = data_len;
184 memcpy(&data.block[1], write_buffer + 1, data_len);
187 struct i2c_smbus_ioctl_data args;
188 args.read_write = I2C_SMBUS_WRITE;
189 args.command = command;
190 args.size = I2C_SMBUS_I2C_BLOCK_DATA;
195 ESP_LOGV(TAG,
"I2C_SMBUS write failed: %s", strerror(errno));
199 ESP_LOGV(TAG,
"I2C write failed: %s", strerror(write_err));
205 if (read_count > 0) {
207 if (bytes_read != (
ssize_t) read_count) {
208 int read_err = errno;
210 if (read_err == EOPNOTSUPP || read_err == ENOSYS) {
211 ESP_LOGV(TAG,
"I2C_SLAVE read not supported, trying I2C_SMBUS for addr=0x%02X",
address);
213 if (read_count > I2C_SMBUS_BLOCK_MAX) {
214 ESP_LOGE(TAG,
"Read size %zu exceeds I2C_SMBUS_BLOCK_MAX (%d)", read_count, I2C_SMBUS_BLOCK_MAX);
217 union i2c_smbus_data data;
218 data.block[0] = read_count;
220 struct i2c_smbus_ioctl_data args;
221 args.read_write = I2C_SMBUS_READ;
223 args.size = I2C_SMBUS_I2C_BLOCK_DATA;
228 ESP_LOGV(TAG,
"I2C_SMBUS read failed: %s", strerror(errno));
233 if (data.block[0] < read_count) {
234 ESP_LOGV(TAG,
"I2C_SMBUS short read: got %u, expected %zu", data.block[0], read_count);
238 memcpy(read_buffer, &data.block[1], read_count);
240 ESP_LOGV(TAG,
"I2C read failed: %s", strerror(read_err));
245 ESP_LOGVV(TAG,
"I2C transaction successful (I2C_SLAVE method)");
248 ESP_LOGV(TAG,
"I2C transaction failed: %s", strerror(err));
252 ESP_LOGVV(TAG,
"I2C transaction successful");