Skip to content
Permalink
Browse files

drivers: adxl362: rework temperature conversion

This commit consolidates the temperature conversion into a single
location and subtracts the bias from the measurement using values from
the datasheet. Also magic numbers have been replaced with more
descriptive macros.

Signed-off-by: Brett Witherspoon <spoonb@cdspooner.com>
  • Loading branch information...
bwitherspoon authored and MaureenHelm committed May 19, 2019
1 parent 935efeb commit 1becd75604944d3f03a54368064bf5dabfb70429
Showing with 18 additions and 22 deletions.
  1. +13 −21 drivers/sensor/adxl362/adxl362.c
  2. +5 −1 drivers/sensor/adxl362/adxl362.h
@@ -368,24 +368,6 @@ static int adxl362_attr_set(struct device *dev, enum sensor_channel chan,
return 0;
}


static int adxl362_read_temperature(struct device *dev, s32_t *temp_celsius)
{
u8_t raw_temp_data[2];
int ret;

/* Reads the temperature of the device. */
ret = adxl362_get_reg(dev, raw_temp_data, ADXL362_REG_TEMP_L, 2);
if (ret) {
return ret;
}

*temp_celsius = (s32_t)(raw_temp_data[1] << 8) + raw_temp_data[0];
*temp_celsius *= 65;

return ret;
}

static int adxl362_fifo_setup(struct device *dev, u8_t mode,
u16_t water_mark_lvl, u8_t en_temp_read)
{
@@ -573,11 +555,13 @@ static int adxl362_sample_fetch(struct device *dev, enum sensor_channel chan)

data->acc_z = (buf[1] << 8) + buf[0];

ret = adxl362_read_temperature(dev, &data->temp);
ret = adxl362_get_reg(dev, buf, ADXL362_REG_TEMP_L, 2);
if (ret) {
return ret;
}

data->temp = (buf[1] << 8) + buf[0];

return 0;
}

@@ -608,6 +592,15 @@ static void adxl362_accel_convert(struct sensor_value *val, int accel,
val->val2 = micro_ms2 % 1000000;
}

static void adxl362_temp_convert(struct sensor_value *val, int temp)
{
/* See sensitivity and bias specifications in table 1 of datasheet */
int milli_c = (temp - ADXL362_TEMP_BIAS_LSB) * ADXL362_TEMP_MC_PER_LSB;

val->val1 = milli_c / 1000;
val->val2 = (milli_c % 1000) * 1000;
}

static int adxl362_channel_get(struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
@@ -625,8 +618,7 @@ static int adxl362_channel_get(struct device *dev,
adxl362_accel_convert(val, data->acc_z, data->selected_range);
break;
case SENSOR_CHAN_DIE_TEMP: /* Temperature in degrees Celsius. */
val->val1 = data->temp / 1000;
val->val2 = (data->temp % 1000) * 1000;
adxl362_temp_convert(val, data->temp);
break;
default:
return -ENOTSUP;
@@ -166,6 +166,10 @@
#define ADXL362_ACCEL_4G_LSB_PER_G 500
#define ADXL362_ACCEL_8G_LSB_PER_G 235

/* ADXL362 temperature sensor specifications */
#define ADXL362_TEMP_MC_PER_LSB 65
#define ADXL362_TEMP_BIAS_LSB 350

struct adxl362_config {
char *spi_name;
u32_t spi_max_frequency;
@@ -191,7 +195,7 @@ struct adxl362_data {
s16_t acc_x;
s16_t acc_y;
s16_t acc_z;
s32_t temp;
s16_t temp;
u8_t selected_range;

#if defined(CONFIG_ADXL362_TRIGGER)

0 comments on commit 1becd75

Please sign in to comment.
You can’t perform that action at this time.