static int drv_als_liteon_ltr553_set_default_config(i2c_dev_t* drv) { int ret = 0; uint8_t value = 0; value = LTR553_SET_BITSLICE(value, LTR553_ALS_MEAS_RATE_REG_INTEG_TIME, AIT_TIME_100); value = LTR553_SET_BITSLICE(value, LTR553_ALS_MEAS_RATE_REG_MEAS_RATE, AMR_RATE_100); ret = sensor_i2c_write(drv, LTR553_ALS_MEAS_RATE, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } ret = sensor_i2c_read(drv, LTR553_ALS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } value = LTR553_SET_BITSLICE(value, LTR553_ALS_CONTR_REG_ALS_GAIN, AG_GAIN_1X); ret = sensor_i2c_write(drv, LTR553_ALS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } return 0; }
static int drv_ps_liteon_ltr553_set_default_config(i2c_dev_t* drv) { int ret = 0; uint8_t value = 0; value = LTR553_SET_BITSLICE(value, LTR553_PS_MEAS_RATE_REG_MEAS_RATE, PMR_RATE_100); ret = sensor_i2c_write(drv, LTR553_PS_MEAS_RATE, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } value = 0; value = LTR553_SET_BITSLICE(value, LTR553_PS_LED_REG_PLUSE_FREQ, LPMF_PERIOD_60K); value = LTR553_SET_BITSLICE(value, LTR553_PS_LED_REG_CURRENT_DUTY, LCD_PER_100); value = LTR553_SET_BITSLICE(value, LTR553_PS_LED_REG_CURRENT, LC_LEVEL_100); ret = sensor_i2c_write(drv, LTR553_PS_MEAS_RATE, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } ret = sensor_i2c_read(drv, LTR553_PS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } value = LTR553_SET_BITSLICE(value, LTR553_PS_CONTR_REG_PS_GAIN, PG_GAIN_X16); ret = sensor_i2c_write(drv, LTR553_PS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } return 0; }
/** * @brief used to write multiple bytes of data from registers. * This should be sent by I2C. * * @param slaveAddr I2C slave address of device. * @param registerAddr Register address to write. * @param length Length of burst of data. * @param data Pointer to block of data. * * @return ML_SUCCESS if successful, a non-zero error code otherwise. */ tMLError MLSLSerialWrite(void *sl_handle, unsigned char slaveAddr, unsigned short length, unsigned char const *data) { tMLError result; const unsigned short dataLength = length - 1; const unsigned char startRegAddr = data[0]; unsigned char i2cWrite[SERIAL_MAX_TRANSFER_SIZE + 1]; unsigned short bytesWritten = 0; while (bytesWritten < dataLength) { unsigned short thisLen = min(SERIAL_MAX_TRANSFER_SIZE, dataLength - bytesWritten); if (bytesWritten == 0) { result = sensor_i2c_write((struct i2c_adapter *) sl_handle, slaveAddr, 1 + thisLen, data); } else { /* manually increment register addr between chunks */ i2cWrite[0] = startRegAddr + bytesWritten; memcpy(&i2cWrite[1], &data[1 + bytesWritten], thisLen); result = sensor_i2c_write((struct i2c_adapter *) sl_handle, slaveAddr, 1 + thisLen, i2cWrite); } if (ML_SUCCESS != result) return result; bytesWritten += thisLen; } return ML_SUCCESS; }
static int drv_ps_liteon_ltr553_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode) { int ret = 0; uint8_t dev_mode = 0; uint8_t value = 0; ret = sensor_i2c_read(drv, LTR553_PS_CONTR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if (unlikely(ret)) { return ret; } switch(mode){ case DEV_POWER_OFF: case DEV_SLEEP: dev_mode = LTR553_SET_BITSLICE(value, LTR553_PS_CONTR_REG_PS_MODE, PM_MODE_STANDBY); break; case DEV_POWER_ON: dev_mode = LTR553_SET_BITSLICE(value, LTR553_PS_CONTR_REG_PS_MODE, PM_MODE_ACTIVE); break; default: return -1; } ret = sensor_i2c_write(drv, LTR553_PS_CONTR, &dev_mode, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)) { return ret; } return 0; }
/** * @brief used to write multiple bytes of data to the fifo. * This should be sent by I2C. * * @param slaveAddr I2C slave address of device. * @param length Length of burst of data. * @param data Pointer to block of data. * * @return Zero if successful; an error code otherwise */ tMLError MLSLSerialWriteFifo(void *sl_handle, unsigned char slaveAddr, unsigned short length, unsigned char const *data) { tMLError result; unsigned char i2cWrite[SERIAL_MAX_TRANSFER_SIZE + 1]; unsigned short bytesWritten = 0; if (length > FIFO_HW_SIZE) { printk(KERN_ERR "maximum fifo write length is %d\n", FIFO_HW_SIZE); return ML_ERROR_INVALID_PARAMETER; } while (bytesWritten < length) { unsigned short thisLen = min(SERIAL_MAX_TRANSFER_SIZE, length - bytesWritten); i2cWrite[0] = MPUREG_FIFO_R_W; memcpy(&i2cWrite[1], &data[bytesWritten], thisLen); result = sensor_i2c_write((struct i2c_adapter *) sl_handle, slaveAddr, thisLen + 1, i2cWrite); if (ML_SUCCESS != result) return result; bytesWritten += thisLen; } return ML_SUCCESS; }
static int drv_acc_bosch_bma253_soft_reset(i2c_dev_t* drv) { int ret = 0; ret = sensor_i2c_write(drv, BMA253_RST_ADDR, BMA253_ENABLE_SOFT_RESET_VALUE, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return -1; } return 0; }
int sensor_i2c_write_register(struct i2c_adapter *i2c_adap, unsigned char address, unsigned char reg, unsigned char value) { unsigned char data[2]; data[0] = reg; data[1] = value; return sensor_i2c_write(i2c_adap, address, 2, data); }
int sensor_tx_data(struct i2c_client *client, char *txData, int length) { //struct sensor_private_data* sensor = //(struct sensor_private_data *)i2c_get_clientdata(client); int i = 0; int ret = 0; DBG("addr=0x%x,len=%d,txdata:",txData[0],length); for(i=1; i<length; i++) DBG("0x%x,",txData[i]); DBG("\n"); ret = sensor_i2c_write(client->adapter, client->addr, length, txData); return ret; }
static int drv_acc_bosch_bma253_set_odr(i2c_dev_t* drv, uint32_t odr) { int ret = 0; uint8_t value = 0x00; uint32_t bw = odr/2; ret = sensor_i2c_read(drv, BMA253_BW_SELECT_ADDR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } if(bw >= BMA253_BW_1000){ value &= BMA253_BW_BIT_MASK; value |= BMA253_BW_1000HZ; } else if(bw >= BMA253_BW_500){ value &= BMA253_BW_BIT_MASK; value |= BMA253_BW_500HZ; } else if(bw >= BMA253_BW_250){ value &= BMA253_BW_BIT_MASK; value |= BMA253_BW_250HZ; } else if(bw >= BMA253_BW_125){ value &= BMA253_BW_BIT_MASK; value |= BMA253_BW_125HZ; } else if(bw >= BMA253_BW_62_5){ value &= BMA253_BW_BIT_MASK; value |= BMA253_BW_62_50HZ; } else if(bw >= BMA253_BW_31_25){ value &= BMA253_BW_BIT_MASK; value |= BMA253_BW_31_25HZ; } else{ value &= BMA253_BW_BIT_MASK; value |= BMA253_BW_15_63HZ; } ret = sensor_i2c_write(drv, BMA253_BW_SELECT_ADDR, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } return 0; }
static int drv_acc_bosch_bma253_set_range(i2c_dev_t* drv, uint32_t range) { int ret = 0; uint8_t value = 0x00; ret = sensor_i2c_read(drv, BMA253_RANGE_SELECT_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } switch (range){ case ACC_RANGE_2G:{ value = BMA253_SET_BITSLICE(value,BMA253_RANGE_SELECT,BMA253_RANGE_2G); }break; case ACC_RANGE_4G:{ value = BMA253_SET_BITSLICE(value,BMA253_RANGE_SELECT,BMA253_RANGE_4G); }break; case ACC_RANGE_8G:{ value = BMA253_SET_BITSLICE(value,BMA253_RANGE_SELECT,BMA253_RANGE_8G); }break; case ACC_RANGE_16G:{ value = BMA253_SET_BITSLICE(value,BMA253_RANGE_SELECT,BMA253_RANGE_16G); }break; default:break; } /* Write the range register 0x0F*/ ret = sensor_i2c_write(drv, BMA253_RANGE_SELECT_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } if((range >= ACC_RANGE_2G)&&(range <= ACC_RANGE_16G)){ current_factor = bma253_factor[range]; } return 0; }
static int drv_acc_bosch_bma253_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode) { uint8_t value,value1 = 0x00; int ret = 0; uint8_t buf[4]; ret = sensor_i2c_read(drv, BMA253_MODE_CTRL_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } switch(mode){ case DEV_POWER_ON:{ if((value & BMA253_MODE_CTRL_MSK) == BME253_NORMAL_MODE){ return 0; } value |= BME253_NORMAL_MODE; ret = sensor_i2c_write(drv, BMA253_MODE_CTRL_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } }break; case DEV_POWER_OFF:{ if((value & BMA253_MODE_CTRL_MSK) == BME253_DEEP_SUSPEND_MODE){ return 0; } value |= BME253_DEEP_SUSPEND_MODE; ret = sensor_i2c_write(drv, BMA253_MODE_CTRL_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } }break; case DEV_SLEEP:{ if((value & BMA253_MODE_CTRL_MSK) == BME253_SLEEP_MODE){ return 0; } value |= BME253_SLEEP_MODE; ret = sensor_i2c_write(drv, BMA253_MODE_CTRL_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } }break; case DEV_SUSPEND:{ if((value & BMA253_MODE_CTRL_MSK) == BME253_SUSPEND_MODE){ return 0; } value |= BME253_SUSPEND_MODE; ret = sensor_i2c_write(drv, BMA253_MODE_CTRL_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES); if(unlikely(ret)){ return ret; } }break; default:break; } return 0; }