Skip to content

Commit 9d262d0

Browse files
xodus7kartben
authored andcommitted
drivers: sensor: lsm6dsv16x: support setting and getting wake attributes
Adds support for the SENSOR_ATTR_SLOPE_TH and SENSOR_ATTR_SLOPE_DUR attributes These set the sensitivity for the SENSOR_TRIG_DELTA trigger which uses the sensor's wakeup feature. Signed-off-by: Corey Wharton <xodus7@cwharton.com>
1 parent 557eb47 commit 9d262d0

File tree

1 file changed

+96
-4
lines changed

1 file changed

+96
-4
lines changed

drivers/sensor/st/lsm6dsv16x/lsm6dsv16x.c

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,55 @@ static int lsm6dsv16x_accel_range_set(const struct device *dev, int32_t range)
234234
return 0;
235235
}
236236

237+
#define LSM6DSV16X_WU_INACT_THS_W_MAX 5
238+
#define LSM6DSV16X_WAKE_UP_THS_MAX 0x3FU
239+
static const float wu_inact_ths_w_lsb[] = {7.8125f, 15.625f, 31.25f, 62.5f, 125.0f, 250.0f};
240+
241+
static int lsm6dsv16x_accel_wake_threshold_set(const struct device *dev,
242+
const struct sensor_value *val)
243+
{
244+
const struct lsm6dsv16x_config *cfg = dev->config;
245+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
246+
lsm6dsv16x_act_thresholds_t thresholds;
247+
248+
if (lsm6dsv16x_act_thresholds_get(ctx, &thresholds) < 0) {
249+
LOG_DBG("failed to get thresholds");
250+
return -EIO;
251+
}
252+
253+
float val_mg = sensor_ms2_to_ug(val) / 1000.0f;
254+
255+
thresholds.inactivity_cfg.wu_inact_ths_w = LSM6DSV16X_WU_INACT_THS_W_MAX;
256+
thresholds.threshold = LSM6DSV16X_WAKE_UP_THS_MAX;
257+
258+
for (uint8_t i = 0; i <= LSM6DSV16X_WU_INACT_THS_W_MAX; i++) {
259+
if (val_mg < (wu_inact_ths_w_lsb[i] * (float)LSM6DSV16X_WAKE_UP_THS_MAX)) {
260+
thresholds.inactivity_cfg.wu_inact_ths_w = i;
261+
thresholds.threshold = (uint8_t)(val_mg / wu_inact_ths_w_lsb[i]);
262+
break;
263+
}
264+
}
265+
266+
return lsm6dsv16x_act_thresholds_set(ctx, &thresholds);
267+
}
268+
269+
static int lsm6dsv16x_accel_wake_duration_set(const struct device *dev,
270+
const struct sensor_value *val)
271+
{
272+
const struct lsm6dsv16x_config *cfg = dev->config;
273+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
274+
lsm6dsv16x_act_thresholds_t thresholds;
275+
276+
if (lsm6dsv16x_act_thresholds_get(ctx, &thresholds) < 0) {
277+
LOG_DBG("failed to get thresholds");
278+
return -EIO;
279+
}
280+
281+
thresholds.duration = MIN(val->val1, 3);
282+
283+
return lsm6dsv16x_act_thresholds_set(ctx, &thresholds);
284+
}
285+
237286
static int lsm6dsv16x_accel_config(const struct device *dev,
238287
enum sensor_channel chan,
239288
enum sensor_attribute attr,
@@ -248,6 +297,10 @@ static int lsm6dsv16x_accel_config(const struct device *dev,
248297
return lsm6dsv16x_accel_range_set(dev, sensor_ms2_to_g(val));
249298
case SENSOR_ATTR_SAMPLING_FREQUENCY:
250299
return lsm6dsv16x_accel_odr_set(dev, val->val1);
300+
case SENSOR_ATTR_SLOPE_TH:
301+
return lsm6dsv16x_accel_wake_threshold_set(dev, val);
302+
case SENSOR_ATTR_SLOPE_DUR:
303+
return lsm6dsv16x_accel_wake_duration_set(dev, val);
251304
case SENSOR_ATTR_CONFIGURATION:
252305
switch (val->val1) {
253306
case 0: /* High Performance */
@@ -402,6 +455,43 @@ static int lsm6dsv16x_attr_set(const struct device *dev,
402455
return 0;
403456
}
404457

458+
static int lsm6dsv16x_accel_wake_threshold_get(const struct device *dev, struct sensor_value *val)
459+
{
460+
const struct lsm6dsv16x_config *cfg = dev->config;
461+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
462+
lsm6dsv16x_act_thresholds_t thresholds;
463+
float val_mg;
464+
465+
if (lsm6dsv16x_act_thresholds_get(ctx, &thresholds) < 0) {
466+
LOG_DBG("failed to get thresholds");
467+
return -EIO;
468+
}
469+
470+
val_mg = wu_inact_ths_w_lsb[thresholds.inactivity_cfg.wu_inact_ths_w];
471+
val_mg *= (float)thresholds.threshold;
472+
473+
sensor_ug_to_ms2(1000.0f * val_mg, val);
474+
475+
return 0;
476+
}
477+
478+
static int lsm6dsv16x_accel_wake_duration_get(const struct device *dev, struct sensor_value *val)
479+
{
480+
const struct lsm6dsv16x_config *cfg = dev->config;
481+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)&cfg->ctx;
482+
lsm6dsv16x_act_thresholds_t thresholds;
483+
484+
if (lsm6dsv16x_act_thresholds_get(ctx, &thresholds) < 0) {
485+
LOG_DBG("failed to get thresholds");
486+
return -EIO;
487+
}
488+
489+
val->val1 = thresholds.duration;
490+
val->val2 = 0;
491+
492+
return 0;
493+
}
494+
405495
static int lsm6dsv16x_accel_get_config(const struct device *dev,
406496
enum sensor_channel chan,
407497
enum sensor_attribute attr,
@@ -429,6 +519,10 @@ static int lsm6dsv16x_accel_get_config(const struct device *dev,
429519
val->val2 = 0;
430520
break;
431521
}
522+
case SENSOR_ATTR_SLOPE_TH:
523+
return lsm6dsv16x_accel_wake_threshold_get(dev, val);
524+
case SENSOR_ATTR_SLOPE_DUR:
525+
return lsm6dsv16x_accel_wake_duration_get(dev, val);
432526
case SENSOR_ATTR_CONFIGURATION: {
433527
lsm6dsv16x_xl_mode_t mode;
434528

@@ -529,10 +623,8 @@ static int lsm6dsv16x_gyro_get_config(const struct device *dev,
529623
return 0;
530624
}
531625

532-
static int lsm6dsv16x_attr_get(const struct device *dev,
533-
enum sensor_channel chan,
534-
enum sensor_attribute attr,
535-
struct sensor_value *val)
626+
static int lsm6dsv16x_attr_get(const struct device *dev, enum sensor_channel chan,
627+
enum sensor_attribute attr, struct sensor_value *val)
536628
{
537629
if (!lsm6dsv16x_is_active(dev)) {
538630
return -EBUSY;

0 commit comments

Comments
 (0)