From 9d77f4499529ad2274883d95e5aad1cadb402a0b Mon Sep 17 00:00:00 2001 From: "Dr. Matthew Swabey" Date: Tue, 14 Mar 2023 21:03:07 -0400 Subject: [PATCH] linux: Fast Linux MCU i2c_read() with I2C_RDRW (#6101) Reading an I2C device from the Linux MCU used a separate write(2) to select the target register & read(2) to get the value(s). This implementation uses ioctl(file, I2C_RDWR, ...) to skip a large bus idle period and extra process sleep by combining them like the stm32. I2C_RDRW requires I2C_FUNC_I2C flag in the I2C driver. I2C_FUNC_I2C is defined in: BCM2835: Pi 1 Models A, A+, B, B+, the Raspberry Pi Zero, the Raspberry Pi Zero W, and the Raspberry Pi Compute Module 1 BCM2836: Pi 2 Model B Identical to BCM2835 except Cortex BCM2837: Pi 3 Model B, later models of the Raspberry Pi 2 Model B, and the Raspberry Pi Compute Module 3 BCM2837B0: Pi 3 Models A+, B+, and the Raspberry Pi Compute Module 3+ BCM2711: Pi 4 Model B, the Raspberry Pi 400, and the Raspberry Pi Compute Module 4 RK3xxx: Rockchips SoCs NanoPi, RockPi, Tinker, etc. SUNXI: H2, H3, etc. Orange Pi AMLOGIC: S905x, Banana Pi, Odroid, etc. TEGRA: NVidia Jetson etc. MediaTek: Several SBCs in other ranges Signed-off-by: Matthew Swabey --- src/linux/gpio.h | 1 + src/linux/i2c.c | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/linux/gpio.h b/src/linux/gpio.h index e2a39be0..df9de752 100644 --- a/src/linux/gpio.h +++ b/src/linux/gpio.h @@ -45,6 +45,7 @@ void gpio_pwm_write(struct gpio_pwm g, uint16_t val); struct i2c_config { int fd; + uint8_t addr; }; struct i2c_config i2c_setup(uint32_t bus, uint32_t rate, uint8_t addr); diff --git a/src/linux/i2c.c b/src/linux/i2c.c index ce91656f..2994403f 100644 --- a/src/linux/i2c.c +++ b/src/linux/i2c.c @@ -4,7 +4,8 @@ // // This file may be distributed under the terms of the GNU GPLv3 license. #include // open -#include // I2C_SLAVE +#include // I2C_SLAVE i2c_msg +#include // i2c_rdwr_ioctl_data I2C_M_RD I2C_FUNC_I2C #include // snprintf #include // ioctl #include // write @@ -42,6 +43,13 @@ i2c_open(uint32_t bus, uint8_t addr) report_errno("open i2c", fd); goto fail; } + // Test for I2C_RDWR support + unsigned long i2c_funcs; // datatype from ioctl spec. + ioctl(fd, I2C_FUNCS, &i2c_funcs); + if ((i2c_funcs & I2C_FUNC_I2C) == 0) { + report_errno("i2c does not support I2C_RDWR", fd); + goto fail; + } int ret = ioctl(fd, I2C_SLAVE, addr); if (ret < 0) { report_errno("ioctl i2c", fd); @@ -73,7 +81,7 @@ i2c_setup(uint32_t bus, uint32_t rate, uint8_t addr) // dtparam=i2c_baudrate= int fd = i2c_open(bus, addr); - return (struct i2c_config){.fd=fd}; + return (struct i2c_config){.fd=fd, .addr=addr}; } void @@ -91,12 +99,29 @@ void i2c_read(struct i2c_config config, uint8_t reg_len, uint8_t *reg , uint8_t read_len, uint8_t *data) { - if(reg_len != 0) - i2c_write(config, reg_len, reg); - int ret = read(config.fd, data, read_len); - if (ret != read_len) { - if (ret < 0) - report_errno("read value i2c", ret); + struct i2c_rdwr_ioctl_data i2c_data; + struct i2c_msg msgs[2]; + + if(reg_len != 0) { + msgs[0].addr = config.addr; + msgs[0].flags = 0x0; + msgs[0].len = reg_len; + msgs[0].buf = reg; + i2c_data.nmsgs = 2; + i2c_data.msgs = &msgs[0]; + } else { + i2c_data.nmsgs = 1; + i2c_data.msgs = &msgs[1]; + } + + msgs[1].addr = config.addr; + msgs[1].flags = I2C_M_RD; + msgs[1].len = read_len; + msgs[1].buf = data; + + int ret = ioctl(config.fd, I2C_RDWR, &i2c_data); + + if(ret < 0) { try_shutdown("Unable to read i2c device"); } }