Skip to content

Commit dd087be

Browse files
committed
driver/sensor: lis2dw12: make use of STdC definitions
Port the lis2dw12 sensor driver on top of the lis2dw12_StdC HAL interface (under ext/hal/st/stmemsc/). Signed-off-by: Armando Visconti <[email protected]>
1 parent 6b1453e commit dd087be

File tree

11 files changed

+124
-284
lines changed

11 files changed

+124
-284
lines changed

drivers/sensor/lis2dw12/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
menuconfig LIS2DW12
99
bool "LIS2DW12 I2C/SPI accelerometer sensor driver"
1010
depends on (I2C && HAS_DTS_I2C) || (SPI && HAS_DTS_SPI)
11+
select HAS_STMEMSC
12+
select USE_STDC_LIS2DW12
1113
help
1214
Enable driver for LIS2DW12 accelerometer sensor driver
1315

drivers/sensor/lis2dw12/lis2dw12.c

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ static int lis2dw12_set_range(struct device *dev, u16_t range)
3636
struct lis2dw12_data *lis2dw12 = dev->driver_data;
3737
const struct lis2dw12_device_config *cfg = dev->config->config_info;
3838
u8_t shift_gain = 0U;
39+
u8_t fs = LIS2DW12_FS_TO_REG(range);
3940

40-
err = lis2dw12->hw_tf->update_reg(lis2dw12, LIS2DW12_CTRL6_ADDR,
41-
LIS2DW12_FS_MASK,
42-
LIS2DW12_FS_TO_REG(range));
41+
err = lis2dw12_full_scale_set(lis2dw12->ctx, fs);
4342

44-
if (cfg->pm == LIS2DW12_LOW_POWER_M1) {
43+
if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) {
4544
shift_gain = LIS2DW12_SHFT_GAIN_NOLP1;
4645
}
4746

@@ -63,23 +62,21 @@ static int lis2dw12_set_range(struct device *dev, u16_t range)
6362
static int lis2dw12_set_odr(struct device *dev, u16_t odr)
6463
{
6564
struct lis2dw12_data *lis2dw12 = dev->driver_data;
65+
u8_t val;
6666

6767
/* check if power off */
6868
if (odr == 0U) {
69-
return lis2dw12->hw_tf->update_reg(lis2dw12,
70-
LIS2DW12_CTRL1_ADDR,
71-
LIS2DW12_ODR_MASK,
72-
LIS2DW12_ODR_POWER_OFF_VAL);
69+
return lis2dw12_data_rate_set(lis2dw12->ctx,
70+
LIS2DW12_XL_ODR_OFF);
7371
}
7472

75-
if (odr > LIS2DW12_MAX_ODR) {
73+
val = LIS2DW12_ODR_TO_REG(odr);
74+
if (val > LIS2DW12_XL_ODR_1k6Hz) {
7675
LOG_ERR("ODR too high");
7776
return -ENOTSUP;
7877
}
7978

80-
return lis2dw12->hw_tf->update_reg(lis2dw12, LIS2DW12_CTRL1_ADDR,
81-
LIS2DW12_ODR_MASK,
82-
LIS2DW12_ODR_TO_REG(odr));
79+
return lis2dw12_data_rate_set(lis2dw12->ctx, val);
8380
}
8481

8582
static inline void lis2dw12_convert(struct sensor_value *val, int raw_val,
@@ -182,39 +179,24 @@ static int lis2dw12_sample_fetch(struct device *dev, enum sensor_channel chan)
182179
struct lis2dw12_data *lis2dw12 = dev->driver_data;
183180
const struct lis2dw12_device_config *cfg = dev->config->config_info;
184181
u8_t shift;
185-
union {
186-
u8_t raw[6];
187-
struct {
188-
s16_t a_axis[3];
189-
};
190-
} buf __aligned(2);
191-
u8_t tmp;
192-
193-
if (lis2dw12->hw_tf->read_reg(lis2dw12, LIS2DW12_STATUS_REG, &tmp)) {
194-
return -EIO;
195-
}
196-
197-
if (!(tmp & LIS2DW12_STS_XLDA_UP)) {
198-
return -EAGAIN;
199-
}
182+
axis3bit16_t buf;
200183

201184
/* fetch raw data sample */
202-
if (lis2dw12->hw_tf->read_data(lis2dw12, LIS2DW12_OUT_X_L_ADDR,
203-
buf.raw, sizeof(buf)) < 0) {
185+
if (lis2dw12_acceleration_raw_get(lis2dw12->ctx, buf.u8bit) < 0) {
204186
LOG_DBG("Failed to fetch raw data sample");
205187
return -EIO;
206188
}
207189

208190
/* adjust to resolution */
209-
if (cfg->pm == LIS2DW12_LOW_POWER_M1) {
191+
if (cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit) {
210192
shift = LIS2DW12_SHIFT_PM1;
211193
} else {
212194
shift = LIS2DW12_SHIFT_PMOTHER;
213195
}
214196

215-
lis2dw12->acc[0] = sys_le16_to_cpu(buf.a_axis[0]) >> shift;
216-
lis2dw12->acc[1] = sys_le16_to_cpu(buf.a_axis[1]) >> shift;
217-
lis2dw12->acc[2] = sys_le16_to_cpu(buf.a_axis[2]) >> shift;
197+
lis2dw12->acc[0] = sys_le16_to_cpu(buf.i16bit[0]) >> shift;
198+
lis2dw12->acc[1] = sys_le16_to_cpu(buf.i16bit[1]) >> shift;
199+
lis2dw12->acc[2] = sys_le16_to_cpu(buf.i16bit[2]) >> shift;
218200

219201
return 0;
220202
}
@@ -251,30 +233,23 @@ static int lis2dw12_init_interface(struct device *dev)
251233
}
252234

253235
static int lis2dw12_set_power_mode(struct lis2dw12_data *lis2dw12,
254-
enum lis2dh_powermode pm)
236+
lis2dw12_mode_t pm)
255237
{
256-
u8_t regval = LIS2DW12_LOW_POWER_M1 | LIS2DW12_LOW_POWER_MODE;
238+
u8_t regval = LIS2DW12_CONT_LOW_PWR_12bit;
257239

258240
switch (pm) {
259-
case LIS2DW12_LOW_POWER_M2:
260-
regval = LIS2DW12_LOW_POWER_M2 | LIS2DW12_LOW_POWER_MODE;
261-
break;
262-
case LIS2DW12_LOW_POWER_M3:
263-
regval = LIS2DW12_LOW_POWER_M3 | LIS2DW12_LOW_POWER_MODE;
264-
break;
265-
case LIS2DW12_LOW_POWER_M4:
266-
regval = LIS2DW12_LOW_POWER_M4 | LIS2DW12_LOW_POWER_MODE;
267-
break;
268-
case LIS2DW12_HIGH_PERF:
269-
regval = LIS2DW12_HP_MODE;
241+
case LIS2DW12_CONT_LOW_PWR_2:
242+
case LIS2DW12_CONT_LOW_PWR_3:
243+
case LIS2DW12_CONT_LOW_PWR_4:
244+
case LIS2DW12_HIGH_PERFORMANCE:
245+
regval = pm;
270246
break;
271247
default:
272248
LOG_DBG("Apply default Power Mode");
273249
break;
274250
}
275251

276-
return lis2dw12->hw_tf->write_reg(lis2dw12, LIS2DW12_CTRL1_ADDR,
277-
regval);
252+
return lis2dw12_write_reg(lis2dw12->ctx, LIS2DW12_CTRL1, &regval, 1);
278253
}
279254

280255
static int lis2dw12_init(struct device *dev)
@@ -288,27 +263,23 @@ static int lis2dw12_init(struct device *dev)
288263
}
289264

290265
/* check chip ID */
291-
if (lis2dw12->hw_tf->read_reg(lis2dw12, LIS2DW12_WHO_AM_I_REG,
292-
&wai) < 0) {
293-
LOG_ERR("Failed to read chip ID");
266+
if (lis2dw12_device_id_get(lis2dw12->ctx, &wai) < 0) {
294267
return -EIO;
295268
}
296269

297-
if (wai != LIS2DW12_WHO_AM_I) {
270+
if (wai != LIS2DW12_ID) {
298271
LOG_ERR("Invalid chip ID");
299272
return -EINVAL;
300273
}
301274

302275
/* reset device */
303-
if (lis2dw12->hw_tf->write_reg(lis2dw12, LIS2DW12_CTRL2_ADDR,
304-
LIS2DW12_RESET_MASK)) {
276+
if (lis2dw12_reset_set(lis2dw12->ctx, PROPERTY_ENABLE) < 0) {
305277
return -EIO;
306278
}
307279

308280
k_busy_wait(100);
309281

310-
if (lis2dw12->hw_tf->update_reg(lis2dw12, LIS2DW12_CTRL2_ADDR,
311-
LIS2DW12_BDU_MASK, LIS2DW12_EN_BIT)) {
282+
if (lis2dw12_block_data_update_set(lis2dw12->ctx, PROPERTY_ENABLE) < 0) {
312283
return -EIO;
313284
}
314285

@@ -318,21 +289,17 @@ static int lis2dw12_init(struct device *dev)
318289
}
319290

320291
/* set default odr and full scale for acc */
321-
if (lis2dw12->hw_tf->update_reg(lis2dw12, LIS2DW12_CTRL1_ADDR,
322-
LIS2DW12_ODR_MASK,
323-
LIS2DW12_DEFAULT_ODR)) {
292+
if (lis2dw12_data_rate_set(lis2dw12->ctx, LIS2DW12_DEFAULT_ODR) < 0) {
324293
return -EIO;
325294
}
326295

327-
if (lis2dw12->hw_tf->update_reg(lis2dw12, LIS2DW12_CTRL6_ADDR,
328-
LIS2DW12_FS_MASK,
329-
LIS2DW12_ACC_FS)) {
296+
if (lis2dw12_full_scale_set(lis2dw12->ctx, LIS2DW12_ACC_FS) < 0) {
330297
return -EIO;
331298
}
332299

333300
lis2dw12->gain =
334301
LIS2DW12_FS_TO_GAIN(LIS2DW12_ACC_FS,
335-
cfg->pm == LIS2DW12_LOW_POWER_M1 ?
302+
cfg->pm == LIS2DW12_CONT_LOW_PWR_12bit ?
336303
LIS2DW12_SHFT_GAIN_NOLP1 : 0);
337304

338305
#ifdef CONFIG_LIS2DW12_TRIGGER

drivers/sensor/lis2dw12/lis2dw12.h

Lines changed: 18 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -15,121 +15,49 @@
1515
#include <gpio.h>
1616
#include <misc/util.h>
1717
#include <sensor.h>
18-
19-
/* COMMON DEFINE FOR ACCEL SENSOR */
20-
#define LIS2DW12_EN_BIT 0x01
21-
#define LIS2DW12_DIS_BIT 0x00
22-
#define LIS2DW12_OUT_LEN 6
23-
24-
/* temperature sensor */
25-
#define LIS2DW12_OUT_TEMP_L_ADDR 0x0d
26-
27-
/* Who Am I */
28-
#define LIS2DW12_WHO_AM_I_REG 0x0f
29-
#define LIS2DW12_WHO_AM_I 0x44
30-
31-
#define LIS2DW12_CTRL1_ADDR 0x20
32-
#define LIS2DW12_LOW_POWER_MASK 0x03
33-
#define LIS2DW12_POWER_MODE_MASK 0x0c
34-
#define LIS2DW12_LOW_POWER_MODE 0x00
35-
#define LIS2DW12_HP_MODE 0x04
36-
#define LIS2DW12_ODR_MASK 0xf0
37-
38-
enum lis2dh_powermode {
39-
LIS2DW12_LOW_POWER_M1,
40-
LIS2DW12_LOW_POWER_M2,
41-
LIS2DW12_LOW_POWER_M3,
42-
LIS2DW12_LOW_POWER_M4,
43-
LIS2DW12_HIGH_PERF
44-
};
45-
46-
/* Acc data rate for Low Power mode */
47-
#define LIS2DW12_MAX_ODR 1600
48-
49-
enum lis2dh_odr {
50-
LIS2DW12_ODR_POWER_OFF_VAL,
51-
LIS2DW12_ODR_1_6HZ_VAL,
52-
LIS2DW12_ODR_12_5HZ_VAL,
53-
LIS2DW12_ODR_25HZ_VAL,
54-
LIS2DW12_ODR_50HZ_VAL,
55-
LIS2DW12_ODR_100HZ_VAL,
56-
LIS2DW12_ODR_200HZ_VAL,
57-
LIS2DW12_ODR_400HZ_VAL,
58-
LIS2DW12_ODR_800HZ_VAL,
59-
LIS2DW12_ODR_1600HZ_VAL
60-
};
18+
#include "lis2dw12_reg.h"
6119

6220
#if defined(CONFIG_LIS2DW12_ODR_1_6)
63-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_1_6HZ_VAL
21+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_1Hz6_LP_ONLY
6422
#elif defined(CONFIG_LIS2DW12_ODR_12_5)
65-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_12_5HZ_VAL
23+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_12Hz5
6624
#elif defined(CONFIG_LIS2DW12_ODR_25)
67-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_25HZ_VAL
25+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_25Hz
6826
#elif defined(CONFIG_LIS2DW12_ODR_50)
69-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_50HZ_VAL
27+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_50Hz
7028
#elif defined(CONFIG_LIS2DW12_ODR_100) || \
7129
defined(CONFIG_LIS2DW12_ODR_RUNTIME)
72-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_100HZ_VAL
30+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_100Hz
7331
#elif defined(CONFIG_LIS2DW12_ODR_200)
74-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_200HZ_VAL
32+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_200Hz
7533
#elif defined(CONFIG_LIS2DW12_ODR_400)
76-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_400HZ_VAL
34+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_400Hz
7735
#elif defined(CONFIG_LIS2DW12_ODR_800)
78-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_800HZ_VAL
36+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_800Hz
7937
#elif defined(CONFIG_LIS2DW12_ODR_1600)
80-
#define LIS2DW12_DEFAULT_ODR LIS2DW12_ODR_1600HZ_VAL
38+
#define LIS2DW12_DEFAULT_ODR LIS2DW12_XL_ODR_1k6Hz
8139
#endif
8240

8341
/* Return ODR reg value based on data rate set */
8442
#define LIS2DW12_ODR_TO_REG(_odr) \
85-
((_odr <= 1) ? LIS2DW12_ODR_1_6HZ_VAL : \
86-
(_odr <= 12) ? LIS2DW12_ODR_12_5HZ_VAL : \
43+
((_odr <= 1) ? LIS2DW12_XL_ODR_1Hz6_LP_ONLY : \
44+
(_odr <= 12) ? LIS2DW12_XL_ODR_12Hz5 : \
8745
((31 - __builtin_clz(_odr / 25))) + 3)
8846

89-
#define LIS2DW12_CTRL2_ADDR 0x21
90-
#define LIS2DW12_BDU_MASK BIT(3)
91-
#define LIS2DW12_RESET_MASK BIT(6)
92-
#define LIS2DW12_BOOT_MASK BIT(7)
93-
#define LIS2DW12_CTRL3_ADDR 0x22
94-
#define LIS2DW12_LIR_MASK BIT(4)
95-
96-
#define LIS2DW12_CTRL4_ADDR 0x23
97-
#define LIS2DW12_INT1_DRDY BIT(0)
98-
99-
#define LIS2DW12_CTRL5_ADDR 0x24
100-
#define LIS2DW12_INT2_DRDY BIT(0)
101-
102-
#define LIS2DW12_CTRL6_ADDR 0x25
103-
#define LIS2DW12_FS_MASK 0x30
104-
105-
enum lis2dh_fs {
106-
LIS2DW12_FS_2G_VAL,
107-
LIS2DW12_FS_4G_VAL,
108-
LIS2DW12_FS_8G_VAL,
109-
LIS2DW12_FS_16G_VAL
110-
};
111-
11247
/* FS reg value from Full Scale */
11348
#define LIS2DW12_FS_TO_REG(_fs) (30 - __builtin_clz(_fs))
11449

11550
#if defined(CONFIG_LIS2DW12_ACCEL_RANGE_RUNTIME) || \
11651
defined(CONFIG_LIS2DW12_ACCEL_RANGE_2G)
117-
#define LIS2DW12_ACC_FS LIS2DW12_FS_2G_VAL
52+
#define LIS2DW12_ACC_FS LIS2DW12_2g
11853
#elif defined(CONFIG_LIS2DW12_ACCEL_RANGE_4G)
119-
#define LIS2DW12_ACC_FS LIS2DW12_FS_4G_VAL
54+
#define LIS2DW12_ACC_FS LIS2DW12_4g
12055
#elif defined(CONFIG_LIS2DW12_ACCEL_RANGE_8G)
121-
#define LIS2DW12_ACC_FS LIS2DW12_FS_8G_VAL
56+
#define LIS2DW12_ACC_FS LIS2DW12_8g
12257
#elif defined(CONFIG_LIS2DW12_ACCEL_RANGE_16G)
123-
#define LIS2DW12_ACC_FS LIS2DW12_FS_16G_VAL
58+
#define LIS2DW12_ACC_FS LIS2DW12_16g
12459
#endif
12560

126-
#define LIS2DW12_OUT_T_REG 0x26
127-
128-
#define LIS2DW12_STATUS_REG 0x27
129-
#define LIS2DW12_STS_XLDA_UP 0x01
130-
131-
#define LIS2DW12_OUT_X_L_ADDR 0x28
132-
13361
/* Acc Gain value in ug/LSB in High Perf mode */
13462
#define LIS2DW12_FS_2G_GAIN 244
13563
#define LIS2DW12_FS_4G_GAIN 488
@@ -155,7 +83,7 @@ enum lis2dh_fs {
15583
*/
15684
struct lis2dw12_device_config {
15785
const char *bus_name;
158-
enum lis2dh_powermode pm;
86+
lis2dw12_mode_t pm;
15987
#ifdef CONFIG_LIS2DW12_TRIGGER
16088
const char *int_gpio_port;
16189
u8_t int_gpio_pin;
@@ -166,20 +94,6 @@ struct lis2dw12_device_config {
16694
/* sensor data forward declaration (member definition is below) */
16795
struct lis2dw12_data;
16896

169-
/* transmission function interface */
170-
struct lis2dw12_tf {
171-
int (*read_data)(struct lis2dw12_data *data, u8_t reg_addr,
172-
u8_t *value, u8_t len);
173-
int (*write_data)(struct lis2dw12_data *data, u8_t reg_addr,
174-
u8_t *value, u8_t len);
175-
int (*read_reg)(struct lis2dw12_data *data, u8_t reg_addr,
176-
u8_t *value);
177-
int (*write_reg)(struct lis2dw12_data *data, u8_t reg_addr,
178-
u8_t value);
179-
int (*update_reg)(struct lis2dw12_data *data, u8_t reg_addr,
180-
u8_t mask, u8_t value);
181-
};
182-
18397
/* sensor data */
18498
struct lis2dw12_data {
18599
struct device *bus;
@@ -188,7 +102,7 @@ struct lis2dw12_data {
188102
/* save sensitivity */
189103
u16_t gain;
190104

191-
const struct lis2dw12_tf *hw_tf;
105+
lis2dw12_ctx_t *ctx;
192106
#ifdef CONFIG_LIS2DW12_TRIGGER
193107
struct device *gpio;
194108
struct gpio_callback gpio_cb;

0 commit comments

Comments
 (0)