in common/util/hal_i2c.c [44:107]
int i2c_master_read(I2C_MSG *msg, uint8_t retry)
{
uint8_t i;
uint8_t *txbuf, *rxbuf;
int ret, status;
if (DEBUG_I2C) {
printf("i2c_master_read: bus %d, addr %x, rxlen %d, txlen %d, txbuf:", msg->bus,
msg->slave_addr, msg->rx_len, msg->tx_len);
for (int i = 0; i < msg->tx_len; i++) {
printf(" %x", msg->data[i]);
}
printf("\n");
}
if (msg->rx_len == 0) {
printf("i2c_master_read with rx_len = 0\n");
return EMSGSIZE;
}
do { // break while getting mutex success but tranmission fail
status = k_mutex_lock(&i2c_mutex[msg->bus], K_MSEC(1000));
if (status == osOK) {
for (i = 0; i < retry; i++) {
txbuf = (uint8_t *)malloc(I2C_BUFF_SIZE * sizeof(uint8_t));
rxbuf = (uint8_t *)malloc(I2C_BUFF_SIZE * sizeof(uint8_t));
memcpy(txbuf, &msg->data[0], msg->tx_len);
ret = i2c_write_read(dev_i2c[msg->bus], msg->slave_addr, txbuf,
msg->tx_len, rxbuf, msg->rx_len);
memcpy(&msg->data[0], rxbuf, msg->rx_len);
if (DEBUG_I2C) {
printf("rxbuf:");
for (int i = 0; i < msg->rx_len; i++) {
printf(" %x", msg->data[i]);
}
printf("\n");
}
free(txbuf);
free(rxbuf);
status = k_mutex_unlock(&i2c_mutex[msg->bus]);
if (status != osOK) {
printf("I2C %d master read release mutex fail\n", msg->bus);
}
return ret; // i2c write and read success
}
printf("I2C %d master read retry reach max\n", msg->bus);
status = k_mutex_unlock(&i2c_mutex[msg->bus]);
if (status != osOK) {
printf("I2C %d master read release mutex fail\n", msg->bus);
}
return EPIPE;
} else {
printf("I2C %d master read get mutex timeout\n", msg->bus);
return ENOLCK;
}
} while (0);
return ECANCELED; // should not reach here
}