Skip to content

Commit

Permalink
fxos8700: Add support for temperature
Browse files Browse the repository at this point in the history
The fxos8700 is primarily an accelerometer/magnetometer, but it does
also include a temperature sensor. Note that the temperature sensor is
uncalibrated and can only be used when the magnetometer is enabled
(magnetometer-only mode or hybrid mode).

Change-Id: I74c4ae68c30e0f9836caa70baed44ad8956b17ea
Signed-off-by: Maureen Helm <[email protected]>
  • Loading branch information
MaureenHelm committed Mar 23, 2017
1 parent 1a344ed commit 4905e40
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions drivers/sensor/fxos8700/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ config FXOS8700_MODE_HYBRID

endchoice

config FXOS8700_TEMP
bool "Enable temperature"
depends on FXOS8700 && (FXOS8700_MODE_MAGN || FXOS8700_MODE_HYBRID)
default n
help
Enable the temperature sensor. Note that the temperature sensor is
uncalibrated and its output for a given temperature may vary from one
device to the next.

choice
prompt "Range"
depends on FXOS8700
Expand Down
30 changes: 30 additions & 0 deletions drivers/sensor/fxos8700/fxos8700.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ static int fxos8700_sample_fetch(struct device *dev, enum sensor_channel chan)
*raw++ = (buffer[i] << 8) | (buffer[i+1]);
}

#ifdef CONFIG_FXOS8700_TEMP
if (i2c_reg_read_byte(data->i2c, config->i2c_address, FXOS8700_REG_TEMP,
&data->temp)) {
SYS_LOG_ERR("Could not fetch temperature");
ret = -EIO;
goto exit;
}
#endif

exit:
k_sem_give(&data->sem);

Expand Down Expand Up @@ -100,6 +109,21 @@ static void fxos8700_magn_convert(struct sensor_value *val, int16_t raw)
val->val2 = micro_g % 1000000;
}

#ifdef CONFIG_FXOS8700_TEMP
static void fxos8700_temp_convert(struct sensor_value *val, int8_t raw)
{
int32_t micro_c;

/* Convert units to micro Celsius. Raw temperature data always has a
* resolution of 0.96 deg C/LSB.
*/
micro_c = raw * 960 * 1000;

val->val1 = micro_c / 1000000;
val->val2 = micro_c % 1000000;
}
#endif

static int fxos8700_channel_get(struct device *dev, enum sensor_channel chan,
struct sensor_value *val)
{
Expand Down Expand Up @@ -191,6 +215,12 @@ static int fxos8700_channel_get(struct device *dev, enum sensor_channel chan,
if (num_channels > 0) {
ret = 0;
}
#ifdef CONFIG_FXOS8700_TEMP
if (chan == SENSOR_CHAN_TEMP) {
fxos8700_temp_convert(val, data->temp);
ret = 0;
}
#endif
}

if (ret != 0) {
Expand Down
4 changes: 4 additions & 0 deletions drivers/sensor/fxos8700/fxos8700.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define FXOS8700_REG_CTRLREG4 0x2d
#define FXOS8700_REG_CTRLREG5 0x2e
#define FXOS8700_REG_M_OUTXMSB 0x33
#define FXOS8700_REG_TEMP 0x51
#define FXOS8700_REG_M_CTRLREG1 0x5b
#define FXOS8700_REG_M_CTRLREG2 0x5c

Expand Down Expand Up @@ -131,6 +132,9 @@ struct fxos8700_data {
struct device *dev;
#endif
int16_t raw[FXOS8700_MAX_NUM_CHANNELS];
#ifdef CONFIG_FXOS8700_TEMP
int8_t temp;
#endif
};

int fxos8700_get_power(struct device *dev, enum fxos8700_power *power);
Expand Down

0 comments on commit 4905e40

Please sign in to comment.