int lps331ap_init(lps331ap_t *dev, i2c_t i2c, uint8_t address, lps331ap_rate_t rate) { uint8_t tmp; /* save device specifics */ dev->i2c = i2c; dev->address = address; /* Acquire exclusive access to the bus. */ i2c_acquire(dev->i2c); /* initialize underlying I2C bus */ if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) { /* Release the bus for other threads. */ i2c_release(dev->i2c); return -1; } /* configure device, for simple operation only CTRL_REG1 needs to be touched */ tmp = LPS331AP_CTRL_REG1_DBDU | LPS331AP_CTRL_REG1_PD | (rate << LPS331AP_CTRL_REG1_ODR_POS); if (i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp) != 1) { i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); return 0; }
int tcs37727_set_rgbc_standby(tcs37727_t *dev) { uint8_t reg; if (dev->initialized == false) { return -1; } i2c_acquire(dev->i2c); if (i2c_read_regs(dev->i2c, dev->addr, TCS37727_ENABLE, ®, 1) != 1) { i2c_release(dev->i2c); return -1; } reg &= ~TCS37727_ENABLE_AEN; if (!(reg & TCS37727_ENABLE_PEN)) { reg &= ~TCS37727_ENABLE_PON; } if (i2c_write_reg(dev->i2c, dev->addr, TCS37727_ENABLE, reg) != 1) { i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); return 0; }
uint16_t bh1750fvi_sample(const bh1750fvi_t *dev) { uint32_t tmp; uint8_t raw[2]; /* power on the device and send single H-mode measurement command */ DEBUG("[bh1750fvi] sample: triggering a conversion\n"); i2c_acquire(dev->i2c); i2c_write_byte(dev->i2c, dev->addr, OP_POWER_ON, 0); i2c_write_byte(dev->i2c, dev->addr, OP_SINGLE_HRES1, 0); i2c_release(dev->i2c); /* wait for measurement to complete */ xtimer_usleep(DELAY_HMODE); /* read the results */ DEBUG("[bh1750fvi] sample: reading the results\n"); i2c_acquire(dev->i2c); i2c_read_bytes(dev->i2c, dev->addr, raw, 2, 0); i2c_release(dev->i2c); /* and finally we calculate the actual LUX value */ tmp = ((uint32_t)raw[0] << 24) | ((uint32_t)raw[1] << 16); tmp /= RES_DIV; return (uint16_t)(tmp); }
int srf02_init(srf02_t *dev, i2c_t i2c, uint8_t addr) { dev->i2c = i2c; dev->addr = (addr >> 1); /* internally we right align the 7-bit addr */ uint8_t rev; /* Acquire exclusive access to the bus. */ i2c_acquire(dev->i2c); /* initialize i2c interface */ if (i2c_init_master(dev->i2c, BUS_SPEED) < 0) { DEBUG("[srf02] error initializing I2C bus\n"); return -1; } /* try to read the software revision (read the CMD reg) from the device */ i2c_read_reg(i2c, dev->addr, REG_CMD, &rev); if (rev == 0 || rev == 255) { i2c_release(dev->i2c); DEBUG("[srf02] error reading the devices software revision\n"); return -1; } else { DEBUG("[srf02] software revision: 0x%02x\n", rev); } /* Release the bus for other threads. */ i2c_release(dev->i2c); DEBUG("[srf02] initialization successful\n"); return 0; }
int isl29125_init(isl29125_t *dev, i2c_t i2c, gpio_t gpio, isl29125_mode_t mode, isl29125_range_t range, isl29125_resolution_t resolution) { DEBUG("isl29125_init\n"); /* initialize device descriptor */ dev->i2c = i2c; dev->res = resolution; dev->range = range; dev->gpio = gpio; /* configuration 1: operation mode, range, resolution */ uint8_t conf1 = 0x00; conf1 |= mode; conf1 |= range; conf1 |= resolution; conf1 |= ISL29125_CON1_SYNCOFF; /* TODO: implement SYNC mode configuration */ /* TODO: implement configuration 2: infrared compensation configuration */ /* acquire exclusive access to the bus */ DEBUG("isl29125_init: i2c_acquire\n"); (void) i2c_acquire(dev->i2c); /* initialize the I2C bus */ DEBUG("isl29125_init: i2c_init_master\n"); (void) i2c_init_master(i2c, I2C_SPEED_NORMAL); /* verify the device ID */ DEBUG("isl29125_init: i2c_read_reg\n"); uint8_t reg_id; int ret = i2c_read_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_ID, ®_id); if ((reg_id == ISL29125_ID) && (ret == 1)) { DEBUG("isl29125_init: ID successfully verified\n"); } else { DEBUG("isl29125_init: ID could not be verified, ret: %i\n", ret); (void) i2c_release(dev->i2c); return -1; } /* configure and enable the sensor */ DEBUG("isl29125_init: i2c_write_reg(ISL29125_REG_RESET)\n"); (void) i2c_write_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_RESET, ISL29125_CMD_RESET); DEBUG("isl29125_init: i2c_write_reg(ISL29125_REG_CONF1)\n"); (void) i2c_write_reg(dev->i2c, ISL29125_I2C_ADDRESS, ISL29125_REG_CONF1, conf1); /* release the I2C bus */ DEBUG("isl29125_init: i2c_release\n"); (void) i2c_release(dev->i2c); DEBUG("isl29125_init: success\n"); return 0; }
int mpu9150_init(mpu9150_t *dev, i2c_t i2c, mpu9150_hw_addr_t hw_addr, mpu9150_comp_addr_t comp_addr) { char temp; dev->i2c_dev = i2c; dev->hw_addr = hw_addr; dev->comp_addr = comp_addr; dev->conf = DEFAULT_STATUS; /* Initialize I2C interface */ if (i2c_init_master(dev->i2c_dev, I2C_SPEED_FAST)) { DEBUG("[Error] I2C device not enabled\n"); return -1; } /* Acquire exclusive access */ i2c_acquire(dev->i2c_dev); /* Reset MPU9150 registers and afterwards wake up the chip */ i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_RESET); hwtimer_wait(HWTIMER_TICKS(MPU9150_RESET_SLEEP_US)); i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_WAKEUP); /* Release the bus, it is acquired again inside each function */ i2c_release(dev->i2c_dev); /* Set default full scale ranges and sample rate */ mpu9150_set_gyro_fsr(dev, MPU9150_GYRO_FSR_2000DPS); mpu9150_set_accel_fsr(dev, MPU9150_ACCEL_FSR_2G); mpu9150_set_sample_rate(dev, MPU9150_DEFAULT_SAMPLE_RATE); /* Disable interrupt generation */ i2c_acquire(dev->i2c_dev); i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_INT_ENABLE_REG, REG_RESET); /* Initialize magnetometer */ if (compass_init(dev)) { i2c_release(dev->i2c_dev); return -2; } /* Release the bus, it is acquired again inside each function */ i2c_release(dev->i2c_dev); mpu9150_set_compass_sample_rate(dev, 10); /* Enable all sensors */ i2c_acquire(dev->i2c_dev); i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_1_REG, MPU9150_PWR_PLL); i2c_read_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, &temp); temp &= ~(MPU9150_PWR_ACCEL | MPU9150_PWR_GYRO); i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_PWR_MGMT_2_REG, temp); i2c_release(dev->i2c_dev); hwtimer_wait(HWTIMER_TICKS(MPU9150_PWR_CHANGE_SLEEP_US)); return 0; }
static int jc42_get_register(const jc42_t* dev, uint8_t reg, uint16_t* data) { i2c_acquire(dev->i2c); if (i2c_read_regs(dev->i2c, dev->addr, reg, data, 2, 0) != 0) { DEBUG("[jc42] Problem reading register 0x%x\n", reg); i2c_release(dev->i2c); return JC42_NODEV; } i2c_release(dev->i2c); return JC42_OK; }
int l3g4200d_init(l3g4200d_t *dev, i2c_t i2c, uint8_t address, gpio_t int1_pin, gpio_t int2_pin, l3g4200d_mode_t mode, l3g4200d_scale_t scale) { char tmp; /* Acquire exclusive access to the bus. */ i2c_acquire(dev->i2c); /* initialize the I2C bus */ if (i2c_init_master(i2c, I2C_SPEED) < 0) { /* Release the bus for other threads. */ i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); /* write device descriptor */ dev->i2c = i2c; dev->addr = address; dev->int1 = int1_pin; dev->int2 = int2_pin; /* set scale */ switch (scale) { case L3G4200D_SCALE_250DPS: dev->scale = 250; break; case L3G4200D_SCALE_500DPS: dev->scale = 500; break; case L3G4200D_SCALE_2000DPS: dev->scale = 2000; break; default: dev->scale = 500; break; } /* configure CTRL_REG1 */ tmp = ((mode & 0xf) << L3G4200D_CTRL1_MODE_POS) | L3G4200D_CTRL1_ALLON; i2c_acquire(dev->i2c); if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL1, tmp) != 1) { i2c_release(dev->i2c); return -1; } tmp = ((scale & 0x3) << L3G4200D_CTRL4_FS_POS) | L3G4200D_CTRL4_BDU; if (i2c_write_reg(dev->i2c, dev->addr, L3G4200D_REG_CTRL4, tmp) != 1) { i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); return 0; }
int tcs37727_read(tcs37727_t *dev, tcs37727_data_t *data) { uint8_t buf[8]; if (dev->initialized == false) { return -1; } i2c_acquire(dev->i2c); if (i2c_read_regs(dev->i2c, dev->addr, (TCS37727_INC_TRANS | TCS37727_CDATA), buf, 8) != 8) { i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); int32_t tmpc = ((uint16_t)buf[1] << 8) | buf[0]; int32_t tmpr = ((uint16_t)buf[3] << 8) | buf[2]; int32_t tmpg = ((uint16_t)buf[5] << 8) | buf[4]; int32_t tmpb = ((uint16_t)buf[7] << 8) | buf[6]; DEBUG("rawr: %"PRIi32" rawg %"PRIi32" rawb %"PRIi32" rawc %"PRIi32"\n", tmpr, tmpg, tmpb, tmpc); /* Remove IR component as described in the DN40. */ int32_t ir = (tmpr + tmpg + tmpb - tmpc) >> 1; tmpr -= ir; tmpg -= ir; tmpb -= ir; /* Color temperature calculation as described in the DN40. */ int32_t ct = (CT_COEF_IF * tmpb) / tmpr + CT_OFFSET_IF; /* Lux calculation as described in the DN40. */ int32_t gi = R_COEF_IF * tmpr + G_COEF_IF * tmpg + B_COEF_IF * tmpb; /* TODO: add Glass Attenuation Factor GA compensation */ int32_t cpl = (dev->atime_us * dev->again) / DGF_IF; int32_t lux = gi / cpl; /* Autogain */ tcs37727_trim_gain(dev, tmpc); data->red = (tmpr < 0) ? 0 : (tmpr * 1000) / cpl; data->green = (tmpg < 0) ? 0 : (tmpg * 1000) / cpl; data->blue = (tmpb < 0) ? 0 : (tmpb * 1000) / cpl; data->clear = (tmpb < 0) ? 0 : (tmpc * 1000) / cpl; data->lux = (lux < 0) ? 0 : lux; data->ct = (ct < 0) ? 0 : ct; return 0; }
int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t range, uint8_t type) { uint8_t reg; /* write device descriptor */ dev->i2c = i2c; dev->addr = address; dev->initialized = false; if (dr > MMA8652_DATARATE_1HZ56 || range > MMA8652_FS_RANGE_8G || type >= MMA8x5x_TYPE_MAX) { return -1; } dev->type = type; i2c_acquire(dev->i2c); /* initialize the I2C bus */ if (i2c_init_master(i2c, I2C_SPEED) < 0) { i2c_release(dev->i2c); return -2; } i2c_release(dev->i2c); if (mma8652_test(dev)) { return -3; } if (mma8652_set_standby(dev) < 0) { return -4; } reg = MMA8652_XYZ_DATA_CFG_FS(range); i2c_acquire(dev->i2c); if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_XYZ_DATA_CFG, ®, 1) != 1) { i2c_release(dev->i2c); return -5; } reg = MMA8652_CTRL_REG1_DR(dr); if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_CTRL_REG1, ®, 1) != 1) { i2c_release(dev->i2c); return -5; } i2c_release(dev->i2c); dev->initialized = true; dev->scale = 1024 >> range; return 0; }
int mpl3115a2_is_ready(const mpl3115a2_t *dev) { uint8_t reg; i2c_acquire(BUS); if (i2c_read_regs(BUS, ADDR, MPL3115A2_STATUS, ®, 1, 0) < 0) { i2c_release(BUS); return -MPL3115A2_ERROR_I2C; } i2c_release(BUS); return reg & MPL3115A2_STATUS_PTDR; }
int mpl3115a2_read_temp(const mpl3115a2_t *dev, int16_t *temp) { uint8_t buf[2]; i2c_acquire(BUS); if (i2c_read_regs(BUS, ADDR, MPL3115A2_OUT_T_MSB, buf, 2, 0) < 0) { i2c_release(BUS); return -MPL3115A2_ERROR_I2C; } i2c_release(BUS); *temp = ((int16_t)(((int16_t)buf[0] << 8) | buf[1]) * 10) / 256; return MPL3115A2_OK; }
int mpl3115a2_reset(const mpl3115a2_t *dev) { uint8_t reg; i2c_acquire(BUS); reg = MPL3115A2_CTRL_REG1_RST; if (i2c_write_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, ®, 1, 0) < 0) { i2c_release(BUS); LOG_ERROR("mpl3115a2_reset: failed!\n"); return -MPL3115A2_ERROR_I2C; } i2c_release(BUS); return MPL3115A2_OK; }
static void exit_handler(int32_t signum) { IN(DEBUG_MODEL_MISC, "signum %d", signum); if(_gExitFlag) return; _gExitFlag=1; cmd_queue_release(); menu_release(); menu_queue_release(); i2c_release(); pipe_uninit(); #if(_DEBUG_ == 1 && _DEBUG_TO_FILE_ == 1) if(gDebugToFile == 1) util_debug_file_uninit(); #endif if(SIGQUIT == signum && _gQuitHandler) _gQuitHandler(signum); else if(SIGKILL == signum && _gKillHandler) _gKillHandler(signum); else if(SIGTERM == signum && _gTermHandler) _gTermHandler(signum); else if(SIGINT == signum && _gIntHandler) { debug_print(DEBUG_MODEL_MISC, "kekeke\n"); _gIntHandler(signum); } exit(0); }
static void i2c_init_port(unsigned int port) { const int i2c_clock_bit[] = {21, 22}; if (!(STM32_RCC_APB1ENR & (1 << i2c_clock_bit[port]))) { /* Only unwedge the bus if the clock is off */ if (i2c_claim(port) == EC_SUCCESS) { i2c_release(port); } /* enable I2C2 clock */ STM32_RCC_APB1ENR |= 1 << i2c_clock_bit[port]; } /* force reset of the i2c peripheral */ STM32_I2C_CR1(port) = 0x8000; STM32_I2C_CR1(port) = 0x0000; /* set clock configuration : standard mode (100kHz) */ STM32_I2C_CCR(port) = I2C_CCR; /* set slave address */ if (port == I2C2) STM32_I2C_OAR1(port) = I2C_ADDRESS; /* configuration : I2C mode / Periphal enabled, ACK enabled */ STM32_I2C_CR1(port) = (1 << 10) | (1 << 0); /* error and event interrupts enabled / input clock is 16Mhz */ STM32_I2C_CR2(port) = (1 << 9) | (1 << 8) | 0x10; /* clear status */ STM32_I2C_SR1(port) = 0; board_i2c_post_init(port); }
void lis3mdl_read_mag(lis3mdl_t *dev, lis3mdl_3d_data_t *data) { uint8_t tmp[2] = {0, 0}; i2c_acquire(dev->i2c); i2c_read_regs(dev->i2c, dev->addr, LIS3MDL_OUT_X_L_REG, &tmp[0], 2); data->x_axis = (tmp[1] << 8) | tmp[0]; i2c_read_regs(dev->i2c, dev->addr, LIS3MDL_OUT_Y_L_REG, &tmp[0], 2); data->y_axis = (tmp[1] << 8) | tmp[0]; i2c_read_regs(dev->i2c, dev->addr, LIS3MDL_OUT_Z_L_REG, &tmp[0], 2); data->z_axis = (tmp[1] << 8) | tmp[0]; data->x_axis = _twos_complement(data->x_axis); data->y_axis = _twos_complement(data->y_axis); data->z_axis = _twos_complement(data->z_axis); /* Divide the raw data by 1000 to geht [G] := Gauss */ data->x_axis /= GAUSS_DIVIDER; data->y_axis /= GAUSS_DIVIDER; data->z_axis /= GAUSS_DIVIDER; i2c_release(dev->i2c); }
int mpu9150_set_sample_rate(mpu9150_t *dev, uint16_t rate) { uint8_t divider; if ((rate < MPU9150_MIN_SAMPLE_RATE) || (rate > MPU9150_MAX_SAMPLE_RATE)) { return -2; } else if (dev->conf.sample_rate == rate) { return 0; } /* Compute divider to achieve desired sample rate and write to rate div register */ divider = (1000 / rate - 1); if (i2c_acquire(dev->i2c_dev)) { return -1; } i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_RATE_DIV_REG, (char) divider); /* Store configured sample rate */ dev->conf.sample_rate = 1000 / (((uint16_t) divider) + 1); /* Always set LPF to a maximum of half the configured sampling rate */ conf_lpf(dev, (dev->conf.sample_rate >> 1)); i2c_release(dev->i2c_dev); return 0; }
int mpu9150_set_accel_fsr(mpu9150_t *dev, mpu9150_accel_ranges_t fsr) { if (dev->conf.accel_fsr == fsr) { return 0; } switch (fsr) { case MPU9150_ACCEL_FSR_2G: case MPU9150_ACCEL_FSR_4G: case MPU9150_ACCEL_FSR_8G: case MPU9150_ACCEL_FSR_16G: if (i2c_acquire(dev->i2c_dev)) { return -1; } i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_ACCEL_CFG_REG, (char)(fsr << 3)); i2c_release(dev->i2c_dev); dev->conf.accel_fsr = fsr; break; default: return -2; } return 0; }
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; }
int mma8652_reset(mma8652_t *dev) { uint8_t reg; dev->initialized = false; reg = MMA8652_CTRL_REG2_RST; i2c_acquire(dev->i2c); if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_CTRL_REG2, ®, 1) != 1) { i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); return 0; }
int lps331ap_disable(const lps331ap_t *dev) { uint8_t tmp; int status; i2c_acquire(dev->i2c); if (i2c_read_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, &tmp) != 1) { i2c_release(dev->i2c); return -1; } tmp &= ~(LPS331AP_CTRL_REG1_PD); status = i2c_write_reg(dev->i2c, dev->address, LPS331AP_REG_CTRL_REG1, tmp); i2c_release(dev->i2c); return status; }
int mpu9150_read_compass(mpu9150_t *dev, mpu9150_results_t *output) { char data[6]; /* Acquire exclusive access */ if (i2c_acquire(dev->i2c_dev)) { return -1; } /* Read raw data */ i2c_read_regs(dev->i2c_dev, dev->hw_addr, MPU9150_EXT_SENS_DATA_START_REG, data, 6); /* Release the bus */ i2c_release(dev->i2c_dev); output->x_axis = (data[1] << 8) | data[0]; output->y_axis = (data[3] << 8) | data[2]; output->z_axis = (data[5] << 8) | data[4]; /* Compute sensitivity adjustment */ output->x_axis = (int16_t) (((float)output->x_axis) * ((((dev->conf.compass_x_adj - 128) * 0.5) / 128.0) + 1)); output->y_axis = (int16_t) (((float)output->y_axis) * ((((dev->conf.compass_y_adj - 128) * 0.5) / 128.0) + 1)); output->z_axis = (int16_t) (((float)output->z_axis) * ((((dev->conf.compass_z_adj - 128) * 0.5) / 128.0) + 1)); /* Normalize data according to full-scale range */ output->x_axis = output->x_axis * 0.3; output->y_axis = output->y_axis * 0.3; output->z_axis = output->z_axis * 0.3; return 0; }
int mpu9150_set_gyro_fsr(mpu9150_t *dev, mpu9150_gyro_ranges_t fsr) { if (dev->conf.gyro_fsr == fsr) { return 0; } switch (fsr) { case MPU9150_GYRO_FSR_250DPS: case MPU9150_GYRO_FSR_500DPS: case MPU9150_GYRO_FSR_1000DPS: case MPU9150_GYRO_FSR_2000DPS: if (i2c_acquire(dev->i2c_dev)) { return -1; } i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_GYRO_CFG_REG, (char)(fsr << 3)); i2c_release(dev->i2c_dev); dev->conf.gyro_fsr = fsr; break; default: return -2; } return 0; }
int mpu9150_set_compass_sample_rate(mpu9150_t *dev, uint8_t rate) { uint8_t divider; if ((rate < MPU9150_MIN_COMP_SMPL_RATE) || (rate > MPU9150_MAX_COMP_SMPL_RATE) || (rate > dev->conf.sample_rate)) { return -2; } else if (dev->conf.compass_sample_rate == rate) { return 0; } /* Compute divider to achieve desired sample rate and write to slave ctrl register */ divider = (dev->conf.sample_rate / rate - 1); if (i2c_acquire(dev->i2c_dev)) { return -1; } i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE4_CTRL_REG, (char) divider); i2c_release(dev->i2c_dev); /* Store configured sample rate */ dev->conf.compass_sample_rate = dev->conf.sample_rate / (((uint16_t) divider) + 1); 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; }
/* I2C receiving of bytes - does not send an offset byte */ int efx_i2c_recv_bytes(struct efx_i2c_interface *i2c, u8 device_id, u8 *bytes, unsigned int len) { int i; int rc; EFX_WARN_ON_PARANOID(getsda(i2c) != 1); EFX_WARN_ON_PARANOID(getscl(i2c) != 1); EFX_WARN_ON_PARANOID(len < 1); /* Select device */ i2c_start(i2c); /* Read data from device */ rc = i2c_send_byte(i2c, i2c_read_cmd(device_id)); if (rc) goto out; for (i = 0; i < (len - 1); i++) /* Read and acknowledge all but the last byte */ bytes[i] = i2c_recv_byte(i2c, 1); /* Read last byte with no acknowledgement */ bytes[i] = i2c_recv_byte(i2c, 0); out: i2c_stop(i2c); i2c_release(i2c); return rc; }
/* This performs a fast write of one or more consecutive bytes to an * I2C device. Not all devices support consecutive writes of more * than one byte; for these devices use efx_i2c_write() instead. */ int efx_i2c_fast_write(struct efx_i2c_interface *i2c, u8 device_id, u8 offset, const u8 *data, unsigned int len) { int i; int rc; EFX_WARN_ON_PARANOID(getsda(i2c) != 1); EFX_WARN_ON_PARANOID(getscl(i2c) != 1); EFX_WARN_ON_PARANOID(len < 1); /* Select device and starting offset */ i2c_start(i2c); rc = i2c_send_byte(i2c, i2c_write_cmd(device_id)); if (rc) goto out; rc = i2c_send_byte(i2c, offset); if (rc) goto out; /* Write data to device */ for (i = 0; i < len; i++) { rc = i2c_send_byte(i2c, data[i]); if (rc) goto out; } out: i2c_stop(i2c); i2c_release(i2c); return rc; }
int hdc1000_reset(hdc1000_t *dev) { char reg[2]; uint16_t tmp = HDC1000_CONFG_RST; reg[0] = (uint8_t)(tmp >> 8); reg[1] = (uint8_t)tmp; dev->initialized = false; i2c_acquire(dev->i2c); if (i2c_write_regs(dev->i2c, dev->addr, HDC1000_CONFG, reg, 2) != 2) { i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); return 0; }
int mma8652_is_ready(mma8652_t *dev) { uint8_t reg; if (dev->initialized == false) { return -1; } i2c_acquire(dev->i2c); if (i2c_read_regs(dev->i2c, dev->addr, MMA8652_STATUS, ®, 1) != 1) { i2c_release(dev->i2c); return -1; } i2c_release(dev->i2c); return reg & MMA8652_STATUS_ZYXDR; }
void srf02_trigger(srf02_t *dev, srf02_mode_t mode) { /* trigger a new measurement by writing the mode to the CMD register */ DEBUG("[srf02] trigger new reading\n"); i2c_acquire(dev->i2c); i2c_write_reg(dev->i2c, dev->addr, REG_CMD, mode); i2c_release(dev->i2c); }