gpt4 book ai didi

c - c语言设置I2C读写

转载 作者:太空宇宙 更新时间:2023-11-04 07:52:27 25 4
gpt4 key购买 nike

首先,我对这方面一窍不通,但正在学习并且真的很想让它发挥作用。我买了一个 raspberrypi 和一个 bno055 博世加速度计。它带有 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 , 但暂时停留在这里。提前感谢阅读/回复。

最佳答案

我最近写了一个library similar to what you're describing适用于 MMA8451 i2c 加速度计。

本质上,Linux 中的 i2c Controller 被分配了一个设备节点(例如 /dev/i2c-1)。您将以文件形式打开此设备节点 like this :

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

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

要读取寄存器,您需要执行 something like this :

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;
}

要写一个寄存器,你需要做 something like this :

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_RDWR ioctl 将i2c_rdwr_ioctl_data 结构作为参数。这是described like this :

Another common data structure is struct i2c_rdwr_ioctl_data

This is the structure as used in the I2C_RDWR ioctl call

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

(Defined in linux/i2c-dev.h) This structure points to the array of i2c_msg to process and defines the number of i2c_msg in the array.

Usage: If the program is to write one byte (example - the index byte), followed by reading one byte, two struct i2c_msg data structures will be needed. One for the write, and another for the read. These two data structures should be declared as an array of two i2c_msg data structures. They will be processed in the order they appear in the array.

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

您需要引用 data sheet for your BNO055准确地弄清楚哪些寄存器做什么。

至于你的例子,它看起来像是来自bno055_support.c .看起来这只是一组您要实现的 stub 。看起来它基本上是真实界面的模拟。所以重要的是接口(interface),而不是实际的代码(所以不用担心 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;
}

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

#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)

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

关于c - c语言设置I2C读写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52975817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com