unsigned char I2c_write(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo) { int i; I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr)) { I2c_stop(); return ERROR_CODE_WRITE_ADDR; } if(I2c_write_byte(sub_addr)) { I2c_stop(); return ERROR_CODE_WRITE_ADDR; } for(i = 0; i<ByteNo; i++) { if(I2c_write_byte(buff[i])) { I2c_stop(); return ERROR_CODE_WRITE_DATA; } } I2C_DELAY; I2c_stop(); I2C_DELAY_LONG; return ERROR_CODE_TRUE; }
unsigned char I2c_read(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo) { int i; I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr)) { I2c_stop(); DPRINTK("Fail to write device addr\n"); return ERROR_CODE_READ_ADDR; } if(I2c_write_byte(sub_addr)) { I2c_stop(); DPRINTK("Fail to write sub addr\n"); return ERROR_CODE_READ_ADDR; } I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr+1)) { I2c_stop(); DPRINTK("Fail to write device addr+1\n"); return ERROR_CODE_READ_ADDR; } for(i = 0; i<ByteNo; i++) buff[i] = I2c_read_byte(); I2C_DELAY; I2C_DELAY_LONG; I2c_stop(); I2C_DELAY_LONG; return ERROR_CODE_TRUE; }
unsigned char I2c_read(unsigned char device_addr, unsigned char sub_addr, unsigned char *buff, int ByteNo) { int i; nack_flag = ByteNo - 1; I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr)) { I2c_stop(); printk("Fail to write device addr\n"); return ERROR_CODE_READ_ADDR; } if(I2c_write_byte(sub_addr)) { I2c_stop(); printk("Fail to write sub addr\n"); return ERROR_CODE_READ_ADDR; } #if 1 // printk("=========i2c_start===\n"); I2c_start(); I2C_DELAY; if(I2c_write_byte(device_addr+1)) { I2c_stop(); printk("Fail to write device addr+1\n"); return ERROR_CODE_READ_ADDR; } for(i = 0; i<ByteNo; i++) buff[i] = I2c_read_byte(i); I2C_DELAY; I2C_DELAY_LONG; I2c_stop(); I2C_DELAY_LONG; #endif printk("buff[0] = %d,buff[1] = %d\n",buff[0],buff[1]); return ERROR_CODE_TRUE; }