drivers: sensor: ccs811: cleanup of raw reading processing

Use CMSIS-standard bit mask and offset macros.  Rename the field to more
closely match the data sheet.  Use slightly less magic numbers for the
scale multipliers.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2018-11-22 16:26:59 -06:00 committed by Maureen Helm
commit 316c6e3aeb
2 changed files with 13 additions and 8 deletions

View file

@ -154,7 +154,7 @@ static int ccs811_sample_fetch(struct device *dev, enum sensor_channel chan)
drv_data->voc = sys_be16_to_cpu(buf[1]);
drv_data->status = status;
drv_data->error = error_from_status(status);
drv_data->resistance = sys_be16_to_cpu(buf[3]);
drv_data->raw = sys_be16_to_cpu(buf[3]);
return 0;
}
@ -180,8 +180,8 @@ static int ccs811_channel_get(struct device *dev,
/*
* Raw ADC readings are contained in least significant 10 bits
*/
uval = (drv_data->resistance & CCS811_VOLTAGE_MASK)
* CCS811_VOLTAGE_SCALE;
uval = ((drv_data->raw & CCS811_RAW_VOLTAGE_MSK)
>> CCS811_RAW_VOLTAGE_POS) * CCS811_RAW_VOLTAGE_SCALE;
val->val1 = uval / 1000000U;
val->val2 = uval % 1000000;
@ -191,7 +191,8 @@ static int ccs811_channel_get(struct device *dev,
* Current readings are contained in most
* significant 6 bits in microAmps
*/
uval = drv_data->resistance >> 10;
uval = ((drv_data->raw & CCS811_RAW_CURRENT_MSK)
>> CCS811_RAW_CURRENT_POS) * CCS811_RAW_CURRENT_SCALE;
val->val1 = uval / 1000000U;
val->val2 = uval % 1000000;

View file

@ -43,9 +43,13 @@
#define CCS811_MODE_DATARDY 0x08
#define CCS811_MODE_THRESH 0x04
#define CCS811_VOLTAGE_SCALE 1613
#define CCS811_VOLTAGE_MASK 0x3FF
#define CCS811_RAW_VOLTAGE_POS 0
#define CCS811_RAW_VOLTAGE_MSK (0x3FF << CCS811_RAW_VOLTAGE_POS)
#define CCS811_RAW_VOLTAGE_SCALE (1650000U / (CCS811_RAW_VOLTAGE_MSK \
>> CCS811_RAW_VOLTAGE_POS))
#define CCS811_RAW_CURRENT_POS 10
#define CCS811_RAW_CURRENT_MSK (0x3F << CCS811_RAW_CURRENT_POS)
#define CCS811_RAW_CURRENT_SCALE 1
#define CCS811_CO2_MIN_PPM 400
#define CCS811_CO2_MAX_PPM 32767
@ -82,7 +86,7 @@ struct ccs811_data {
#endif
u16_t co2;
u16_t voc;
u16_t resistance;
u16_t raw;
u8_t status;
u8_t error;
u8_t mode;