作者:chenzhufly(空灵)发表于2008/04/03 email:
经过几天的瞎折腾,终于把AT91RM9200在linux2.6.20下TWI测试程序调试好了,总结如下
1.上拉电阻10K估计不合适,我换成2K的,SCL和SDA上就不断的有数据了,先前只有第一次有数据.
2.速度不能太快,由于我做的是两层板,干扰比较大,我I2C时钟跑在200K的时候,就不正常啦.我调成100K后,情况大为好转
测试程序如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(void)
{
int i;
int fd;
int res;
unsigned char buf[8];
printf("Start to test the driver for I2C\n");
printf("Get a char\n");
getchar();
fd= open("/dev/i2c", O_RDWR);
if( fd < 0 )
{
printf( "Fail to open AT24C32!\n" );
exit(1);
}
res=ioctl(fd,I2C_TENBIT,0);
// printf("res=%d\n",res);
res=ioctl(fd,I2C_SLAVE,0x50);
// printf("res=%d\n",res);
res=ioctl(fd,I2C_TIMEOUT,10);
// printf("res=%d\n",res);
res=ioctl(fd,I2C_RETRIES,1);
// printf("res=%d\n",res);
buf[0]=0x00;
buf[1]=0x11;
buf[2]=0x22;
buf[3]=0x33;
buf[4]=0x44;
if ( write(fd,buf,5) != 5) {
printf("write iic error1... ... \n");
}
else
{
printf("write iic datas \n");
for(i= 1;i<5;i++)
{
printf("0x%x ",buf[i]);
}
printf("\n");
}
buf[0]=0x00;
if ( write(fd,buf,1) != 1) {
printf("write iic error2... ... \n");
}
if ( read(fd,buf,4) != 4) {
printf("read iic data1 error... ... \n");
} else {
printf("read iic datas \n");
for(i= 0;i<4;i++)
{
printf("0x%x ",buf[i]);
}
printf("\n");
}
buf[0]=0x00;
buf[1]=0x55;
buf[2]=0x66;
buf[3]=0x77;
buf[4]=0x88;
if ( write(fd,buf,5) != 5) {
/* ERROR HANDLING: i2c transaction failed */
printf("write iic error3... ... \n");
}
else{
printf("write iic datas \n");
for(i= 1;i<5;i++)
{
printf("0x%x ",buf[i]);
}
printf("\n");
}
buf[0]=0x00;
if ( write(fd,buf,1) != 1) {
/* ERROR HANDLING: i2c transaction failed */
printf("write iic error4... ... \n");
}
if (read(fd,buf,4) != 4) {
printf("read iic data2 error... ... \n");
} else {
printf("read iic datas \n");
for(i= 0;i<4;i++)
{
printf("0x%x ",buf[i]);
}
printf("\n");
}
printf("\n finished!\n");
close(fd);
return 0;
}
测试结果:
Start to test the driver for I2C
Get a char
write iic datas
0x11 0x22 0x33 0x44
read iic datas
0x11 0x22 0x33 0x44
write iic datas
0x55 0x66 0x77 0x88
read iic datas
0x55 0x66 0x77 0x88
finished!
阅读(1665) | 评论(0) | 转发(0) |