- 论坛徽章:
- 0
|
int main(int argc, char *argv[])
{
unsigned char I2C_WrAddr = EX_GPIO_WRITE_ADDR;
unsigned char I2C_RdAddr = EX_GPIO_READ_ADDR;
unsigned long Delay = EX_GPIO_DELAY;
unsigned char data[2];
unsigned long Bit;
unsigned long enable;
int fd=-1;
unsigned int Data1;
char device[10];
unsigned long tp=0;
snprintf(device, 10, "%d", EM86XX_CHIP_NUMBER);
if((fd=open("/dev/i2c",O_RDONLY))<0)
{
printf("EX_GPIO: open device error.n");
exit(-1);
}
else{
printf("fd is %d n",fd);
if(ioctl(fd,I2C_SUB,&tp)<0) // sub address
{
printf("EX_GPIO: (sub/nosub)ioctl error.n");
close(fd);
exit(-1);
}
if(ioctl(fd,I2C_SOFT,&tp)<0) // soft hardware
{
printf("EX_GPIO: ioctl (soft/hw) error.n");
close(fd);
exit(-1);
}
if(ioctl(fd,I2C_ADDRWR,&I2C_WrAddr)<0) // WR_ADDR
{
printf("EX_GPIO: ioctl (ADDRWR) error.n");
close(fd);
exit(-1);
}
if(ioctl(fd,I2C_ADDRRD,&I2C_RdAddr)<0) // RD_ADDR
{
printf("EX_GPIO: ioctl (ADDRD) error.n");
close(fd);
exit(-1);
}
if(ioctl(fd,I2C_SETDELAY,&Delay)<0) //time Delay
{
printf("EX_GPIO: ioctl (Delay us) error.n");
close(fd);
exit(-1);
}
}
Bit = atoi(argv[1]);
//I2C_Read_NoSubAddr(&I2C,data,2);
/*****************************************************/
if(lseek(fd,I2C_RdAddr,SEEK_SET)<0)
{
printf("EX_GPIO: lseek read error.n");
close(fd);
exit(-1);
}
if(read(fd,&data,2)<0)
{
printf("EX_GPIO: read error.n");
close(fd);
exit(-1);
}
/*****************************************************/
Data1 = (unsigned int)(data[0]) + (((unsigned int)(data[1])) << 8);
if(Bit > 7)
Bit -= 2;
enable = atoi(argv[2]);
if(enable)
{
Data1 |= (1 << (Bit));
}
else
{
Data1 &= (~(((1 << (Bit)))));
}
data[0] = Data1 & 0x00FF;
data[1] = (Data1 >> 8) & 0x00FF;
//I2C_Write_NoSubAddr(&I2C,data,2);
/*****************************************************/
if(lseek(fd,I2C_WrAddr,SEEK_SET)<0)
{
printf("EX_GPIO: lseek write error.n");
close(fd);
exit(-1);
}
if(write(fd,&data,2)<0)
{ printf("fd is %dn",fd);
printf("EX_GPIO: write error.n");
printf("EX_GPIO :%sn",strerror(errno));
close(fd);
exit(-1);
}
/*****************************************************/
close(fd);
fprintf(stderr, "set expander GPIO %ld %sn", Bit, (enable? "high": "low"));
return 0;
}
文中有乱码,我已替你禁用 Smiles
platinum
[ 本帖最后由 platinum 于 2005-11-11 11:52 编辑 ] |
|