#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "i2c-rw.h"
static int fd;
static char *chips = NULL;
int main(int argc, char *argv[])
{
static char *dev_name = NULL;
int slave_address,address_number,page_size;
printf("i2c read and write on satir !\n");
dev_name = "/dev/i2c-rw";
if(argc <= 1)
{
printf("false chips to selest is failed !\n");
exit(1);
}
else
if(argc > 1)
{
if ((fd = open(dev_name,O_RDWR)) < 0)
{
printf("open %s on %s is failed !\n",chips,dev_name);
exit(1);
}
if(strcmp(argv[1],"7172")==0)
{
chips = "ADV7172";
slave_address = 0xd4;
address_number = 1;
page_size = 32;
config_i2c_device(slave_address, address_number,page_size);
init_adv7172();
}
else
if(strcmp(argv[1],"5150")==0)
{
chips = "TVP5150";
slave_address = 0xba;
address_number = 1;
page_size = 32;
config_i2c_device(slave_address, address_number,page_size);
init_tvp5150();
}
else
if(strcmp(argv[1],"2464")==0)
{
chips = "24LC64";
slave_address = 0xa0;
address_number = 2;
page_size = 32;
config_i2c_device(slave_address, address_number,page_size);
init_24lc64();
}
else
if(strcmp(argv[1],"8973")==0)
{
chips = "WM8973";
slave_address = 0x34;
address_number = 1;
page_size = 32;
config_i2c_device(slave_address, address_number,page_size);
init_wm8973();
}
else
if(strcmp(argv[1],"7102")==0)
{
chips = "SAA7102";
slave_address = 0x88;
address_number = 1;
page_size = 254;
config_i2c_device(slave_address, address_number,page_size);
init_saa7102();
}
else
{
printf("false chips to selest is failed !\n");
}
close(fd);
printf("\n");
exit(0);
}
}
int config_i2c_device(int slave_address, int address_number, int page_size)
{
ioctl(fd,SET_I2C_SLAVE_ADDRESS,&slave_address);
ioctl(fd,SET_I2C_ADDRESS_NUMBER,&address_number);
ioctl(fd,SET_I2C_PAGE_SIZE,&page_size);
}
int write_i2c(int address,char *write_buf,int length)
{
int i;
if(ioctl(fd,SET_I2C_ADDRESS,&address) < 0)
{
printf("\nfalse on set %s address at 0x%4x !\n",chips, address);
}
else
{
printf("\nset %s address is 0x%4x !\n",chips,address);
}
if(write(fd,write_buf,length) < 0 )
{
printf("\nfalse on write %4d bytes on %s at 0x%4x !\n\n",length,chips,address);
}
else
{
printf("\nwrite %4d bytes on %s at 0x%4x success !\n\n",length,chips,address);
for(i=0;i<length;i++)
{
printf("write_buf[0x%02lx] = 0x%02lx ; ",address++,write_buf[i]);
if((i%4) == 3)
printf("\n");
}
}
}
int read_i2c(int address,char *read_buf,int length)
{
int i;
if(ioctl(fd,SET_I2C_ADDRESS,&address) < 0)
{
printf("\nfalse on set %s address at 0x%4x !\n",chips, address);
}
else
{
printf("\nset %s address is 0x%4x !\n",chips,address);
}
if(read(fd,read_buf,length) < 0)
{
printf("\nfalse on read %4d bytes on %s at 0x%4x !\n\n",length,chips,address);
}
else
{
printf("\nread %4d bytes on %s at 0x%4x success !\n\n",length,chips,address);
for(i=0;i<length;i++)
{
printf("read_buf[0x%02lx] = 0x%02lx ; ",address++,read_buf[i]);
if((i%4) == 3)
printf("\n");
}
}
}
int init_24lc64(void)
{
char read_buf[254],write_buf[254];
int i,address,length;
printf("select chips to operate is %s !\n",chips);
address = 0;
length =200;
read_i2c(address,read_buf,length);
for(i=0;i<length;i++)
{
write_buf[i] = i;
}
write_i2c(address,write_buf,length);
read_i2c(address,read_buf,length);
for(i=0;i<length;i++)
{
write_buf[i] = 0xff - ( i & 0xff);
}
write_i2c(address,write_buf,length);
address = 0;
if(ioctl(fd,GET_I2C_ADDRESS,&address) < 0)
{
printf("\nfalse on get %s address !\n",chips);
}
else
{
printf("\nget %s address is 0x%4x !\n",chips,address);
}
return 0;
}
int init_adv7172(void)
{
char read_buf[254],write_buf[254];
int i,address,length;
printf("select chips to operate is %s !\n",chips);
address = 0;
length = 0x23;
read_i2c(address,read_buf,length);
printf("\n");
write_buf[0] = 0x15; write_buf[1] = 0x67; write_buf[2] = 0x6e; write_buf[3] = 0x0 ;
write_buf[4] = 0x0 ; write_buf[5] = 0x0 ; write_buf[6] = 0x01; write_buf[7] = 0x0 ;
write_buf[8] = 0x0 ; write_buf[9] = 0x0 ;
write_buf[0xa] = 0x0d; write_buf[0xb] = 0x0 ; write_buf[0xc] = 0xcb;
write_buf[0xd] = 0x8a; write_buf[0xe] = 0x09; write_buf[0xf] = 0x2a ;
write_buf[0x10] = 0x0 ; write_buf[0x11] = 0x0 ; write_buf[0x12] = 0x0 ; write_buf[0x13] = 0x0 ;
write_buf[0x14] = 0x0 ; write_buf[0x15] = 0x0 ; write_buf[0x16] = 0x0 ; write_buf[0x17] = 0x0 ;
write_buf[0x18] = 0x0 ; write_buf[0x19] = 0x0 ; write_buf[0x1a] = 0x0 ; write_buf[0x1b] = 0x0 ;
write_buf[0x1c] = 0x0 ; write_buf[0x1d] = 0x0 ; write_buf[0x1e] = 0x0 ; write_buf[0x1f] = 0x0 ;
write_buf[0x20] = 0x0 ; write_buf[0x21] = 0x0 ; write_buf[0x22] = 0x0 ;
write_i2c(address,write_buf,length);
printf("\n");
read_i2c(address,read_buf,length);
printf("\n");
return 0;
}
/*
int init_tvp5150(void)
{
char read_buf[254],write_buf[254];
int i,address,length;
address = 0x04;
length = 0x01;
read_i2c(address,read_buf,length);
printf("\n");
}
*/
int init_tvp5150(void)
{
char read_buf[254],write_buf[254];
int i,address,length;
printf("select chips to operate is %s !\n",chips);
address = 0;
length = 0x31;
//length = 0xfd;
read_i2c(address,read_buf,length);
printf("\n");
sleep(1);
for(i=0;i<=length;i++)
{
write_buf[i] = read_buf[i];//0xff;
}
write_buf[0x0 ] = 0x0 ;
write_buf[0x01] = 0x15 ;
write_buf[0x02] = 0x40;//60 ;
write_buf[0x03] = 0x8f ;
write_buf[0x04] = 0xdc ;
write_buf[0x05] = 0x0 ;
write_buf[0x06] = 0x10;
write_buf[0x07] = 0x60 ;
write_buf[0x08] = 0x0 ;
write_buf[0x09] = 0x80 ;
write_buf[0x0a] = 0x80;
write_buf[0x0b] = 0x0 ;
write_buf[0x0c] = 0x80 ;
write_buf[0x0d] = 0x07;//47 ;
write_buf[0x0e] = 0x0 ;
write_buf[0x0f] = 0x12 ;
//10 is reserved
write_buf[0x11] = 0x0 ;
write_buf[0x12] = 0x0 ;
write_buf[0x13] = 0x0 ;
write_buf[0x14] = 0x0 ;
write_buf[0x15] = 0x01 ;
write_buf[0x16] = 0x80 ;
//17 is reserved
write_buf[0x18] = 0x0 ;
write_buf[0x19] = 0x0 ;
write_buf[0x1a] = 0x0c ;
write_buf[0x1b] = 0x14 ;
write_buf[0x1c] = 0x0 ;
write_buf[0x1d] = 0x0 ;
write_buf[0x1e] = 0x0 ;
//1f-27 is reserved
//write_buf[0x28] = 0x0 ;
//29-2b is reserved
//2c-2d is read only
//write_buf[0x2e] = 0x0f ;
//write_buf[0x2f] = 0x01 ;
//write_buf[0x30] = 0x0 ;
//31-7f is reserved
//write_buf[0x80] = 0x51 ;
//write_buf[0x81] = 0x50 ;
//write_buf[0x82] = 0x04 ;
//write_buf[0x83] = 0x0 ;
//84-8c is read only
//8d-8f is reserved
//90-b0 is read only
//write_buf[0xb1] = 0x0 ;
//write_buf[0xb2] = 0x0 ;
//write_buf[0xb3] = 0x0 ;
//write_buf[0xb4] = 0x0 ;
//write_buf[0xb5] = 0x0 ;
//write_buf[0xb6] = 0x0 ;
//write_buf[0xb7] = 0x0 ;
//write_buf[0xb8] = 0x0 ;
//write_buf[0xb9] = 0x0 ;
//write_buf[0xba] = 0x0 ;
//write_buf[0xbb] = 0x0 ;
//bc-bf is reserved
//write_buf[0xc0] = 0x0 ;
//write_buf[0xc1] = 0x0 ;
//write_buf[0xc2] = 0x04 ;
//write_buf[0xc3] = 0xdc ;
//write_buf[0xc4] = 0x0f ;
//write_buf[0xc5] = 0x0 ;
//c6-c7 is read only
//write_buf[0xc8] = 0x80 ;
//write_buf[0xc9] = 0x0 ;
//write_buf[0xca] = 0x0 ;
//write_buf[0xcb] = 0x4e ;
//write_buf[0xcc] = 0x0 ;
//write_buf[0xcd] = 0x01 ;
//ce is reserved
//write_buf[0xcf] = 0x0 ;
//write_buf[0xd0] = 0x0 ;
//d1-fb is 0xff
//write_buf[0xfc] = 0x7f ;
//fd-ff is reserved
addre