.c
#include <stdio.h> #include <string.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-dev.h> #include <linux/i2c.h> #include "PCA9698.h" /************************************************************************ * 函数说明: * 往i2c设备写数据,写一个字节 * 参数说明: * 1. fd: 文件描述符 * 2. addr: i2c设备地址 * 3. offset: i2c设备内寄存器偏移地址 * 4. buf: 要写入的字节 * 返回值: * 正常返回0,出错,返回错误码; ************************************************************************/ int PCA9698_write_oneport (int fd, int addr, int offset, unsigned char buf) { ; struct i2c_rdwr_ioctl_data PCA9698_data; PCA9698_data.nmsgs = ; /* 每次只写一个字节 */ PCA9698_data.msgs = (struct i2c_msg*)malloc(PCA9698_data.nmsgs*sizeof(struct i2c_msg)); if(!PCA9698_data.msgs) { perror("PCA9698_write_oneport function malloc error.\n"); ; } ioctl(fd,I2C_TIMEOUT,); /*超时时间*/ ioctl(fd,I2C_RETRIES,); /*重复次数*/ (PCA9698_data.msgs[]).buf = (unsigned ); (PCA9698_data.msgs[]).len = ; (PCA9698_data.msgs[]).addr = addr; //i2c 设备地址 (PCA9698_data.msgs[]).flags = ; //write (PCA9698_data.msgs[]).buf[] = (unsigned char)(offset&0x7f); // i2c 写入目标的地址 (PCA9698_data.msgs[]).buf[] = (unsigned char)buf; //the data to write ret = ioctl(fd, I2C_RDWR, (unsigned long)&PCA9698_data); ) { perror("PCA9698_write_oneport ioctl error\n"); return ret; } ]).buf); free(PCA9698_data.msgs); return ret; } /************************************************************************* * 函数说明: * 从i2c设备读数据,读一个字节 * 参数说明: * 1. fd: 文件描述符 * 2. addr: i2c设备地址 * 3. offset: i2c设备内寄存器偏移地址 * 4. buf: 保存读出的字节指针 * 返回值: * 正常返回0,出错,返回错误码; ************************************************************************/ int PCA9698_read_oneport (int fd, int addr, int offset, unsigned char *buf) { int ret; struct i2c_rdwr_ioctl_data PCA9698_data; PCA9698_data.nmsgs = ; PCA9698_data.msgs = (struct i2c_msg*)malloc(PCA9698_data.nmsgs*sizeof(struct i2c_msg)); if(!PCA9698_data.msgs) { perror("PCA9698_read_oneport functionmalloc error\n"); exit(); } ioctl(fd,I2C_TIMEOUT,); /*超时时间*/ ioctl(fd,I2C_RETRIES,); /*重复次数*/ (PCA9698_data.msgs[]).len = ; //i2c 目标数据的地址 (PCA9698_data.msgs[]).addr = addr; // i2c 设备地址 (PCA9698_data.msgs[]).flags = ; //write (PCA9698_data.msgs[]).buf = (unsigned ); (PCA9698_data.msgs[]).buf[] = (unsigned char)(offset&0x7f); //i2c 数据地址 (PCA9698_data.msgs[]).len = ; //读出的数据 (PCA9698_data.msgs[]).addr = addr; // i2c 设备地址 (PCA9698_data.msgs[]).flags = I2C_M_RD; //read (PCA9698_data.msgs[]).buf = (unsigned );//存放返回值的地址。 (PCA9698_data.msgs[]).buf[] = ; //初始化读缓冲 ret = ioctl(fd, I2C_RDWR, (unsigned long)&PCA9698_data); ) { perror("PCA9698_read_oneport ioctl error.\n"); return ret; } //printf("buff[0] = %x\n",PCA9698_data.msgs[1].buf[0]); *buf = PCA9698_data.msgs[].buf[]; ]).buf); ]).buf); free(PCA9698_data.msgs); return ret; } /************************************************************************ * 函数说明: * 往i2c设备写数据,写一系列字节 * 参数说明: * 1. fd: 文件描述符 * 2. addr: i2c设备地址 * 3. offset: i2c设备内寄存器偏移地址 * 4. buf: 要写入的字节 * 返回值: * 正常返回0,出错,返回错误码; ************************************************************************/ int PCA9698_write_ports (int fd, int addr, int offset, unsigned char *buf) { ; ; struct i2c_rdwr_ioctl_data PCA9698_data; PCA9698_data.nmsgs = ; /* 每次只写一个字节 */ PCA9698_data.msgs = (struct i2c_msg*)malloc(PCA9698_data.nmsgs*sizeof(struct i2c_msg)); if(!PCA9698_data.msgs) { perror("PCA9698_write_ports function malloc error.\n"); ; } ioctl(fd,I2C_TIMEOUT,); /*超时时间*/ ioctl(fd,I2C_RETRIES,); /*重复次数*/ (PCA9698_data.msgs[]).buf = (unsigned ); (PCA9698_data.msgs[]).len = ; (PCA9698_data.msgs[]).addr = addr; //i2c 设备地址 (PCA9698_data.msgs[]).flags = ; //write (PCA9698_data.msgs[]).buf[] = (unsigned char)(offset|0x80); // i2c 写入目标的地址 ;num<;num++) (PCA9698_data.msgs[]).buf[+num] = (unsigned char)*(buf+num); //the data to write ret = ioctl(fd, I2C_RDWR, (unsigned long)&PCA9698_data); ) { perror("PCA9698_write_ports ioctl error\n"); return ret; } ]).buf); free(PCA9698_data.msgs); return ret; } /************************************************************************* * 函数说明: * 从i2c设备读数据,读一系列字节 * 参数说明: * 1. fd: 文件描述符 * 2. addr: i2c设备地址 * 3. offset: i2c设备内寄存器偏移地址 * 4. buf: 保存读出的字节指针 * 返回值: * 正常返回0,出错,返回错误码; ************************************************************************/ int PCA9698_read_ports (int fd, int addr, int offset, unsigned char *buf) { int ret; ; struct i2c_rdwr_ioctl_data PCA9698_data; PCA9698_data.nmsgs = ; PCA9698_data.msgs = (struct i2c_msg*)malloc(PCA9698_data.nmsgs*sizeof(struct i2c_msg)); if(!PCA9698_data.msgs) { perror("PCA9698_read_ports functionmalloc error\n"); exit(); } ioctl(fd,I2C_TIMEOUT,); /*超时时间*/ ioctl(fd,I2C_RETRIES,); /*重复次数*/ (PCA9698_data.msgs[]).len = ; //i2c 目标数据的地址 (PCA9698_data.msgs[]).addr = addr; // i2c 设备地址 (PCA9698_data.msgs[]).flags = ; //write (PCA9698_data.msgs[]).buf = (unsigned ); (PCA9698_data.msgs[]).buf[] = (unsigned char)(offset|0x80); //i2c 数据地址 (PCA9698_data.msgs[]).len = ; //读出的数据 (PCA9698_data.msgs[]).addr = addr; // i2c 设备地址 (PCA9698_data.msgs[]).flags = I2C_M_RD; //read (PCA9698_data.msgs[]).buf = (unsigned );//存放返回值的地址。 ;num<;num++) (PCA9698_data.msgs[]).buf[num] = ; //初始化读缓冲 ret = ioctl(fd, I2C_RDWR, (unsigned long)&PCA9698_data); ) { perror("PCA9698_read_ports ioctl error.\n"); return ret; } //printf("buff[0] = %x\n",PCA9698_data.msgs[1].buf[0]); ;num<;num++) *(buf+num) = PCA9698_data.msgs[].buf[num]; ]).buf); ]).buf); free(PCA9698_data.msgs); return ret; } /************************************************************************ * 函数说明: * 往i2c设备写数据,写一位 * 参数说明: * 1. fd: 文件描述符 * 2. p_i: 端口号 0对应P0_0,11对应P1_1 * 3. H_L:高低 --1 -0 * 返回值: * 正常返回0,出错,返回错误码; *注意: * 使用前需要使用PCA9698_write_oneport或者PCA9698_write_ports做方向初始化 ************************************************************************/ int PCA9698_write_onebit (int fd, int p_i, unsigned char H_L) { ; unsigned char buf; unsigned char temp; int offset_in; int offset_out; struct i2c_rdwr_ioctl_data PCA9698_data; //解析地址以及数据 ) { : offset_in=PCA9688_P0_REG_IN; offset_out=PCA9688_P0_REG_OUT; break; : offset_in=PCA9688_P1_REG_IN; offset_out=PCA9688_P1_REG_OUT; //printf("P1\n"); break; : offset_in=PCA9688_P2_REG_IN; offset_out=PCA9688_P2_REG_OUT; break; : offset_in=PCA9688_P3_REG_IN; offset_out=PCA9688_P3_REG_OUT; break; : offset_in=PCA9688_P4_REG_IN; offset_out=PCA9688_P4_REG_OUT; break; } ret=PCA9698_read_oneport (fd,PCA9698_ADDR,offset_in,&buf); ) { perror("get data from port err\n"); return ret; } ) { :temp=0x01;break; :temp=0x02;break; :temp=0x04;break; :temp=0x08;break; :temp=0x10;break; :temp=0x20;break; :temp=0x40;break; :temp=0x80;break; } ) buf|=temp; ) buf&=~temp; else ; PCA9698_data.nmsgs = ; /* 每次只写一个字节 */ PCA9698_data.msgs = (struct i2c_msg*)malloc(PCA9698_data.nmsgs*sizeof(struct i2c_msg)); if(!PCA9698_data.msgs) { perror("PCA9698_write_onebit function malloc error.\n"); ; } ioctl(fd,I2C_TIMEOUT,); /*超时时间*/ ioctl(fd,I2C_RETRIES,); /*重复次数*/ (PCA9698_data.msgs[]).buf = (unsigned ); (PCA9698_data.msgs[]).len = ; (PCA9698_data.msgs[]).addr = PCA9698_ADDR; //i2c 设备地址 (PCA9698_data.msgs[]).flags = ; //write (PCA9698_data.msgs[]).buf[] = (unsigned char)(offset_out&0x7f); // i2c 写入目标的地址 (PCA9698_data.msgs[]).buf[] = (unsigned char)buf; //the data to write ret = ioctl(fd, I2C_RDWR, (unsigned long)&PCA9698_data); ) { perror("PCA9698_write_onebit ioctl error\n"); return ret; } ]).buf); free(PCA9698_data.msgs); return ret; } /************************************************************************* * 函数说明: * 从i2c设备读数据,读一位 * 参数说明: * 1. fd: 文件描述符 * 2. p_i: 端口号 0对应P0_0,11对应P1_1 * 3. value:返回值 高低--1-0 * 返回值: * 正常返回0,出错,返回错误码; *注意: * 使用前需要使用PCA9698_write_oneport或者PCA9698_write_ports做方向初始化 ************************************************************************/ int PCA9698_read_onebit (int fd, int p_i, unsigned char *value) { int ret; struct i2c_rdwr_ioctl_data PCA9698_data; int offset; unsigned char temp; //解析地址以及数据 ) { : offset=PCA9688_P0_REG_IN; break; : offset=PCA9688_P1_REG_IN; break; : offset=PCA9688_P2_REG_IN; break; : offset=PCA9688_P3_REG_IN; break; : offset=PCA9688_P4_REG_IN; break; } PCA9698_data.nmsgs = ; PCA9698_data.msgs = (struct i2c_msg*)malloc(PCA9698_data.nmsgs*sizeof(struct i2c_msg)); if(!PCA9698_data.msgs) { perror("PCA9698_read_onebit functionmalloc error\n"); exit(); } ioctl(fd,I2C_TIMEOUT,); /*超时时间*/ ioctl(fd,I2C_RETRIES,); /*重复次数*/ (PCA9698_data.msgs[]).len = ; //i2c 目标数据的地址 (PCA9698_data.msgs[]).addr = PCA9698_ADDR; // i2c 设备地址 (PCA9698_data.msgs[]).flags = ; //write (PCA9698_data.msgs[]).buf = (unsigned ); (PCA9698_data.msgs[]).buf[] = (unsigned char)(offset&0x7f); //i2c 数据地址 (PCA9698_data.msgs[]).len = ; //读出的数据 (PCA9698_data.msgs[]).addr = PCA9698_ADDR; // i2c 设备地址 (PCA9698_data.msgs[]).flags = I2C_M_RD; //read (PCA9698_data.msgs[]).buf = (unsigned );//存放返回值的地址。 (PCA9698_data.msgs[]).buf[] = ; //初始化读缓冲 ret = ioctl(fd, I2C_RDWR, (unsigned long)&PCA9698_data); ) { perror("PCA9698_read_onebit ioctl error.\n"); return ret; } //printf("buff[0] = %x\n",PCA9698_data.msgs[1].buf[0]); temp = PCA9698_data.msgs[].buf[]; ) { :*value=temp&0x01;break; :*value=(temp>>)&0x01;break; :*value=(temp>>)&0x01;break; :*value=(temp>>)&0x01;break; :*value=(temp>>)&0x01;break; :*value=(temp>>)&0x01;break; :*value=(temp>>)&0x01;break; :*value=(temp>>)&0x01;break; } ]).buf); ]).buf); free(PCA9698_data.msgs); return ret; } /************************************************************************ * 函数说明: * 往i2c设备写数据,设置中断 * 参数说明: * 1. fd: 文件描述符 * 2.port 0-4 * 3.state 0-on 1-off * 返回值: * 正常返回0,出错,返回错误码; *注意: * 中断仅当IO输入时有效,发生中断后,读取输入寄存器,中断自动清除 ************************************************************************/ int PCA9698_set_int (int fd, int port,unsigned char state) { ; int offset; unsigned char temp; switch(port) { :offset=PCA9688_P0_REG_INT;break; :offset=PCA9688_P1_REG_INT;break; :offset=PCA9688_P2_REG_INT;break; :offset=PCA9688_P3_REG_INT;break; :offset=PCA9688_P4_REG_INT;break; } ) temp=0x00; ) temp=0xff; ret=PCA9698_write_oneport(fd,PCA9698_ADDR,offset,temp); return ret; } /************************************************************************ * 函数说明: * 往i2c设备写数据,设置中断 * 参数说明: * 1. fd: 文件描述符 * 2.port 0-4 * 3.buf P0 0-7 P1 10-17 * 返回值: * 正常返回0,出错,返回错误码; *注意: * 放在IO中断服务程序中,查询IN状态,返回产生中断的IO的键值 ************************************************************************/ int PCA9698_int_irq (int fd, int port,int *buf) { ; int offset; unsigned char temp; ]={-,-,-,-,-,-,-,-}; ,i=,j=; switch(port) { :offset=PCA9688_P0_REG_IN;break; :offset=PCA9688_P1_REG_IN;break; :offset=PCA9688_P2_REG_IN;break; :offset=PCA9688_P3_REG_IN;break; :offset=PCA9688_P4_REG_IN;break; } ret=PCA9698_read_oneport(fd,PCA9698_ADDR,offset,&temp); //printf("temp=%d\n",temp); ;num<;num++) { if((temp>>num)&0x01) { temp1[i]=num; //printf("temp1[%d]=%d\n",i,num); i++; } } ;j<i;j++) temp1[j]=temp1[j]+port*; -i;j++) temp1[j]=-; ;num<;num++) { *(buf+num)=temp1[num]; //printf("temp1[%d]=%d\n",num,temp1[num]); } return ret; }
.h
/************************************************************************************** * 声明: * 这个函数库主要用于i2c读写PCA9698,提供了以下功能: * * write by tla001 2016-1-30 **************************************************************************************/ #ifndef __PCA9698_H__ #define __PCA9698_H__ /** *AD0 AD1 AD2 PCA9698_ADDR *0 0 0 0x40 *1 1 1 0x4E */ #define PCA9698_ADDR 0x40>>1 /**********control register addr*********************************** * *PCA9688_CON_OUTMODE 输出结构配置寄存器--控制端口配置为开漏或图腾 *PCA9688_CON_ALLBANK 控制全组寄存器--所有IO输出相同电平 *PCA9688_CON_MODESEL 模式选择寄存器--16种模式 * ********************************************************************/ #define PCA9688_CON_OUTMODE 0x28 #define PCA9688_CON_ALLBANK 0x29 #define PCA9688_CON_MODESEL 0x2a /**********port register addr**************************************** * *REG_IN 只读寄存器, 反应输入逻辑电平的状态 *REG_OUT 设置相应端口的输出状态 *REG_TURN 使相应的输出寄存器的极性发生反转 *REG_SET 设置 I/O 口为输出或输入 *REG_INT 设置相应输入端口响应中断 * **********************************************************************/ #define PCA9688_P0_REG_IN 0x00 #define PCA9688_P0_REG_OUT 0x08 #define PCA9688_P0_REG_TURN 0x10 #define PCA9688_P0_REG_SET 0x18 #define PCA9688_P0_REG_INT 0x20 #define PCA9688_P1_REG_IN 0x01 #define PCA9688_P1_REG_OUT 0x09 #define PCA9688_P1_REG_TURN 0x11 #define PCA9688_P1_REG_SET 0x19 #define PCA9688_P1_REG_INT 0x21 #define PCA9688_P2_REG_IN 0x02 #define PCA9688_P2_REG_OUT 0x0a #define PCA9688_P2_REG_TURN 0x12 #define PCA9688_P2_REG_SET 0x1a #define PCA9688_P2_REG_INT 0x22 #define PCA9688_P3_REG_IN 0x03 #define PCA9688_P3_REG_OUT 0x0b #define PCA9688_P3_REG_TURN 0x13 #define PCA9688_P3_REG_SET 0x1b #define PCA9688_P3_REG_INT 0x23 #define PCA9688_P4_REG_IN 0x04 #define PCA9688_P4_REG_OUT 0x0c #define PCA9688_P4_REG_TURN 0x14 #define PCA9688_P4_REG_SET 0x1c #define PCA9688_P4_REG_INT 0x24 #define array_size(array_buffer) (sizeof(array_buffer)/sizeof(*array_buffer)) int PCA9698_write_oneport (int fd, int addr, int offset, unsigned char buf); int PCA9698_read_oneport (int fd, int addr, int offset, unsigned char *buf); int PCA9698_write_ports (int fd, int addr, int offset, unsigned char *buf); int PCA9698_read_ports (int fd, int addr, int offset, unsigned char *buf); int PCA9698_read_onebit (int fd, int p_i, unsigned char *value); int PCA9698_write_onebit (int fd, int p_i, unsigned char H_L); int PCA9698_int_irq (int fd, int port,int *buf); int PCA9698_set_int (int fd, int port,unsigned char state); #endif //__PCA9698_H__
test
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <linux/i2c-dev.h> #include <linux/i2c.h> #include "PCA9698.h" int main ( int argc, char ** argv ) { int fd,ret; unsigned char buf; ; fd = open( "/dev/i2c-2", O_RDWR ); ) { perror( "open error\n" ); ; } ret==ioctl(fd,I2C_TENBIT,); //ret==ioctl(fd,I2C_SLAVE,PCA9698_ADDR); /***保留写法***没测试**** ret==ioctl(fd,I2C_SLAVE,PCA9698_ADDR>>1); if(ret<0) { printf("set slave addr err:%d\n",ret); return -1; } ret==ioctl(fd,I2C_TENBIT,0); if(ret<0) { printf("set slave addr 8bit err:%d\n",ret); return -1; } else printf("set slave addr 8bit succeed\n"); ret=write(fd,PCA9688_P0_REG_SET,1); printf("reg_set %d\n",ret); ret=write(fd,0x00,1); printf("reg_set data %d\n",ret); while(1) { for(num=0;num<5;num++) { //sleep(1); ret=write(fd,PCA9688_P0_REG_OUT+num,1); printf("reg_out %d\n",ret); ret=write(fd,0x00,1); printf("reg_out data %d\n",ret); sleep(1); write(fd,PCA9688_P0_REG_OUT+num,1); write(fd,0xff,1); //sleep(1); write(fd,PCA9688_P0_REG_OUT+num,1); read(fd,buf,1); printf("num %d buf=%d\n",num,buf); } } ***************/ /********************一次读写一个端口测试********************************************** ret=PCA9698_write_oneport(fd,PCA9698_ADDR,PCA9688_P0_REG_SET,0x00); if(ret<0) { printf("init err ret=%d\n",ret); //return 0; } while(1) { sleep(1); ret=PCA9698_write_oneport(fd,PCA9698_ADDR,PCA9688_P0_REG_OUT,0x00); if(ret<0) { printf("init err ret1=%d\n",ret); } sleep(1); ret=PCA9698_write_oneport(fd,PCA9698_ADDR,PCA9688_P0_REG_OUT,0xff); if(ret<0) { printf("init err ret2=%d\n",ret); } sleep(1); ret=PCA9698_read_oneport (fd, PCA9698_ADDR,PCA9688_P0_REG_IN,&buf); if(ret<0) { printf("init err ret3=%d\n",ret); } printf("buf=%d\n",buf); } **************************************************************************/ /********************一次读写所有端口测试********************************************** unsigned char data1[5]={0x00,0x00,0x00,0x00,0x00}; unsigned char data2[5]={0xff,0xff,0xff,0xff,0xff}; unsigned char data3[5]={0}; ret=PCA9698_write_ports(fd,PCA9698_ADDR,PCA9688_P0_REG_SET,data1); if(ret<0) { printf("init err ret=%d\n",ret); //return 0; } while(1) { sleep(1); ret=PCA9698_write_ports(fd,PCA9698_ADDR,PCA9688_P0_REG_OUT,data1); if(ret<0) { printf("init err ret1=%d\n",ret); } ret=PCA9698_read_ports (fd, PCA9698_ADDR,PCA9688_P0_REG_IN,data3); if(ret<0) { printf("init err ret3=%d\n",ret); } for(num=0;num<5;num++) printf("buf %d=%d\n",num,*(data3+num)); sleep(1); ret=PCA9698_write_ports(fd,PCA9698_ADDR,PCA9688_P0_REG_OUT,data2); if(ret<0) { printf("init err ret2=%d\n",ret); } sleep(1); ret=PCA9698_read_ports (fd, PCA9698_ADDR,PCA9688_P0_REG_IN,data3); if(ret<0) { printf("init err ret3=%d\n",ret); } for(num=0;num<5;num++) printf("buf %d=%d\n",num,*(data3+num)); } **************************************************************************/ /********************一次读写一个bit**********************************************/ ; //unsigned char value_=0; ]={}; ret=PCA9698_write_oneport(fd,PCA9698_ADDR,PCA9688_P0_REG_SET,0x00); ret=PCA9698_write_oneport(fd,PCA9698_ADDR,PCA9688_P1_REG_SET,0x00); //设置P4口输入,开启中断 ret=PCA9698_write_oneport(fd,PCA9698_ADDR,PCA9688_P4_REG_SET,0xff); ret=PCA9698_set_int (fd,,); ) { printf("init err ret=%d\n",ret); //return 0; } ) { pin=; sleep(); ret=PCA9698_write_onebit(fd,pin,); ret=PCA9698_read_onebit (fd,pin,&buf); printf("P%d=%d\n",pin,buf); sleep(); ret=PCA9698_write_onebit(fd,pin,); ret=PCA9698_read_onebit (fd,pin,&buf); printf("P%d=%d\n",pin,buf); pin=; sleep(); ret=PCA9698_write_onebit(fd,pin,); ret=PCA9698_read_onebit (fd,pin,&buf); printf("P%d=%d\n",pin,buf); sleep(); ret=PCA9698_write_onebit(fd,pin,); ret=PCA9698_read_onebit (fd,pin,&buf); printf("P%d=%d\n",pin,buf); pin=; sleep(); ret=PCA9698_write_onebit(fd,pin,); ret=PCA9698_read_onebit (fd,pin,&buf); printf("P%d=%d\n",pin,buf); sleep(); ret=PCA9698_write_onebit(fd,pin,); ret=PCA9698_read_onebit (fd,pin,&buf); printf("P%d=%d\n",pin,buf); sleep(); ret=PCA9698_int_irq (fd, ,data3); ;num<;num++) { ) printf("int=%d\n",data3[num]); } } /**************************************************************************/ close( fd ); ; }