📄 iaccess.c
字号:
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <linux/i2c.h>//#include "i2c-algo-GM.h"//copy from I2C driverstruct i2c_GM_msg { unsigned char addr; /* device address */ unsigned char waddr; /* word address */ short len; /* msg length */ char *buf; /* pointer to msg data */ int clockdiv; /* for clock div */ int multiread_once ; /* decide to read multibyte once. if 0: read one byte by one; if 1: read multibyte once*/};static struct i2c_GM_msg i2c_ap ;static int fd ;static void help(void){ printf("read command: [iaccess 1 address arg1 arg2 arg3 ... ]\n"); printf("\t arg1: offset + data bytes number\n"); printf("\t arg2 arg3 ...: value\n"); printf("write command: [iaccess 0 address arg1 arg2 arg3 ... ]\n"); printf("\t arg1: offset + data bytes number\n"); printf("\t arg2 arg3 ...: value\n"); return;}int main(int argc,char *argv[]) { int ret=0, i, rw, addr, offset; int adapter_nr =0 ; char filename[20], *stop_at; unsigned char buf[20]; if (argc <= 1){ help(); return -1; } rw = strtol(argv[1], &stop_at, 0); if(rw == 1){ //for check READ function addr = strtol(argv[2], &stop_at, 0); offset = strtol(argv[3], &stop_at, 0); //for(i=0; i<offset-1; i++) // buf[i] = strtol(argv[i+4], &stop_at, 0); printf("iaccess %d 0x%02x", rw, addr); //for(i=0; i<offset-1; i++) // printf(" 0x%02x", buf[i]); } else if(rw == 0){ //for check WRITE function addr = strtol(argv[2], &stop_at, 0); offset = strtol(argv[3], &stop_at, 0); for(i=0; i<offset; i++) buf[i] = strtol(argv[i+4], &stop_at, 0); printf("iaccess %d 0x%02x", rw, addr); for(i=0; i<offset; i++) printf(" 0x%02x", buf[i]); printf("\n"); }else { help(); return -1; } /* open the i2c dev file */ memset(filename,0,20); sprintf(filename,"/dev/i2c-%d",adapter_nr); if ((fd = open(filename,O_RDWR))<0) { printf("Error in file open...%s\n",filename); exit(1); } //Set Clock div //i2c_ap.clockdiv = 0 ; //Adopt deault clock div 45 MHZ i2c_ap.clockdiv = 45 ; //Adopt deault clock div MHZ if(rw == 1){ // for read i2c_ap.addr = addr; i2c_ap.waddr = 1; i2c_ap.len = offset; i2c_ap.buf = buf; //printf("addr=0x%02x,waddr=0x%02x,len=0x%02x,buf=0x%02x\n",i2c_ap.addr,i2c_ap.waddr,i2c_ap.len,buf[0]); if ((ret=ioctl(fd,I2C_RDWR,&i2c_ap)) < 0) { printf("Error in ioctl I2C_Read(Err:0x%2x)\n",ret); return -1; } printf(" Read: "); for(i=0; i<offset; i++) printf("0x%02x ",buf[i]); printf("\n"); } if(rw == 0){ //for write i2c_ap.addr = addr; i2c_ap.waddr = 0; i2c_ap.len = offset; i2c_ap.buf = buf; if ((ret=ioctl(fd,I2C_RDWR,&i2c_ap)) < 0) { printf("Error in ioctl I2C_Write(Err:0x%2x)\n",ret); return -1; } } close(fd); return 0; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -