static int psu_info_get_product_name(int id, UI8_T *data) { DIAG_PRINT("%s, id:%d", __FUNCTION__, id); int ret = 0; ret = i2c_read_block(id, RPS_PSU_EEPROM_ADDR, PSUI_PRODUCT_NAME_REG, data, PSUI_PRODUCT_NAME_SIZE); if (ret < 0 && PSUI_DEBUG_FLAG) printf("I2C command 0x%X Read Fail, id=%d\n", PSUI_PRODUCT_NAME_REG,id); return ret; }
static int psu_info_get_product_ser(psu_type_t psu_type, int id, UI8_T *data) { int ret = 0; int addr = 0 ; DIAG_PRINT("%s, id:%d", __FUNCTION__, id); if (psu_type == PSU_TYPE_AC_F2B) { addr = PSUI_PRODUCT_SER_NO_REG_F2B; } else { addr = PSUI_PRODUCT_SER_NO_REG_B2F; } ret = i2c_read_block(id, RPS_PSU_EEPROM_ADDR, addr, data, PSUI_PRODUCT_SER_NO_SIZE); if (ret < 0) printf("I2C command 0x%X Read Fail, id=%d\n", addr,id); return ret; }
// Retrieves only the mpu's gyro axes data, then returns // the data as an Axes struct pointer. static Axes *read_gyro(Sensor *s) { // malloc new axes struct Axes *res = malloc(sizeof(Axes)); // Read the results in a burst, starting from xout reg uint8_t *readings = i2c_read_block( s->i2c, s->i2c_addr, MPU_XOUT_H, 6 ); // Extract x y z values res->x = (readings[0] << 8) | readings[1]; res->y = (readings[2] << 8) | readings[3]; res->z = (readings[4] << 8) | readings[5]; // Free the array free(readings); // Set type of results res->type = GYRO; // No next axes (not reading aux here) res->next = NULL; // Return the resulting readings return res; }
static Axes *read_burst(Sensor *s) { // Get the current data count int fifo_count = (FETCH_REG(MPU_FIFO_COUNTH) << 8) + FETCH_REG(MPU_FIFO_COUNTL); // Return null if currently empty if (!fifo_count) { // Return null return NULL; } // Adjust count to a multiple of three fifo_count = (fifo_count / 6) * 6; // Otherwise generate an array of uint8_ts uint8_t *block = i2c_read_block( s->i2c, s->i2c_addr, MPU_FIFO_R_W, fifo_count ), *data = (block + fifo_count); // Create pointers to Axes Axes *head = NULL; // Parse into axes data for (int i = fifo_count / 6; i > 0; i--) { // malloc next link with the previous head as its next head = axes_malloc(head); // Decrement data data -= 6; // Extract x y z values head->x = (data[0] << 8) | data[1]; head->y = (data[2] << 8) | data[3]; head->z = (data[4] << 8) | data[5]; } // Free the byte array free(block); // Return the Axes return head; }
int main(int argc, char *argv[]) { int fd, i; unsigned char value; unsigned char wbuf[] = { 0x11, 0x22, 0x33, 0x44, 0x55 }; unsigned char rbuf[sizeof(wbuf)]; // 打开i2c总线1,需要根据具体情况来的打开 fd = i2c_open_dev(1); if (fd < 0) { // 打开失败,可能是没有该i2c总线 printf("open i2c fail %d\n", fd); exit(1); } // 设置i2c从设备地址为0x22 if(i2c_change_slave(fd, 0x22)<0){ printf("set slave failed\n"); close(fd); exit(2); } printf("byte test...\n"); // 单字节写入 printf("write..\n"); for(i=0; i<256; ++i){ value = i; if(i2c_write(fd, i, value)==1){ // 写入成功 printf("[%02x]=%02x ", i, value); } else{ // 写入失败 printf("[%02x]=XX ", i); } if(i%8==7) printf("\n"); } // 读取 printf("read..\n"); for(i=0; i<256; ++i){ if(i2c_read(fd, i, &value)==1){ // 读取成功 printf("[%02x]=%02x ", i, value); } else{ // 读取失败 printf("[%02x]=XX ", i); } if(i%8==7) printf("\n"); } printf("block test...\n"); // 块写入 if(i2c_write_block(fd, 0, wbuf, sizeof(wbuf))!=sizeof(wbuf)){ printf("i2c_write_block failed\n"); } // 块读出 if(i2c_read_block(fd, 0, rbuf, sizeof(rbuf))!=sizeof(rbuf)){ printf("i2c_read_block failed\n"); } else{ for(i=0; i<sizeof(rbuf); ++i){ printf("r[%02x]=%02x\n", i, rbuf[i]); } } // 最后关闭文件 close(fd); return 0; }