static int apds9960_ambient_setup(struct device *dev, int gain) { struct apds9960_data *data = dev->driver_data; u16_t th; /* ADC value */ if (i2c_reg_write_byte(data->i2c, APDS9960_I2C_ADDRESS, APDS9960_ATIME_REG, APDS9960_DEFAULT_ATIME)) { LOG_ERR("Default integration time not set for ADC"); return -EIO; } /* ALS Gain */ if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS, APDS9960_CONTROL_REG, APDS9960_CONTROL_AGAIN, (gain & APDS9960_AGAIN_64X))) { LOG_ERR("Ambient Gain is not set"); return -EIO; } th = sys_cpu_to_le16(APDS9960_DEFAULT_AILT); if (i2c_burst_write(data->i2c, APDS9960_I2C_ADDRESS, APDS9960_INT_AILTL_REG, (u8_t *)&th, sizeof(th))) { LOG_ERR("ALS low threshold not set"); return -EIO; } th = sys_cpu_to_le16(APDS9960_DEFAULT_AIHT); if (i2c_burst_write(data->i2c, APDS9960_I2C_ADDRESS, APDS9960_INT_AIHTL_REG, (u8_t *)&th, sizeof(th))) { LOG_ERR("ALS low threshold not set"); return -EIO; } /* Enable ALS */ if (i2c_reg_update_byte(data->i2c, APDS9960_I2C_ADDRESS, APDS9960_ENABLE_REG, APDS9960_ENABLE_AEN, APDS9960_ENABLE_AEN)) { LOG_ERR("ALS is not enabled"); return -EIO; } return 0; }
static int bmg160_write(struct device *dev, u8_t reg_addr, u8_t *data, u8_t len) { const struct bmg160_device_config *dev_cfg = dev->config->config_info; struct bmg160_device_data *bmg160 = dev->driver_data; int ret = 0; bmg160_bus_config(dev); k_sem_take(&bmg160->sem, K_FOREVER); if (i2c_burst_write(bmg160->i2c, dev_cfg->i2c_addr, reg_addr, data, len) < 0) { ret = -EIO; } k_sem_give(&bmg160->sem); return ret; }
static int bmg160_write(struct device *dev, uint8_t reg_addr, uint8_t *data, uint8_t len) { struct bmg160_device_config *dev_cfg = dev->config->config_info; struct bmg160_device_data *bmg160 = dev->driver_data; int ret = 0; bmg160_bus_config(dev); nano_sem_take(&bmg160->sem, TICKS_UNLIMITED); if (i2c_burst_write(bmg160->i2c, dev_cfg->i2c_addr, reg_addr, data, len) < 0) { ret = -EIO; } nano_sem_give(&bmg160->sem); return ret; }
int hmc5883l_init(struct device *dev) { struct hmc5883l_data *drv_data = dev->driver_data; u8_t chip_cfg[3], id[3], idx; drv_data->i2c = device_get_binding(CONFIG_HMC5883L_I2C_MASTER_DEV_NAME); if (drv_data->i2c == NULL) { LOG_ERR("Failed to get pointer to %s device.", CONFIG_HMC5883L_I2C_MASTER_DEV_NAME); return -EINVAL; } /* check chip ID */ if (i2c_burst_read(drv_data->i2c, HMC5883L_I2C_ADDR, HMC5883L_REG_CHIP_ID, id, 3) < 0) { LOG_ERR("Failed to read chip ID."); return -EIO; } if (id[0] != HMC5883L_CHIP_ID_A || id[1] != HMC5883L_CHIP_ID_B || id[2] != HMC5883L_CHIP_ID_C) { LOG_ERR("Invalid chip ID."); return -EINVAL; } /* check if CONFIG_HMC5883L_FS is valid */ for (idx = 0U; idx < ARRAY_SIZE(hmc5883l_fs_strings); idx++) { if (!strcmp(hmc5883l_fs_strings[idx], CONFIG_HMC5883L_FS)) { break; } } if (idx == ARRAY_SIZE(hmc5883l_fs_strings)) { LOG_ERR("Invalid full-scale range value."); return -EINVAL; } drv_data->gain_idx = idx; /* check if CONFIG_HMC5883L_ODR is valid */ for (idx = 0U; idx < ARRAY_SIZE(hmc5883l_odr_strings); idx++) { if (!strcmp(hmc5883l_odr_strings[idx], CONFIG_HMC5883L_ODR)) { break; } } if (idx == ARRAY_SIZE(hmc5883l_odr_strings)) { LOG_ERR("Invalid ODR value."); return -EINVAL; } /* configure device */ chip_cfg[0] = idx << HMC5883L_ODR_SHIFT; chip_cfg[1] = drv_data->gain_idx << HMC5883L_GAIN_SHIFT; chip_cfg[2] = HMC5883L_MODE_CONTINUOUS; if (i2c_burst_write(drv_data->i2c, HMC5883L_I2C_ADDR, HMC5883L_REG_CONFIG_A, chip_cfg, 3) < 0) { LOG_ERR("Failed to configure chip."); return -EIO; } #ifdef CONFIG_HMC5883L_TRIGGER if (hmc5883l_init_interrupt(dev) < 0) { LOG_ERR("Failed to initialize interrupts."); return -EIO; } #endif return 0; }