Skip to content

Commit 1c0acad

Browse files
aviscontiMaureenHelm
authored andcommitted
driver/sensor: lis2ds12: make use of STdC definitions
Port the lis2ds12 sensor driver on top of the lis2ds12_StdC HAL interface (in modules/hal/st/sensor/stmemsc/). Signed-off-by: Armando Visconti <[email protected]>
1 parent dabaaa1 commit 1c0acad

File tree

6 files changed

+123
-300
lines changed

6 files changed

+123
-300
lines changed

drivers/sensor/lis2ds12/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
menuconfig LIS2DS12
77
bool "LIS2DS12 I2C/SPI accelerometer sensor driver"
88
depends on I2C || SPI
9+
select HAS_STMEMSC
10+
select USE_STDC_LIS2DS12
911
help
1012
Enable driver for LIS2DS12 accelerometer sensor driver
1113

drivers/sensor/lis2ds12/lis2ds12.c

Lines changed: 70 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -41,98 +41,65 @@ static struct lis2ds12_config lis2ds12_config = {
4141
#endif
4242
};
4343

44-
#if defined(LIS2DS12_ODR_RUNTIME)
45-
static const uint16_t lis2ds12_hr_odr_map[] = {0, 12, 25, 50, 100, 200, 400, 800};
46-
47-
static int lis2ds12_freq_to_odr_val(uint16_t freq)
48-
{
49-
size_t i;
50-
51-
for (i = 0; i < ARRAY_SIZE(lis2ds12_hr_odr_map); i++) {
52-
if (freq == lis2ds12_hr_odr_map[i]) {
53-
return i;
54-
}
55-
}
56-
57-
return -EINVAL;
58-
}
59-
60-
static int lis2ds12_accel_odr_set(const struct device *dev, uint16_t freq)
44+
static int lis2ds12_set_odr(const struct device *dev, uint16_t odr)
6145
{
62-
struct lis2ds12_data *data = dev->data;
63-
int odr;
46+
const struct lis2ds12_data *data = dev->data;
47+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
48+
uint8_t val;
6449

65-
odr = lis2ds12_freq_to_odr_val(freq);
66-
if (odr < 0) {
67-
return odr;
50+
/* check if power off */
51+
if (odr == 0U) {
52+
return lis2ds12_xl_data_rate_set(ctx, LIS2DS12_XL_ODR_OFF);
6853
}
6954

70-
if (data->hw_tf->update_reg(data,
71-
LIS2DS12_REG_CTRL1,
72-
LIS2DS12_MASK_CTRL1_ODR,
73-
odr << LIS2DS12_SHIFT_CTRL1_ODR) < 0) {
74-
LOG_DBG("failed to set accelerometer sampling rate");
75-
return -EIO;
76-
}
77-
78-
return 0;
79-
}
80-
#endif
81-
82-
#ifdef LIS2DS12_FS_RUNTIME
83-
static const uint16_t lis2ds12_accel_fs_map[] = {2, 16, 4, 8};
84-
static const uint16_t lis2ds12_accel_fs_sens[] = {1, 8, 2, 4};
85-
86-
static int lis2ds12_accel_range_to_fs_val(int32_t range)
87-
{
88-
size_t i;
89-
90-
for (i = 0; i < ARRAY_SIZE(lis2ds12_accel_fs_map); i++) {
91-
if (range == lis2ds12_accel_fs_map[i]) {
92-
return i;
93-
}
55+
val = LIS2DS12_HR_ODR_TO_REG(odr);
56+
if (val > LIS2DS12_XL_ODR_800Hz_HR) {
57+
LOG_ERR("ODR too high");
58+
return -EINVAL;
9459
}
9560

96-
return -EINVAL;
61+
return lis2ds12_xl_data_rate_set(ctx, val);
9762
}
9863

99-
static int lis2ds12_accel_range_set(const struct device *dev, int32_t range)
64+
static int lis2ds12_set_range(const struct device *dev, uint8_t range)
10065
{
101-
int fs;
66+
int err;
10267
struct lis2ds12_data *data = dev->data;
68+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
10369

104-
fs = lis2ds12_accel_range_to_fs_val(range);
105-
if (fs < 0) {
106-
return fs;
107-
}
108-
109-
if (data->hw_tf->update_reg(data,
110-
LIS2DS12_REG_CTRL1,
111-
LIS2DS12_MASK_CTRL1_FS,
112-
fs << LIS2DS12_SHIFT_CTRL1_FS) < 0) {
113-
LOG_DBG("failed to set accelerometer full-scale");
114-
return -EIO;
70+
switch (range) {
71+
default:
72+
case 2U:
73+
err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_2g);
74+
data->gain = lis2ds12_from_fs2g_to_mg(1);
75+
break;
76+
case 4U:
77+
err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_4g);
78+
data->gain = lis2ds12_from_fs4g_to_mg(1);
79+
break;
80+
case 8U:
81+
err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_8g);
82+
data->gain = lis2ds12_from_fs8g_to_mg(1);
83+
break;
84+
case 16U:
85+
err = lis2ds12_xl_full_scale_set(ctx, LIS2DS12_16g);
86+
data->gain = lis2ds12_from_fs16g_to_mg(1);
87+
break;
11588
}
11689

117-
data->gain = (float)(lis2ds12_accel_fs_sens[fs] * GAIN_XL);
118-
return 0;
90+
return err;
11991
}
120-
#endif
12192

12293
static int lis2ds12_accel_config(const struct device *dev,
12394
enum sensor_channel chan,
12495
enum sensor_attribute attr,
12596
const struct sensor_value *val)
12697
{
12798
switch (attr) {
128-
#ifdef LIS2DS12_FS_RUNTIME
12999
case SENSOR_ATTR_FULL_SCALE:
130-
return lis2ds12_accel_range_set(dev, sensor_ms2_to_g(val));
131-
#endif
132-
#ifdef LIS2DS12_ODR_RUNTIME
100+
return lis2ds12_set_range(dev, sensor_ms2_to_g(val));
133101
case SENSOR_ATTR_SAMPLING_FREQUENCY:
134-
return lis2ds12_accel_odr_set(dev, val->val1);
135-
#endif
102+
return lis2ds12_set_odr(dev, val->val1);
136103
default:
137104
LOG_DBG("Accel attribute not supported.");
138105
return -ENOTSUP;
@@ -160,17 +127,18 @@ static int lis2ds12_attr_set(const struct device *dev,
160127
static int lis2ds12_sample_fetch_accel(const struct device *dev)
161128
{
162129
struct lis2ds12_data *data = dev->data;
163-
uint8_t buf[6];
130+
stmdev_ctx_t *ctx = (stmdev_ctx_t *)data->ctx;
131+
int16_t buf[3];
164132

165-
if (data->hw_tf->read_data(data, LIS2DS12_REG_OUTX_L,
166-
buf, sizeof(buf)) < 0) {
167-
LOG_DBG("failed to read sample");
133+
/* fetch raw data sample */
134+
if (lis2ds12_acceleration_raw_get(ctx, buf) < 0) {
135+
LOG_ERR("Failed to fetch raw data sample");
168136
return -EIO;
169137
}
170138

171-
data->sample_x = (int16_t)((uint16_t)(buf[0]) | ((uint16_t)(buf[1]) << 8));
172-
data->sample_y = (int16_t)((uint16_t)(buf[2]) | ((uint16_t)(buf[3]) << 8));
173-
data->sample_z = (int16_t)((uint16_t)(buf[4]) | ((uint16_t)(buf[5]) << 8));
139+
data->sample_x = sys_le16_to_cpu(buf[0]);
140+
data->sample_y = sys_le16_to_cpu(buf[1]);
141+
data->sample_z = sys_le16_to_cpu(buf[2]);
174142

175143
return 0;
176144
}
@@ -265,35 +233,40 @@ static int lis2ds12_init(const struct device *dev)
265233
{
266234
const struct lis2ds12_config * const config = dev->config;
267235
struct lis2ds12_data *data = dev->data;
236+
stmdev_ctx_t *ctx;
268237
uint8_t chip_id;
238+
int ret;
269239

270240
data->comm_master = device_get_binding(config->comm_master_dev_name);
271241
if (!data->comm_master) {
272-
LOG_DBG("master not found: %s",
242+
LOG_ERR("master not found: %s",
273243
config->comm_master_dev_name);
274244
return -EINVAL;
275245
}
276246

277247
config->bus_init(dev);
278248

279-
/* s/w reset the sensor */
280-
if (data->hw_tf->write_reg(data,
281-
LIS2DS12_REG_CTRL2,
282-
LIS2DS12_SOFT_RESET) < 0) {
283-
LOG_DBG("s/w reset fail");
284-
return -EIO;
249+
ctx = (stmdev_ctx_t *)data->ctx;
250+
/* check chip ID */
251+
ret = lis2ds12_device_id_get(ctx, &chip_id);
252+
if (ret < 0) {
253+
LOG_ERR("Not able to read dev id");
254+
return ret;
285255
}
286256

287-
if (data->hw_tf->read_reg(data, LIS2DS12_REG_WHO_AM_I, &chip_id) < 0) {
288-
LOG_DBG("failed reading chip id");
289-
return -EIO;
257+
if (chip_id != LIS2DS12_ID) {
258+
LOG_ERR("Invalid chip ID 0x%02x", chip_id);
259+
return -EINVAL;
290260
}
291261

292-
if (chip_id != LIS2DS12_VAL_WHO_AM_I) {
293-
LOG_DBG("invalid chip id 0x%x", chip_id);
294-
return -EIO;
262+
/* reset device */
263+
ret = lis2ds12_reset_set(ctx, PROPERTY_ENABLE);
264+
if (ret < 0) {
265+
return ret;
295266
}
296267

268+
k_busy_wait(100);
269+
297270
LOG_DBG("chip id 0x%x", chip_id);
298271

299272
#ifdef CONFIG_LIS2DS12_TRIGGER
@@ -304,23 +277,18 @@ static int lis2ds12_init(const struct device *dev)
304277
#endif
305278

306279
/* set sensor default odr */
307-
if (data->hw_tf->update_reg(data,
308-
LIS2DS12_REG_CTRL1,
309-
LIS2DS12_MASK_CTRL1_ODR,
310-
LIS2DS12_DEFAULT_ODR) < 0) {
311-
LOG_DBG("failed setting odr");
312-
return -EIO;
280+
ret = lis2ds12_set_odr(dev, 12);
281+
if (ret < 0) {
282+
LOG_ERR("odr init error (12.5 Hz)");
283+
return ret;
313284
}
314285

315286
/* set sensor default scale */
316-
if (data->hw_tf->update_reg(data,
317-
LIS2DS12_REG_CTRL1,
318-
LIS2DS12_MASK_CTRL1_FS,
319-
LIS2DS12_DEFAULT_FS) < 0) {
320-
LOG_DBG("failed setting scale");
321-
return -EIO;
287+
ret = lis2ds12_set_range(dev, CONFIG_LIS2DS12_FS);
288+
if (ret < 0) {
289+
LOG_ERR("range init error %d", CONFIG_LIS2DS12_FS);
290+
return ret;
322291
}
323-
data->gain = LIS2DS12_DEFAULT_GAIN;
324292

325293
return 0;
326294
}

drivers/sensor/lis2ds12/lis2ds12.h

Lines changed: 6 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -14,72 +14,12 @@
1414
#include <zephyr/types.h>
1515
#include <drivers/sensor.h>
1616
#include <drivers/gpio.h>
17+
#include "lis2ds12_reg.h"
1718

18-
#define LIS2DS12_REG_WHO_AM_I 0x0F
19-
#define LIS2DS12_VAL_WHO_AM_I 0x43
20-
21-
#define LIS2DS12_REG_CTRL1 0x20
22-
#define LIS2DS12_MASK_CTRL1_ODR (BIT(7) | BIT(6) | \
23-
BIT(5) | BIT(4))
24-
#define LIS2DS12_SHIFT_CTRL1_ODR 4
25-
#define LIS2DS12_MASK_CTRL1_FS (BIT(3) | BIT(2))
26-
#define LIS2DS12_SHIFT_CTRL1_FS 2
27-
#define LIS2DS12_MASK_CTRL1_HF_ODR BIT(1)
28-
#define LIS2DS12_MASK_CTRL1_HF_BDU BIT(0)
29-
30-
#define LIS2DS12_REG_CTRL2 0x21
31-
#define LIS2DS12_SOFT_RESET 0x40
32-
33-
#define LIS2DS12_REG_CTRL3 0x22
34-
#define LIS2DS12_SHIFT_LIR 0x02
35-
#define LIS2DS12_MASK_LIR 0x04
36-
37-
#define LIS2DS12_REG_CTRL4 0x23
38-
#define LIS2DS12_SHIFT_INT1_DRDY 0x00
39-
#define LIS2DS12_MASK_INT1_DRDY 0x01
40-
41-
#define LIS2DS12_REG_OUT_T 0x26
42-
43-
#define LIS2DS12_REG_STATUS 0x27
44-
#define LIS2DS12_INT_DRDY 0x01
45-
46-
#define LIS2DS12_REG_OUTX_L 0x28
47-
#define LIS2DS12_REG_OUTX_H 0x29
48-
#define LIS2DS12_REG_OUTY_L 0x2A
49-
#define LIS2DS12_REG_OUTY_H 0x2B
50-
#define LIS2DS12_REG_OUTZ_L 0x2C
51-
#define LIS2DS12_REG_OUTZ_H 0x2D
52-
53-
54-
#if (CONFIG_LIS2DS12_ODR == 0)
55-
#define LIS2DS12_ODR_RUNTIME 1
56-
#define LIS2DS12_DEFAULT_ODR 0
57-
#else
58-
#define LIS2DS12_DEFAULT_ODR (CONFIG_LIS2DS12_ODR << 4)
59-
#endif
60-
61-
/* Accel sensor sensitivity unit is 0.061 mg/LSB */
62-
#define GAIN_XL (61LL / 1000.0)
63-
64-
#if CONFIG_LIS2DS12_FS == 0
65-
#define LIS2DS12_FS_RUNTIME 1
66-
#define LIS2DS12_DEFAULT_FS (0 << 2)
67-
#define LIS2DS12_DEFAULT_GAIN GAIN_XL
68-
#elif CONFIG_LIS2DS12_FS == 2
69-
#define LIS2DS12_DEFAULT_FS (0 << 2)
70-
#define LIS2DS12_DEFAULT_GAIN GAIN_XL
71-
#elif CONFIG_LIS2DS12_FS == 4
72-
#define LIS2DS12_DEFAULT_FS (2 << 2)
73-
#define LIS2DS12_DEFAULT_GAIN (2.0 * GAIN_XL)
74-
#elif CONFIG_LIS2DS12_FS == 8
75-
#define LIS2DS12_DEFAULT_FS (3 << 2)
76-
#define LIS2DS12_DEFAULT_GAIN (4.0 * GAIN_XL)
77-
#elif CONFIG_LIS2DS12_FS == 16
78-
#define LIS2DS12_DEFAULT_FS (1 << 2)
79-
#define LIS2DS12_DEFAULT_GAIN (8.0 * GAIN_XL)
80-
#else
81-
#error "Bad LIS2DS12 FS value (should be 0, 2, 4, 8, 16)"
82-
#endif
19+
/* Return ODR reg value based on data rate set */
20+
#define LIS2DS12_HR_ODR_TO_REG(_odr) \
21+
((_odr <= 12) ? LIS2DS12_XL_ODR_12Hz5_HR : \
22+
((31 - __builtin_clz(_odr / 25))) + 2)
8323

8424
struct lis2ds12_config {
8525
char *comm_master_dev_name;
@@ -91,28 +31,13 @@ struct lis2ds12_config {
9131
#endif
9232
};
9333

94-
struct lis2ds12_data;
95-
96-
struct lis2ds12_transfer_function {
97-
int (*read_data)(struct lis2ds12_data *data, uint8_t reg_addr,
98-
uint8_t *value, uint8_t len);
99-
int (*write_data)(struct lis2ds12_data *data, uint8_t reg_addr,
100-
uint8_t *value, uint8_t len);
101-
int (*read_reg)(struct lis2ds12_data *data, uint8_t reg_addr,
102-
uint8_t *value);
103-
int (*write_reg)(struct lis2ds12_data *data, uint8_t reg_addr,
104-
uint8_t value);
105-
int (*update_reg)(struct lis2ds12_data *data, uint8_t reg_addr,
106-
uint8_t mask, uint8_t value);
107-
};
108-
10934
struct lis2ds12_data {
35+
stmdev_ctx_t *ctx;
11036
const struct device *comm_master;
11137
int sample_x;
11238
int sample_y;
11339
int sample_z;
11440
float gain;
115-
const struct lis2ds12_transfer_function *hw_tf;
11641

11742
#ifdef CONFIG_LIS2DS12_TRIGGER
11843
const struct device *gpio;

0 commit comments

Comments
 (0)