uint64_t si70xx_get_serial(si70xx_t *dev) { uint8_t out[2]; uint8_t in_first[8] = { 0 }; uint8_t in_second[8] = { 0 }; /* read the lower bytes */ out[0] = SI70XX_READ_ID_FIRST_A; out[1] = SI70XX_READ_ID_FIRST_B; i2c_acquire(dev->i2c_dev); i2c_write_bytes(dev->i2c_dev, dev->address, (char *) out, 2); i2c_read_bytes(dev->i2c_dev, dev->address, (char *) in_first, 8); /* read the higher bytes */ out[0] = SI70XX_READ_ID_SECOND_A; out[1] = SI70XX_READ_ID_SECOND_B; i2c_write_bytes(dev->i2c_dev, dev->address, (char *) out, 2); i2c_read_bytes(dev->i2c_dev, dev->address, (char *) in_second, 8); i2c_release(dev->i2c_dev); /* calculate the ID */ uint32_t id_first = ((uint32_t)in_first[0] << 24) + ((uint32_t)in_first[2] << 16) + (in_first[4] << 8) + (in_first[6] << 0); uint32_t id_second = ((uint32_t)in_second[0] << 24) + ((uint32_t)in_second[2] << 16) + (in_second[4] << 8) + (in_second[6] << 0); return (((uint64_t) id_first) << 32) + id_second; }
int8_t bmp085_read_calibration_data(uint8_t *cdata) { uint8_t data[] = {BMP085_ADDRESS_WRITE, BMP085_REG_CAL_START}; /* Set I2C driver to normal mode */ if(i2c_set_mode(I2C_MODE_NORMAL) != I2C_OK) { return BMP085_ERROR_MODE_CHANGE; } /* Write module address, control register address and control register data */ if(i2c_write_bytes(data, 2, true, false) != I2C_OK) { return BMP085_ERROR_I2C; } /* Send read */ data[0] = BMP085_ADDRESS_READ; if(i2c_write_bytes(data, 1, true, false) != I2C_OK) { return BMP085_ERROR_I2C; } /* Read 22 bytes */ if(i2c_read_bytes(cdata, 22, false, true) != I2C_OK) { return BMP085_ERROR_I2C; } /* Swap endianess to make correct values */ for(data[0] = 0; data[0] < 11; data[0]++) { data[1] = cdata[data[0] * 2 + 1]; cdata[data[0] * 2 + 1] = cdata[data[0] * 2]; cdata[data[0] * 2] = data[1]; } return BMP085_OK; }
static int goodix_read_version(struct goodix_ts_data *ts) { int ret; uint8_t version[2]={0x69,0xff}; //command of reading Guitar's version uint8_t version_data[41]; //store touchscreen version infomation memset(version_data, 0 , sizeof(version_data)); version_data[0]=0x6A; ret=i2c_write_bytes(ts->client,version,2); if (ret < 0) goto error_i2c_version; msleep(16); ret=i2c_read_bytes(ts->client,version_data, 40); if (ret < 0) goto error_i2c_version; dev_info(&ts->client->dev," Guitar Version: %s\n", &version_data[1]); version[1] = 0x00; //cancel the command i2c_write_bytes(ts->client, version, 2); if(strncmp(version_data,"GT801_1R01",sizeof("GT801_1R01"))) return 1; else if(strncmp(version_data,"GT801_1R06",sizeof("GT801_1R06"))) return 2; else return 0; error_i2c_version: return ret; }
static int goodix_init_panel(struct goodix_ts_data *ts) { int ret = -1; int count; //There are some demo configs. May be changed as some different panels. #if 1//GUITAR_CONFIG_43 uint8_t config_info[54]={0x30, 0x19,0x05,0x03,0x28,0x02,0x14,0x14,0x10,0x46,0xB8,0x14,0x00,0x1E,0x00,0xED,0xCB, 0xA9,0x87,0x65,0x43,0x21,0x01,0x00,0x00,0x00,0x00,0x4D,0xC2,0x20,0x01,0x01,0x83, 0x50,0x3C,0x1E,0xB4,0x00,0x0A,0x50,0x82,0x1E,0x00,0x64,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x01}; #else //TCL_5.0 inch uint8_t config_info[54]={0x30, 0x19,0x05,0x06,0x28,0x02,0x14,0x14,0x10,0x40,0xB8,TOUCH_MAX_WIDTH>>8,TOUCH_MAX_WIDTH&0xFF, TOUCH_MAX_HEIGHT>>8,TOUCH_MAX_HEIGHT&0xFF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xE1,0x00, 0x00,0x00,0x00,0x0D,0xCF,0x20,0x03,0x05,0x83,0x50,0x3C,0x1E,0x28,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}; #endif for(count = 5; count > 0; count--) { ret = i2c_write_bytes(ts->client,config_info,54);; if(ret == 1) //Initiall success break; else msleep(10); } return ret==1 ? 0 : ret; } static int goodix_read_version(struct goodix_ts_data *ts) { #define GT80X_VERSION_LENGTH 40 int ret; uint8_t version[2] = {0x69,0xff}; //command of reading Guitar's version uint8_t version_data[GT80X_VERSION_LENGTH+1]; //store touchscreen version infomation memset(version_data+1, 0, GT80X_VERSION_LENGTH); ret=i2c_write_bytes(ts->client,version,2); if (ret < 0) goto error_i2c_version; msleep(50); //change: 16ms->50ms version_data[0] = 0x6A; ret=i2c_read_bytes(ts->client,version_data, GT80X_VERSION_LENGTH); if (ret < 0) goto error_i2c_version; dev_info(&ts->client->dev," Guitar Version: %s\n", version_data+1); version[1] = 0x00; //cancel the command i2c_write_bytes(ts->client, version, 2); return 0; error_i2c_version: return ret; }
int i2c_write_regs(i2c_t dev, uint16_t addr, uint16_t reg, const void *data, size_t len, uint8_t flags) { if (flags & (I2C_NOSTOP | I2C_NOSTART)) { return -EOPNOTSUPP; } /* First set ADDR and register with no stop */ int ret = i2c_write_bytes(dev, addr, ®, (flags & I2C_REG16) ? 2 : 1, flags | I2C_NOSTOP); if (ret < 0) { return ret; } /* Then write data to the device */ return i2c_write_bytes(dev, addr, data, len, flags | I2C_NOSTART); }
/*Function as i2c_master_send */ static int gt_i2c_write_bytes(struct i2c_client *client,uint8_t *buf,int len) { int ret = 1; if (len <= ADDR_LENGTH) { if (i2c_enable_commands(client, buf[0] << 8 | buf[1])) { ret = -1; } } else { if (i2c_write_bytes(client, buf[0] << 8 | buf[1], &buf[2], len - ADDR_LENGTH)) { ret = -1; } } return ret; #if 0 struct i2c_msg msg; int ret=-1; //豸ַ msg.flags=!I2C_M_RD;//дϢ msg.addr=client->addr; msg.len=len; msg.buf=data; ret=i2c_transfer(client->adapter,&msg, 1); return ret; #endif }
void adxl346_init(void) { uint8_t config[2]; config[0] = ADXL346_BW_RATE_ADDR; config[1] = (ADXL346_BW_RATE_RATE(11)); i2c_write_bytes(ADXL346_ADDRESS, config, sizeof(config)); config[0] = ADXL346_DATA_FORMAT_ADDR; config[1] = (ADXL346_DATA_FORMAT_FULL_RES | ADXL346_DATA_FORMAT_RANGE_PM_16g); i2c_write_bytes(ADXL346_ADDRESS, config, sizeof(config)); config[0] = ADXL346_POWER_CTL_ADDR; config[1] = ADXL346_POWER_CTL_MEASURE; i2c_write_bytes(ADXL346_ADDRESS, config, sizeof(config)); }
static u8 guitar_nvram_recall( struct goodix_ts_data *ts ) { int ret; u8 inbuf[3] = {REG_NVRCS_H,REG_NVRCS_L,0}; ret = i2c_read_bytes( ts->client, inbuf, 3 ); if ( ret < 0 ) { return fail; } if ( ( inbuf[2]&BIT_NVRAM_LOCK) == BIT_NVRAM_LOCK ) { return fail; } inbuf[2] = ( 1 << BIT_NVRAM_RECALL ); //recall command ret = i2c_write_bytes( ts->client , inbuf, 3); if (ret <= 0) { return fail; } return success; }
static int goodix_ts_power(struct gt818_ts_data * ts, int on) { int ret = -1; struct gt818_platform_data *pdata = ts->client->dev.platform_data; unsigned char i2c_control_buf[3] = {0x06,0x92,0x01}; //suspend cmd if(ts != NULL && !ts->use_irq) return -2; switch(on) { case 0: i2c_pre_cmd(ts); // set the io port high level to avoid level change which might stop gt818 from sleeping gpio_direction_output(pdata->gpio_reset, 1); gpio_direction_output(pdata->gpio_pendown, 1); msleep(5); ret = i2c_write_bytes(ts->client, i2c_control_buf, 3); if(ret < 0) { printk(KERN_INFO"**gt818 suspend fail**\n"); } else { //printk(KERN_INFO"**gt818 suspend**\n"); ret = 0; } // i2c_end_cmd(ts); return ret; case 1: gpio_pull_updown(pdata->gpio_pendown, 1); gpio_direction_output(pdata->gpio_pendown, 0); msleep(1); gpio_direction_output(pdata->gpio_pendown, 1); msleep(1); gpio_direction_input(pdata->gpio_pendown); gpio_pull_updown(pdata->gpio_pendown, 0); /* msleep(2); gpio_pull_updown(pdata->gpio_reset, 1); gpio_direction_output(pdata->gpio_reset, 0); msleep(2); gpio_direction_input(pdata->gpio_reset); gpio_pull_updown(pdata->gpio_reset, 0); msleep(30); */ msleep(1); ret = i2c_pre_cmd(ts); //printk(KERN_INFO"**gt818 reusme**\n"); ret = i2c_end_cmd(ts); return ret; default: printk(KERN_DEBUG "%s: Cant't support this command.", gt818_ts_name); return -EINVAL; } }
static s32 tpd_enable_ps(s32 enable) { u8 state; s32 ret = -1; // u8 buffer[3] = {0x80,0x42}; mutex_lock(&i2c_access); if (enable) { // buffer[2] = 1; state = 1; tpd_proximity_flag = 1; GTP_INFO("TPD proximity function to be on."); } else { // buffer[2] = 0; state = 0; //tpd_proximity_detect = 1; tpd_proximity_flag = 0; GTP_INFO("TPD proximity function to be off."); } ret = i2c_write_bytes(i2c_client_point, TPD_PROXIMITY_ENABLE_REG, &state, 1); //ret = gtp_i2c_write(i2c_client_point, buffer, sizeof(buffer)); mutex_unlock(&i2c_access); if (ret < 0) { GTP_ERROR("TPD %s proximity cmd failed.", state ? "enable" : "disable"); //GTP_ERROR("TPD %s proximity cmd failed.", buffer[2] ? "enable" : "disable"); return ret; } GTP_INFO("TPD proximity function %s success.", state ? "enable" : "disable"); //GTP_INFO("TPD proximity function %s success.", buffer[2] ? "enable" : "disable"); return 0; }
uint8_t u8x8_byte_riotos_hw_i2c(u8x8_t *u8g2, uint8_t msg, uint8_t arg_int, void *arg_ptr) { static uint8_t buffer[255]; static uint8_t index; i2c_t dev = (i2c_t) u8g2->dev; switch (msg) { case U8X8_MSG_BYTE_SEND: memcpy(&buffer[index], arg_ptr, arg_int); index += arg_int; break; case U8X8_MSG_BYTE_INIT: i2c_init_master(dev, I2C_SPEED_FAST); break; case U8X8_MSG_BYTE_SET_DC: break; case U8X8_MSG_BYTE_START_TRANSFER: i2c_acquire(dev); index = 0; break; case U8X8_MSG_BYTE_END_TRANSFER: i2c_write_bytes(dev, u8x8_GetI2CAddress(u8g2), buffer, index); i2c_release(dev); break; default: return 0; } return 1; }
static u8 guitar_nvram_store( struct goodix_ts_data *ts ) { int ret; int i; u8 inbuf[3] = {REG_NVRCS_H,REG_NVRCS_L, 0x18}; ret = i2c_read_bytes(ts->client, inbuf, 3); if ( ret < 0 ) { return fail; } if ((inbuf[2] & BIT_NVRAM_LOCK ) == BIT_NVRAM_LOCK) { return fail; } inbuf[2] = 0x18; inbuf[2] |= (1<<BIT_NVRAM_STROE); //store command for ( i = 0 ; i < 300 ; i++ ) { ret = i2c_write_bytes( ts->client, inbuf, 3 ); if ( ret > 0 ) return success; } return fail; }
static s32 tpd_enable_ps(s32 enable) { u8 state; s32 ret = -1; if (enable) { state = 1; tpd_proximity_flag = 1; GTP_INFO("TPD proximity function to be on."); } else { state = 0; tpd_proximity_flag = 0; GTP_INFO("TPD proximity function to be off."); } ret = i2c_write_bytes(i2c_client_point, TPD_PROXIMITY_ENABLE_REG, &state, 1); if (ret < 0) { GTP_ERROR("TPD %s proximity cmd failed.", state ? "enable" : "disable"); return ret; } GTP_INFO("TPD proximity function %s success.", state ? "enable" : "disable"); return 0; }
static ssize_t goodix_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { int i = 0; struct goodix_ts_data *ts = (struct goodix_ts_data *)dev_get_drvdata(dev); unsigned char config_data[100]; struct goodix_i2c_rmi_platform_data *pdata = ts->client->dev.platform_data; int SHUTDOWN_PORT = pdata->gpio_shutdown; if (!strcmp(attr->attr.name, "ctpconfig")) { printk(KERN_ALERT"goodix set panel config\n"); gpio_set_value(SHUTDOWN_PORT, 1); msleep(100); gpio_set_value(SHUTDOWN_PORT, 0); msleep(100); for (i=0; i<100; i++){ if (sscanf(buf, "%x", &config_data[i])== 1) { printk("%x\n", config_data[i]); buf += 5; } else break; } printk(KERN_ALERT"total data = %d\n", i); i2c_write_bytes(ts->client,&config_data[0],i); } return count; }
/******************************************************* 功能: Guitar初始化函数,用于发送配置信息,获取版本信息 参数: ts: client私有数据结构体 return: 执行结果码,0表示正常执行 *******************************************************/ static int goodix_init_panel(struct goodix_ts_data *ts) { int ret=-1; int ICverison=0; struct goodix_i2c_rmi_platform_data *pdata = ts->client->dev.platform_data; printk(KERN_ALERT"goodix init panel\n"); ICverison = goodix_read_version(ts); if(2==ICverison)//New IC ret=i2c_write_bytes(ts->client,pdata->config_info,pdata->config_info_len); else{ printk("Wrong Guitar version"); goto error_i2c_transfer; } if (ret < 0){ printk("I2C transfer error in panel in"); goto error_i2c_transfer; } msleep(1); return 0; error_i2c_transfer: return ret; }
static u8 clear_mix_flag(struct goodix_ts_data *ts) { s32 i = 0; u8 buf[3]; buf[0] = 0x14; buf[1] = 0x00; buf[2] = 0x80; for (i = 0; i < 5; i++) { if (i2c_write_bytes(ts->client, buf, 3) > 0) { break; } } i2c_end_cmd(ts); if (i >= 5) { DEBUG_UPDATE("Clear mix flag failed!\n"); return fail; } return success; }
/******************************************************* 功能: 发送后缀命令 ts: client私有数据结构体 return: 执行结果码,0表示正常执行 *******************************************************/ static int i2c_end_cmd(struct goodix_ts_data *ts) { int ret; u8 end_cmd_data[2]={0x80, 0x00}; ret=i2c_write_bytes(ts->client,end_cmd_data,2); return ret;//*/ }
/******************************************************************************* * Function : raysens_ctpm_fw_online * Description : entry online mode and check * Parameters : none * Return : 0: successful ; error: -1 *******************************************************************************/ static int raysens_ctpm_fw_online(struct i2c_client *client) { int i; unsigned char IICBufOnlineR[10]; //由正常工作切换到在线编程模式 i2c_write_bytes(client, Register[18], 1); //进入在线编程的握手指令 i2c_write_bytes(client, Register[0], 10); //4 i2c_read_data(client, IICBufOnlineR, 10); for (i = 0; i < 10; i++) { if (IICBufOnlineR[i] != Register[0][i]) { TS_DBG("ONLINE_HANDLE error! \n"); strcpy(fw_status,"***err: ONLINE_HANDLE error!***\n"); return -1; } TS_DBG("ONLINE_HANDLE ok! \n"); } //开始在线编程的命令 i2c_write_bytes(client, Register[1], 10); //4 TS_DBG("ONLINE_BEGIN data \n"); msleep(20); //判断是否已进入在线编程 i2c_write_bytes(client, Register[2], 3); //i2c_read_interface(I2C_CTPM_ADDRESS, IICBufOnlineR, 10);// i2c_read_data(client, IICBufOnlineR, 4); pr_info("IICBufOnlineR = %x, %x, %x, %x \n", IICBufOnlineR[0], IICBufOnlineR[1], IICBufOnlineR[2], IICBufOnlineR[3]); if ((IICBufOnlineR[0] == Register[3][0]) && (IICBufOnlineR[1] == Register[3][1]) && (IICBufOnlineR[2] == Register[3][2]) && (IICBufOnlineR[3] == Register[3][3])) { //如果读到的数不是 0xab9d573b TS_DBG("ONLINE_BEG_CHECK ok \n"); } else { TS_DBG("ONLINE_BEG_CHECK error \n"); strcpy(fw_status,"***err: ONLINE_BEG_CHECK error!***\n"); return -1; } return 1; }
static int i2c_end_cmd(struct gt818_ts_data *ts) { int ret; u8 end_cmd_data[2] = {0}; end_cmd_data[0] = 0x80; end_cmd_data[1] = 0x00; ret = i2c_write_bytes(ts->client,end_cmd_data,2); udelay(20); return ret; }
static int i2c_pre_cmd(struct gt818_ts_data *ts) { int ret; u8 pre_cmd_data[2] = {0}; pre_cmd_data[0] = 0x0f; pre_cmd_data[1] = 0xff; ret = i2c_write_bytes(ts->client,pre_cmd_data,2); udelay(20); return ret; }
int8_t bmp085_read_temperature(uint16_t *p_temp) { uint8_t data[] = {BMP085_ADDRESS_WRITE, BMP085_REG_CONTROL, BMP085_CREG_TEMP}; /* Set I2C driver to normal mode */ if(i2c_set_mode(I2C_MODE_NORMAL) != I2C_OK) { return BMP085_ERROR_MODE_CHANGE; } /* Write module address, control register address and control register data */ if(i2c_write_bytes(data, 3, true, true) != I2C_OK) { return BMP085_ERROR_I2C; } /* Wait for conversion complete (max time = 4.5ms) */ _delay_ms(5); /* Set register to MSB */ data[0] = BMP085_ADDRESS_WRITE; data[1] = BMP085_REG_MSB; if(i2c_write_bytes(data, 2, true, false) != I2C_OK) { return BMP085_ERROR_I2C; } /* Send read */ data[0] = BMP085_ADDRESS_READ; if(i2c_write_bytes(data, 1, true, false) != I2C_OK) { return BMP085_ERROR_I2C; } /* Read MSB and LSB */ if(i2c_read_bytes(data, 2, false, true) != I2C_OK) { return BMP085_ERROR_I2C; } /* Align bytes to little endian */ *p_temp = ((uint16_t)data[0] << 8) | ((uint16_t)data[1]); return BMP085_OK; }
static int goodix_init_panel(struct gt818_ts_data *ts) { int ret = -1; int i = 0; #if 1 u8 config_info[] = { 0x06,0xA2, 0x00,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E, 0x10,0x12,0x00,0x00,0x10,0x00,0x20,0x00, 0x30,0x00,0x40,0x00,0x50,0x00,0x60,0x00, 0xE0,0x00,0xD0,0x00,0xC0,0x00,0xB0,0x00, 0xA0,0x00,0x90,0x00,0x80,0x00,0x70,0x00, 0xF0,0x00,0x13,0x13,0x90,0x90,0x90,0x27, 0x27,0x27,0x0F,0x0E,0x0A,0x40,0x30,0x01, 0x03,0x00,MAX_FINGER_NUM,0x00,0x14,0xFA,0x1B,0x00, 0x00,0x66,0x5A,0x6A,0x5E,0x00,0x00,0x05, 0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x14,0x10,0xEF,0x03,0x00,0x00,0x00,0x00, 0x00,0x00,0x20,0x40,0x70,0x90,0x0F,0x40, 0x30,0x3C,0x28,0x00,0x00,0x00,0x00,0x00, 0x00,0x01 }; #endif u8 read_config_info[sizeof(config_info)] = {0}; read_config_info[0] = 0x06; read_config_info[1] = 0xa2; ret = i2c_write_bytes(ts->client, config_info, (sizeof(config_info)/sizeof(config_info[0]))); if (ret < 0) { printk("config gt818 fail\n"); return ret; } ret = i2c_read_bytes(ts->client, read_config_info, (sizeof(config_info)/sizeof(config_info[0]))); if (ret < 0){ printk("read gt818 config fail\n"); return ret; } for(i = 2; i < 106; i++){ if(read_config_info[i] != config_info[i]){ printk("write gt818 config error\n"); ret = -1; return ret; } } msleep(10); return 0; }
static int mc_i2c_read(wm_i2c *i2c, int reg, uint8_t *data, int nbytes) { int i, nwritten = 0; #if I2C_SUPPORT_CONCURRENCY if (i2c->slave_addr != i2c_current_slave_addr) i2c_config(i2c); #endif if (reg >= 0) { if (!i2c_write_bytes(i2c, reg, NULL, 0)) goto bail; } #if I2C_READCMD_AHEAD if (nbytes > 0) { i2c_read_start(i2c); /* issue the first READ CMD in advance to keep it always in the txFIFO */ nwritten++; } #endif for (i = 0; i < nbytes; i++) { i2c_timer_start(i2c); #if I2C_READCMD_AHEAD if (nwritten < nbytes) { i2c_read_start(i2c); nwritten++; } #endif while (!(i2c->reg->STATUS.WORDVAL & 0x08 /* RFNE */)) { #if !I2C_READCMD_AHEAD if (nwritten < nbytes) { if ((i2c->reg->STATUS.WORDVAL & (0x08|0x02)) == 0x02 /* RFNE | TFNF */) { i2c->reg->DATA_CMD.WORDVAL = 0x100; /* READ CMD */ nwritten++; } } #endif if (i2c_timeout(i2c, i2c->rxtout)) { #if I2C_VERBOSE mc_log_debug("I2C: read: timeout [%d]! STATUS = 0x%x, RISTAT = 0x%x, ABRT = 0x%x\n", i, i2c->reg->STATUS.WORDVAL, i2c->reg->RAW_INTR_STAT.WORDVAL, i2c->reg->TX_ABRT_SOURCE.WORDVAL); #endif goto bail; } } data[i] = i2c->reg->DATA_CMD.WORDVAL; } return 0; bail: return -1; }
int mcp23017_read(int bus, uint8_t addr, uint8_t reg, uint8_t *data) { uint8_t res_data = 0; int result = -1; if (i2c_write_bytes(bus, addr, ®, 1) < 0) { return -1; } if (i2c_read_bytes(bus, addr, &res_data, 1) > 0) { *data = res_data; result = 1; } return result; }
void pic_write(uint8_t addr, uint8_t value) { /* toggle the pin for 4 us */ gpio_clear(PIC_INT_WAKE_PIN); _usleep(4); /* write to gpio expander */ i2c_acquire(PIC_I2C); uint8_t bytes[] = { addr, value }; i2c_write_bytes(PIC_I2C, PIC_I2C_ADDR, bytes, 2, 0); i2c_release(PIC_I2C); /* put PIC in sleep mode again */ gpio_set(PIC_INT_WAKE_PIN); }
int i2c_multidata_write(uchar chip, uint addr, int alen, uchar * buffer, int len) { if (alen > 1) { printf("I2C multi write: addr len %d not supported\n", alen); return 1; } if (i2c_write_bytes(chip, addr, buffer, len)) { printf("I2C multi write: I/O error\n"); i2c_init(i2c_speed, CFG_I2C_SLAVE); return 1; } return 0; }
static bool i2c_test(struct i2c_client * client) { int ret,retry; uint8_t test_data[1] = { 0 }; //only write a data address. for(retry=0; retry < 5; retry++) { ret = i2c_write_bytes(client, test_data, 1); //Test i2c. if (ret == 1) break; msleep(10); } return ret==1 ? true : false; }
int i2c_write_regs(i2c_t bus, uint8_t addr, uint8_t reg, const void *data, int len) { assert((bus < I2C_NUMOF) && data && (len > 0) && (len < 255)); /* the nrf52's TWI device does not support to do two consecutive transfers * without a repeated start condition in between. So we have to put all data * to be transfered into a temporary buffer * * CAUTION: this might become critical when transferring large blocks of * data as the temporary buffer is allocated on the stack... */ uint8_t buf_tmp[len + 1]; buf_tmp[0] = reg; memcpy(&buf_tmp[1], data, len); return i2c_write_bytes(bus, addr, buf_tmp, (len + 1)) - 1; }
uint8_t si70xx_get_revision(si70xx_t *dev) { uint8_t out[2]; uint8_t in = 0; /* read the revision number */ out[0] = SI70XX_READ_REVISION_A; out[1] = SI70XX_READ_REVISION_B; i2c_acquire(dev->i2c_dev); i2c_write_bytes(dev->i2c_dev, dev->address, (char *) out, 2); i2c_read_byte(dev->i2c_dev, dev->address, (char *) &in); i2c_release(dev->i2c_dev); return in; }
int cmd_write(int argc, char **argv) { int res; uint8_t addr; int length = argc - 2; char data[BUFSIZE]; if (i2c_dev < 0) { puts("Error: no I2C device was initialized"); return 1; } if (argc < 3) { puts("Error: not enough arguments given"); printf("Usage:\n%s: ADDR BYTE0 [BYTE1 [BYTE_n [...]]]\n", argv[0]); return 1; } addr = (uint8_t)atoi(argv[1]); for (int i = 0; i < length; i++) { data[i] = (char)atoi(argv[i + 2]); } if (length == 1) { printf("i2c_write_byte(I2C_%i, 0x%02x, 0x%02x)\n", i2c_dev, addr, (unsigned int)data[0]); res = i2c_write_byte(i2c_dev, addr, data[0]); } else { printf("i2c_write_bytes(I2C_%i, 0x%02x, [", i2c_dev, addr); for (int i = 0; i < length; i++) { printf(", 0x%02x", (unsigned int)data[i]); } puts("])"); res = i2c_write_bytes(i2c_dev, addr, data, length); } if (res < 0) { puts("Error: no bytes were written"); return 1; } else { printf("I2C_%i: successfully wrote %i bytes to the bus\n", i2c_dev, res); return 0; } }