问题1:I2C设备驱动的设备节点在哪?
内核编译I2C总线驱动,能看到/dev/i2c-0设备节点
 加载at24.ko能设备驱动,却找不到at24的设备节点,只有几个设备相关的目录
[root@embedsky nfs]# find / -name "at24"
/sys/bus/i2c/drivers/at24
/sys/module/at24
问题2:AT24的地址怎么变成0x50了?
 数据手册里AT24C02/04/08,他们的地址都是0xA0,而我看网上的例子都是用0x50地址,用扫描工具看到确实有个0x50地址的设备
[root@EmbedSky nfs]# i2cdetect -y -r 0  0123456789abcdef
00:    -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- -- 
内核里linux-2.6.30.4/drivers/misc/eeprom/at24.c有相关介绍。这个50是什么貌似是内核专门为eeprom而分配的。那么问题来了,以后我自己写一个内核没有的I2C设备驱动,我怎么知道该设备的地址变成多少?
/** However, misconfiguration can lose data. "Set 16-bit memory address"
* to a part with 8-bit addressing will overwrite data. Writing with too
* big a page size also loses data. And it's not safe to assume that the
* conventional addresses 0x50..0x57 only hold eeproms; a PCF8563 RTC
* uses 0x51, for just one example.
*/
问题3:测试程序并没有调用到设备驱动,而是依靠总线驱动操作设备,而我想通过设备驱动实现(因为设备驱动抽象,不用自己去计算地址什么的)!
 最终我成功操作at24c02芯片,但是使用的不是设备驱动(at24.ko),而是总线驱动那个设备节点(网上找不到操作设备驱动的例子,所以在这里跪求)。也就是说:即使没有写设备驱动,应用层也可以通过总线驱动的设备节点来操作设备,而系统调用仅是一个ioctrl。但该调用方法很不灵活
// 写入的结构体
struct i2c_at24_w
{
  unsigned char addr;
  unsigned char wdata[8];
};
// 读出的结构体
struct i2c_at24_r
{
  unsigned char addr;
  unsigned char rdata[128];
};
int main()
{
  int fd =open("/dev/i2c-0", O_RDWR);
  if (fd< 0) {
   printf("open /dev/i2c-0 failed\n");
   goto exit;
  }
  struct i2c_msgmsg;
  struct i2c_at24_r rd = {0};
  struct i2c_at24_w wd = {0};
  struct i2c_rdwr_ioctl_data ioctl_data;
  struct i2c_msg msgs;
  // 要写入的消息
  ioctl_data.nmsgs= 1;
  ioctl_data.msgs= &msgs;
  // 0地址写入8Byte 0x33,AT24C02一次最多能写入8byte
  for (int i = 0; i < 8;i++) {
  wd.wdata[i] = 0x33;
  }
  wd.addr  = 0x00;
  msgs.addr= 0x50;
  msgs.flags = 0;
  msgs.len = sizeof(struct i2c_at24_w);
  msgs.buf = (unsigned char*)&wd;
  printf("ioctl write addr 0, return :%d\n", ioctl(fd, I2C_RDWR, &ioctl_data));
ioctl_data.nmsgs= 1;
  ioctl_data.msgs= &msgs;
  // 写入要读的地址
  msgs.addr= 0x50;
  msgs.flags = 0;
  msgs.len = sizeof(rd.addr);
  msgs.buf = (unsigned char*)&rd.addr;
  printf("ioctl write address, return :%d\n", ioctl(fd, I2C_RDWR, &ioctl_data));
  // 连续读取128byte
  msgs.addr = 0x50;
  msgs.flags |= I2C_M_RD;
  msgs.len  = sizeof(rd.rdata);
  msgs.buf  = (unsigned char*)&rd.rdata[0];
  printf("ioctl read, return :%d\n", ioctl(fd, I2C_RDWR, &ioctl_data));
  close(fd);
}