@@ -234,6 +234,55 @@ static int lsm6dsv16x_accel_range_set(const struct device *dev, int32_t range)
234
234
return 0 ;
235
235
}
236
236
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
+
237
286
static int lsm6dsv16x_accel_config (const struct device * dev ,
238
287
enum sensor_channel chan ,
239
288
enum sensor_attribute attr ,
@@ -248,6 +297,10 @@ static int lsm6dsv16x_accel_config(const struct device *dev,
248
297
return lsm6dsv16x_accel_range_set (dev , sensor_ms2_to_g (val ));
249
298
case SENSOR_ATTR_SAMPLING_FREQUENCY :
250
299
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 );
251
304
case SENSOR_ATTR_CONFIGURATION :
252
305
switch (val -> val1 ) {
253
306
case 0 : /* High Performance */
@@ -402,6 +455,43 @@ static int lsm6dsv16x_attr_set(const struct device *dev,
402
455
return 0 ;
403
456
}
404
457
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
+
405
495
static int lsm6dsv16x_accel_get_config (const struct device * dev ,
406
496
enum sensor_channel chan ,
407
497
enum sensor_attribute attr ,
@@ -429,6 +519,10 @@ static int lsm6dsv16x_accel_get_config(const struct device *dev,
429
519
val -> val2 = 0 ;
430
520
break ;
431
521
}
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 );
432
526
case SENSOR_ATTR_CONFIGURATION : {
433
527
lsm6dsv16x_xl_mode_t mode ;
434
528
@@ -529,10 +623,8 @@ static int lsm6dsv16x_gyro_get_config(const struct device *dev,
529
623
return 0 ;
530
624
}
531
625
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 )
536
628
{
537
629
if (!lsm6dsv16x_is_active (dev )) {
538
630
return - EBUSY ;
0 commit comments