Skip to content

Commit acf7657

Browse files
arthur-mmlwcfriedt
authored andcommitted
drivers: sensor: lis2dh: Add self-test attribute
Add support for enabling and configuring the self-test mode of the LIS2DH accelerometer through a dedicated sensor attribute. Signed-off-by: Arthur Gay <[email protected]>
1 parent 0b237da commit acf7657

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

drivers/sensor/st/lis2dh/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,11 @@ config LIS2DH_MEASURE_TEMPERATURE
161161
the reported temperature to a reference.
162162
This option does not apply to the LSM330DLHC.
163163

164+
config LIS2DH_SELF_TEST
165+
bool "Self test attribute"
166+
help
167+
Enable support for configuring the sensor self test bits.
168+
Refer to the datasheet of the sensor for a description of
169+
the appropriate self test procedure.
170+
164171
endif # LIS2DH

drivers/sensor/st/lis2dh/lis2dh.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <zephyr/sys/__assert.h>
1313
#include <zephyr/logging/log.h>
1414
#include <zephyr/pm/device.h>
15+
#include <zephyr/drivers/sensor/lis2dh.h>
1516

1617
LOG_MODULE_REGISTER(lis2dh, CONFIG_SENSOR_LOG_LEVEL);
1718
#include "lis2dh.h"
@@ -286,6 +287,16 @@ static int lis2dh_acc_hp_filter_set(const struct device *dev, int32_t val)
286287
}
287288
#endif
288289

290+
#ifdef CONFIG_LIS2DH_SELF_TEST
291+
static int lis2dh_self_test_set(const struct device *dev, int32_t val)
292+
{
293+
struct lis2dh_data *lis2dh = dev->data;
294+
uint8_t value = (val << LIS2DH_CTRL4_ST_SHIFT) & LIS2DH_CTRL4_ST_MASK;
295+
296+
return lis2dh->hw_tf->update_reg(dev, LIS2DH_REG_CTRL4, LIS2DH_CTRL4_ST_MASK, value);
297+
}
298+
#endif
299+
289300
static int lis2dh_acc_config(const struct device *dev,
290301
enum sensor_channel chan,
291302
enum sensor_attribute attr,
@@ -308,6 +319,10 @@ static int lis2dh_acc_config(const struct device *dev,
308319
#ifdef CONFIG_LIS2DH_ACCEL_HP_FILTERS
309320
case SENSOR_ATTR_CONFIGURATION:
310321
return lis2dh_acc_hp_filter_set(dev, val->val1);
322+
#endif
323+
#ifdef CONFIG_LIS2DH_SELF_TEST
324+
case SENSOR_ATTR_LIS2DH_SELF_TEST:
325+
return lis2dh_self_test_set(dev, val->val1);
311326
#endif
312327
default:
313328
LOG_DBG("Accel attribute not supported.");

drivers/sensor/st/lis2dh/lis2dh.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
#define LIS2DH_EN_DRDY1_INT1 BIT(4)
102102

103103
#define LIS2DH_REG_CTRL4 0x23
104+
#define LIS2DH_CTRL4_ST_SHIFT 1
105+
#define LIS2DH_CTRL4_ST_MASK (BIT_MASK(2) << LIS2DH_CTRL4_ST_SHIFT)
104106
#define LIS2DH_CTRL4_BDU_BIT BIT(7)
105107
#define LIS2DH_FS_SHIFT 4
106108
#define LIS2DH_FS_MASK (BIT_MASK(2) << LIS2DH_FS_SHIFT)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright The Zephyr Project Contributors
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_DRIVERS_SENSOR_LIS2DH_H_
8+
#define ZEPHYR_INCLUDE_DRIVERS_SENSOR_LIS2DH_H_
9+
10+
#include <zephyr/drivers/sensor.h>
11+
12+
enum lis2dh_self_test {
13+
LIS2DH_SELF_TEST_DISABLE = 0,
14+
LIS2DH_SELF_TEST_POSITIVE = 1,
15+
LIS2DH_SELF_TEST_NEGATIVE = 2,
16+
};
17+
18+
enum sensor_attribute_lis2dh {
19+
SENSOR_ATTR_LIS2DH_SELF_TEST = SENSOR_ATTR_PRIV_START,
20+
};
21+
22+
#endif /* ZEPHYR_INCLUDE_DRIVERS_SENSOR_LIS2DH_H_ */

0 commit comments

Comments
 (0)