Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 1 | #include <linux/i2c-dev.h> |
| 2 | #include <linux/i2c.h> |
| 3 | #include <linux/types.h> |
| 4 | #include <sys/ioctl.h> |
| 5 | |
| 6 | #define I2C_DATA_MAX 256 |
| 7 | |
| 8 | static inline __s32 i2c_read_after_write(int file, __u8 slave_addr, __u8 tx_len, |
| 9 | __u8* tx_buf, int rx_len, __u8* rx_buf) |
| 10 | { |
| 11 | struct i2c_rdwr_ioctl_data msgst; |
| 12 | struct i2c_msg msg[2]; |
| 13 | int ret; |
| 14 | |
| 15 | msg[0].addr = slave_addr & 0xFF; |
| 16 | msg[0].flags = 0; |
| 17 | msg[0].buf = (__u8*)tx_buf; |
| 18 | msg[0].len = tx_len; |
| 19 | |
| 20 | msg[1].addr = slave_addr & 0xFF; |
| 21 | msg[1].flags = I2C_M_RD | I2C_M_RECV_LEN; |
| 22 | msg[1].buf = (__u8*)rx_buf; |
| 23 | msg[1].len = rx_len; |
| 24 | |
| 25 | msgst.msgs = msg; |
| 26 | msgst.nmsgs = 2; |
| 27 | |
| 28 | ret = ioctl(file, I2C_RDWR, &msgst); |
| 29 | |
| 30 | if (ret < 0) |
| 31 | return ret; |
| 32 | |
| 33 | return ret; |
| 34 | } |