/* 定义 OD2101 波特率 */
enum{ UART_9600,UART_300,UART_600,UART_900,UART_1200,
UART_1800,UART_2400,UART_3600,UART_4800,UART_7200,
UART_14400,UART_19200,UART_28800,UART_38400,
UART_57600,UART_115200 };
int od2101_open(int chipAddr);
int od2101_setBaudRate(int fd, int value);
int od2101_write(int fd, char *data, int length);
int od2101_read(int fd, char *data, int length);
int od2101_select(int fd);
int od2101_read_byte(int fd);
int od2101_close(int fd);
#endif /*OD2101_H_*/
源码:
#include \"od2101.h\"
#define I2C_RETRIES 0x0701 /* number of times a device address */
#define I2C_TIMEOUT 0x0702 /* set timeout - call with int */
#define I2C_TENBIT 0x0704 /* 0 for 7 bit addrs, != 0 for 10 bit */
#define I2C_SLAVE 0x0703 /* Change slave address */
#define I2C_SLAVE_FORCE 0x0706 /* Change slave address */
#define I2C_FUNCS 0x0705 /* Get the adapter functionality */
#define I2C_RDWR 0x0707 /* Combined R/W transfer (one stop only)*/
#define I2C_PEC 0x0708 /* != 0 for SMBus PEC */
#define REG_WR 0x00 /* 数据读寄存器 */
#define REG_RD 0x00 /* 数据写寄存器 */
#define REG_UARTBUF 0x01 /*UART 接收缓存接收字节数 */
#define REG_I2CBUF 0x02 /*I2C 可加载字节数 */
#define REG_CTRL 0x03 /*UART 接口控制寄存器 */
#ifdef _WIN32
int ioctl(int d, int request, ...) {
return 0;
}
#endif
int od2101_open(int chipAddr) {
int fd = open(\"/dev/i2c/0\打开I2C设备
if (fd < 0) {
printf(\"####i2c test device open failed####\\n\");
return (-1);
}
if (ioctl(fd, I2C_TENBIT, 0)<0) { //设置地址7位模式
printf(\"####i2c ioctl(fd,I2C_TENBIT,0) failed####\\n\");
close(fd);
return (-1);
}
if (ioctl(fd, I2C_SLAVE, chipAddr>>1)<0) {
; //设置I2C从设备地址[6:0]
printf(\"####i2c ioctl(fd,I2C_SLAVE,chipAddr) failed####\\n\");
close(fd);
return (-1);
}
ioctl(fd, I2C_TIMEOUT, 10);/*超时时间*/
ioctl(fd, I2C_RETRIES, 2);/*重复次数*/
return fd;
}
static int readRegister(int fd, int regIndex) {
char data[1];
data[0]=regIndex;
if (write(fd, data, 1)<1) {
return EOF;
}
if (read(fd, data, 1)<1) {
return EOF;
}
return (data[0]&0xff);
}
static int writeRegister(int fd, int regIndex, int value) {
int res;
char data[2];
data[0]=regIndex;
data[1]=value;
res= write(fd, data,2);
//printf(\"writeRegister %d %d res = %d\\n\
return res;
}
int od2101_setBaudRate(int fd, int value) {
int regValue=readRegister(fd, REG_CTRL); /* 读取 OD2101 控制寄存器 */
if (regValue!= EOF) {
writeRegister(fd, REG_CTRL, (regValue&0xf0)|value); /* 设置 OD2101UART 波特率*/
printf(\"od2101_setBaudRate size=%d \\n\
return 0;
}
return EOF;
}
int od2101_write(int fd, char *data, int length) {
int i, maxWriteSize=readRegister(fd, REG_I2CBUF); /* 读取 OD2101 最大发送数 */
if (maxWriteSizelength=maxWriteSize;}
if (length>0) {
for (i=0; iwriteRegister(fd, REG_WR, data[i]); //输出到串口}
}
return length;
}
int od2101_read_byte(int fd){
return readRegister(fd, REG_RD);//读串口内容
}
int od2101_read(int fd, char *data, int length) {
int i, maxReadSize=readRegister(fd, REG_UARTBUF); /* 读取 OD2101 最大接收数 */
if(maxReadSize>0) printf(\"max size=%d \\n\
if (maxReadSizelength=maxReadSize;}
if (length>0) {
for (i=0; idata[i]=readRegister(fd, REG_RD);//读串口内容}
}
return length;
}
int od2101_select(int fd) {
int maxReadSize=readRegister(fd, REG_UARTBUF); /* 读取 OD2101 最大接收数 */
return maxReadSize;
}
int od2101_close(int fd){
close(fd);
}