void imu_init() { lock = 0; i2c_open(&imuConn, PIN_IMU_SCL, PIN_IMU_SDA, 0); // Gyro initialization. // 22 -> 11011 Set internal clock and scale. // 21 -> 9 Set sample rate. // 23 -> 101 Trigger interrupt when new data is ready (extra int pins). // 62 -> 1 Set clock source. write_to_register(&imuConn, GYRO_ADDR, 0x16, 0x1A); write_to_register(&imuConn, GYRO_ADDR, 0x15, 0x09); write_to_register(&imuConn, GYRO_ADDR, 0x17, 0x05); write_to_register(&imuConn, GYRO_ADDR, 0x3E, 1); // Accel initialization. // 45 -> 1000, 100 Wake it from sleep, keep it awake. // 49 -> 01 Set the data range. 00->2, 01->4, 10->8, 11->16 (+- g). write_to_register(&imuConn, ACCL_ADDR, 0x2D, 16); write_to_register(&imuConn, ACCL_ADDR, 0x2D, 8); write_to_register(&imuConn, ACCL_ADDR, 0x31, 0); // Might need to change this later. //writeToRegister(&imuConn, ACCL_ADDR, 0x1E, -25/-4); -25 // Tuning values. //writeToRegister(&imuConn, ACCL_ADDR, 0x1F, 12/-4); 12 //writeToRegister(&imuConn, ACCL_ADDR, 0x20, 249/4); 249 }
int main(int argc, char *argv[]) { int fd; int sock; daisy7_imu imu_data; sock = gcs_udp_open(); if (sock==0) return(1); printf("Starting...sock %X\n", sock); // init i2c fd = i2c_open(I2C_G20_DEVICE); if (!(fd<0)) { // i2c opened. Init daisy7 daisy7_init(fd); while(1) { daisy7_readall(fd, &imu_data); gcs_heartbeat(sock); gcs_raw_imu(sock,imu_data); // printf("acc(%5d - %5d - %5d) gyro(%5d - %5d - %5d) \r", // imu_data.acc.raw_x,imu_data.acc.raw_y,imu_data.acc.raw_z, // imu_data.gyro.raw_x,imu_data.gyro.raw_y,imu_data.gyro.raw_z); usleep(2500); } } gcs_udp_close(sock); return(0); }
/*! * \brief Command initialization. * * \param argc Command-line argument count. * \param argv Command-line arguments. * \param pI2C Pointer to \h_i2c handle. */ static void MainInit(int argc, char *argv[], i2c_t *pI2C) { // Name of this process Argv0 = basename(argv[0]); // Get the environment //EnvGet(); // Parse input arguments argv = OptsGet(Argv0, &PkgInfo, &I2CShPgmInfo, I2CShOptsInfo, true, &argc, argv); if( OptDevName == NULL || OptDevName[0] == 0 ) { OptDevName = "/dev/i2c/0"; } if( OptVerbose ) { printf("I2C device: %s\n\n", OptDevName); } if( i2c_open(pI2C, OptDevName) < 0 ) { LOGSYSERROR("%s: Failed to open.", OptDevName); exit(EC_ERROR); } }
//--------------------------------------------------------------------------- void QboxDialog::on_openBtn_clicked() { if(isOpen()) { close_frontend(); return; } #if 1 i2c_close(); unsigned char tmp8[2]; i2c_fd = i2c_open("/dev/i2c-5"); if(i2c_fd < 0) { qDebug("Failed to open i2c"); return; } // read device id from system register if(i2c_read(1, 0x00) > 0) qDebug("Device ID Register: 0x%02x, Device id: 0x%02x revision: 0x%02x", i2c_rx_buf[0], (i2c_rx_buf[0] >> 4), (i2c_rx_buf[0] & 0x0f)); // read system mode register if(i2c_read(1, 0x01) > 0) { tmp8[0] = (i2c_rx_buf[0] >> 3) & 0x03; tmp8[1] = i2c_rx_buf[0] & 0x07; qDebug("System mode: 0x%02x, Modulation: %s, Mode: %s", i2c_rx_buf[0], tmp8[0] == 0x00 ? "BPSK":(tmp8[0] == 0x01 ? "QPSK":"Reservd"), tmp8[1] == 0x00 ? "DVB-S":(tmp8[1] == 0x01 ? "DSS":"Reservd")); }
i2c *i2c_newbus(int sclpin, int sdapin, int scldrive) { i2c *bus = i2c_open(&list[buscnt++], sclpin, sdapin, scldrive); //return bus; bus = &list[buscnt-1]; return bus; }
int main(int argc, char *argv[]) { int mode,i; char data[8]; if( i2c_open(BUS,I2C_DEVICE) < 0) return -1; if(argc == 2 && argv[1][0] == 'h') mode = 1; else mode = 0; data[0] = 0; data[1] = 0; for(i=2;i<8;i++) data[i] = '0'; //i2c_write(data,10); data[0] = 0; data[1] = 0; i2c_write(data,2); dumpEeprom(mode); i2c_close(); return 0; }
int i2c_write(int device, unsigned char *buf, int count) { int cnt = count; int timeout = 5; device &= 0xFF; i2c_open(); __i2c_send_nack(); /* Master does not send ACK, slave sends it */ W_try_again: if (timeout < 0) goto W_timeout; cnt = count; __i2c_send_start(); if (i2c_put_data( (device << 1) | I2C_WRITE ) < 0) goto device_err; #if 0 //CONFIG_JZ_TPANEL_ATA2508 if (address == 0xff) { while (cnt) { if (i2c_put_data_nack(*buf) < 0) break; cnt--; buf++; } } else #endif { while (cnt) { if (i2c_put_data(*buf) < 0) break; cnt--; buf++; } } __i2c_send_stop(); i2c_close(); return count - cnt; device_err: timeout--; __i2c_send_stop(); goto W_try_again; W_timeout: logf("Write I2C device 0x%2x failed.", device); __i2c_send_stop(); i2c_close(); return -1; }
int i2c_init_taos() { fTAOS = i2c_open("/dev/i2c-1", ADDRESS_TAOS); printf("i2c: taos:file %d\n", fTAOS); if (fTAOS == -1) return -1; i2c_taos_config(); return 0; }
static unsigned long i2c_get_funcs(int bus) { int fd = i2c_open(bus, 0, 0); unsigned long result; xioctl(fd, I2C_FUNCS, &result); close(fd); return result; }
int i2c_init_encoder() { fENCODER = i2c_open("/dev/i2c-1", ADDRESS_ENCODER); printf("i2c: encoder:file %d\n", fENCODER); if (fENCODER != -1) return 0; else return -1; }
int ledrgb_close () { if(ledrgb_open_count && --ledrgb_open_count == 0) { i2c_open(); i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_CONFIG, 0); i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_ENABLE, 0); i2c_close(); } }
int i2c_init_gpio() { fGPIO = i2c_open("/dev/i2c-1", ADDRESS_GPIO); printf("i2c: gpio:file %d\n", fGPIO); if (fGPIO != -1) { set_gpio_config(); return 0; } else { return -1; } }
/*! * \brief Command initialization. * * \param argc Command-line argument count. * \param argv Command-line arguments. * \param pI2C Pointer to \h_i2c handle. */ static void MainInit(int argc, char *argv[], i2c_t *pI2C) { // Name of this process Argv0 = basename(argv[0]); // Get the environment //EnvGet(); // Parse input arguments argv = OptsGet(Argv0, &PkgInfo, &I2CWritePgmInfo, I2CWriteOptsInfo, true, &argc, argv); // Final option checks if( OptI2CAddr < 0x00 ) { fprintf(stderr, "%s: Address option required\n", Argv0); exit(EC_BAD_OPT); } else if( (OptI2CAddr < I2C_ADDR_DEV_LOW) || (OptI2CAddr > I2C_ADDR_DEV_HIGH) ) { fprintf(stderr, "%s: Address out of range: 0x%x\n", Argv0, OptI2CAddr); exit(EC_BAD_OPT); } // Convert command-line arguments ArgsGet(argc, argv); // Opened device specified if( OptDevFd >= 0 ) { pI2C->fd = OptDevFd; pI2C->addr = (ushort_t)(-1); } // I2C Bus device specified else { if( OptDevName == NULL || OptDevName[0] == 0 ) { OptDevName = "/dev/i2c/0"; } if( OptVerbose ) { printf("I2C device: %s\n\n", OptDevName); } if( i2c_open(pI2C, OptDevName) < 0 ) { LOGSYSERROR("%s: Failed to open.", OptDevName); exit(EC_ERROR); } } }
int cam_init(void) { int i=0; unsigned char pair[2]; int not_finished=1; i2ch=i2c_open(I2CBUS, CAM_ADDR); // do a reset // reset the sensor pair[0]=MT9M001_RESET; pair[1]=0x0001; i2c_write(i2ch, CAM_ADDR, pair, 2); delay_ms(999); do { // obtain address and value to program pair[0]=params_1280x1024[i]; pair[1]=params_1280x1024[i+1]; // reached end? if ((pair[0]==0xaa) && (pair[1]==0xbb)) { if ((params_1280x1024[i+2]==0xcc) && (params_1280x1024[i+3]==0xdd)) { // end sequence seen. not_finished=0; break; } } //i2c_write(i2ch, pair, 2); i2c_write(i2ch, CAM_ADDR, pair, 2); delay_ms(10); i=i+2; }while (not_finished); // test //pair[0]=OV9655_COM3; pair[1]=0x80; //i2c_write_ignore_nack(i2ch, CAM_ADDR, pair, 2); //delay_ms(10); cam_id_dump(); i2c_close(i2ch); printf("Camera should be working\n"); return(0); }
int i2c_read(int device, unsigned char *buf, int count) { int cnt = count; int timeout = 5; device &= 0xFF; i2c_open(); L_try_again: if (timeout < 0) goto L_timeout; __i2c_send_nack(); /* Master does not send ACK, slave sends it */ __i2c_send_start(); if (i2c_put_data( (device << 1) | I2C_READ ) < 0) goto device_err; __i2c_send_ack(); /* Master sends ACK for continue reading */ while (cnt) { if (cnt == 1) { if (i2c_get_data(buf, 0) < 0) break; } else { if (i2c_get_data(buf, 1) < 0) break; } cnt--; buf++; } __i2c_send_stop(); i2c_close(); return count - cnt; device_err: timeout--; __i2c_send_stop(); goto L_try_again; L_timeout: __i2c_send_stop(); logf("Read I2C device 0x%2x failed.", device); i2c_close(); return -1; }
int i2c_init_servo() { fSERVO = i2c_open("/dev/i2c-1", ADDRESS_SERVO); printf("i2c: servo:file %d\n", fSERVO); if (fSERVO == -1) return -1; i2c_servo_set_servo(SERVO_LEFT, servo_left_close); i2c_servo_set_servo(SERVO_RIGHT, servo_right_close); u08 buf[2]; buf[0] = 0x00, buf[1] = SERVO_AS_SR_DEF; i2c_write(fSERVO, buf, 2); return 0; }
void i2cget_main(void) { int bus = atolx_range(toys.optargs[0], 0, INT_MAX); int chip = atolx_range(toys.optargs[1], 0, 0x7f); int addr = atolx_range(toys.optargs[2], 0, 0xff); int fd, byte; confirm("Read register 0x%02x from chip 0x%02x on bus %d?", addr, chip, bus); fd = i2c_open(bus, (toys.optflags&FLAG_f)?I2C_SLAVE_FORCE:I2C_SLAVE, chip); if (i2c_read_byte(fd, addr, &byte) == -1) perror_exit("i2c_read_byte"); printf("0x%02x\n", byte); close(fd); }
void ledrgb_set(uint8_t rgb_mask, uint8_t r, uint8_t g, uint8_t b) { //TRACE_INFO("ledrgb_set %x %x %x\r\n", r, g, b); i2c_open(); if (rgb_mask & 0x1) i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_RPWM, r); if (rgb_mask & 0x2) i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_GPWM, g); if (rgb_mask & 0x4) i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_BPWM, b); i2c_close(); }
void i2cdetect_main(void) { if (toys.optflags & FLAG_l) { if (toys.optc) error_exit("-l doesn't take arguments"); dirtree_read("/sys/class/i2c-dev", i2cdetect_dash_l); } else if (toys.optflags & FLAG_F) { if (toys.optc != 1) error_exit("-F BUS"); i2cdetect_dash_F(atolx_range(*toys.optargs, 0, INT_MAX)); } else { int bus, first = 0x03, last = 0x77, fd, row, addr, byte; if (toys.optflags & FLAG_a) { first = 0x00; last = 0x7f; } if (toys.optc != 1 && toys.optc != 3) error_exit("bad args"); bus = atolx_range(*toys.optargs, 0, INT_MAX); if (toys.optc == 3) { first = atolx_range(toys.optargs[1], 0, 0x7f); last = atolx_range(toys.optargs[1], 0, 0x7f); if (first > last) error_exit("first > last"); } confirm("Probe chips 0x%02x-0x%02x on bus %d?", first, last, bus); fd = i2c_open(bus, 0, 0); printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); for (row = 0; row <= 0x70; row += 16) { xprintf("%02x:", row & 0xf0); for (addr = row; addr < row + 16; ++addr) { if (addr < first || addr > last) printf(" "); else { if (ioctl(fd, I2C_SLAVE, addr) == -1) { if (errno == EBUSY) { xprintf(" UU"); continue; } perror_exit("ioctl(I2C_SLAVE)"); } if (i2c_read_byte(fd, addr, &byte) == -1) xprintf(" --"); else xprintf(" %02x", addr); } } putchar('\n'); } close(fd); } }
int main(void){ i2c_init(); i2c_open(addr); i2c_write(R_Vel,0x03,1000); i2c_write(L_Vel,0x02,500); //y = i2c_read(L_Enc); //t = i2c_read(R_Enc); printf("%u\n", t); printf("%u\n", y); return(0); }
/** * Open I2C device * * @param self * @param args * @return */ static PyObject* py_open(PyObject* self, PyObject* args) { /* Parse arguments */ if (!PyArg_ParseTuple(args, "i", &address)) { return NULL; } /* Open slave device*/ fd = i2c_open(bus, address); if (fd < 0) { return PyErr_SetFromErrno(PyExc_IOError); } Py_RETURN_NONE; }
unsigned char cam_reg_read(unsigned char addr, unsigned char reg) { int ret; unsigned char buf[2]; // To read from the camera, we first write the register value buf[0]=reg; //i2c_write_ignore_nack(i2ch, CAM_ADDR, buf, 1); i2c_write(i2ch, (char *) CAM_ADDR, buf, 1); i2c_close(i2ch); i2ch=i2c_open(I2CBUS, CAM_ADDR); ret=i2c_read_no_ack(i2ch, CAM_ADDR, buf, 1); if (ret<0) printf("cam_reg_read error!\n"); return(buf[0]); }
int cam_trigger() { int i=0; unsigned char pair[2]; i2ch=i2c_open(I2CBUS, CAM_ADDR); // trigger an exposure pair[0]=MT9M001_FRAME_RESTART; pair[1]=0x0001; i2c_write(i2ch, CAM_ADDR, pair, 2); cam_id_dump(); i2c_close(i2ch); return(0); }
static be_jse_symbol_t *i2c_module_handle_cb(be_jse_vm_ctx_t *execInfo, be_jse_symbol_t *var, const char *name) { if (0 == strcmp(name, "open")) { return i2c_open(); } if (0 == strcmp(name, "read")) { return i2c_read(); } if (0 == strcmp(name, "write")) { return i2c_write(); } if (0 == strcmp(name, "close")) { return i2c_close(); } return (BE_JSE_FUNC_UNHANDLED); }
int main (void) { if (i2c_open()<0) { printf("Apertura del bus I2C fallita\n"); return 1; } ds1307_init(); input_data(); set_data(giorno_in,mese_in,anno_in); printf("Data sul DS1307: %d/%d/%d\r\n",read_day(),read_month(),read_year()); input_ora(); set_ora(ora_in,minuto_in); while (1){ printf("Data sul DS1307: %02X/%02X/%02X\r\n", read_day(), read_month(), read_year()); printf("Ora sul DS1307: %02X:%02X:%02X\r\n",read_hour(),read_min(),read_sec()); sleep(10); } return 1; }
int ledrgb_open () { if(!ledrgb_open_count++) { i2c_open(); i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_ENABLE, LEDRGB_CHIPEN); //delay(500); Task_sleep(1); //i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_CONFIG, LEDRGB_CPMODE_AUTO|LEDRGB_PWM_HF|LEDRGB_INT_CLK_EN|LEDRGB_R_TO_BATT); i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_CONFIG, LEDRGB_CPMODE_AUTO|LEDRGB_PWM_HF|LEDRGB_INT_CLK_EN|LEDRGB_R_TO_BATT|LEDRGB_PWRSAVE_EN); //delay(20); i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_OPMODE, LEDRGB_RMODE_DC|LEDRGB_GMODE_DC|LEDRGB_BMODE_DC); //config lights: i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_RCURRENT, 0x20); i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_GCURRENT, 0x20); i2cMasterWrite(LEDRGB_PHY_ADDR, 1, LEDRGB_REG_BCURRENT, 0x20); i2c_close(); } }
int aversive_open(aversive_dev_t* dev) { #if CONFIG_USE_I2C if (i2c_open(&dev->i2c_dev, 0) == -1) { DEBUG_ERROR("i2c_open\n"); return -1; } #elif CONFIG_USE_CAN dev->can_dev = can_open(); if (dev->can_dev == 0) { DEBUG_ERROR("can_open\n"); return -1; } #endif /* CONFIG_USE_XXX */ #if (CONFIG_DO_ADC == 0) #if (CONFIG_DO_KEYVAL == 0) /* send the following to initialize aversive */ aversive_sync_sequence(dev); aversive_stop(dev); aversive_set_asserv(dev, 0); aversive_set_power(dev, 0); aversive_set_pos(dev, 0, 0, 0); aversive_set_power(dev, 1); aversive_set_asserv(dev, 1); aversive_set_blocking_params(dev, 5, 300, 8000); aversive_set_blocking_params2(dev, 150, 150); #endif /* CONFIG_DO_KEYVAL == 0 */ #endif /* CONFIG_DO_ADC == 0 */ return 0; }
int main(int argc, char** argv) { if (argc < 3) { fprintf(stderr, "Must specify I2C bus number and command\n"); return -1; } int returnVal = -1; int fd = i2c_open(atoi(argv[1]), I2C_ADDRESS); if (fd == -1) return -1; if (strcmp(argv[2], "s") == 0) { returnVal = switch_application(fd); } else if (strcmp(argv[2], "v") == 0) { unsigned char version[INFO_SIZE]; version[INFO_SIZE] = '\0'; returnVal = read_version(fd, version, INFO_SIZE); if (returnVal != -1) printf("%s\n", version); } else if (strcmp(argv[2], "c") == 0) { unsigned char buffer[2]; returnVal = read_checksum(fd, buffer, sizeof(buffer)); if (returnVal != -1) { printf("%02X %02X\n", buffer[0], buffer[1]); } } else { fprintf(stderr, "Unknown command\n"); } i2c_close(fd); return returnVal; }
void i2cdump_main(void) { int bus = atolx_range(toys.optargs[0], 0, INT_MAX); int chip = atolx_range(toys.optargs[1], 0, 0x7f); int fd, row, addr, byte; confirm("Dump chip 0x%02x on bus %d?", chip, bus); fd = i2c_open(bus, (toys.optflags&FLAG_f)?I2C_SLAVE_FORCE:I2C_SLAVE, chip); printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f 0123456789abcdef\n"); for (row = 0; row <= 0xf0; row += 16) { xprintf("%02x:", row & 0xf0); for (addr = row; addr < row + 16; ++addr) { if (i2c_read_byte(fd, addr, &byte) == -1) perror_exit("i2c_read_byte"); printf(" %02x", byte); toybuf[addr-row] = isprint(byte) ? byte : (byte ? '?' : '.'); } printf(" %16.16s\n", toybuf); } close(fd); }
/** Initialize Gsensor IIC bus Initialize Gsensor IIC bus @param void @return void */ BOOL GSensor_I2C_Init(GSENSOR_INFO GSensorInfo) { UINT erReturn; g_GsensorInfo.I2C_RegBytes = GSensorInfo.I2C_RegBytes; g_GsensorInfo.I2C_PinMux = GSensorInfo.I2C_PinMux; g_GsensorInfo.I2C_BusClock = GSensorInfo.I2C_BusClock; g_GsensorInfo.I2C_Slave_WAddr = GSensorInfo.I2C_Slave_WAddr; g_GsensorInfo.I2C_Slave_RAddr = GSensorInfo.I2C_Slave_RAddr; if (g_GsensorInfo.I2C_RegBytes > GSENSOR_I2C_REGISTER_2BYTE) { debug_msg("Error register Byte Count !!\r\n"); return FALSE; } if (g_GsensorInfo.I2C_PinMux > I2C_PINMUX_2ND) { debug_msg("Error I2C Pin mux !!\r\n"); return FALSE; } if (g_GsensorInfo.I2C_BusClock > I2C_BUS_CLOCK_1MHZ) { debug_msg("Error I2C Bus Clock !!\r\n"); return FALSE; } erReturn = i2c_open(&(g_GsensorInfo.I2C_Channel)); if(erReturn != I2C_STS_OK) { debug_err(("Error open I2C driver!!\r\n")); return FALSE; } i2c_setConfig(g_GsensorInfo.I2C_Channel, I2C_CONFIG_ID_PINMUX, g_GsensorInfo.I2C_PinMux); //select to control I2C 1st device i2c_setConfig(g_GsensorInfo.I2C_Channel, I2C_CONFIG_ID_BUSCLOCK, g_GsensorInfo.I2C_BusClock); //up to 400KHZ return TRUE; }