Linux I2C:读取消息长度
Linux I2C: read length of message
TL;DR: 如何读取 I2C 消息,作为主机,首先接收到消息长度?
我有一个 I2C 从设备 (SL030) 连接到我的 Linux 系统。当 I2C 从机需要发送消息时,i 等待读取请求发出,然后发送消息长度,然后发送消息数据。
消息如下所示:
+------+ +-------+ +--------+ +--------+
| 0xA1 | - | len n | - | data 0 | ... | data n |
+------+ +-------+ +--------+ +--------+
0xA1
是slave address+read bit,来自master,其他都来自slave。当 len
可变时,我无法让我的系统读取 len
字节。
我尝试使用 ioctl()
与 I2C_RDWR
和 I2C_M_RECV_LEN
。读取的字节数始终相同。
这是我的测试代码。不好意思,我尽量缩短了。
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct I2c_T
{
int fd; //file descriptor
int slave; //slave address
};
struct I2c_packet_T
{
uint8_t len;
uint8_t data[256];
};
static void die(const char *str)
{
perror(str);
exit(1);
}
static void i2c_sendPacket(struct I2c_T *self, struct I2c_packet_T *packet)
{
unsigned char data[packet->len+1];
data[0]=packet->len;
memcpy(data+1,packet->data,packet->len);
for(int i=0;i<3;i++) //Try writing it 3 times, because there could be a hardware problem
{
if(write(self->fd,packet->data,packet->len)>=0)
{
return; //success
}
usleep(100);
}
die("can not write i2c data");
}
static void i2c_getPacket(struct I2c_T *self, struct I2c_packet_T *packet)
{
uint8_t buffer[1024]={8}; //it always reads buffer[0] byte and doesn't work when it is 0
struct i2c_msg msg=
{
.addr = self->slave,
.flags = I2C_M_RECV_LEN | I2C_M_RD,
.len = 65, //must be at least 32+buffer[0] otherwise it does not work
.buf = buffer
};
struct i2c_rdwr_ioctl_data data={&msg,1};
bool success;
for(int i=0;i<3;i++) //Try reading it 3 times, because there could be a hardware problem
{
usleep(100);
if(ioctl(self->fd,I2C_RDWR,&data)>=0)
{
success=1;
break;
}
}
if(!success)
{
die("Can not get packet");
}
fprintf(stderr,"data is len %"PRIu16", data:",msg.len);
for(unsigned i=0;i<msg.len+32;i++)
{
fprintf(stderr," %02"PRIx8"",msg.buf[i]);
}
fprintf(stderr,"\n");
memcpy(packet->data,msg.buf,msg.len);
}
void i2c_handlePacket
(struct I2c_T *self, struct I2c_packet_T *sendPacket, struct I2c_packet_T *receivePacket)
{
i2c_sendPacket(self,sendPacket);
i2c_getPacket(self,receivePacket);
}
void i2c_test(const char *path, uint8_t slave)
{
struct I2c_T self;
self.fd=-1;
self.slave=slave;
self.fd = open(path, O_RDWR);
if(self.fd<0)
{
die("can not open i2c device");
}
if(ioctl(self.fd,I2C_SLAVE,self.slave)<0)
{
die("can not set i2c address");
}
struct I2c_packet_T send={2,{1,1}};
struct I2c_packet_T receive={0};
i2c_handlePacket(&self,&send,&receive);
close(self.fd);
}
int main(void)
{
i2c_test("/dev/i2c-1",0x50);
}
不管从设备指示的消息长度如何,读取的字节数是 8,正如我在逻辑分析仪中看到的那样。我尝试了不同的标志、不同的长度并进行了其他测试,但到目前为止没有成功。
我在 Raspberry Pi 3B 上使用 Debian 9(扩展版)。
阅读 BCM2708 的数据表后,我发现此硬件无法做到这一点,这不是驱动程序问题。
一个解决方案可能是在软件中使用 bit bang I2C,但这在用户 space 程序上效率不高。对于内核模块,请参见此处 https://github.com/kadamski/i2c-gpio-param
我最后只读取了 256 个字节并忽略了软件中未使用的字节。
TL;DR: 如何读取 I2C 消息,作为主机,首先接收到消息长度?
我有一个 I2C 从设备 (SL030) 连接到我的 Linux 系统。当 I2C 从机需要发送消息时,i 等待读取请求发出,然后发送消息长度,然后发送消息数据。 消息如下所示:
+------+ +-------+ +--------+ +--------+
| 0xA1 | - | len n | - | data 0 | ... | data n |
+------+ +-------+ +--------+ +--------+
0xA1
是slave address+read bit,来自master,其他都来自slave。当 len
可变时,我无法让我的系统读取 len
字节。
我尝试使用 ioctl()
与 I2C_RDWR
和 I2C_M_RECV_LEN
。读取的字节数始终相同。
这是我的测试代码。不好意思,我尽量缩短了。
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct I2c_T
{
int fd; //file descriptor
int slave; //slave address
};
struct I2c_packet_T
{
uint8_t len;
uint8_t data[256];
};
static void die(const char *str)
{
perror(str);
exit(1);
}
static void i2c_sendPacket(struct I2c_T *self, struct I2c_packet_T *packet)
{
unsigned char data[packet->len+1];
data[0]=packet->len;
memcpy(data+1,packet->data,packet->len);
for(int i=0;i<3;i++) //Try writing it 3 times, because there could be a hardware problem
{
if(write(self->fd,packet->data,packet->len)>=0)
{
return; //success
}
usleep(100);
}
die("can not write i2c data");
}
static void i2c_getPacket(struct I2c_T *self, struct I2c_packet_T *packet)
{
uint8_t buffer[1024]={8}; //it always reads buffer[0] byte and doesn't work when it is 0
struct i2c_msg msg=
{
.addr = self->slave,
.flags = I2C_M_RECV_LEN | I2C_M_RD,
.len = 65, //must be at least 32+buffer[0] otherwise it does not work
.buf = buffer
};
struct i2c_rdwr_ioctl_data data={&msg,1};
bool success;
for(int i=0;i<3;i++) //Try reading it 3 times, because there could be a hardware problem
{
usleep(100);
if(ioctl(self->fd,I2C_RDWR,&data)>=0)
{
success=1;
break;
}
}
if(!success)
{
die("Can not get packet");
}
fprintf(stderr,"data is len %"PRIu16", data:",msg.len);
for(unsigned i=0;i<msg.len+32;i++)
{
fprintf(stderr," %02"PRIx8"",msg.buf[i]);
}
fprintf(stderr,"\n");
memcpy(packet->data,msg.buf,msg.len);
}
void i2c_handlePacket
(struct I2c_T *self, struct I2c_packet_T *sendPacket, struct I2c_packet_T *receivePacket)
{
i2c_sendPacket(self,sendPacket);
i2c_getPacket(self,receivePacket);
}
void i2c_test(const char *path, uint8_t slave)
{
struct I2c_T self;
self.fd=-1;
self.slave=slave;
self.fd = open(path, O_RDWR);
if(self.fd<0)
{
die("can not open i2c device");
}
if(ioctl(self.fd,I2C_SLAVE,self.slave)<0)
{
die("can not set i2c address");
}
struct I2c_packet_T send={2,{1,1}};
struct I2c_packet_T receive={0};
i2c_handlePacket(&self,&send,&receive);
close(self.fd);
}
int main(void)
{
i2c_test("/dev/i2c-1",0x50);
}
不管从设备指示的消息长度如何,读取的字节数是 8,正如我在逻辑分析仪中看到的那样。我尝试了不同的标志、不同的长度并进行了其他测试,但到目前为止没有成功。
我在 Raspberry Pi 3B 上使用 Debian 9(扩展版)。
阅读 BCM2708 的数据表后,我发现此硬件无法做到这一点,这不是驱动程序问题。
一个解决方案可能是在软件中使用 bit bang I2C,但这在用户 space 程序上效率不高。对于内核模块,请参见此处 https://github.com/kadamski/i2c-gpio-param
我最后只读取了 256 个字节并忽略了软件中未使用的字节。