简体   繁体   English

Linux I2C:读取消息长度

[英]Linux I2C: read length of message

TL;DR: How to read a I2C message, as a master, where the message length is received first? TL;DR:如何读取 I2C 消息,作为主机,首先接收消息长度的位置?

I have a I2C slave (SL030) connected to my Linux system.我有一个 I2C 从机 (SL030) 连接到我的 Linux 系统。 When the I2C slaves needs to send a message, i waits till a read request is made and then sends the length of the message and then the message data.当 I2C 从机需要发送消息时,我会等到发出读取请求,然后发送消息的长度,然后发送消息数据。 A message looks like this:一条消息如下所示:

+------+   +-------+   +--------+     +--------+
| 0xA1 | - | len n | - | data 0 | ... | data n |
+------+   +-------+   +--------+     +--------+

The 0xA1 is the slave address+read bit and comes from the master, everything else comes from the slave. 0xA1是从机地址+读取位,来自主机,其他一切都来自从机。 I am not able to make my system read len byte when len is variable.len是可变的时,我无法让我的系统读取len字节。

I tried it using ioctl() with I2C_RDWR and I2C_M_RECV_LEN .我尝试使用ioctl()I2C_RDWRI2C_M_RECV_LEN The amount of read bytes is always the same.读取字节的数量始终相同。

Here is my testing code.这是我的测试代码。 Sorry for the length, i tried to make it shorter.对不起,我试图让它更短。

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

The amount of bytes read is 8, as i see with my logic analyzer, regardless of the message length indicated by the slave.正如我在逻辑分析仪中看到的那样,读取的字节数为 8,无论从站指示的消息长度如何。 I tried different flags, different length and made other tests but no success so far.我尝试了不同的标志,不同的长度并进行了其他测试,但到目前为止没有成功。

I am on Debian 9 (stretch) on a Raspberry Pi 3B.我在 Raspberry Pi 3B 上使用 Debian 9(拉伸)。

After reading the datasheet of the BCM2708 i see that this is not possible with this hardware and this is not a driver problem.在阅读了 BCM2708 的数据表后,我发现使用此硬件是不可能的,这不是驱动程序问题。

A solution could be to bit bang I2C in software, but this is not very efficient on a user space program.一个解决方案可能是在软件中使用 I2C,但这在用户空间程序上效率不高。 For a kernel module see here https://github.com/kadamski/i2c-gpio-param对于 kernel 模块,请参见此处https://github.com/kadamski/i2c-gpio-param

I ended up just reading 256 byte and ignore unused bytes in software.我最终只读取了 256 个字节并忽略了软件中未使用的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM