📄 i2c.c
字号:
/* * * Rev: $Id: i2c.c 987 2005-07-18 10:13:16Z hennerich $ * Revision: $Revision: 987 $ * Source: $Source$ * Created: 06.07.2005 18:16 * Author: Michael Hennerich * mail: hennerich@blackfin.uclinux.org * Description: Simple I2C Routines * * Copyright (C) 2005 Michael Hennerich * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * **************************************************************************** * MODIFICATION HISTORY: ***************************************************************************/ #include "i2c-dev.h"#include <string.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/poll.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <sys/ioctl.h> #define I2C_DEVICE "/dev/i2c-0"#define I2C_SLAVE_ADDR 0x38 /* Randomly picked */ #define I2C_DEVID (0x60>>1) // For OV2640, OV2630//#define main#undef mainint i2c_write_register(char * , unsigned char , unsigned char , unsigned short );int i2c_read_register(char * , unsigned char , unsigned char );i2c_dump_register(char * , unsigned char , unsigned short , unsigned short );i2c_scan_bus(char * );#if mainint main(){ i2c_scan_bus(I2C_DEVICE); i2c_write_register(I2C_DEVICE,I2C_DEVID,0xff,0x00); i2c_dump_register(I2C_DEVICE,I2C_DEVID,0,256); i2c_write_register(I2C_DEVICE,I2C_DEVID,0xff,0x01); i2c_dump_register(I2C_DEVICE,I2C_DEVID,0,256); exit( 0 );}#endifint i2c_write_register(char * device, unsigned char client, unsigned char reg, unsigned short value){ int addr = I2C_SLAVE_ADDR; char msg_data[32]; struct i2c_msg msg = { addr, 0, 0, msg_data }; struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 }; int fd,i; if ( (fd = open( device, O_RDWR ) ) < 0 ) { fprintf(stderr, "Error: could not open %s\n", device); exit( 1 ); } if ( ioctl( fd, I2C_SLAVE, addr ) < 0 ) { fprintf(stderr, "Error: could not bind address %x \n", addr ); } /* msg.len = 3; msg.flags = 0; msg_data[0] = reg; msg_data[2] = (0xFF & value); msg_data[1] = (value >> 8); msg.addr = client;*/ msg.len = 2; msg.flags = 0; msg_data[0] = reg; msg_data[1] = (0xFF & value); msg.addr = client; if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ) { fprintf(stderr, "Error: could not write \n"); } close( fd ); return 0;}int i2c_read_register(char * device, unsigned char client, unsigned char reg){ int addr = I2C_SLAVE_ADDR; char msg_data[32]; struct i2c_msg msg = { addr, 0, 0, msg_data }; struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 }; int fd,i; if ( (fd = open( device, O_RDWR ) ) < 0 ) { fprintf(stderr, "Error: could not open %s\n", device); exit( 1 ); } if ( ioctl( fd, I2C_SLAVE, addr ) < 0 ) { fprintf(stderr, "Error: could not bind address %x \n", addr ); } msg_data[0]= reg; msg.addr = client; msg.len = 1; msg.flags = 0; if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ) { fprintf(stderr, "Error: could not write \n"); }; msg.len = 1; msg_data[0]=0; msg.flags = I2C_M_RD ; if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ) { fprintf(stderr, "Error: could not read back\n"); close( fd ); return -1; } close( fd ); return ((unsigned char)msg_data[0]);}i2c_dump_register(char * device, unsigned char client, unsigned short start, unsigned short end){ int addr = I2C_SLAVE_ADDR; char msg_data[32]; struct i2c_msg msg = { addr, 0, 0, msg_data }; struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 }; int fd,i; if ( (fd = open( device, O_RDWR ) ) < 0 ) { fprintf(stderr, "Error: could not open %s\n", device); exit( 1 ); } if ( ioctl( fd, I2C_SLAVE, addr ) < 0 ) { fprintf(stderr, "Error: could not bind address %x \n", addr ); } for(i = start; i < end; i++) { msg_data[0]= i; msg.addr = client; msg.len = 1; msg.flags = 0; if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ) { fprintf(stderr, "Error: could not write \n"); }; msg.len = 1; msg_data[0]=0; msg.flags = I2C_M_RD ; if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ) { fprintf(stderr, "Error: could not read back\n"); } else { fprintf(stderr, "Register %02x : %02x%02x \n",i, (unsigned char)msg_data[0]); } } close( fd ); return;}i2c_scan_bus(char * device){ int addr = I2C_SLAVE_ADDR; char msg_data[32]; struct i2c_msg msg = { addr, 0, 0, msg_data }; struct i2c_rdwr_ioctl_data rdwr = { &msg, 1 }; int fd,i; if ( (fd = open( device, O_RDWR ) ) < 0 ) { fprintf(stderr, "Error: could not open %s\n", device); exit( 1 ); } if ( ioctl( fd, I2C_SLAVE, addr ) < 0 ) { fprintf(stderr, "Error: could not bind address %x \n", addr ); } msg.len = 1; msg.flags = 0; msg_data[0]=0; msg_data[1]=0; for ( i = 0; i < 128; i++){ msg.addr = i; if ( ioctl( fd, I2C_RDWR, &rdwr ) < 0 ) { //fprintf(stderr, "Error: could not write \n"); }else fprintf(stderr, "FOUND I2C device at 0x%X (8-bit Adrress 0x%X) \n",msg.addr,msg.addr<<1);} close( fd ); return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -