BeagleBone I2C测试

gaofeisz_sibet   2012-11-27 13:50 楼主
参考chenzhufly的文章:
 
写了一个应用程序测试I2C读写EEPROM芯片AT24C02:
//
//app template, replace xxx with a specified name
//Gao Fei
//2012.10.3
//
#define APP_DEBUG
#include <stdio.h>
//#include <sys/types.h>
#include <fcntl.h>
//#include <sys/ioctl.h>
//#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include "/root/bone-fs/home/root/header-files/print_debug.h"
#include <errno.h>
int main(int argc, char **argv)
{
 int fd;
 int ret;
 //unsigned char x;
 //unsigned char wrbuf[3] = {0, 0, 0xcc};
 //unsigned char wrbuf[2] = {0xaa, 0x11};
 unsigned char wrbuf[4] = {0xaa, 0x11, 0xff, 0xaa};//分别向0xaa、0xa1、0xa2地址写字节
 //unsigned char rdaddr[2] = {0, 0};
 unsigned char rdaddr[1] = {0xaa};//0xaa为读起始地址
 //unsigned char rddata = 0x00;
 unsigned char rddata[3];
 
 ADEBUG("----------main----------\n");
 fd = open("/dev/i2c-2", O_RDWR);
 if (fd < 0)
 {
  ADEBUG("!!!i2c device open failed!!!\n");
  return -1;
 }
 ADEBUG("i2c device open success\n");
 //ret = ioctl(fd, I2C_TENBIT, 0);
 //if (ret < 0)
 // return -1;
 //ADEBUG("ioctl success\n");
 ret = ioctl(fd, I2C_SLAVE_FORCE, 0x50);//0x50是器件地址,7bit
 if (ret < 0)
 {
  ADEBUG("!!!ioctl I2C_SLAVE failed!!!\n");
  ADEBUG("errno = %d\n", errno);
  return -1;
 }
 ADEBUG("ioctl success\n");
 //i2c_smbus_write_byte_data(fd, 0x10, 0x65);
 //x = 0x00;
 //write(fd, &x, 1);
 //usleep(100);
 //x = 0x00;
 //write(fd, &x, 1);
 //usleep(100);
 //x = 0x11;
 //write(fd, &x, 1);
 //usleep(4000);
 
 //ret = write(fd, wrbuf, 3);
 //ret = write(fd, wrbuf, 2);
 ret = write(fd, wrbuf, 4);
 ADEBUG("write ret: %d\n", ret);
 ADEBUG("errno = %d\n", errno);
 //sleep(1);
 usleep(10000);//!!!!!!
 
 ret = write(fd, rdaddr, 1);
 ADEBUG("write ret: %d\n", ret);
 ADEBUG("errno = %d\n", errno);
 /*ADEBUG("read ret: %d\n", read(fd, &rddata, 1));
 ADEBUG("rddata: %d\n", rddata);
 ADEBUG("read ret: %d\n", read(fd, &rddata, 1));
 //ADEBUG("errno = %d\n", errno);
 ADEBUG("rddata: %d\n", rddata);
 ADEBUG("read ret: %d\n", read(fd, &rddata, 1));
 //ADEBUG("errno = %d\n", errno);
 ADEBUG("rddata: %d\n", rddata);*/
 ADEBUG("read ret: %d\n", read(fd, rddata, 3));
 ADEBUG("rddata: %d,%d,%d\n", rddata[0], rddata[1], rddata[2]);
 
 //sleep(1);
 close(fd);
 return 0;
}

回复评论 (1)

加油 加油!
生活就是油盐酱醋再加一点糖,快活就是一天到晚乐呵呵的忙 =================================== 做一个简单的人,踏实而务实,不沉溺幻想,不庸人自扰
点赞  2012-11-27 16:09
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复