xmax = 0x7FFFFFFF / exp;
x = exp;
xp = 1;
factorial = 1;
y = 1 << power;
divider = 0;
for (i = 1; i <= rounds; i++) {
xp *= x;
factorial *= i;
y += (xp >> divider) / factorial;
divider += power; /* divide when next multiplication would overflow */ if (xp >= xmax) {
xp >>= power;
divider -= power;
}
}
staticint sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc)
{ int x;
u32 exp = 0;
/* we calculate as a multiple of 16384 (2^14) */
mutex_lock(&data->lock);
x = ((int)resistance_raw - data->res_calibbias) * 106;
mutex_unlock(&data->lock);
ret = i2c_master_recv(client, (u8 *)&tgres, sizeof(tgres)); if (ret < 0) return ret; if (ret != sizeof(tgres)) {
dev_warn(data->dev, "i2c_master_recv ret: %d sizeof: %zu\n", ret, sizeof(tgres)); return -EIO;
}
crc = crc8(sgp40_crc8_table, (u8 *)&tgres.res_ticks, 2, SGP40_CRC8_INIT); if (crc != tgres.res_crc) {
dev_err(data->dev, "CRC error while measure-raw\n"); return -EIO;
}
*resistance_raw = be16_to_cpu(tgres.res_ticks);
return0;
}
staticint sgp40_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{ struct sgp40_data *data = iio_priv(indio_dev); int ret, voc;
u16 resistance_raw;
switch (mask) { case IIO_CHAN_INFO_RAW: switch (chan->type) { case IIO_RESISTANCE:
ret = sgp40_measure_resistance_raw(data, &resistance_raw); if (ret) return ret;
*val = resistance_raw; return IIO_VAL_INT; case IIO_TEMP:
mutex_lock(&data->lock);
*val = data->temp;
mutex_unlock(&data->lock); return IIO_VAL_INT; case IIO_HUMIDITYRELATIVE:
mutex_lock(&data->lock);
*val = data->rht;
mutex_unlock(&data->lock); return IIO_VAL_INT; default: return -EINVAL;
} case IIO_CHAN_INFO_PROCESSED:
ret = sgp40_measure_resistance_raw(data, &resistance_raw); if (ret) return ret;
ret = sgp40_calc_voc(data, resistance_raw, &voc); if (ret) return ret;
/* set default values */
data->rht = 50000; /* 50 % */
data->temp = 25000; /* 25 °C */
data->res_calibbias = 30000; /* resistance raw value for voc index of 250 */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.