首页 > 解决方案 > c语言设置I2C读写

问题描述

首先,我对这些东西很陌生,但是正在学习并且真的很想让它发挥作用。我买了一个 raspberrypi 和一个 bno055 bosch 加速度计。它带有一个 bno055.c、bno055.h 和一个 bno055_support.c 文件。在进入编程和 c 并学习/尝试之后,我似乎需要以某种方式定义如何进行 I2C 读写。它需要进行设置,以便您可以定义读取/写入的字节数。您可以在下面找到预定义的两个功能:

/*  \Brief: The API is used as I2C bus write
 *  \Return : Status of the I2C write
 *  \param dev_addr : The device address of the sensor
 *  \param reg_addr : Address of the first register,
 *   will data is going to be written
 *  \param reg_data : It is a value hold in the array,
 *      will be used for write the value into the register
 *  \param cnt : The no of byte of data to be write
 */
s8 BNO055_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
    s32 BNO055_iERROR = BNO055_INIT_VALUE;
    u8 array[I2C_BUFFER_LEN];
    u8 stringpos = BNO055_INIT_VALUE;

    array[BNO055_INIT_VALUE] = reg_addr;
    for (stringpos = BNO055_INIT_VALUE; stringpos < cnt; stringpos++)
        array[stringpos + BNO055_I2C_BUS_WRITE_ARRAY_INDEX] =
            *(reg_data + stringpos);
    }
    /*
    * Please take the below APIs as your reference for
    * write the data using I2C communication
    * "BNO055_iERROR = I2C_WRITE_STRING(DEV_ADDR, ARRAY, CNT+1)"
    * add your I2C write APIs here
    * BNO055_iERROR is an return value of I2C read API
    * Please select your valid return value
    * In the driver BNO055_SUCCESS defined as 0
    * and FAILURE defined as -1
    * Note :
    * This is a full duplex operation,
    * The first read data is discarded, for that extra write operation
    * have to be initiated. For that cnt+1 operation done
    * in the I2C write string function
    * For more information please refer data sheet SPI communication:
    */
    return (s8)BNO055_iERROR;
}

 /* \Brief: The API is used as I2C bus read
 *  \Return : Status of the I2C read
 *  \param dev_addr : The device address of the sensor
 *  \param reg_addr : Address of the first register,
 *  will data is going to be read
 *  \param reg_data : This data read from the sensor,
 *   which is hold in an array
 *  \param cnt : The no of byte of data to be read
 */
s8 BNO055_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
    s32 BNO055_iERROR = BNO055_INIT_VALUE;
    u8 array[I2C_BUFFER_LEN] = {BNO055_INIT_VALUE};
    u8 stringpos = BNO055_INIT_VALUE;

    array[BNO055_INIT_VALUE] = reg_addr;

    /* Please take the below API as your reference
     * for read the data using I2C communication
     * add your I2C read API here.
     * "BNO055_iERROR = I2C_WRITE_READ_STRING(DEV_ADDR,
     * ARRAY, ARRAY, 1, CNT)"
     * BNO055_iERROR is an return value of SPI write API
     * Please select your valid return value
     * In the driver BNO055_SUCCESS defined as 0
     * and FAILURE defined as -1
     */
    for (stringpos = BNO055_INIT_VALUE; stringpos < cnt; stringpos++)
        *(reg_data + stringpos) = array[stringpos];
    return (s8)BNO055_iERROR;
}

我的问题是有人可以指导我度过这个挑战吗?我正在学习https://www.kernel.org/doc/Documentation/i2c/dev-interface,但暂时停留在这里。提前谢谢阅读/回复。

标签: caccelerometeri2cgyroscopemagnetometer

解决方案


我最近编写了一个类似于您为 MMA8451 i2c 加速度计描述的库。

本质上,Linux 中的 i2c 控制器被分配了一个设备节点(例如/dev/i2c-1)。您将打开此设备节点作为文件,如下所示

int file = open(path, O_RDWR); //path = /dev/i2c-1

获得文件句柄后,您可以使用ioctl读取和写入 i2c 寄存器。i2c 内核模块支持 I2C_RDWR ioctl,它允许您与 i2c 寄存器交互。

要读取寄存器,您可以执行以下操作:

int mma8451_get_i2c_register(int file, unsigned char addr, unsigned char reg, unsigned char *val) {
    unsigned char inbuf, outbuf;
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[2];

    outbuf = reg;
    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = sizeof(outbuf);
    messages[0].buf   = &outbuf;

    messages[1].addr  = addr;
    messages[1].flags = I2C_M_RD;
    messages[1].len   = sizeof(inbuf);
    messages[1].buf   = &inbuf;

    packets.msgs      = messages;
    packets.nmsgs     = 2;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        return 0;
    }
    *val = inbuf;

    return 1;
}

要编写寄存器,您可以执行以下操作:

int mma8451_set_i2c_register(int file, unsigned char addr, unsigned char reg, unsigned char value) {
    unsigned char outbuf[2];
    struct i2c_rdwr_ioctl_data packets;
    struct i2c_msg messages[1];

    messages[0].addr  = addr;
    messages[0].flags = 0;
    messages[0].len   = sizeof(outbuf);
    messages[0].buf   = outbuf;

    outbuf[0] = reg;
    outbuf[1] = value;

    packets.msgs  = messages;
    packets.nmsgs = 1;
    if(ioctl(file, I2C_RDWR, &packets) < 0) {
        return 0;
    }

    return 1;
}

编辑:I2C_RDWRioctl 将i2c_rdwr_ioctl_data结构作为参数。它是这样描述的:

另一种常见的数据结构是struct i2c_rdwr_ioctl_data

这是 I2C_RDWR ioctl 调用中使用的结构

struct i2c_rdwr_ioctl_data { struct i2c_msg __user *msgs; /* pointers to i2c_msgs */ __u32 nmsgs; /* number of i2c_msgs */ };

(定义在linux/i2c-dev.h) 这个结构体指向要处理的i2c_msg的数组,定义了数组中i2c_msg的个数。

用法:如果程序要写入一个字节(例如——索引字节),然后读取一个字节,则需要两个 struct i2c_msg 数据结构。一个用于写入,另一个用于读取。这两个数据结构应该声明为两个 i2c_msg 数据结构的数组。它们将按照它们在数组中出现的顺序进行处理。

i2c_rdwr_ioctl_data结构包含一个指向结构数组的指针i2c_msg。这些结构包含您要发送或接收的实际消息。例如,我的加速度计为了读取一个寄存器,我首先需要将我想要读取的寄存器写入设备,然后我可以读取它(因此为什么i2c_msg我的读取函数中有两个 ')。如果我只是写一个寄存器,我只需要一个。

您需要参考BNO055 的数据表,以准确了解哪些寄存器的作用。

至于您的示例,它看起来像是来自bno055_support.c。看起来这只是您要实现的一组存根。看起来它基本上是一个真实界面的模拟。所以重要的是接口,而不是实际的代码(所以不用担心cnt)。重要的位在这里:

s8 I2C_routine(void)
{
    bno055.bus_write = BNO055_I2C_bus_write;
    bno055.bus_read = BNO055_I2C_bus_read;
    bno055.delay_msec = BNO055_delay_msek;
    bno055.dev_addr = BNO055_I2C_ADDR1;

    return BNO055_INIT_VALUE;
}

这会将设备结构上的函数指针设置为要定义的写入函数,并设置设备的地址和延迟。从那里您需要实现与此接口匹配的功能:

#define BNO055_BUS_WRITE_FUNC(dev_addr, reg_addr, reg_data, wr_len)\
    bus_write(dev_addr, reg_addr, reg_data, wr_len)


#define BNO055_BUS_READ_FUNC(dev_addr, reg_addr, reg_data, r_len)\
    bus_read(dev_addr, reg_addr, reg_data, r_len)

我上面给你的函数应该是非常接近的替身。祝你好运!


推荐阅读