-
Notifications
You must be signed in to change notification settings - Fork 5
/
st_lsm6dsl_sensor_v1.c
450 lines (389 loc) · 12.4 KB
/
st_lsm6dsl_sensor_v1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include "st_lsm6dsl_sensor_v1.h"
#define DBG_ENABLE
#define DBG_LEVEL DBG_INFO
#define DBG_SECTION_NAME "sensor.st.lsm6dsl"
#define DBG_COLOR
#include <rtdbg.h>
#define SENSOR_ACC_RANGE_2G 2000
#define SENSOR_ACC_RANGE_4G 4000
#define SENSOR_ACC_RANGE_8G 8000
#define SENSOR_ACC_RANGE_16G 16000
static LSM6DSL_Object_t lsm6dsl;
static struct rt_i2c_bus_device *i2c_bus_dev;
static int32_t rt_func_ok(void)
{
return 0;
}
static int32_t get_tick(void)
{
return rt_tick_get();
}
static int32_t rt_i2c_write_reg(uint16_t addr, uint16_t reg, uint8_t *data, uint16_t len)
{
rt_uint8_t tmp = reg;
struct rt_i2c_msg msgs[2];
msgs[0].addr = addr; /* Slave address */
msgs[0].flags = RT_I2C_WR; /* Write flag */
msgs[0].buf = &tmp; /* Slave register address */
msgs[0].len = 1; /* Number of bytes sent */
msgs[1].addr = addr; /* Slave address */
msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START; /* Read flag */
msgs[1].buf = data; /* Read data pointer */
msgs[1].len = len; /* Number of bytes read */
if (rt_i2c_transfer(i2c_bus_dev, msgs, 2) != 2)
{
return -RT_ERROR;
}
return RT_EOK;
}
static int32_t rt_i2c_read_reg(uint16_t addr, uint16_t reg, uint8_t *data, uint16_t len)
{
rt_uint8_t tmp = reg;
struct rt_i2c_msg msgs[2];
msgs[0].addr = addr; /* Slave address */
msgs[0].flags = RT_I2C_WR; /* Write flag */
msgs[0].buf = &tmp; /* Slave register address */
msgs[0].len = 1; /* Number of bytes sent */
msgs[1].addr = addr; /* Slave address */
msgs[1].flags = RT_I2C_RD; /* Read flag */
msgs[1].buf = data; /* Read data pointer */
msgs[1].len = len; /* Number of bytes read */
if (rt_i2c_transfer(i2c_bus_dev, msgs, 2) != 2)
{
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t _lsm6dsl_init(struct rt_sensor_intf *intf)
{
rt_uint8_t id, i2c_addr = (rt_uint32_t)(intf->user_data) & 0xff;
LSM6DSL_IO_t io_ctx;
i2c_bus_dev = (struct rt_i2c_bus_device *)rt_device_find(intf->dev_name);
if (i2c_bus_dev == RT_NULL)
{
return -RT_ERROR;
}
/* Configure the accelero driver */
io_ctx.BusType = LSM6DSL_I2C_BUS; /* I2C */
io_ctx.Address = i2c_addr;
io_ctx.Init = rt_func_ok;
io_ctx.DeInit = rt_func_ok;
io_ctx.ReadReg = rt_i2c_read_reg;
io_ctx.WriteReg = rt_i2c_write_reg;
io_ctx.GetTick = get_tick;
if (LSM6DSL_RegisterBusIO(&lsm6dsl, &io_ctx) != LSM6DSL_OK)
{
return -RT_ERROR;
}
else if (LSM6DSL_ReadID(&lsm6dsl, &id) != LSM6DSL_OK)
{
LOG_E("read id failed");
return -RT_ERROR;
}
if (LSM6DSL_Init(&lsm6dsl) != LSM6DSL_OK)
{
LOG_E("acc init failed");
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t _lsm6dsl_set_range(rt_sensor_t sensor, rt_int32_t range)
{
if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
{
LSM6DSL_ACC_SetFullScale(&lsm6dsl, range / 1000);
LOG_D("acce set range %d", range);
}
else if (sensor->info.type == RT_SENSOR_CLASS_GYRO)
{
LSM6DSL_GYRO_SetFullScale(&lsm6dsl, range);
LOG_D("gyro set range %d", range);
}
return RT_EOK;
}
static rt_err_t _lsm6dsl_set_odr(rt_sensor_t sensor, rt_uint16_t odr)
{
if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
{
LSM6DSL_ACC_SetOutputDataRate(&lsm6dsl, odr);
LOG_D("acce set odr %d", odr);
}
else if (sensor->info.type == RT_SENSOR_CLASS_GYRO)
{
LSM6DSL_GYRO_SetOutputDataRate(&lsm6dsl, odr);
LOG_D("gyro set odr %d", odr);
}
return RT_EOK;
}
static rt_err_t _lsm6dsl_acc_set_mode(rt_sensor_t sensor, rt_uint8_t mode)
{
if (mode == RT_SENSOR_MODE_POLLING)
{
LOG_D("set mode to POLLING");
}
else if (mode == RT_SENSOR_MODE_INT)
{
LOG_D("set mode to RT_SENSOR_MODE_INT");
}
else if (mode == RT_SENSOR_MODE_FIFO)
{
LSM6DSL_FIFO_Set_Mode(&lsm6dsl, LSM6DSL_FIFO_MODE);
LSM6DSL_FIFO_Set_INT1_FIFO_Full(&lsm6dsl, 1);
LOG_D("set mode to RT_SENSOR_MODE_FIFO");
}
else
{
LOG_D("Unsupported mode, code is %d", mode);
return -RT_ERROR;
}
return RT_EOK;
}
static rt_err_t _lsm6dsl_set_power(rt_sensor_t sensor, rt_uint8_t power)
{
if (power == RT_SENSOR_POWER_DOWN)
{
if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
{
LSM6DSL_ACC_Disable(&lsm6dsl);
}
else if (sensor->info.type == RT_SENSOR_CLASS_GYRO)
{
LSM6DSL_GYRO_Disable(&lsm6dsl);
}
else if (sensor->info.type == RT_SENSOR_CLASS_STEP)
{
LSM6DSL_ACC_Disable(&lsm6dsl);
LSM6DSL_ACC_Disable_Pedometer(&lsm6dsl);
}
LOG_D("set power down");
}
else if (power == RT_SENSOR_POWER_NORMAL)
{
lsm6dsl_xl_power_mode_set(&lsm6dsl.Ctx, LSM6DSL_XL_NORMAL);
if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
{
LSM6DSL_ACC_Enable(&lsm6dsl);
}
else if (sensor->info.type == RT_SENSOR_CLASS_GYRO)
{
LSM6DSL_GYRO_Enable(&lsm6dsl);
}
else if (sensor->info.type == RT_SENSOR_CLASS_STEP)
{
LSM6DSL_ACC_Enable(&lsm6dsl);
LSM6DSL_ACC_Enable_Pedometer(&lsm6dsl);
}
LOG_D("set power normal");
}
else if (power == RT_SENSOR_POWER_HIGH)
{
lsm6dsl_xl_power_mode_set(&lsm6dsl.Ctx, LSM6DSL_XL_HIGH_PERFORMANCE);
if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
{
LSM6DSL_ACC_Enable(&lsm6dsl);
}
else if (sensor->info.type == RT_SENSOR_CLASS_GYRO)
{
LSM6DSL_GYRO_Enable(&lsm6dsl);
}
LOG_D("set power high");
}
else
{
LOG_W("Unsupported mode, code is %d", power);
return -RT_ERROR;
}
return RT_EOK;
}
static rt_size_t _lsm6dsl_polling_get_data(rt_sensor_t sensor, struct rt_sensor_data *data)
{
if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
{
LSM6DSL_Axes_t acce;
LSM6DSL_ACC_GetAxes(&lsm6dsl, &acce);
data->type = RT_SENSOR_CLASS_ACCE;
data->data.acce.x = acce.x;
data->data.acce.y = acce.y;
data->data.acce.z = acce.z;
data->timestamp = rt_sensor_get_ts();
}
else if (sensor->info.type == RT_SENSOR_CLASS_GYRO)
{
LSM6DSL_Axes_t gyro;
LSM6DSL_GYRO_GetAxes(&lsm6dsl, &gyro);
data->type = RT_SENSOR_CLASS_GYRO;
data->data.gyro.x = gyro.x;
data->data.gyro.y = gyro.y;
data->data.gyro.z = gyro.z;
data->timestamp = rt_sensor_get_ts();
}
else if (sensor->info.type == RT_SENSOR_CLASS_STEP)
{
uint16_t step;
LSM6DSL_ACC_Get_Step_Count(&lsm6dsl, &step);
data->type = RT_SENSOR_CLASS_STEP;
data->data.step = step;
data->timestamp = rt_sensor_get_ts();
}
return 1;
}
static rt_size_t _lsm6dsl_acc_fifo_get_data(rt_sensor_t sensor, struct rt_sensor_data *data, rt_size_t len)
{
LSM6DSL_Axes_t acce;
rt_uint8_t i;
for (i = 0; i < len; i++)
{
if (LSM6DSL_ACC_GetAxes(&lsm6dsl, &acce) == 0)
{
data[i].type = RT_SENSOR_CLASS_ACCE;
data[i].data.acce.x = acce.x;
data[i].data.acce.y = acce.y;
data[i].data.acce.z = acce.z;
data[i].timestamp = rt_sensor_get_ts();
}
else
break;
}
return i;
}
static RT_SIZE_TYPE lsm6dsl_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
{
RT_ASSERT(buf);
if (sensor->config.mode == RT_SENSOR_MODE_POLLING)
{
return _lsm6dsl_polling_get_data(sensor, buf);
}
else if (sensor->config.mode == RT_SENSOR_MODE_INT)
{
return 0;
}
else if (sensor->config.mode == RT_SENSOR_MODE_FIFO)
{
return _lsm6dsl_acc_fifo_get_data(sensor, buf, len);
}
else
return 0;
}
static rt_err_t lsm6dsl_control(struct rt_sensor_device *sensor, int cmd, void *args)
{
rt_err_t result = RT_EOK;
switch (cmd)
{
case RT_SENSOR_CTRL_GET_ID:
LSM6DSL_ReadID(&lsm6dsl, args);
break;
case RT_SENSOR_CTRL_SET_RANGE:
result = _lsm6dsl_set_range(sensor, (rt_int32_t)args);
break;
case RT_SENSOR_CTRL_SET_ODR:
result = _lsm6dsl_set_odr(sensor, (rt_uint32_t)args & 0xffff);
break;
case RT_SENSOR_CTRL_SET_MODE:
result = _lsm6dsl_acc_set_mode(sensor, (rt_uint32_t)args & 0xff);
break;
case RT_SENSOR_CTRL_SET_POWER:
result = _lsm6dsl_set_power(sensor, (rt_uint32_t)args & 0xff);
break;
case RT_SENSOR_CTRL_SELF_TEST:
break;
default:
return -RT_ERROR;
}
return result;
}
static struct rt_sensor_ops sensor_ops =
{
lsm6dsl_fetch_data,
lsm6dsl_control
};
int rt_hw_lsm6dsl_init(const char *name, struct rt_sensor_config *cfg)
{
rt_int8_t result;
rt_sensor_t sensor_acce = RT_NULL, sensor_gyro = RT_NULL, sensor_step = RT_NULL;
#ifdef PKG_USING_LSM6DSL_ACCE
/* accelerometer sensor register */
{
sensor_acce = rt_calloc(1, sizeof(struct rt_sensor_device));
if (sensor_acce == RT_NULL)
return -1;
sensor_acce->info.type = RT_SENSOR_CLASS_ACCE;
sensor_acce->info.vendor = RT_SENSOR_VENDOR_STM;
sensor_acce->info.model = "lsm6dsl_acc";
sensor_acce->info.unit = RT_SENSOR_UNIT_MGAUSS;
sensor_acce->info.intf_type = RT_SENSOR_INTF_I2C;
sensor_acce->info.range_max = SENSOR_ACC_RANGE_16G;
sensor_acce->info.range_min = SENSOR_ACC_RANGE_2G;
sensor_acce->info.period_min = 5;
rt_memcpy(&sensor_acce->config, cfg, sizeof(struct rt_sensor_config));
sensor_acce->ops = &sensor_ops;
result = rt_hw_sensor_register(sensor_acce, name, RT_DEVICE_FLAG_RDWR, RT_NULL);
if (result != RT_EOK)
{
LOG_E("device register err code: %d", result);
goto __exit;
}
}
#endif
#ifdef PKG_USING_LSM6DSL_GYRO
/* gyroscope sensor register */
{
sensor_gyro = rt_calloc(1, sizeof(struct rt_sensor_device));
if (sensor_gyro == RT_NULL)
goto __exit;
sensor_gyro->info.type = RT_SENSOR_CLASS_GYRO;
sensor_gyro->info.vendor = RT_SENSOR_VENDOR_STM;
sensor_gyro->info.model = "lsm6dsl_gyro";
sensor_gyro->info.unit = RT_SENSOR_UNIT_MG;
sensor_gyro->info.intf_type = RT_SENSOR_INTF_I2C;
sensor_gyro->info.range_max = SENSOR_ACC_RANGE_16G;
sensor_gyro->info.range_min = SENSOR_ACC_RANGE_2G;
sensor_gyro->info.period_min = 5;
rt_memcpy(&sensor_gyro->config, cfg, sizeof(struct rt_sensor_config));
sensor_gyro->ops = &sensor_ops;
result = rt_hw_sensor_register(sensor_gyro, name, RT_DEVICE_FLAG_RDWR, RT_NULL);
if (result != RT_EOK)
{
LOG_E("device register err code: %d", result);
goto __exit;
}
}
#endif
#ifdef PKG_USING_LSM6DSL_STEP
/* step sensor register */
{
sensor_step = rt_calloc(1, sizeof(struct rt_sensor_device));
if (sensor_step == RT_NULL)
goto __exit;
sensor_step->info.type = RT_SENSOR_CLASS_STEP;
sensor_step->info.vendor = RT_SENSOR_VENDOR_STM;
sensor_step->info.model = "lsm6dsl_step";
sensor_step->info.unit = RT_SENSOR_UNIT_ONE;
sensor_step->info.intf_type = RT_SENSOR_INTF_I2C;
sensor_step->info.period_min = 5;
rt_memcpy(&sensor_step->config, cfg, sizeof(struct rt_sensor_config));
sensor_step->ops = &sensor_ops;
result = rt_hw_sensor_register(sensor_step, name, RT_DEVICE_FLAG_RDWR, RT_NULL);
if (result != RT_EOK)
{
LOG_E("device register err code: %d", result);
goto __exit;
}
}
#endif
result = _lsm6dsl_init(&cfg->intf);
if (result != RT_EOK)
{
LOG_E("_lsm6dsl init err code: %d", result);
goto __exit;
}
LOG_I("sensor init success");
return RT_EOK;
__exit:
if (sensor_acce)
rt_free(sensor_acce);
if (sensor_gyro)
rt_free(sensor_gyro);
if (sensor_step)
rt_free(sensor_step);
return -RT_ERROR;
}