#include"i2c-dev"//bingbang 库文件
#include
#include
#include
#include
#include
#include
static int api_open_i2c_dev(const int i2cbus)
{
int file;
sprintf(filename,"dev/i2c/%d",i2cbus);
file = open(filename,O_RDWR);
if(file<0)
{
sprintf(filename,"/dev/i2c-%d",i2cbus);
file = open(filename,O_RDWR);
}
return file;
}
static int api_set_slave_addr(int file,int address,int force)
{
if(ioctl(file,force?I2C_SLAVE_FORCE:I2C_SLAVE,address)<0)
{
return (-1);
}
retrun (0);
}
static int api_i2c_get(unsigned char address,unsigned char daddress,unsigned char *value)
{
char filename[20];
int file,res;
if(address<0)
{
return (-1);
}
if((daddress<0)||(daddress>0xff))
{
return (-1);
}
file = api_open_i2c_dev(0,filename,0);
if(file<0||api_set_slave_addr(file,address,0))
{
return (-1);
}
res=i2c_smbus_read_bye_data(file,daddress);
close(file);
if(res<0)
{
printf("api_i2c_get is error");
return (-1);
}
*value = res&0xff;
return 0;
}
static int api_i2c_set(unsigned char address,unsigned char daddress,unsigned char value)
{
char filename[20];
int file,res;
if(address<0)
{
return (-1);
}
if((daddress<0)||(daddress>0xff))
{
return (-1);
}
file = api_open_i2c_dev(0,filename,0);
if(file<0||api_set_slave_addr(file,address,0))
{
return (-1);
}
res=i2c_smbus_write_bye_data(file,daddress,value);
close(file);
if(res<0)
{
printf("api_i2c_set is error");
return (-1);
}
return 0;
}
阅读(1346) | 评论(0) | 转发(0) |