- 论坛徽章:
- 0
|
本帖最后由 kut00 于 2010-10-14 10:32 编辑
平台是9260,linux版本2.6.30。在看了《在linux下使用9263ek上的eeprom.pdf》之后,使用cat和echo 能正常的读写at24c512,但是自己写了一个小的测试程序时,每次只有在cat之后运行的第一次能正常读,后来就不行了。然后在cat一次,又能读一次。 测试程序代码附在下面,各位帮我看看。
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <linux/i2c-dev.h>
- #include <linux/i2c.h>
-
- int main(void)
- {
- int i;
- int fd;
- int res;
- unsigned char buf[128];
-
- printf("Get a char\n");
- getchar();
- fd= open("/dev/i2c-0",O_RDWR);
- if( fd < 0 )
- {
- printf( "Fail to open AT24c512!\n" );
- exit(1);
- }
- res=ioctl(fd,I2C_TENBIT,0);//I2C_TENBIT 0为7/8bit 1为10bit
- printf("res=%d\n",res);
- res=ioctl(fd,I2C_SLAVE_FORCE,0x50);// I2C_SLAVE为设置i2c从机地址,I2C_SLAVE_FORCE 修改i2c从机地址
- printf("res=%d\n",res);
- res=ioctl(fd,I2C_TIMEOUT,1);
- printf("res=%d\n",res);
- res=ioctl(fd,I2C_RETRIES,1);
- printf("res=%d\n",res);
- buf[0]=0x01; //子地址
- buf[1]=0x88;
- buf[2]=0x77;
- buf[3]=0x55;
- buf[4]=0x44;
- buf[5]=0x33;
- buf[6]=0x66;
- buf[7]=0x22;
- buf[8]=0x11;
- /*
- if(write(fd,buf,9) != 9)
- {
- printf("write error...\n");
- }
-
- sleep(1);*/
- printf("read....14 \n");
- buf[0]=0x00;
- res=ioctl(fd,I2C_SLAVE_FORCE,0x50);// I2C_SLAVE为设置i2c从机地址,I2C_SLAVE_FORCE 修改i2c从机地址
- printf("res=%d\n",res);
- if ( write(fd,buf,1) != 1) {
- printf("write read address error...\n");
- }
- else
- {
- if( read(fd,buf,8) != 8)
- {
- printf("read error...\n");
- }
- else
- {
- for(i=0; i<8; i++)
- {
- printf("buffer[%d]=%x,",i,buf[i]);
- }
- }
- }
- printf("\n end!\n");
- close(fd);
- return 0;
- }
复制代码 |
|