Exemplo n.º 1
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);
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
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));
}
Exemplo n.º 5
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));
}
Exemplo n.º 6
0
Arquivo: jc42.c Projeto: 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;
}
Exemplo n.º 7
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;
}
Exemplo n.º 8
0
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));
}
Exemplo n.º 9
0
int mpl3115a2_is_ready(const mpl3115a2_t *dev)
{
    uint8_t reg;

    i2c_acquire(BUS);
    if (i2c_read_regs(BUS, ADDR, MPL3115A2_STATUS, &reg, 1, 0) < 0) {
        i2c_release(BUS);
        return -MPL3115A2_ERROR_I2C;
    }
    i2c_release(BUS);

    return reg & MPL3115A2_STATUS_PTDR;
}
Exemplo n.º 10
0
void adxl345_read(const adxl345_t *dev, adxl345_data_t *data)
{
    int8_t result[6];

    assert(dev && data);

    i2c_acquire(BUS);
    i2c_read_regs(BUS, ADDR, ACCEL_ADXL345_DATA_X0, result, 6, 0);
    i2c_release(BUS);

    data->x = (((result[1] << 8)+result[0]) * dev->scale_factor);
    data->y = (((result[3] << 8)+result[2]) * dev->scale_factor);
    data->z = (((result[5] << 8)+result[4]) * dev->scale_factor);
}
Exemplo n.º 11
0
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;
}
Exemplo n.º 12
0
Arquivo: main.c Projeto: bumpy-b/RIOT
int cmd_read_reg(int argc, char **argv)
{
    int res;
    uint8_t addr, reg;
    int length;
    char data[BUFSIZE];

    if (i2c_dev < 0) {
        puts("Error: no I2C device initialized");
        return 1;
    }
    if (argc < 4) {
        puts("Error: not enough arguments given");
        printf("Usage:\n%s ADDR REG LENGTH]\n", argv[0]);
        return 1;
    }

    addr = (uint8_t)atoi(argv[1]);
    reg = (uint8_t)atoi(argv[2]);
    length = atoi(argv[3]);

    if (length < 1 || length > BUFSIZE) {
        puts("Error: invalid LENGTH parameter given");
        return 1;
    }
    else if (length == 1) {
        printf("i2c_read_reg(I2C_%i, 0x%02x, 0x%02x, char *res)\n", i2c_dev, addr, reg);
        res = i2c_read_reg(i2c_dev, addr, reg, data);
    }
    else {
        printf("i2c_read_regs(I2C_%i, 0x%02x, 0x%02x, char *res, %i)\n", i2c_dev, addr, reg, length);
        res = i2c_read_regs(i2c_dev, addr, reg, data, length);
    }

    if (res < 1) {
        puts("Error: no bytes were read");
        return 1;
    }
    else {
        printf("I2C_%i: successfully read %i bytes from reg 0x%02x:\n  [", i2c_dev, res, reg);
        for (int i = 0; i < res; i++) {
            printf("0x%02x, ", (unsigned int)data[i]);
        }
        puts("])");
        return 0;
    }

}
Exemplo n.º 13
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, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    return reg & MMA8652_STATUS_ZYXDR;
}
Exemplo n.º 14
0
int mpl3115a2_read_pressure(const mpl3115a2_t *dev, uint32_t *pres, uint8_t *status)
{
    uint8_t buf[4];

    i2c_acquire(BUS);
    if (i2c_read_regs(BUS, ADDR, MPL3115A2_STATUS, buf, 4, 0) < 0) {
        i2c_release(BUS);
        return -MPL3115A2_ERROR_I2C;
    }
    i2c_release(BUS);

    *status = buf[0];

    *pres = ((uint32_t)buf[1] << 16) | ((uint32_t)buf[2] << 8) | buf[3];
    *pres = *pres / 64;

    return MPL3115A2_OK;
}
Exemplo n.º 15
0
int mma8652_test(mma8652_t *dev)
{
    uint8_t reg;

    /* Acquire exclusive access to the bus. */
    i2c_acquire(dev->i2c);
    if (i2c_read_regs(dev->i2c, dev->addr, MMA8652_WHO_AM_I, &reg, 1) != 1) {
        /* Release the bus for other threads. */
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    if (reg != mma8x5x_device_id[dev->type]) {
        return -1;
    }

    return 0;
}
Exemplo n.º 16
0
int mpl3115a2_init(mpl3115a2_t *dev, const mpl3115a2_params_t *params)
{
    uint8_t reg;

    assert(dev);
    assert(params);

    /* write device descriptor */
    memcpy(dev, params, sizeof(mpl3115a2_params_t));

    i2c_acquire(BUS);
    /* test device */
    if (i2c_read_regs(BUS, ADDR, MPL3115A2_WHO_AM_I, &reg, 1, 0) < 0) {
        /* Release the bus for other threads. */
        i2c_release(BUS);
        LOG_ERROR("mpl3115a2_init: I2C error!\n");
        return -MPL3115A2_ERROR_I2C;
    }
    if (reg != MPL3115A2_ID) {
        LOG_ERROR("mpl3115a2_init: invalid WHO_AM_I value (0x%02x)!\n", (int)reg);
        return -MPL3115A2_ERROR_DEV;
    }
    /* set sample rate */
    reg = MPL3115A2_CTRL_REG1_OS(dev->params.ratio);
    if (i2c_write_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1, 0) < 0) {
        i2c_release(BUS);
        LOG_ERROR("mpl3115a2_init: failed to set sample rate!\n");
        return -MPL3115A2_ERROR_CNF;
    }
    /* configure device */
    reg = MPL3115A2_PT_DATA_CFG_TDEFE |
          MPL3115A2_PT_DATA_CFG_PDEFE |
          MPL3115A2_PT_DATA_CFG_DREM;
    if (i2c_write_regs(BUS, ADDR, MPL3115A2_PT_DATA_CFG, &reg, 1, 0) < 0) {
        i2c_release(BUS);
        LOG_ERROR("mpl3115a2_init: config failure!\n");
        return -MPL3115A2_ERROR_CNF;
    }

    i2c_release(BUS);

    return MPL3115A2_OK;
}
Exemplo n.º 17
0
int hdc1000_test(hdc1000_t *dev)
{
    char reg[2];
    uint16_t tmp;

    i2c_acquire(dev->i2c);
    if (i2c_read_regs(dev->i2c, dev->addr, HDC1000_MANUFACTURER_ID, reg, 2) != 2) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    tmp = ((uint16_t)reg[0] << 8) | reg[1];
    if (tmp != HDC1000_MID_VALUE) {
        return -1;
    }

    return 0;
}
Exemplo n.º 18
0
int l3g4200d_read(l3g4200d_t *dev, l3g4200d_data_t *data)
{
    uint8_t tmp[6];
    int16_t res;

    i2c_acquire(dev->i2c);
    /* get acceleration in x direction */
    i2c_read_regs(dev->i2c, dev->addr, L3G4200D_REG_OUT_X_L | L3G4200D_AUTOINC, tmp, 6);
    i2c_release(dev->i2c);

    /* parse and normalize data into result vector */
    res = (tmp[1] << 8) | tmp[0];
    data->acc_x = (int16_t)((dev->scale * res) / MAX_VAL);
    res = (tmp[3] << 8) | tmp[2];
    data->acc_y = (int16_t)((dev->scale * res) / MAX_VAL);
    res = (tmp[5] << 8) | tmp[4];
    data->acc_z = (int16_t)((dev->scale * res) / MAX_VAL);
    return 0;
}
Exemplo n.º 19
0
int mpl3115a2_set_standby(const mpl3115a2_t *dev)
{
    uint8_t reg;

    i2c_acquire(BUS);
    if (i2c_read_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1, 0) < 0) {
        i2c_release(BUS);
        return -MPL3115A2_ERROR_I2C;
    }

    reg &= ~MPL3115A2_CTRL_REG1_SBYB;
    if (i2c_write_regs(BUS, ADDR, MPL3115A2_CTRL_REG1, &reg, 1, 0) < 0) {
        i2c_release(BUS);
        return -MPL3115A2_ERROR_I2C;
    }
    i2c_release(BUS);

    return MPL3115A2_OK;
}
Exemplo n.º 20
0
int mpu9150_read_temperature(mpu9150_t *dev, int32_t *output)
{
    char data[2];
    int16_t temp;

    /* Acquire exclusive access */
    if (i2c_acquire(dev->i2c_dev)) {
        return -1;
    }
    /* Read raw temperature value */
    i2c_read_regs(dev->i2c_dev, dev->hw_addr, MPU9150_TEMP_START_REG, data, 2);
    /* Release the bus */
    i2c_release(dev->i2c_dev);

    temp = (data[0] << 8) | data[1];
    *output = ((((int32_t)temp) * 1000) / 340) + (35*1000);

    return 0;
}
Exemplo n.º 21
0
int mpu9150_read_accel(mpu9150_t *dev, mpu9150_results_t *output)
{
    char data[6];
    int16_t temp;
    float fsr;

    switch (dev->conf.accel_fsr) {
        case MPU9150_ACCEL_FSR_2G:
            fsr = 2000.0;
            break;
        case MPU9150_ACCEL_FSR_4G:
            fsr = 4000.0;
            break;
        case MPU9150_ACCEL_FSR_8G:
            fsr = 8000.0;
            break;
        case MPU9150_ACCEL_FSR_16G:
            fsr = 16000.0;
            break;
        default:
            return -2;
    }

    /* Acquire exclusive access */
    if (i2c_acquire(dev->i2c_dev)) {
        return -1;
    }
    /* Read raw data */
    i2c_read_regs(dev->i2c_dev, dev->hw_addr, MPU9150_ACCEL_START_REG, data, 6);
    /* Release the bus */
    i2c_release(dev->i2c_dev);

    /* Normalize data according to configured full scale range */
    temp = (data[0] << 8) | data[1];
    output->x_axis = (temp * fsr) / MAX_VALUE;
    temp = (data[2] << 8) | data[3];
    output->y_axis = (temp * fsr) / MAX_VALUE;
    temp = (data[4] << 8) | data[5];
    output->z_axis = (temp * fsr) / MAX_VALUE;

    return 0;
}
Exemplo n.º 22
0
int mma8652_set_standby(mma8652_t *dev)
{
    uint8_t reg;

    i2c_acquire(dev->i2c);
    if (i2c_read_regs(dev->i2c, dev->addr, MMA8652_CTRL_REG1, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }

    reg &= ~MMA8652_CTRL_REG1_ACTIVE;

    if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_CTRL_REG1, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    return 0;
}
Exemplo n.º 23
0
int mma8652_read(mma8652_t *dev, int16_t *x, int16_t *y, int16_t *z, uint8_t *status)
{
    uint8_t buf[7];

    if (dev->initialized == false) {
        return -1;
    }

    i2c_acquire(dev->i2c);
    if (i2c_read_regs(dev->i2c, dev->addr, MMA8652_STATUS, buf, 7) != 7) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    *status = buf[0];
    *x = (int32_t)((int16_t)(((int16_t)buf[1] << 8) | buf[2]) >> 4) * 1000 / dev->scale;
    *y = (int32_t)((int16_t)(((int16_t)buf[3] << 8) | buf[4]) >> 4) * 1000 / dev->scale;
    *z = (int32_t)((int16_t)(((int16_t)buf[5] << 8) | buf[6]) >> 4) * 1000 / dev->scale;

    return 0;
}
Exemplo n.º 24
0
int tcs37727_set_rgbc_active(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 | 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;
}
Exemplo n.º 25
0
int mma8652_set_active(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_CTRL_REG1, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }

    reg |= MMA8652_CTRL_REG1_ACTIVE;

    if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_CTRL_REG1, &reg, 1) != 1) {
        i2c_release(dev->i2c);
        return -1;
    }
    i2c_release(dev->i2c);

    return 0;
}
Exemplo n.º 26
0
int i2c_read_reg(i2c_t dev, uint8_t address, uint8_t reg, char *data)
{
    return i2c_read_regs(dev, address, reg, data, 1);
}
Exemplo n.º 27
0
Arquivo: i2c.c Projeto: A-Paul/RIOT
int i2c_read_reg(i2c_t dev, uint16_t addr, uint16_t reg,
                 void *data, uint8_t flags)
{
    return i2c_read_regs(dev, addr, reg, data, 1, flags);
}
Exemplo n.º 28
0
Arquivo: ccs811.c Projeto: A-Paul/RIOT
int ccs811_set_baseline(const ccs811_t *dev, uint16_t baseline)
{
    ASSERT_PARAM(dev != NULL);

    uint8_t data[2] = { baseline >> 8, baseline & 0xff };

    /* write baseline register */
    if (_reg_write(dev, CCS811_REG_THRESHOLDS, data, 5) != CCS811_OK) {
        DEBUG_DEV("could not set baseline value, "
                  "could not write register CCS811_REG_BASELINE", dev);
        return CCS811_ERROR_I2C;
    }

    return CCS811_OK;
}

#endif /* MODULE_CCS811_FULL */

/**
 * function for internal use only
 */

static int _reg_read(const ccs811_t *dev, uint8_t reg, uint8_t *data, uint32_t len)
{
    DEBUG_DEV("read %"PRIu32" bytes from sensor registers starting at addr %02x",
              dev, len, reg);

    int res = CCS811_OK;

    if (i2c_acquire(dev->params.i2c_dev) != CCS811_OK) {
        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

    res = i2c_read_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) {
        if (ENABLE_DEBUG) {
            printf("[ccs811] %s dev=%d addr=%02x: read following bytes: ",
                   __func__, dev->params.i2c_dev, dev->params.i2c_addr);
            for (unsigned i = 0; i < len; i++) {
                    printf("%02x ", data[i]);
            }
            printf("\n");
        }
    }
    else {
        DEBUG_DEV("could not read %"PRIu32" bytes from sensor registers "
                  "starting at addr %02x, reason %i", dev, len, reg, res);
        return -CCS811_ERROR_I2C;
    }

    return CCS811_OK;
}
Exemplo n.º 29
0
/**
 * Initialize compass
 * Caution: This internal function does not acquire exclusive access to the I2C bus.
 *          Acquisation and release is supposed to be handled by the calling function.
 */
static int compass_init(mpu9150_t *dev)
{
    char data[3];

    /* Enable Bypass Mode to speak to compass directly */
    conf_bypass(dev, 1);

    /* Check whether compass answers correctly */
    i2c_read_reg(dev->i2c_dev, dev->comp_addr, COMPASS_WHOAMI_REG, data);
    if (data[0] != MPU9150_COMP_WHOAMI_ANSWER) {
        DEBUG("[Error] Wrong answer from compass\n");
        return -1;
    }

    /* Configure Power Down mode */
    i2c_write_reg(dev->i2c_dev, dev->comp_addr, COMPASS_CNTL_REG, MPU9150_COMP_POWER_DOWN);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_COMP_MODE_SLEEP_US));
    /* Configure Fuse ROM access */
    i2c_write_reg(dev->i2c_dev, dev->comp_addr, COMPASS_CNTL_REG, MPU9150_COMP_FUSE_ROM);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_COMP_MODE_SLEEP_US));
    /* Read sensitivity adjustment values from Fuse ROM */
    i2c_read_regs(dev->i2c_dev, dev->comp_addr, COMPASS_ASAX_REG, data, 3);
    dev->conf.compass_x_adj = data[0];
    dev->conf.compass_y_adj = data[1];
    dev->conf.compass_z_adj = data[2];
    /* Configure Power Down mode again */
    i2c_write_reg(dev->i2c_dev, dev->comp_addr, COMPASS_CNTL_REG, MPU9150_COMP_POWER_DOWN);
    hwtimer_wait(HWTIMER_TICKS(MPU9150_COMP_MODE_SLEEP_US));

    /* Disable Bypass Mode to configure MPU as master to the compass */
    conf_bypass(dev, 0);

    /* Configure MPU9150 for single master mode */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_I2C_MST_REG, BIT_WAIT_FOR_ES);

    /* Set up slave line 0 */
    /* Slave line 0 reads the compass data */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr,
            MPU9150_SLAVE0_ADDR_REG, (BIT_SLAVE_RW | dev->comp_addr));
    /* Slave line 0 read starts at compass data register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE0_REG_REG, COMPASS_DATA_START_REG);
    /* Enable slave line 0 and configure read length to 6 consecutive registers */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE0_CTRL_REG, (BIT_SLAVE_EN | 0x06));

    /* Set up slave line 1 */
    /* Slave line 1 writes to the compass */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE1_ADDR_REG, dev->comp_addr);
    /* Slave line 1 write starts at compass control register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE1_REG_REG, COMPASS_CNTL_REG);
    /* Enable slave line 1 and configure write length to 1 register */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_SLAVE1_CTRL_REG, (BIT_SLAVE_EN | 0x01));
    /* Configure data which is written by slave line 1 to compass control */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr,
            MPU9150_SLAVE1_DATA_OUT_REG, MPU9150_COMP_SINGLE_MEASURE);

    /* Slave line 0 and 1 operate at each sample */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr,
            MPU9150_I2C_DELAY_CTRL_REG, (BIT_SLV0_DELAY_EN | BIT_SLV1_DELAY_EN));
    /* Set I2C bus to VDD */
    i2c_write_reg(dev->i2c_dev, dev->hw_addr, MPU9150_YG_OFFS_TC_REG, BIT_I2C_MST_VDDIO);

    return 0;
}
Exemplo n.º 30
-1
static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
{
    /* Enable the device */
    _enable(dev);

    /* Wait integration time in ms for ADC to complete */
    switch (dev->integration) {
        case TSL2561_INTEGRATIONTIME_13MS:
            xtimer_usleep(13700);
            break;

        case TSL2561_INTEGRATIONTIME_101MS:
            xtimer_usleep(101000);
            break;

        default: /* TSL2561_INTEGRATIONTIME_402MS */
            xtimer_usleep(402000);
            break;
    }

    char buffer[2] = { 0 };
    /* Read full spectrum channel */
    i2c_read_regs(dev->i2c_dev, dev->addr,
                  TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN0,
                  buffer, 2);
    *full = (buffer[1] << 8) | buffer[0];

    memset(buffer, 0, sizeof(buffer));

    /* Read infrared spectrum channel */
    i2c_read_regs(dev->i2c_dev, dev->addr,
                  TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN1,
                  buffer, 2);
    *ir = (buffer[1] << 8) | buffer[0];

    /* Turn the device off to save power */
    _disable(dev);
}