Skip to content

Commit a6cbc32

Browse files
aviscontinashif
authored andcommitted
drivers/sensor: lis2dux12: fix odr and range
The current implementation assumes that sensor odr and range are always configured in the Device Tree at compile time which might not be the case. Instead, application can set odr and range either at compile time through the DT or using SENSOR_ATTR_SAMPLING_FREQUENCY and SENSOR_ATTR_FULL_SCALE attributes at runtime, so each driver instance must keep trace of the latest values set and use them in the sensor APIs which require them (e.g. lis2dux12_mode_set). Signed-off-by: Armando Visconti <[email protected]> (cherry picked from commit cf20aa0)
1 parent 81e8c6d commit a6cbc32

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

drivers/sensor/st/lis2dux12/lis2dux12.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ LOG_MODULE_REGISTER(LIS2DUX12, CONFIG_SENSOR_LOG_LEVEL);
2727

2828
static int lis2dux12_set_odr(const struct device *dev, uint8_t odr)
2929
{
30+
struct lis2dux12_data *data = dev->data;
3031
const struct lis2dux12_config *cfg = dev->config;
3132
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
3233
lis2dux12_md_t mode = {.odr = odr};
3334

35+
data->odr = odr;
3436
return lis2dux12_mode_set(ctx, &mode);
3537
}
3638

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

4547
err = lis2dux12_mode_set(ctx, &val);
4648

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

71+
data->range = range;
6972
return 0;
7073
}
7174

@@ -187,7 +190,7 @@ static int lis2dux12_sample_fetch_accel(const struct device *dev)
187190
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
188191

189192
/* fetch raw data sample */
190-
lis2dux12_md_t mode = {.fs = cfg->range};
193+
lis2dux12_md_t mode = {.fs = data->range};
191194
lis2dux12_xl_data_t xzy_data = {0};
192195

193196
if (lis2dux12_xl_data_get(ctx, &mode, &xzy_data) < 0) {
@@ -355,11 +358,7 @@ static int lis2dux12_init(const struct device *dev)
355358

356359
/* set sensor default pm and odr */
357360
LOG_DBG("%s: pm: %d, odr: %d", dev->name, cfg->pm, cfg->odr);
358-
lis2dux12_md_t mode = {
359-
.odr = cfg->odr,
360-
.fs = cfg->range,
361-
};
362-
ret = lis2dux12_mode_set(ctx, &mode);
361+
ret = lis2dux12_set_odr(dev, cfg->odr);
363362
if (ret < 0) {
364363
LOG_ERR("%s: odr init error (12.5 Hz)", dev->name);
365364
return ret;

drivers/sensor/st/lis2dux12/lis2dux12.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct lis2dux12_data {
5151
int sample_y;
5252
int sample_z;
5353
float gain;
54+
uint8_t range;
55+
uint8_t odr;
5456

5557
#ifdef CONFIG_LIS2DUX12_ENABLE_TEMP
5658
int sample_temp;

0 commit comments

Comments
 (0)