int i2c_read (uint8_t chip_id, uint8_t reg_addr, uint8_t* buffer, uint16_t len) { int status = 0; /* send chip internal register address (pointer register) */ if (status == 0) i2c_send_start (); if (status == 0) status = i2c_write_byte (chip_id << 1 | I2C_WRITE_OPER); if (status == 0) status = i2c_write_byte (reg_addr); if (status == 0) i2c_send_repeated_start (); /* send again chip id before to switch to read mode */ if (status == 0) status = i2c_write_byte ((chip_id << 1) | I2C_READ_OPER); if (status == 0) i2c_switch_to_read_operation(len > 1); /* read specified number of bytes */ if (status == 0) { while (len > 1) { *buffer++ = i2c_read_byte (--len > 1); } } i2c_send_stop (); if (status == 0) *buffer++ = i2c_read_byte (false); return status; }
float ds1721_read() { unsigned char hi, lo; unsigned char i; short tmp = 1; float rev; float frac[] = { 0.0625, 0.125, 0.25, 0.5 }; i2c_send_start(); i2c_send_byte(DS1721_ADDR | DS1721_WRITE); i2c_send_byte(DS1721_CMD_READ); i2c_send_start(); /* restart */ i2c_send_byte(DS1721_ADDR | DS1721_READ); hi = i2c_read_byte(1); lo = i2c_read_byte(1); // _delay_us(70); /* WHAT ?! */ // i2c_send_stop(); // task_sm.piezo0_count = 5; //////////////// //////////////// if (hi & 0x80) { /* negative */ tmp = (hi << 8) + lo; tmp = ~tmp; tmp++; hi = tmp >> 8; lo = tmp & 0x00ff; tmp = -1; }
void ak8975a_read_raw_data(int16_t *val) { start: // Launch the first acquisition i2c_write_byte(AK8975A_ADDRESS, AK8975A_CNTL, 0x01); //nrf_delay_ms(1); // Wait for a data to become available while ((i2c_read_byte(AK8975A_ADDRESS, AK8975A_ST1) & 0x01) == 0); // If there is no overflow if((i2c_read_byte(AK8975A_ADDRESS, AK8975A_ST2) & 0x0C)==0) { int16_t v[3]; // Read the six raw data registers sequentially into data array // WARNING : code valid for little endian only ! i2c_read_bytes(AK8975A_ADDRESS, AK8975A_XOUT_L, 6, (uint8_t *)v); // WARNING, magnetometer axis are not the same as the accel / gyro ones // Thus : x <--> y, and z <--> -z val[0] = v[1]; val[1] = v[0]; val[2] = -v[2]; return; } goto start; }
int main(void) { uart_init(); debug_init(); lcd_init(LCD_DISP_ON); lcd_clrscr(); DEBUG("init"); msg("begin loop"); while(1) { msg("i2c_start"); i2c_start(); msg("i2c_read_byte(1)"); uint8_t temp = i2c_read_byte(1); DEBUG("get_temp(0): %d", temp); lcd_gotoxy(0, 0); lcd_puts_P(PSTR("Temp: ")); lcd_puts(itoa(temp)); lcd_puts_P(PSTR(DEG"C")); msg("i2c_read_byte(0)"); i2c_read_byte(0); msg("i2c_stop"); i2c_stop(); msg("delay"); _delay_ms(1000); } }
uint8 hal_dev_mag3110_read_reg(uint8 addr) { uint8 result; i2c_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_WRITE); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_write_byte(I2C_MAG, addr); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_repeated_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_READ); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_set_rx_mode(I2C_MAG); i2c_give_nack(I2C_MAG); result = i2c_read_byte(I2C_MAG); i2c_wait(I2C_MAG); i2c_stop(I2C_MAG); result = i2c_read_byte(I2C_MAG); pause(); return result; }
static int check_chipid(struct ftdi_context *ftdi) { int ret; uint8_t ver = 0xff; uint16_t id = 0xffff; ret = i2c_read_byte(ftdi, 0x00, (uint8_t *)&id + 1); if (ret < 0) return ret; ret = i2c_read_byte(ftdi, 0x01, (uint8_t *)&id); if (ret < 0) return ret; ret = i2c_read_byte(ftdi, 0x02, &ver); if (ret < 0) return ret; if ((id & 0xff00) != (CHIP_ID & 0xff00)) { fprintf(stderr, "Invalid chip id: %04x\n", id); return -EINVAL; } /* compute embedded flash size from CHIPVER field */ flash_size = (128 + (ver & 0xF0)) * 1024; printf("CHIPID %04x, CHIPVER %02x, Flash size %d kB\n", id, ver, flash_size / 1024); return 0; }
void imu_g_read_data_raw(vector *v) { unsigned char xl,xh,yl,yh,zl,zh; i2c_start(); i2c_write_byte(0xD0); i2c_write_byte(0x1d); i2c_start(); i2c_write_byte(0xD1); xh = i2c_read_byte(); xl = i2c_read_byte(); yh = i2c_read_byte(); yl = i2c_read_byte(); zh = i2c_read_byte(); zl = i2c_read_last_byte(); i2c_stop(); v->x = xh << 8 | xl; v->y = yh << 8 | yl; v->z = zh << 8 | zl; }
uint8_t I2C_ReadOneByte(uint8_t SlaveAddr, uint8_t RegAddr) { uint8_t result; i2c_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_WRITE); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, RegAddr); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_repeated_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_READ); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_set_rx_mode(I2C0_B); i2c_give_nack(I2C0_B); result = i2c_read_byte(I2C0_B); i2c_wait(I2C0_B); i2c_stop(I2C0_B); result = i2c_read_byte(I2C0_B); pause(40); return result; }
void i2c_read_bytes(i2c_connection conn, size_t n, uint8_t *buf, enum i2c_ack_type last_ack_type) { uint8_t *p = buf; while (n > 1) { *p++ = i2c_read_byte(conn, I2C_ACK); n--; } if (n == 1) *p = i2c_read_byte(conn, last_ack_type); }
uint16_t sensor_read (uint8_t HR, uint8_t LR) { uint8_t sh, sl; uint16_t s; sh = i2c_read_byte(HR); sl = i2c_read_byte(LR); s = ((uint16_t)sh<<8) + (uint16_t)sl; return s; }
void read_xyz(void) { // sign extend byte to 16 bits - need to cast to signed since function // returns uint8_t which is unsigned acc_X = (int8_t) i2c_read_byte(MMA_ADDR, REG_XHI); DelayMS(100); acc_Y = (int8_t) i2c_read_byte(MMA_ADDR, REG_YHI); DelayMS(100); acc_Z = (int8_t) i2c_read_byte(MMA_ADDR, REG_ZHI); }
int16_t adxl346_read_z(void) { uint8_t acceleration[2]; int16_t z; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAZ0_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[0]); i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAZ1_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[1]); z = (acceleration[1] << 8) | acceleration[0]; return z; }
int16_t adxl346_read_y(void) { uint8_t acceleration[2]; int16_t y; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAY0_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[0]); i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAY1_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[1]); y = (acceleration[1] << 8) | acceleration[0]; return y; }
int16_t adxl346_read_x(void) { uint8_t acceleration[2]; int16_t x; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAX0_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[0]); i2c_write_byte(ADXL346_ADDRESS, ADXL346_DATAX1_ADDR); i2c_read_byte(ADXL346_ADDRESS, &acceleration[1]); x = (acceleration[1] << 8) | acceleration[0]; return x; }
uint16_t i2c_reg_read(uint8_t regAddr) { uint16_t dat; i2c_start(); i2c_send_byte(I2C_INA_ADDR << 1 | I2C_WRITE_CMD); i2c_send_byte(regAddr); i2c_repeat_start(); i2c_send_byte(I2C_INA_ADDR << 1 | I2C_READ_CMD); dat = i2c_read_byte(1) << 8; dat |= i2c_read_byte(1); i2c_stop(); return dat; }
int get_fet_str(int board_id , char * fet){ int err=0; int rc=0; int fetflag = 0; int vpdrev ; pthread_mutex_lock(&i2c_mutex); err = setI2CSwitches(board_id); if (err){ rc = FET_ERROR_BOARD_NA; goto get_out; } vpdrev = (unsigned char)i2c_read_byte(MAIN_BOARD_I2C_EEPROM_DEV_ADDR, 0 , &err); if (err){ rc = FET_ERROR_BOARD_NA; goto get_out; } if (vpdrev == 0xFF) { fprintf(stderr,"VPD not written at all\n"); rc = FET_ERROR_BLANK_VPD; goto get_out; } else if (vpdrev < FET_SUPPORT_VPD_REV){ fprintf(stderr,"VPD from revision %d (need at least %d to include FET information)\n", vpdrev , FET_SUPPORT_VPD_REV); rc = FET_ERROR_VPD_FET_NOT_SUPPORT; goto get_out; } fetflag = (unsigned char)i2c_read_byte(MAIN_BOARD_I2C_EEPROM_DEV_ADDR, MAIN_BOARD_VPD_FET_FLAG_ADDR_START , &err); if (fetflag != 1) { fprintf(stderr,"VPD does not include FET information\n"); rc = FET_ERROR_VPD_FET_NOT_SET; goto get_out; } get_out: resetI2CSwitches(); pthread_mutex_unlock(&i2c_mutex); if (rc == 0){ readMain_I2C_eeprom(fet , board_id , MAIN_BOARD_VPD_FET_STR_ADDR_START , MAIN_BOARD_VPD_FET_STR_ADDR_LENGTH); } return rc; }
extern byte_t usb_in ( byte_t* data, byte_t len ) #endif { uchar i; if(status == STATUS_ADDRESS_ACK) { if(len > expected) { len = expected; } // consume bytes for(i=0;i<len;i++) { expected--; *data = i2c_read_byte(expected == 0); data++; } // end transfer on last byte if((saved_cmd & CMD_I2C_END) && !expected) i2c_stop(); } else { memset(data, 0, len); } return len; }
/************************************************************************* ** 函数名称: pcf8536_read(uint8 addr,uint8 *p,uint8 num) ** 功能描述: 读pcf8563 ** 输 入: uint8 addr :高八位为器件地址,低八位为内部寄存器地址 uint8 *p :读出的数据存放地址的起始地址 uint8 num :读出数据的个数 **************************************************************************/ void pcf8536_read(uint8 addr,uint8 *p,uint8 num) { uint8 *pbuf = p; i2c_start(); if(i2c_write_byte(WR_ADDR_CMD)==SLA_W) { i2c_write_byte(addr); } else { syserr=ERR_SLA_W; } i2c_start(); if(i2c_write_byte(RD_ADDR_CMD)==SLA_W) { i2c_write_byte(addr); } else { syserr=ERR_SLA_W; } for(; num > 0; num--) { *pbuf = i2c_read_byte(); pbuf++; } }
unsigned char I2C_GPIO_Read(unsigned char addr, unsigned char* buf, unsigned int len) { unsigned int i; unsigned char state, data; #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0u; #endif OS_ENTER_CRITICAL(); i2c_start(); //起始条件,开始数据通信 //发送地址和数据读写方向 data = (addr << 1) | 1; //低位为1,表示读数据 state = i2c_write_byte(data); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); return 1; } //读入数据 for (i=0; i<len; i++) { buf[i] = i2c_read_byte(); } i2c_stop(); //终止条件,结束数据通信 OS_EXIT_CRITICAL(); return 0; }
static int sensor_report_value(struct i2c_client *client) { struct sensor_private_data *sensor = (struct sensor_private_data *) i2c_get_clientdata(client); struct sensor_platform_data *pdata = sensor->pdata; int ret = 0; int x = 0, y = 0, z = 0; struct sensor_axis axis; char buffer[6] = {0}; int i = 0; int value = 0; memset(buffer, 0, 6); #if 0 /* Data bytes from hardware xL, xH, yL, yH, zL, zH */ do { buffer[0] = sensor->ops->read_reg; ret = sensor_rx_data(client, buffer, sensor->ops->read_len); if (ret < 0) return ret; } while (0); #else for(i=0; i<6; i++) { i2c_read_byte(client, sensor->ops->read_reg + i,&buffer[i]); } #endif x = (short) (((buffer[0]) << 8) | buffer[1]); y = (short) (((buffer[2]) << 8) | buffer[3]); z = (short) (((buffer[4]) << 8) | buffer[5]); //printk("%s: x=%d y=%d z=%d \n",__func__, x,y,z); if(pdata && pdata->orientation) { axis.x = (pdata->orientation[0])*x + (pdata->orientation[1])*y + (pdata->orientation[2])*z; axis.y = (pdata->orientation[3])*x + (pdata->orientation[4])*y + (pdata->orientation[5])*z; axis.z = (pdata->orientation[6])*x + (pdata->orientation[7])*y + (pdata->orientation[8])*z; } else { axis.x = x; axis.y = y; axis.z = z; } //filter gyro data if((abs(axis.x) > pdata->x_min)||(abs(axis.y) > pdata->y_min)||(abs(axis.z) > pdata->z_min)) { gyro_report_value(client, &axis); /* »¥³âµØ»º´æÊý¾Ý. */ mutex_lock(&(sensor->data_mutex) ); sensor->axis = axis; mutex_unlock(&(sensor->data_mutex) ); } return ret; }
/****************************************************************** * i2c_read_bit ******************************************************************/ int i2c_read_bit(int bus, uint8_t regAddr, uint8_t bitNum,\ uint8_t *data) { uint8_t b; uint8_t count = i2c_read_byte(bus, regAddr, &b); *data = b & (1 << bitNum); return count; }
int readMain_I2C_eeprom (char * vpd_str , int board_id , int startAddress , int length){ int rc = 0; int err=0; pthread_mutex_lock(&i2c_mutex); err = setI2CSwitches(board_id); if (err==0){ for (int i = 0; i < length; i++) { vpd_str[i] = (unsigned char)i2c_read_byte(MAIN_BOARD_I2C_EEPROM_DEV_ADDR, (unsigned char)(startAddress+i), &err); if (err!= 0) break; } } if (err!=0) { rc = err; } resetI2CSwitches(); pthread_mutex_unlock(&i2c_mutex); return rc; }
// Reads alert protocol 8-bit response from LTC2943 int8_t LTC2943_ara_protocol(uint8_t i2c_alert_address, uint8_t *response) { int32_t ack; ack = i2c_read_byte(i2c_alert_address, response); return(ack); }
uint8_t adxl346_is_present(void) { uint8_t is_present; i2c_write_byte(ADXL346_ADDRESS, ADXL346_DEVID_ADDR); i2c_read_byte(ADXL346_ADDRESS, &is_present); return (is_present == ADXL346_DEVID_VALUE); }
/****************************************************************** * i2c_write_bit ******************************************************************/ int i2c_write_bit(int bus, uint8_t regAddr, uint8_t bitNum,\ uint8_t data) { uint8_t b; // read back the current state of the register i2c_read_byte(bus, regAddr, &b); // modify that bit in the register b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum)); // write it back return i2c_write_byte(bus, regAddr, b); }
void imu_a_read_data_raw(vector *v) { unsigned char xl,xh,yl,yh,zl,zh; i2c_start(); i2c_write_byte(0x30); i2c_write_byte(0xa8); i2c_start(); i2c_write_byte(0x31); xl = i2c_read_byte(); xh = i2c_read_byte(); yl = i2c_read_byte(); yh = i2c_read_byte(); zl = i2c_read_byte(); zh = i2c_read_last_byte(); i2c_stop(); v->x = (xh << 8 | xl) /16; v->y = (yh << 8 | yl) /16; v->z = (zh << 8 | zl) /16; }
unsigned char I2C_GPIO_Read_iM205(unsigned char addr, unsigned char reg, unsigned char *val) { unsigned char state, data; #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0u; #endif APP_TRACE_INFO(("\r\nI2C_GPIO_Read_iM205(0x%0X,0x%0X,)", addr, reg)); OS_ENTER_CRITICAL(); i2c_start(); //起始条件,开始数据通信 //发送地址和数据读写方向 data = (addr << 1) | 0; //低位为0,表示写数据 state = i2c_write_byte(data); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); //APP_TRACE_INFO(("\r\nwrite byte err1!")); return 1; } //写入数据 state = i2c_write_byte( reg ); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); //APP_TRACE_INFO(("\r\nwrite byte err2!")); return 1; } i2c_restart(); //发送地址和数据读写方向 data = (addr << 1) | 1; //低位为1,表示读数据 state = i2c_write_byte(data); if(state != 0) { i2c_stop(); OS_EXIT_CRITICAL(); //APP_TRACE_INFO(("\r\n read byte err3!")); return 1; } //读入数据 *val = i2c_read_byte(); i2c_stop(); //终止条件,结束数据通信 OS_EXIT_CRITICAL(); return 0; }
int8_t gyro_read_byte(uint8_t reg){ /*i2c_send_start(); i2c_send_addr(GYRO_ADDR,TW_WRITE); i2c_send_data( reg); i2c_send_start(); i2c_send_addr(GYRO_ADDR,TW_READ); int8_t data= i2c_receive_data(0); i2c_send_stop(); return data; */ return i2c_read_byte(GYRO_ADDR,reg); }
//initializes mma8451 sensor //i2c has to already be enabled int init_mma() { //check for device if it is present if(i2c_read_byte(MMA_ADDR, REG_WHOAMI) == WHOAMI) { Delay(10); //set active mode, 14 bit samples and 100 Hz ODR (0x19) i2c_write_byte(MMA_ADDR, REG_CTRL1, 0x01); return 1; } //else error return 0; }
static u8 cpc710_eeprom_read (unsigned int sdram, unsigned int offset) { u8 dev = (sdram << 1) | 0xa0; u8 data; if (!i2c_read_byte (&data, dev, offset)) { puts ("I2C error !\n"); hang (); } return data; }