drivers: sensor: lsm303dlhc_mag: Fix gain factor
The magnetometer on the LSM303DLHC has a different gain conversion factor for LSB to Gauss for the Z axis than it does for X, Y. This commit takes into account the different conversion factors, and adds the correct coefficients for each gain setting and axis. Signed-off-by: Kevin Townsend <kevin.townsend@linaro.org>
This commit is contained in:
parent
f9e2fc3fc4
commit
21321fba1c
2 changed files with 30 additions and 16 deletions
|
@ -52,11 +52,18 @@ static int lsm303dlhc_sample_fetch(const struct device *dev,
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void lsm303dlhc_convert(struct sensor_value *val,
|
||||
static void lsm303dlhc_convert_xy(struct sensor_value *val,
|
||||
int64_t raw_val)
|
||||
{
|
||||
val->val1 = raw_val / LSM303DLHC_MAGN_LSB_GAUSS;
|
||||
val->val2 = (1000000 * raw_val / LSM303DLHC_MAGN_LSB_GAUSS) % 1000000;
|
||||
val->val1 = raw_val / LSM303DLHC_MAGN_LSB_GAUSS_XY;
|
||||
val->val2 = (1000000 * raw_val / LSM303DLHC_MAGN_LSB_GAUSS_XY) % 1000000;
|
||||
}
|
||||
|
||||
static void lsm303dlhc_convert_z(struct sensor_value *val,
|
||||
int64_t raw_val)
|
||||
{
|
||||
val->val1 = raw_val / LSM303DLHC_MAGN_LSB_GAUSS_Z;
|
||||
val->val2 = (1000000 * raw_val / LSM303DLHC_MAGN_LSB_GAUSS_Z) % 1000000;
|
||||
}
|
||||
|
||||
static int lsm303dlhc_channel_get(const struct device *dev,
|
||||
|
@ -67,18 +74,18 @@ static int lsm303dlhc_channel_get(const struct device *dev,
|
|||
|
||||
switch (chan) {
|
||||
case SENSOR_CHAN_MAGN_X:
|
||||
lsm303dlhc_convert(val, drv_data->magn_x);
|
||||
lsm303dlhc_convert_xy(val, drv_data->magn_x);
|
||||
break;
|
||||
case SENSOR_CHAN_MAGN_Y:
|
||||
lsm303dlhc_convert(val, drv_data->magn_y);
|
||||
lsm303dlhc_convert_xy(val, drv_data->magn_y);
|
||||
break;
|
||||
case SENSOR_CHAN_MAGN_Z:
|
||||
lsm303dlhc_convert(val, drv_data->magn_z);
|
||||
lsm303dlhc_convert_z(val, drv_data->magn_z);
|
||||
break;
|
||||
case SENSOR_CHAN_MAGN_XYZ:
|
||||
lsm303dlhc_convert(val, drv_data->magn_x);
|
||||
lsm303dlhc_convert(val + 1, drv_data->magn_y);
|
||||
lsm303dlhc_convert(val + 2, drv_data->magn_z);
|
||||
lsm303dlhc_convert_xy(val, drv_data->magn_x);
|
||||
lsm303dlhc_convert_xy(val + 1, drv_data->magn_y);
|
||||
lsm303dlhc_convert_z(val + 2, drv_data->magn_z);
|
||||
break;
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
|
|
|
@ -37,19 +37,26 @@
|
|||
LSM303DLHC_MAGN_ODR_SHIFT)
|
||||
|
||||
#if (CONFIG_LSM303DLHC_MAGN_RANGE == 1)
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS 1100
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_XY 1100
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_Z 980
|
||||
#elif (CONFIG_LSM303DLHC_MAGN_RANGE == 2)
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS 760
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_XY 855
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_Z 760
|
||||
#elif (CONFIG_LSM303DLHC_MAGN_RANGE == 3)
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS 600
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_XY 670
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_Z 600
|
||||
#elif (CONFIG_LSM303DLHC_MAGN_RANGE == 4)
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS 400
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_XY 450
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_Z 400
|
||||
#elif (CONFIG_LSM303DLHC_MAGN_RANGE == 5)
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS 355
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_XY 400
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_Z 355
|
||||
#elif (CONFIG_LSM303DLHC_MAGN_RANGE == 6)
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS 295
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_XY 330
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_Z 295
|
||||
#elif (CONFIG_LSM303DLHC_MAGN_RANGE == 7)
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS 205
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_XY 230
|
||||
#define LSM303DLHC_MAGN_LSB_GAUSS_Z 205
|
||||
#endif
|
||||
|
||||
#define LSM303DLHC_MAGN_FS_SHIFT 5
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue