Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 additions & 10 deletions drivers/sensor/st/lis2dux12/lis2dux12.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ LOG_MODULE_REGISTER(LIS2DUX12, CONFIG_SENSOR_LOG_LEVEL);

static int lis2dux12_set_odr(const struct device *dev, uint8_t odr)
{
struct lis2dux12_data *data = dev->data;
const struct lis2dux12_config *cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
lis2dux12_md_t mode = {.odr = odr};

data->odr = odr;
Copy link
Member

@ubieda ubieda Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling lis2dux12_set_range() includes odr when calling lis2dux12_mode_set(), but lis2dux12_set_odr() does not consider fs (thus setting it to zero).

Suggestion:

lis2dux12_md_t mode = {.odr = odr .fs = data->range};

EDIT: Not blocking this PR as it's not the issue being fixed. Can be followed on a separate one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ubieda Good advise! I'm doing it as a separate PR. Thanks for catching it.

return lis2dux12_mode_set(ctx, &mode);
}

Expand All @@ -40,7 +42,7 @@ static int lis2dux12_set_range(const struct device *dev, uint8_t range)
struct lis2dux12_data *data = dev->data;
const struct lis2dux12_config *cfg = dev->config;
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
lis2dux12_md_t val = { .odr = cfg->odr, .fs = range };
lis2dux12_md_t val = { .odr = data->odr, .fs = range };

err = lis2dux12_mode_set(ctx, &val);

Expand All @@ -66,6 +68,7 @@ static int lis2dux12_set_range(const struct device *dev, uint8_t range)
break;
}

data->range = range;
return 0;
}

Expand Down Expand Up @@ -107,14 +110,47 @@ static int lis2dux12_freq_to_odr_val(const struct device *dev, uint16_t freq)
return -EINVAL;
}

static int lis2dux12_set_fs(const struct device *dev, int16_t fs)
{
int ret;
uint8_t range;

switch (fs) {
case 2:
range = LIS2DUX12_DT_FS_2G;
break;
case 4:
range = LIS2DUX12_DT_FS_4G;
break;
case 8:
range = LIS2DUX12_DT_FS_8G;
break;
case 16:
range = LIS2DUX12_DT_FS_16G;
break;
default:
LOG_ERR("fs [%d] not supported.", fs);
return -EINVAL;
}

ret = lis2dux12_set_range(dev, range);
if (ret < 0) {
LOG_ERR("%s: range init error %d", dev->name, range);
return ret;
}

LOG_DBG("%s: set fs to %d g", dev->name, fs);
return ret;
}

static int lis2dux12_accel_config(const struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr, const struct sensor_value *val)
{
int odr_val;

switch (attr) {
case SENSOR_ATTR_FULL_SCALE:
return lis2dux12_set_range(dev, sensor_ms2_to_g(val));
return lis2dux12_set_fs(dev, sensor_ms2_to_g(val));
case SENSOR_ATTR_SAMPLING_FREQUENCY:
odr_val = lis2dux12_freq_to_odr_val(dev, val->val1);
if (odr_val < 0) {
Expand Down Expand Up @@ -154,7 +190,7 @@ static int lis2dux12_sample_fetch_accel(const struct device *dev)
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;

/* fetch raw data sample */
lis2dux12_md_t mode = {.fs = cfg->range};
lis2dux12_md_t mode = {.fs = data->range};
lis2dux12_xl_data_t xzy_data = {0};

if (lis2dux12_xl_data_get(ctx, &mode, &xzy_data) < 0) {
Expand Down Expand Up @@ -185,7 +221,7 @@ static int lis2dux12_sample_fetch_temp(const struct device *dev)
return -EIO;
}

data->sample_temp = sys_le16_to_cpu(temp_data.heat.raw);
data->sample_temp = sys_le16_to_cpu(temp_data.heat.deg_c);

return 0;
}
Expand Down Expand Up @@ -246,7 +282,7 @@ static inline int lis2dux12_get_channel(enum sensor_channel chan, struct sensor_
break;
#if defined(CONFIG_LIS2DUX12_ENABLE_TEMP)
case SENSOR_CHAN_DIE_TEMP:
sensor_value_from_double(val, data->sample_temp);
sensor_value_from_float(val, data->sample_temp);
break;
#endif
default:
Expand Down Expand Up @@ -322,11 +358,7 @@ static int lis2dux12_init(const struct device *dev)

/* set sensor default pm and odr */
LOG_DBG("%s: pm: %d, odr: %d", dev->name, cfg->pm, cfg->odr);
lis2dux12_md_t mode = {
.odr = cfg->odr,
.fs = cfg->range,
};
ret = lis2dux12_mode_set(ctx, &mode);
ret = lis2dux12_set_odr(dev, cfg->odr);
if (ret < 0) {
LOG_ERR("%s: odr init error (12.5 Hz)", dev->name);
return ret;
Expand Down
4 changes: 3 additions & 1 deletion drivers/sensor/st/lis2dux12/lis2dux12.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ struct lis2dux12_data {
int sample_y;
int sample_z;
float gain;
uint8_t range;
uint8_t odr;

#ifdef CONFIG_LIS2DUX12_ENABLE_TEMP
int sample_temp;
float sample_temp;
#endif

#ifdef CONFIG_LIS2DUX12_TRIGGER
Expand Down
1 change: 1 addition & 0 deletions samples/shields/x_nucleo_iks4a1/standard/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ CONFIG_LPS2XDF_TRIGGER_OWN_THREAD=y
CONFIG_LSM6DSO16IS_TRIGGER_OWN_THREAD=y
CONFIG_LSM6DSO16IS_ENABLE_TEMP=y
CONFIG_LSM6DSV16X_TRIGGER_OWN_THREAD=y
CONFIG_LSM6DSV16X_ENABLE_TEMP=y
CONFIG_CBPRINTF_FP_SUPPORT=y
12 changes: 12 additions & 0 deletions samples/shields/x_nucleo_iks4a1/standard/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ int main(void)
struct sensor_value lsm6dso16is_xl[3], lsm6dso16is_gy[3];
#ifdef CONFIG_LSM6DSO16IS_ENABLE_TEMP
struct sensor_value lsm6dso16is_temp;
#endif
#ifdef CONFIG_LSM6DSV16X_ENABLE_TEMP
struct sensor_value lsm6dsv16x_temp;
#endif
struct sensor_value lsm6dsv16x_xl[3], lsm6dsv16x_gy[3];
const struct device *const lis2mdl = DEVICE_DT_GET_ONE(st_lis2mdl);
Expand Down Expand Up @@ -285,6 +288,9 @@ int main(void)
sensor_channel_get(lsm6dso16is, SENSOR_CHAN_GYRO_XYZ, lsm6dso16is_gy);
#ifdef CONFIG_LSM6DSO16IS_ENABLE_TEMP
sensor_channel_get(lsm6dso16is, SENSOR_CHAN_DIE_TEMP, &lsm6dso16is_temp);
#endif
#ifdef CONFIG_LSM6DSV16X_ENABLE_TEMP
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_DIE_TEMP, &lsm6dsv16x_temp);
#endif
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_ACCEL_XYZ, lsm6dsv16x_xl);
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_GYRO_XYZ, lsm6dsv16x_gy);
Expand Down Expand Up @@ -334,6 +340,12 @@ int main(void)
sensor_value_to_double(&lsm6dsv16x_gy[1]),
sensor_value_to_double(&lsm6dsv16x_gy[2]));

#ifdef CONFIG_LSM6DSV16X_ENABLE_TEMP
/* temperature */
printf("LSM6DSV16X: Temperature: %.1f C\n",
sensor_value_to_double(&lsm6dsv16x_temp));
#endif

printf("LPS22DF: Temperature: %.1f C\n", sensor_value_to_double(&lps22df_temp));
printf("LPS22DF: Pressure:%.3f kpa\n", sensor_value_to_double(&lps22df_press));

Expand Down