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
|
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/**
* LIS2MDL magnetometer module for Chrome EC.
* This driver supports LIS2MDL magnetometer in cascade with LSM6DSx (x stands
* for L or M) accel/gyro module.
*/
#include "common.h"
#include "driver/mag_lis2mdl.h"
#include "driver/sensorhub_lsm6dsm.h"
#include "driver/accelgyro_lsm6dsm.h"
#include "driver/stm_mems_common.h"
#include "hwtimer.h"
#include "mag_cal.h"
#include "task.h"
#ifdef CONFIG_MAG_LSM6DSM_LIS2MDL
#ifndef CONFIG_SENSORHUB_LSM6DSM
#error "Need Sensor Hub LSM6DSM support"
#endif
#endif
#define CPRINTF(format, args...) cprintf(CC_ACCEL, format, ## args)
void lis2mdl_normalize(const struct motion_sensor_t *s,
intv3_t v,
uint8_t *raw)
{
struct mag_cal_t *cal = LIS2MDL_CAL(s);
int i;
#ifdef CONFIG_MAG_BMI160_LIS2MDL
struct lis2mdl_private_data *private = LIS2MDL_DATA(s);
intv3_t hn1;
hn1[X] = ((int16_t)((raw[1] << 8) | raw[0]));
hn1[Y] = ((int16_t)((raw[3] << 8) | raw[2]));
hn1[Z] = ((int16_t)((raw[5] << 8) | raw[4]));
/* Only when LIS2MDL is in forced mode */
if (private->hn_valid) {
for (i = X; i <= Z; i++)
v[i] = (hn1[i] + private->hn[i]) / 2;
memcpy(private->hn, hn1, sizeof(intv3_t));
} else {
private->hn_valid = 1;
memcpy(v, hn1, sizeof(intv3_t));
}
#else
v[X] = ((int16_t)((raw[1] << 8) | raw[0]));
v[Y] = ((int16_t)((raw[3] << 8) | raw[2]));
v[Z] = ((int16_t)((raw[5] << 8) | raw[4]));
#endif
for (i = X; i <= Z; i++)
v[i] = LIS2MDL_RATIO(v[i]);
mag_cal_update(cal, v);
v[X] += cal->bias[X];
v[Y] += cal->bias[Y];
v[Z] += cal->bias[Z];
}
static int set_range(const struct motion_sensor_t *s, int range, int rnd)
{
struct stprivate_data *data = s->drv_data;
/* Range is fixed by hardware */
if (range != s->default_range)
return EC_ERROR_INVAL;
data->base.range = range;
return EC_SUCCESS;
}
static int get_range(const struct motion_sensor_t *s)
{
struct stprivate_data *data = s->drv_data;
return data->base.range;
}
/**
* set_offset - Set data offset
* @s: Motion sensor pointer
* @offset: offset vector
* @temp: Temp
*/
static int set_offset(const struct motion_sensor_t *s,
const int16_t *offset, int16_t temp)
{
struct mag_cal_t *cal = LIS2MDL_CAL(s);
cal->bias[X] = offset[X];
cal->bias[Y] = offset[Y];
cal->bias[Z] = offset[Z];
rotate_inv(cal->bias, *s->rot_standard_ref, cal->bias);
return EC_SUCCESS;
}
/**
* get_offset - Get data offset
* @s: Motion sensor pointer
* @offset: offset vector
* @temp: Temp
*/
static int get_offset(const struct motion_sensor_t *s,
int16_t *offset, int16_t *temp)
{
struct mag_cal_t *cal = LIS2MDL_CAL(s);
intv3_t offset_int;
rotate(cal->bias, *s->rot_standard_ref, offset_int);
offset[X] = offset_int[X];
offset[Y] = offset_int[Y];
offset[Z] = offset_int[Z];
*temp = EC_MOTION_SENSE_INVALID_CALIB_TEMP;
return EC_SUCCESS;
}
#ifdef CONFIG_MAG_LSM6DSM_LIS2MDL
int lis2mdl_thru_lsm6dsm_read(const struct motion_sensor_t *s, intv3_t v)
{
int ret;
uint8_t raw[OUT_XYZ_SIZE];
/*
* This is mostly for debugging, read happens through LSM6DSM/BMI160
* FIFO.
*/
mutex_lock(s->mutex);
ret = sensorhub_slv0_data_read(LSM6DSM_MAIN_SENSOR(s), raw);
mutex_unlock(s->mutex);
lis2mdl_normalize(s, v, raw);
rotate(v, *s->rot_standard_ref, v);
return ret;
}
int lis2mdl_thru_lsm6dsm_init(const struct motion_sensor_t *s)
{
int ret = EC_ERROR_UNIMPLEMENTED;
struct mag_cal_t *cal = LIS2MDL_CAL(s);
struct stprivate_data *data = s->drv_data;
mutex_lock(s->mutex);
/* Magnetometer in cascade mode */
ret = sensorhub_check_and_rst(
LSM6DSM_MAIN_SENSOR(s),
CONFIG_ACCELGYRO_SEC_ADDR,
LIS2MDL_WHO_AM_I_REG, LIS2MDL_WHO_AM_I,
LIS2MDL_CFG_REG_A_ADDR, LIS2MDL_SW_RESET);
if (ret != EC_SUCCESS)
goto err_unlock;
ret = sensorhub_config_ext_reg(
LSM6DSM_MAIN_SENSOR(s),
CONFIG_ACCELGYRO_SEC_ADDR,
LIS2MDL_CFG_REG_A_ADDR,
LIS2MDL_ODR_50HZ | LIS2MDL_CONT_MODE);
if (ret != EC_SUCCESS)
goto err_unlock;
ret = sensorhub_config_slv0_read(
LSM6DSM_MAIN_SENSOR(s),
CONFIG_ACCELGYRO_SEC_ADDR,
LIS2MDL_OUT_REG, OUT_XYZ_SIZE);
if (ret != EC_SUCCESS)
goto err_unlock;
mutex_unlock(s->mutex);
init_mag_cal(cal);
cal->radius = 0.0f;
data->resol = LIS2DSL_RESOLUTION;
return sensor_init_done(s);
err_unlock:
mutex_unlock(s->mutex);
return ret;
}
#else /* END: CONFIG_MAG_LSM6DSM_LIS2MDL */
/**
* Checks whether or not data is ready. If the check succeeds EC_SUCCESS will be
* returned and the ready target written with the axes that are available, see:
* <ul>
* <li>LIS2MDL_X_DIRTY</li>
* <li>LIS2MDL_Y_DIRTY</li>
* <li>LIS2MDL_Z_DIRTY</li>
* <li>LIS2MDL_XYZ_DIRTY</li>
* </ul>
*
* @param s Motion sensor pointer
* @param[out] ready Writeback pointer to store the result.
* @return EC_SUCCESS when the status register was read successfully.
*/
static int lis2mdl_is_data_ready(const struct motion_sensor_t *s, int *ready)
{
int ret, tmp;
ret = st_raw_read8(s->port, s->addr, LIS2MDL_STATUS_REG, &tmp);
if (ret != EC_SUCCESS) {
*ready = 0;
return ret;
}
*ready = tmp & LIS2MDL_XYZ_DIRTY_MASK;
return EC_SUCCESS;
}
/**
* Read the most recent data from the sensor. If no new data is available,
* simply return the last available values.
*
* @param s Motion sensor pointer
* @param v A vector of 3 ints for x, y, z values.
* @return EC_SUCCESS when the values were read successfully or no new data was
* available.
*/
int lis2mdl_read(const struct motion_sensor_t *s, intv3_t v)
{
int ret = EC_SUCCESS, ready = 0;
uint8_t raw[OUT_XYZ_SIZE];
ret = lis2mdl_is_data_ready(s, &ready);
if (ret != EC_SUCCESS)
return ret;
/*
* If sensor data is not ready, return the previous read data.
* Note: return success so that the motion sensor task can read again to
* get the latest updated sensor data quickly.
*/
if (!ready) {
if (v != s->raw_xyz)
memcpy(v, s->raw_xyz, sizeof(intv3_t));
return ret;
}
mutex_lock(s->mutex);
ret = st_raw_read_n(s->port, s->addr, LIS2MDL_OUT_REG, raw,
OUT_XYZ_SIZE);
mutex_unlock(s->mutex);
if (ret == EC_SUCCESS) {
lis2mdl_normalize(s, v, raw);
rotate(v, *s->rot_standard_ref, v);
}
return ret;
}
/**
* Initialize the sensor. This function will verify the who-am-I register
*/
int lis2mdl_init(const struct motion_sensor_t *s)
{
int ret = EC_ERROR_UNKNOWN, who_am_i, count = LIS2MDL_STARTUP_MS;
struct mag_cal_t *cal = LIS2MDL_CAL(s);
/* Check who am I value */
do {
ret = st_raw_read8(s->port, LIS2MDL_ADDR, LIS2MDL_WHO_AM_I_REG,
&who_am_i);
if (ret != EC_SUCCESS) {
/* Make sure we wait for the chip to start up. Sleep 1ms
* and try again.
*/
udelay(10);
count--;
} else {
break;
}
} while (count > 0);
if (ret != EC_SUCCESS)
return ret;
if (who_am_i != LIS2MDL_WHO_AM_I)
return EC_ERROR_ACCESS_DENIED;
mutex_lock(s->mutex);
/* Reset the sensor */
ret = st_raw_write8(s->port, LIS2MDL_ADDR, LIS2MDL_CFG_REG_A_ADDR,
LIS2MDL_FLAG_SW_RESET);
if (ret != EC_SUCCESS)
goto lis2mdl_init_error;
mutex_unlock(s->mutex);
if (ret != EC_SUCCESS)
return ret;
init_mag_cal(cal);
cal->radius = 0.0f;
LIS2MDL_ST_DATA(s)->base.odr = 0;
return sensor_init_done(s);
lis2mdl_init_error:
mutex_unlock(s->mutex);
return ret;
}
/**
* Set the data rate of the sensor. Use a rate of 0 or below to turn off the
* magnetometer. All other values will turn on the sensor in continuous mode.
* The rate will be set to the nearest available value:
* <ul>
* <li>LIS2MDL_ODR_10HZ</li>
* <li>LIS2MDL_ODR_20HZ</li>
* <li>LIS2MDL_ODR_50HZ</li>
* </ul>
*
* @param s Motion sensor pointer
* @param rate Rate (mHz)
* @param rnd Flag used to tell whether or not to round up (1) or down (0)
* @return EC_SUCCESS when the rate was successfully changed.
*/
int lis2mdl_set_data_rate(const struct motion_sensor_t *s, int rate, int rnd)
{
int ret = EC_SUCCESS, normalized_rate = 0;
uint8_t reg_val = 0;
struct mag_cal_t *cal = LIS2MDL_CAL(s);
struct stprivate_data *data = LIS2MDL_ST_DATA(s);
if (rate > 0) {
if (rnd)
/* Round up */
reg_val = rate <= 10000 ? LIS2MDL_ODR_10HZ
: rate <= 20000 ? LIS2MDL_ODR_20HZ
: LIS2MDL_ODR_50HZ;
else
/* Round down */
reg_val = rate < 20000 ? LIS2MDL_ODR_10HZ
: rate < 50000 ? LIS2MDL_ODR_20HZ
: LIS2MDL_ODR_50HZ;
}
normalized_rate = rate <= 0 ? 0
: reg_val == LIS2MDL_ODR_10HZ ? 10000
: reg_val == LIS2MDL_ODR_20HZ ? 20000
: 50000;
/* b/130417518 - verify why skipping init_mag_cal is needed here. */
if (normalized_rate == data->base.odr)
return ret;
init_mag_cal(cal);
if (normalized_rate > 0)
cal->batch_size = MAX(
MAG_CAL_MIN_BATCH_SIZE,
(normalized_rate * 1000) /
MAG_CAL_MIN_BATCH_WINDOW_US);
else
cal->batch_size = 0;
mutex_lock(s->mutex);
if (rate <= 0) {
ret = st_raw_write8(s->port, LIS2MDL_ADDR,
LIS2MDL_CFG_REG_A_ADDR,
LIS2MDL_FLAG_SW_RESET);
} else {
/* Add continuous and temp compensation flags */
reg_val |= LIS2MDL_MODE_CONT | LIS2MDL_FLAG_TEMP_COMPENSATION;
ret = st_raw_write8(s->port, LIS2MDL_ADDR,
LIS2MDL_CFG_REG_A_ADDR, reg_val);
}
mutex_unlock(s->mutex);
if (ret == EC_SUCCESS)
data->base.odr = normalized_rate;
return ret;
}
int get_data_rate(const struct motion_sensor_t *s)
{
return LIS2MDL_ST_DATA(s)->base.odr;
}
#endif /* CONFIG_MAG_LIS2MDL */
const struct accelgyro_drv lis2mdl_drv = {
#ifdef CONFIG_MAG_LSM6DSM_LIS2MDL
.init = lis2mdl_thru_lsm6dsm_init,
.read = lis2mdl_thru_lsm6dsm_read,
.set_data_rate = lsm6dsm_set_data_rate,
.get_data_rate = st_get_data_rate,
#else /* CONFIG_MAG_LSM6DSM_LIS2MDL */
.init = lis2mdl_init,
.read = lis2mdl_read,
.set_data_rate = lis2mdl_set_data_rate,
.get_data_rate = get_data_rate,
#endif /* !CONFIG_MAG_LSM6DSM_LIS2MDL */
.set_range = set_range,
.get_range = get_range,
.get_resolution = st_get_resolution,
.set_offset = set_offset,
.get_offset = get_offset,
};
|