免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2147 | 回复: 0

关于9260eeprom [复制链接]

论坛徽章:
0
发表于 2010-10-14 10:31 |显示全部楼层
本帖最后由 kut00 于 2010-10-14 10:32 编辑

平台是9260,linux版本2.6.30。在看了《在linux下使用9263ek上的eeprom.pdf》之后,使用cat和echo 能正常的读写at24c512,但是自己写了一个小的测试程序时,每次只有在cat之后运行的第一次能正常读,后来就不行了。然后在cat一次,又能读一次。 测试程序代码附在下面,各位帮我看看。

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <termios.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <stdlib.h>
  10. #include <linux/i2c-dev.h>
  11. #include <linux/i2c.h>
  12.          
  13. int main(void)
  14. {
  15.     int i;
  16.     int fd;
  17.     int res;
  18.     unsigned char buf[128];
  19.    
  20.     printf("Get a char\n");
  21.     getchar();
  22.     fd= open("/dev/i2c-0",O_RDWR);
  23.     if( fd < 0 )
  24.     {
  25.         printf( "Fail to open AT24c512!\n" );
  26.         exit(1);
  27.     }

  28.     res=ioctl(fd,I2C_TENBIT,0);//I2C_TENBIT 0为7/8bit 1为10bit
  29.     printf("res=%d\n",res);
  30.     res=ioctl(fd,I2C_SLAVE_FORCE,0x50);// I2C_SLAVE为设置i2c从机地址,I2C_SLAVE_FORCE 修改i2c从机地址
  31.     printf("res=%d\n",res);
  32.     res=ioctl(fd,I2C_TIMEOUT,1);
  33.     printf("res=%d\n",res);
  34.     res=ioctl(fd,I2C_RETRIES,1);
  35.     printf("res=%d\n",res);

  36.     buf[0]=0x01;  //子地址
  37.     buf[1]=0x88;
  38.     buf[2]=0x77;
  39.     buf[3]=0x55;
  40.     buf[4]=0x44;
  41.     buf[5]=0x33;
  42.     buf[6]=0x66;
  43.     buf[7]=0x22;
  44.     buf[8]=0x11;
  45.     /*
  46.     if(write(fd,buf,9) != 9)
  47.     {
  48.         printf("write error...\n");
  49.     }
  50.    
  51.     sleep(1);*/
  52.     printf("read....14 \n");
  53.     buf[0]=0x00;
  54.     res=ioctl(fd,I2C_SLAVE_FORCE,0x50);// I2C_SLAVE为设置i2c从机地址,I2C_SLAVE_FORCE 修改i2c从机地址
  55.     printf("res=%d\n",res);
  56.     if ( write(fd,buf,1) != 1) {
  57.         printf("write read address  error...\n");
  58.     }
  59.     else
  60.     {
  61.         if( read(fd,buf,8) != 8)
  62.         {
  63.             printf("read  error...\n");
  64.         }
  65.         else
  66.         {
  67.             for(i=0; i<8; i++)
  68.             {
  69.                 printf("buffer[%d]=%x,",i,buf[i]);
  70.             }
  71.         }
  72.     }
  73.     printf("\n end!\n");
  74.     close(fd);
  75.     return 0;
  76. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP