/** * @brief Receive an amount of bytes from an I2C chip in master mode. * @param client I2C client to receive data from. * @param buf Buffer to store the received data in. * @param count Length of \p buf. * @return count Length of \p buf in bytes. */ int i2c_master_recv(const struct i2c_client *client, char *buf, int count) { struct i2c_bus *bus = client->bus; struct i2c_msg msg; int ret; msg.dest_addr = client->addr; set_bit(I2C_RD_FLAG, &msg.flags); msg.len = count; msg.buff = buf; msg.idx = 0; ret = i2c_bus_xfer(bus, &msg, 1); return (ret == 1) ? count : -EINVAL; }
/** * @brief Sent a single buffer to a chip in master mode. * @param client I2C client to set \p buff to. * @param buf Buffer which has to be sent over an I2C bus. * @param count Length of \p buf. * @return count Length of \p buf in bytes. * @return Amount of bytes sent if succesfull, otherwise an error code. */ int i2c_master_send(const struct i2c_client *client, const char *buf, int count) { int ret; struct i2c_bus *bus; struct i2c_msg msg; bus = client->bus; msg.dest_addr = client->addr; msg.flags = 0; msg.len = count; msg.idx = 0; msg.buff = (char*)buf; ret = i2c_bus_xfer(bus, &msg, 1); return (ret == 1) ? count : -EINVAL; }
/** * @brief I2C bus read function * @param handle[in]: i2c bus handle * @param data[out]: data buffer * @param len[in]: data length * @return data length/ERROR_ID */ int gp_i2c_bus_read(int handle, unsigned char* data, unsigned int len) { return i2c_bus_xfer(handle, data, len, I2C_BUS_READ); }
/** * @brief I2C bus write function * @param handle[in]: i2c bus handle * @param data[in]: data to write * @param len[in]: data length * @return data length/ERROR_ID */ int gp_i2c_bus_write(int handle, unsigned char* data, unsigned int len) { return i2c_bus_xfer(handle, data, len, I2C_BUS_WRITE); }