示例#1
0
文件: bh1750fvi.c 项目: A-Paul/RIOT
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);
}
示例#2
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
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;
}
示例#3
0
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;
}
示例#4
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, &reg, 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, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -5;
    }
    i2c_release(dev->i2c);

    dev->initialized = true;
    dev->scale = 1024 >> range;

    return 0;
}
示例#5
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;
}
示例#6
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
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;
}
示例#7
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;
}
示例#8
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, &reg, 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;
}
示例#9
0
文件: srf02.c 项目: LucaZulberti/RIOT
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;
}
示例#10
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
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;
}
示例#11
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
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;
}
示例#12
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
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;
}
示例#13
0
文件: mpu9150.c 项目: 4dahalibut/RIOT
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;
}
示例#14
0
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);
}
示例#15
0
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;
}
示例#16
0
文件: srf02.c 项目: LucaZulberti/RIOT
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);
}
示例#17
0
void lis3mdl_enable(lis3mdl_t *dev)
{
    i2c_acquire(dev->i2c);
    /* Z-axis medium-power mode */
    i2c_write_reg(dev->i2c, dev->addr,
                  LIS3MDL_CTRL_REG3, LIS3MDL_MASK_REG3_Z_MEDIUM_POWER);
    i2c_release(dev->i2c);
}
示例#18
0
void lis3mdl_enable(const lis3mdl_t *dev)
{
    i2c_acquire(DEV_I2C);
    /* Z-axis medium-power mode */
    i2c_write_reg(DEV_I2C, DEV_ADDR,
                  LIS3MDL_CTRL_REG3, LIS3MDL_MASK_REG3_Z_MEDIUM_POWER);
    i2c_release(DEV_I2C);
}
示例#19
0
void lis3mdl_disable(const lis3mdl_t *dev)
{
    uint8_t tmp = ( LIS3MDL_MASK_REG3_LOW_POWER_EN   /**< enable power-down mode */
                  | LIS3MDL_MASK_REG3_Z_LOW_POWER);  /**< Z-axis low-power mode */

    i2c_acquire(DEV_I2C);
    i2c_write_reg(DEV_I2C, DEV_ADDR, LIS3MDL_CTRL_REG3, tmp);
    i2c_release(DEV_I2C);
}
示例#20
0
void lis3mdl_disable(lis3mdl_t *dev)
{
    uint8_t tmp = ( LIS3MDL_MASK_REG3_LOW_POWER_EN   /**< enable power-down mode */
                  | LIS3MDL_MASK_REG3_Z_LOW_POWER);  /**< Z-axis low-power mode */

    i2c_acquire(dev->i2c);
    i2c_write_reg(dev->i2c, dev->addr, LIS3MDL_CTRL_REG3, tmp);
    i2c_release(dev->i2c);
}
示例#21
0
文件: isl29125.c 项目: ryankurte/RIOT
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, &reg_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;
}
示例#22
0
void lis3mdl_read_temp(const lis3mdl_t *dev, int16_t *value)
{
    i2c_acquire(DEV_I2C);
    i2c_read_regs(DEV_I2C, DEV_ADDR, LIS3MDL_TEMP_OUT_L_REG, (uint8_t*)value, 2);
    i2c_release(DEV_I2C);

    *value = _twos_complement(*value);

    *value = (TEMP_OFFSET + (*value / TEMP_DIVIDER));
}
示例#23
0
void lis3mdl_read_temp(lis3mdl_t *dev, int16_t *value)
{
    i2c_acquire(dev->i2c);
    i2c_read_regs(dev->i2c, dev->addr, LIS3MDL_TEMP_OUT_L_REG, (uint8_t*)value, 2);
    i2c_release(dev->i2c);

    *value = _twos_complement(*value);

    *value = (TEMP_OFFSET + (*value / TEMP_DIVIDER));
}
示例#24
0
文件: ccs811.c 项目: A-Paul/RIOT
static int _reg_write(const ccs811_t *dev, uint8_t reg, uint8_t *data, uint32_t len)
{
    DEBUG_DEV("write %"PRIu32" bytes to sensor registers starting at addr %02x",
              dev, len, reg);

    int res = CCS811_OK;

    if (ENABLE_DEBUG && data && len) {
        printf("[css811] %s dev=%d addr=%02x: write following bytes: ",
               __func__, dev->params.i2c_dev, dev->params.i2c_addr);
        for (unsigned i = 0; i < len; i++) {
            printf("%02x ", data[i]);
        }
        printf("\n");
    }

    if (i2c_acquire(dev->params.i2c_dev)) {
        DEBUG_DEV("could not aquire I2C bus", dev);
        return -CCS811_ERROR_I2C;
    }

#if MODULE_CCS811_FULL
    if (dev->params.wake_pin != GPIO_UNDEF) {
        /* wake the sensor with low active WAKE signal */
        gpio_clear(dev->params.wake_pin);
        /* t_WAKE is 50 us */
        xtimer_usleep(50);
    }
#endif

    if (!data || !len) {
        res = i2c_write_byte(dev->params.i2c_dev, dev->params.i2c_addr, reg, 0);
    }
    else {
        res = i2c_write_regs(dev->params.i2c_dev, dev->params.i2c_addr, reg, data, len, 0);
    }
    i2c_release(dev->params.i2c_dev);

#if MODULE_CCS811_FULL
    if (dev->params.wake_pin != GPIO_UNDEF) {
        /* let the sensor enter to sleep mode */
        gpio_set(dev->params.wake_pin);
        /* minimum t_DWAKE is 20 us */
        xtimer_usleep(20);
    }
#endif

    if (res != CCS811_OK) {
        DEBUG_DEV("could not write %"PRIu32" bytes to sensor registers "
                  "starting at addr %02x, reason %i", dev, len, reg, res);
        return -CCS811_ERROR_I2C;
    }

    return CCS811_OK;
}
示例#25
0
int tcs37727_init(tcs37727_t *dev, i2c_t i2c, uint8_t address, int atime_us)
{
    /* write device descriptor */
    dev->i2c = i2c;
    dev->addr = address;
    dev->initialized = false;

    i2c_acquire(dev->i2c);

    /* initialize the I2C bus */
    if (i2c_init_master(i2c, I2C_SPEED) < 0) {
        i2c_release(dev->i2c);
        return -1;
    }

    i2c_release(dev->i2c);

    if (tcs37727_test(dev)) {
        return -2;
    }

    i2c_acquire(dev->i2c);

    if (i2c_write_reg(dev->i2c, dev->addr, TCS37727_CONTROL,
                      TCS37727_CONTROL_AGAIN_4) != 1) {
        i2c_release(dev->i2c);
        return -3;
    }
    dev->again = 4;

    if (i2c_write_reg(dev->i2c, dev->addr, TCS37727_ATIME,
                      TCS37727_ATIME_TO_REG(atime_us)) != 1) {
        i2c_release(dev->i2c);
        return -3;
    }
    dev->atime_us = atime_us;

    dev->initialized = true;

    i2c_release(dev->i2c);
    return 0;
}
示例#26
0
文件: jc42.c 项目: A-Paul/RIOT
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;
}
示例#27
0
void tcs37727_set_rgbc_active(const tcs37727_t *dev)
{
    uint8_t reg;

    assert(dev);

    i2c_acquire(BUS);
    i2c_read_reg(BUS, ADR, TCS37727_ENABLE, &reg);
    reg |= (TCS37727_ENABLE_AEN | TCS37727_ENABLE_PON);
    i2c_write_reg(BUS, ADR, TCS37727_ENABLE, reg);
    i2c_release(BUS);
}
示例#28
0
/**
 * @brief       Utility method to perform and reconstruct a measurement.
 */
static uint32_t si70xx_measure(si70xx_t *dev, uint8_t command)
{
    uint8_t result[2];

    i2c_acquire(dev->i2c_dev);
    i2c_write_byte(dev->i2c_dev, dev->address, command);
    i2c_read_bytes(dev->i2c_dev, dev->address, (char *) result, 2);
    i2c_release(dev->i2c_dev);

    /* reconstruct raw result */
    return ((uint32_t)result[0] << 8) + (result[1] & 0xfc);
}
示例#29
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;
}
示例#30
0
文件: srf02.c 项目: LucaZulberti/RIOT
uint16_t srf02_read(srf02_t *dev)
{
    uint8_t res[2];

    /* read the results */
    i2c_acquire(dev->i2c);
    i2c_read_regs(dev->i2c, dev->addr, REG_HIGH, res, 2);
    i2c_release(dev->i2c);
    DEBUG("[srf02] result - high: 0x%02x low: 0x%02x\n", res[0], res[1]);

    /* compile result - TODO: fix for different host byte order other than LE */
    return ((((uint16_t)res[0]) << 8) | (res[1] & 0xff));
}