我需要读取4字节的数据从MAX30100芯片使用c/c++和I2C上的覆盆子Pi。我试着用蟒蛇做的。而且起作用了。但是问题是数据速率太慢,我需要更新数据至少250次/秒,这意味着读取的频率是>= 250 the。因此,我将读取从python转换为c/c++。
使用wiringPi读取或写入一个字节没有问题。但是,我需要从fifo读取4个字节( fifo的地址是0x04),wiringPi没有提供一个功能来进行块数据读取。只有读字节/字函数。
然后,我尝试使用SMBus进行块读取,在这里可以找到:https://github.com/leon-anavi/rpi-examples/blob/master/BMP180/c/smbus.c
但是,当我调用i2c_smbus_read_block_data()时,我的覆盆子Pi就完全冻结了。
下面是我添加到wiiringPiI2C.c中的读取块数据:
void i2c_smbus_read_block_data(int fd, int command, uint8_t *values, int length)
{
union i2c_smbus_data data;
int i, err;
err = i2c_smbus_access(fd, I2C_SMBUS_READ, command,
I2C_SMBUS_BLOCK_DATA, &data);
if (err < 0)
return;
printf("test1");
for (i = 1; i <= length; i++)
values[i-1] = data.block[i];
}wiiringPiI2C.c可以在这里找到:https://github.com/WiringPi/WiringPi/blob/master/wiringPi/wiringPiI2C.c
有人知道那里发生了什么吗?还是有更好的解决方案?
发布于 2019-06-04 23:56:18
对那些还感兴趣的人来说。这是我的一个工作解决方案,我只是简单地测试了一下。(C++)
#include <unistd.h> //Needed for I2C port
#include <fcntl.h> //Needed for I2C port
#include <sys/ioctl.h> //Needed for I2C port
#include <linux/i2c-dev.h> //Needed for I2C port
#include <linux/i2c.h> //Needed for I2C port
#include <iostream>
#include <iomanip>
#include <string>
#include <cerrno>
#include <cstdint>
#include <cstring>
const std::string i2c_filename = "/dev/i2c-1";
const int i2c_addr = 0x5b; //<<<<<The I2C address of the slave
static inline int i2c_rdwr_block(int fd, uint8_t reg, uint8_t read_write, uint8_t length, unsigned char* buffer)
{
struct i2c_smbus_ioctl_data ioctl_data;
union i2c_smbus_data smbus_data;
int rv;
if(length > I2C_SMBUS_BLOCK_MAX)
{
std::cerr << "Requested Length is greater than the maximum specified" << std::endl;
return -1;
}
// First byte is always the size to write and to receive
// https://github.com/torvalds/linux/blob/master/drivers/i2c/i2c-core-smbus.c
// (See i2c_smbus_xfer_emulated CASE:I2C_SMBUS_I2C_BLOCK_DATA)
smbus_data.block[0] = length;
if ( read_write != I2C_SMBUS_READ )
{
for(int i = 0; i < length; i++)
{
smbus_data.block[i + 1] = buffer[i];
}
}
ioctl_data.read_write = read_write;
ioctl_data.command = reg;
ioctl_data.size = I2C_SMBUS_I2C_BLOCK_DATA;
ioctl_data.data = &smbus_data;
rv = ioctl (fd, I2C_SMBUS, &ioctl_data);
if (rv < 0)
{
std::cerr << "Accessing I2C Read/Write failed! Error is: " << strerror(errno) << std::endl;
return rv;
}
if (read_write == I2C_SMBUS_READ)
{
for(int i = 0; i < length; i++)
{
// Skip the first byte, which is the length of the rest of the block.
buffer[i] = smbus_data.block[i+1];
}
}
return rv;
}
static int setup_i2c(std::string filename)
{
//----- OPEN THE I2C BUS -----
int fd;
int rv;
if ((fd = open(filename.c_str(), O_RDWR)) < 0)
{
//ERROR HANDLING: you can check errno to see what went wrong
std::cout << "Failed to open the i2c bus. Error code: " << fd << std::endl;
return fd;
}
if ((rv = ioctl(fd, I2C_SLAVE, i2c_addr)) < 0)
{
std::cout << "Failed to acquire bus access and/or talk to slave. Error code: " << rv << std::endl;
//ERROR HANDLING; you can check errno to see what went wrong
return rv;
}
return fd;
}
int main()
{
int fd_i2c = setup_i2c(i2c_filename);
int i2c_data_length = 3;
int rv;
unsigned char buffer[i2c_data_length + 1] = {0};
if (fd_i2c < 0)
{
std::cerr << "Set UP I2C Bus Error. Exit now!" << std::endl;
return -1;
}
//std::cout << "File Descriptor: " << fd_i2c << std::endl;
//rv = read_i2c(fd_i2c, buffer, i2c_data_length);
rv = i2c_rdwr_block(fd_i2c, 0x22, I2C_SMBUS_READ, i2c_data_length, buffer);
if (rv < 0)
{
std::cerr << "Reading I2C Bus Error..." << std::endl;
return -1;
}
std::cout << "Buffer Value: " ;
for (int i = 0; i < i2c_data_length; i++)
{
std::cout << "0x" << std::setfill('0') << std::setw(2) << std::hex << (int) buffer[i] << " " ;
}
std::cout << std::endl;
unsigned char values[i2c_data_length] = {0};
values[0] = 0x01;
values[1] = 0x02;
values[2] = 0x03;
//rv = write_i2c(fd_i2c, values, i2c_data_length);
rv = i2c_rdwr_block(fd_i2c, 0x22, I2C_SMBUS_WRITE, i2c_data_length, values);
if (rv < 0)
{
std::cerr << "Writing I2C Bus Error..." << std::endl;
return -1;
}
return 0;
}这段代码的关键是选项I2C_SMBUS_I2C_BLOCK_DATA (在"linux/i2c-dev.h“中定义,也见"linux/i2c.h")。这将将SMBus块数据转换为I2C块数据。具体来说,SMBus块数据是--“命令,block_size,数据”,而I2C块数据是--“命令,数据”,并使用双线定时来确定停止信号。
请参阅Linux内核源代码linux/drivers/i2c/i2c-core-smbus.c和函数,
static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
unsigned short flags,
char read_write, u8 command, int size,
union i2c_smbus_data *data)(i2c_smbus_xfer是第一个调用的函数,返回到i2c_smbus_xfer_emulated是适配器没有对SMBus的本机支持。因此,这两个函数应该实现相同的内容。)
看
case I2C_SMBUS_I2C_BLOCK_DATA:仔细地向您展示如何完成从SMBus到I2C总线的转换。
也比较
case I2C_SMBUS_BLOCK_DATA:看看它是如何不翻译的。(直接发送SMBus数据)
https://stackoverflow.com/questions/55976683
复制相似问题