348 lines
7.6 KiB
C
Executable File
348 lines
7.6 KiB
C
Executable File
#include <stdio.h>
|
|
#include <linux/types.h>
|
|
#include <stdlib.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <sys/ioctl.h>
|
|
#include <errno.h>
|
|
#include <linux/i2c.h>
|
|
#include <linux/i2c-dev.h>
|
|
|
|
#define FY_TRUE 1
|
|
#define FY_FALSE 0
|
|
#define write_ctl 0
|
|
|
|
|
|
#define IS_TENBIT 1 //I2C_TENBIT
|
|
#define TIMEOUT 0xa //I2C_TIMEOUT
|
|
#define RETRIES 0x3 //I2C_RETRIES
|
|
#define slave_addr 0x50 //I2C_SLAVE_FORCE
|
|
#define data_addr 0x00
|
|
#define data_len 0x8
|
|
unsigned char reg[9] = {data_addr,0xa1,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8};
|
|
unsigned char val[8] = {0};
|
|
|
|
|
|
int SAMPLE_I2C_Transfer_7bit(void)
|
|
{
|
|
struct i2c_rdwr_ioctl_data i2c_data;
|
|
struct i2c_msg msgs[2] = {{0}};
|
|
|
|
int ret,fd,i;
|
|
|
|
fd = open("/dev/i2c-0",O_RDWR);
|
|
if(fd < 0)
|
|
printf("open i2c-0 error\n");
|
|
|
|
i2c_data.nmsgs = 1;
|
|
i2c_data.msgs = msgs;
|
|
|
|
#if write_ctl
|
|
/* write */
|
|
i2c_data.msgs[0].addr = slave_addr;
|
|
i2c_data.msgs[0].len = data_len + 1;
|
|
i2c_data.msgs[0].flags = 0;
|
|
i2c_data.msgs[0].buf = reg;
|
|
|
|
ret = ioctl(fd, I2C_RDWR, &i2c_data);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to write eeprom register!");
|
|
#endif
|
|
usleep(10000);
|
|
|
|
/* read */
|
|
i2c_data.nmsgs = 2;
|
|
reg[0] = data_addr;
|
|
|
|
i2c_data.msgs[0].len = 1;
|
|
i2c_data.msgs[0].addr = slave_addr;
|
|
i2c_data.msgs[0].flags = 0;//write
|
|
i2c_data.msgs[0].buf = reg;
|
|
|
|
i2c_data.msgs[1].len = data_len;
|
|
i2c_data.msgs[1].addr = slave_addr;
|
|
i2c_data.msgs[1].flags = I2C_M_RD;//read
|
|
i2c_data.msgs[1].buf = val;
|
|
|
|
ret = ioctl(fd, I2C_RDWR, &i2c_data);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to read eeprom register!");
|
|
|
|
for(i = 0;i < data_len;i++)
|
|
printf("val[%d] = 0x%x\n",i,val[i]);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int SAMPLE_I2C_Transfer_10bit(void)
|
|
{
|
|
struct i2c_rdwr_ioctl_data i2c_data;
|
|
struct i2c_msg msgs[2] = {{0}};
|
|
|
|
int ret,fd,i;
|
|
|
|
fd = open("/dev/i2c-0",O_RDWR);
|
|
if(fd < 0)
|
|
printf("open i2c-0 error\n");
|
|
|
|
ret = ioctl(fd, I2C_TENBIT, IS_TENBIT);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to set address mode!");
|
|
|
|
printf("set address mode success\n");
|
|
if(IS_TENBIT)
|
|
printf("Now is 10bit address mode\n");
|
|
else
|
|
printf("Now is not 10bit address mode\n");
|
|
|
|
|
|
i2c_data.nmsgs = 1;
|
|
i2c_data.msgs = msgs;
|
|
|
|
#if write_ctl
|
|
/* write */
|
|
i2c_data.msgs[0].addr = slave_addr;
|
|
i2c_data.msgs[0].len = data_len + 1;
|
|
i2c_data.msgs[0].flags = 0 | I2C_M_TEN;
|
|
i2c_data.msgs[0].buf = reg;
|
|
|
|
ret = ioctl(fd, I2C_RDWR, &i2c_data);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to write eeprom register!");
|
|
#endif
|
|
usleep(10000);
|
|
|
|
/* read */
|
|
i2c_data.nmsgs = 2;
|
|
|
|
i2c_data.msgs[0].len = 1;
|
|
i2c_data.msgs[0].addr = slave_addr;
|
|
i2c_data.msgs[0].flags = 0 | I2C_M_TEN;//write
|
|
i2c_data.msgs[0].buf = reg;
|
|
|
|
i2c_data.msgs[1].len = data_len;
|
|
i2c_data.msgs[1].addr = slave_addr;
|
|
i2c_data.msgs[1].flags = I2C_M_RD | I2C_M_TEN;//read
|
|
i2c_data.msgs[1].buf = val;
|
|
|
|
ret = ioctl(fd, I2C_RDWR, &i2c_data);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to read eeprom register!");
|
|
|
|
for(i = 0;i < data_len;i++)
|
|
printf("val[%d] = 0x%x\n",i,val[i]);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int SAMPLE_I2C_IO_I2C_SLAVE_FORCE(void)
|
|
{
|
|
struct i2c_rdwr_ioctl_data i2c_data;
|
|
struct i2c_msg msgs[2] = {{0}};
|
|
|
|
int ret,fd,i;
|
|
|
|
fd = open("/dev/i2c-0",O_RDWR);
|
|
if(fd < 0)
|
|
printf("open i2c-0 error\n");
|
|
|
|
ret = ioctl(fd, I2C_SLAVE_FORCE, slave_addr);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to set slave address!");
|
|
|
|
printf("set slave address success,slave_addr is 0x%x\n",slave_addr);
|
|
|
|
i2c_data.nmsgs = 1;
|
|
i2c_data.msgs = msgs;
|
|
|
|
#if write_ctl
|
|
/* write */
|
|
i2c_data.msgs[0].addr = slave_addr;
|
|
i2c_data.msgs[0].len = data_len + 1;
|
|
i2c_data.msgs[0].flags = 0;
|
|
i2c_data.msgs[0].buf = reg;
|
|
|
|
ret = ioctl(fd, I2C_RDWR, &i2c_data);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to write eeprom register!");
|
|
#endif
|
|
usleep(10000);
|
|
|
|
/* read */
|
|
i2c_data.nmsgs = 2;
|
|
reg[0] = data_addr;
|
|
|
|
i2c_data.msgs[0].len = 1;
|
|
i2c_data.msgs[0].addr = slave_addr;
|
|
i2c_data.msgs[0].flags = 0;//write
|
|
i2c_data.msgs[0].buf = reg;
|
|
|
|
i2c_data.msgs[1].len = data_len;
|
|
i2c_data.msgs[1].addr = slave_addr;
|
|
i2c_data.msgs[1].flags = I2C_M_RD;//read
|
|
i2c_data.msgs[1].buf = val;
|
|
|
|
ret = ioctl(fd, I2C_RDWR, &i2c_data);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to read eeprom register!");
|
|
|
|
for(i = 0;i < data_len;i++)
|
|
printf("val[%d] = 0x%x\n",i,val[i]);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int SAMPLE_I2C_IO_I2C_TIMEOUT(void)
|
|
{
|
|
struct i2c_rdwr_ioctl_data i2c_data;
|
|
struct i2c_msg msgs[2] = {{0}};
|
|
|
|
int ret,fd,i;
|
|
|
|
fd = open("/dev/i2c-0",O_RDWR);
|
|
if(fd < 0)
|
|
printf("open i2c-0 error\n");
|
|
|
|
ret = ioctl(fd, I2C_TIMEOUT, TIMEOUT);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to set transfer timeout!");
|
|
|
|
printf("set transfer timeout success,value is 0x%x\n",TIMEOUT);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int SAMPLE_I2C_IO_I2C_RETRIES(void)
|
|
{
|
|
struct i2c_rdwr_ioctl_data i2c_data;
|
|
struct i2c_msg msgs[2] = {{0}};
|
|
|
|
int ret,fd,i;
|
|
|
|
fd = open("/dev/i2c-0",O_RDWR);
|
|
if(fd < 0)
|
|
printf("open i2c-0 error\n");
|
|
|
|
ret = ioctl(fd, I2C_RETRIES, RETRIES);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to set retries count!");
|
|
|
|
printf("set retries count success,value is 0x%x\n",RETRIES);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int SAMPLE_I2C_IO_I2C_FUNCS(void)
|
|
{
|
|
struct i2c_rdwr_ioctl_data i2c_data;
|
|
struct i2c_msg msgs[2] = {{0}};
|
|
unsigned long i2c_func;
|
|
int ret,fd,i;
|
|
|
|
fd = open("/dev/i2c-0",O_RDWR);
|
|
if(fd < 0)
|
|
printf("open i2c-0 error\n");
|
|
|
|
ret = ioctl(fd, I2C_FUNCS, &i2c_func);
|
|
if(ret < 0)
|
|
perror("ERROR: Unable to read eeprom register!");
|
|
|
|
printf("Adapter Functionality value is 0x%x\n",i2c_func);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
void SAMPLE_I2C_Usage(void)
|
|
{
|
|
printf("\n");
|
|
printf("----------------------------------------------------------------\n");
|
|
printf( " XC01: I2C Test" "\n");
|
|
printf("----------------------------------------------------------------\n");
|
|
printf("\n");
|
|
|
|
printf("please choose the case which you want to run:\n");
|
|
printf("\t1: 7bit Address For Transfer (Support 100k,400k,1M)\n");
|
|
printf("\t2: 10bit Address For Transfer (Support 100k,400k,1M)\n");
|
|
printf("\t3: Set Slave Address\n");
|
|
printf("\t4: Set Transfer Timeout (Default is 10ms)\n");
|
|
printf("\t5: Set Retries Count (Default is 3)\n");
|
|
printf("\t6: Get Adapter Functionality\n");
|
|
|
|
printf("\n");
|
|
printf("================================================================\n");
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
char ch;
|
|
int bExit = FY_FALSE;
|
|
|
|
while (1)
|
|
{
|
|
SAMPLE_I2C_Usage();
|
|
ch = getchar();
|
|
if(10 == ch)
|
|
{
|
|
continue;
|
|
}
|
|
getchar();
|
|
switch (ch)
|
|
{
|
|
case '1':
|
|
{
|
|
SAMPLE_I2C_Transfer_7bit();
|
|
break;
|
|
}
|
|
case '2':
|
|
{
|
|
SAMPLE_I2C_Transfer_10bit();
|
|
break;
|
|
}
|
|
case '3':
|
|
{
|
|
SAMPLE_I2C_IO_I2C_SLAVE_FORCE();
|
|
break;
|
|
}
|
|
case '4':
|
|
{
|
|
SAMPLE_I2C_IO_I2C_TIMEOUT();
|
|
break;
|
|
}
|
|
case '5':
|
|
{
|
|
SAMPLE_I2C_IO_I2C_RETRIES();
|
|
break;
|
|
}
|
|
case '6':
|
|
{
|
|
SAMPLE_I2C_IO_I2C_FUNCS();
|
|
break;
|
|
}
|
|
case 'q':
|
|
case 'Q':
|
|
{
|
|
bExit = FY_TRUE;
|
|
break;
|
|
}
|
|
default :
|
|
{
|
|
printf("input invaild! please try again.\n");
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (bExit)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|