summaryrefslogtreecommitdiff
path: root/common/online_calibration.c
blob: 3bc56f85c7062fcf4de42518ecbf0cc5edb89e97 (plain)
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
/* Copyright 2020 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.
 */

#include "accelgyro.h"
#include "atomic.h"
#include "hwtimer.h"
#include "online_calibration.h"
#include "common.h"
#include "mag_cal.h"
#include "util.h"
#include "vec3.h"
#include "task.h"
#include "ec_commands.h"
#include "accel_cal.h"
#include "mkbp_event.h"
#include "gyro_cal.h"

#define CPRINTS(format, args...) cprints(CC_MOTION_SENSE, format, ##args)

#ifndef CONFIG_MKBP_EVENT
#error "Must use CONFIG_MKBP_EVENT for online calibration"
#endif /* CONFIG_MKBP_EVENT */

/** Bitmap telling which online calibration values are valid. */
static uint32_t sensor_calib_cache_valid_map;
/** Bitmap telling which online calibration values are dirty. */
static uint32_t sensor_calib_cache_dirty_map;

struct mutex g_calib_cache_mutex;

static int get_temperature(struct motion_sensor_t *sensor, int *temp)
{
	struct online_calib_data *entry = sensor->online_calib_data;
	uint32_t now;

	if (sensor->drv->read_temp == NULL)
		return EC_ERROR_UNIMPLEMENTED;

	now = __hw_clock_source_read();
	if (entry->last_temperature < 0 ||
	    time_until(entry->last_temperature_timestamp, now) >
		    CONFIG_TEMP_CACHE_STALE_THRES) {
		int t;
		int rc = sensor->drv->read_temp(sensor, &t);

		if (rc == EC_SUCCESS) {
			entry->last_temperature = t;
			entry->last_temperature_timestamp = now;
		} else {
			return rc;
		}
	}

	*temp = entry->last_temperature;
	return EC_SUCCESS;
}

static void data_int16_to_fp(const struct motion_sensor_t *s,
			     const int16_t *data, fpv3_t out)
{
	int i;
	fp_t range = INT_TO_FP(s->current_range);

	for (i = 0; i < 3; ++i) {
		fp_t v = INT_TO_FP((int32_t)data[i]);

		out[i] = fp_div(v, INT_TO_FP((data[i] >= 0) ? 0x7fff : 0x8000));
		out[i] = fp_mul(out[i], range);
		/* Check for overflow */
		out[i] = CLAMP(out[i], -range, range);
	}
}

static void data_fp_to_int16(const struct motion_sensor_t *s, const fpv3_t data,
			     int16_t *out)
{
	int i;
	fp_t range = INT_TO_FP(s->current_range);

	for (i = 0; i < 3; ++i) {
		int32_t iv;
		fp_t v = fp_div(data[i], range);

		v = fp_mul(v, INT_TO_FP(0x7fff));
		iv = FP_TO_INT(v);
		/* Check for overflow */
		out[i] = ec_motion_sensor_clamp_i16(iv);
	}
}

/**
 * Check a gyroscope for new bias. This function checks a given sensor (must be
 * a gyroscope) for new bias values. If found, it will update the appropriate
 * caches and notify the AP.
 *
 * @param sensor Pointer to the gyroscope sensor to check.
 */
static bool check_gyro_cal_new_bias(struct motion_sensor_t *sensor,
				    fpv3_t bias_out)
{
	struct online_calib_data *calib_data =
		(struct online_calib_data *)sensor->online_calib_data;
	struct gyro_cal_data *data =
		(struct gyro_cal_data *)calib_data->type_specific_data;
	int temp_out;
	uint32_t timestamp_out;

	/* Check that we have a new bias. */
	if (data == NULL || calib_data == NULL ||
	    !gyro_cal_new_bias_available(&data->gyro_cal))
		return false;

	/* Read the calibration values. */
	gyro_cal_get_bias(&data->gyro_cal, bias_out, &temp_out, &timestamp_out);
	return true;
}

static void set_gyro_cal_cache_values(struct motion_sensor_t *sensor,
				      fpv3_t bias)
{
	size_t sensor_num = sensor - motion_sensors;
	struct online_calib_data *calib_data =
		(struct online_calib_data *)sensor->online_calib_data;

	mutex_lock(&g_calib_cache_mutex);
	/* Convert result to the right scale and save to cache. */
	data_fp_to_int16(sensor, bias, calib_data->cache);
	/* Set valid and dirty. */
	sensor_calib_cache_valid_map |= BIT(sensor_num);
	sensor_calib_cache_dirty_map |= BIT(sensor_num);
	mutex_unlock(&g_calib_cache_mutex);
	/* Notify the AP. */
	mkbp_send_event(EC_MKBP_EVENT_ONLINE_CALIBRATION);
}

/**
 * Update the data stream (accel/mag) for a given sensor and data in all
 * gyroscopes that are interested.
 *
 * @param sensor Pointer to the sensor that generated the data.
 * @param data 3 floats/fixed point data points generated by the sensor.
 * @param timestamp The timestamp at which the data was generated.
 */
static void update_gyro_cal(struct motion_sensor_t *sensor, fpv3_t data,
			    uint32_t timestamp)
{
	int i;
	fpv3_t gyro_cal_data_out;

	/*
	 * Find gyroscopes, while we don't currently have instance where more
	 * than one are present in a board, this loop will work with any number
	 * of them.
	 */
	for (i = 0; i < SENSOR_COUNT; ++i) {
		struct motion_sensor_t *s = motion_sensors + i;
		struct gyro_cal_data *gyro_cal_data =
			(struct gyro_cal_data *)
				s->online_calib_data->type_specific_data;
		bool has_new_gyro_cal_bias = false;

		/*
		 * If we're not looking at a gyroscope OR if the calibration
		 * data is NULL, skip this sensor.
		 */
		if (s->type != MOTIONSENSE_TYPE_GYRO || gyro_cal_data == NULL)
			continue;

		/*
		 * Update the appropriate data stream (accel/mag) depending on
		 * which sensors the gyroscope is tracking.
		 */
		if (sensor->type == MOTIONSENSE_TYPE_ACCEL &&
		    gyro_cal_data->accel_sensor_id == sensor - motion_sensors) {
			gyro_cal_update_accel(&gyro_cal_data->gyro_cal,
					      timestamp, data[X], data[Y],
					      data[Z]);
			has_new_gyro_cal_bias =
				check_gyro_cal_new_bias(s, gyro_cal_data_out);
		} else if (sensor->type == MOTIONSENSE_TYPE_MAG &&
			   gyro_cal_data->mag_sensor_id ==
				   sensor - motion_sensors) {
			gyro_cal_update_mag(&gyro_cal_data->gyro_cal, timestamp,
					    data[X], data[Y], data[Z]);
			has_new_gyro_cal_bias =
				check_gyro_cal_new_bias(s, gyro_cal_data_out);
		}

		if (has_new_gyro_cal_bias)
			set_gyro_cal_cache_values(s, gyro_cal_data_out);
	}
}

void online_calibration_init(void)
{
	size_t i;

	for (i = 0; i < SENSOR_COUNT; i++) {
		struct motion_sensor_t *s = motion_sensors + i;
		void *type_specific_data = NULL;

		if (s->online_calib_data) {
			s->online_calib_data->last_temperature = -1;
			type_specific_data =
				s->online_calib_data->type_specific_data;
		}

		if (!type_specific_data)
			continue;

		switch (s->type) {
		case MOTIONSENSE_TYPE_ACCEL: {
			accel_cal_reset((struct accel_cal *)type_specific_data);
			break;
		}
		case MOTIONSENSE_TYPE_MAG: {
			init_mag_cal((struct mag_cal_t *)type_specific_data);
			break;
		}
		case MOTIONSENSE_TYPE_GYRO: {
			init_gyro_cal(
				&((struct gyro_cal_data *)type_specific_data)
					 ->gyro_cal);
			break;
		}
		default:
			break;
		}
	}
}

bool online_calibration_has_new_values(void)
{
	bool has_dirty;

	mutex_lock(&g_calib_cache_mutex);
	has_dirty = sensor_calib_cache_dirty_map != 0;
	mutex_unlock(&g_calib_cache_mutex);

	return has_dirty;
}

bool online_calibration_read(struct motion_sensor_t *sensor,
			     struct ec_response_online_calibration_data *out)
{
	int sensor_num = sensor - motion_sensors;
	bool has_valid;

	mutex_lock(&g_calib_cache_mutex);
	has_valid = (sensor_calib_cache_valid_map & BIT(sensor_num)) != 0;
	if (has_valid) {
		/* Update data in out */
		memcpy(out->data, sensor->online_calib_data->cache,
		       sizeof(out->data));
		/* Clear dirty bit */
		sensor_calib_cache_dirty_map &= ~(1 << sensor_num);
	}
	mutex_unlock(&g_calib_cache_mutex);

	return has_valid;
}

int online_calibration_process_data(struct ec_response_motion_sensor_data *data,
				    struct motion_sensor_t *sensor,
				    uint32_t timestamp)
{
	int sensor_num = sensor - motion_sensors;
	int rc;
	int temperature;
	struct online_calib_data *calib_data;
	fpv3_t fdata;
	bool is_spoofed = IS_ENABLED(CONFIG_ONLINE_CALIB_SPOOF_MODE) &&
			  (sensor->flags & MOTIONSENSE_FLAG_IN_SPOOF_MODE);
	bool has_new_calibration_values = false;

	/* Convert data to fp. */
	data_int16_to_fp(sensor, data->data, fdata);

	calib_data = sensor->online_calib_data;
	switch (sensor->type) {
	case MOTIONSENSE_TYPE_ACCEL: {
		struct accel_cal *cal =
			(struct accel_cal *)(calib_data->type_specific_data);

		if (is_spoofed) {
			/* Copy the data to the calibration result. */
			cal->bias[X] = fdata[X];
			cal->bias[Y] = fdata[Y];
			cal->bias[Z] = fdata[Z];
			has_new_calibration_values = true;
		} else {
			/* Possibly update the gyroscope calibration. */
			update_gyro_cal(sensor, fdata, timestamp);

			/*
			 * Temperature is required for accelerometer
			 * calibration.
			 */
			rc = get_temperature(sensor, &temperature);
			if (rc != EC_SUCCESS)
				return rc;

			has_new_calibration_values = accel_cal_accumulate(
				cal, timestamp, fdata[X], fdata[Y], fdata[Z],
				temperature);
		}

		if (has_new_calibration_values) {
			mutex_lock(&g_calib_cache_mutex);
			/* Convert result to the right scale. */
			data_fp_to_int16(sensor, cal->bias, calib_data->cache);
			/* Set valid and dirty. */
			sensor_calib_cache_valid_map |= BIT(sensor_num);
			sensor_calib_cache_dirty_map |= BIT(sensor_num);
			mutex_unlock(&g_calib_cache_mutex);
			/* Notify the AP. */
			mkbp_send_event(EC_MKBP_EVENT_ONLINE_CALIBRATION);
		}
		break;
	}
	case MOTIONSENSE_TYPE_MAG: {
		struct mag_cal_t *cal =
			(struct mag_cal_t *)(calib_data->type_specific_data);
		int idata[] = {
			(int)data->data[X],
			(int)data->data[Y],
			(int)data->data[Z],
		};

		if (is_spoofed) {
			/* Copy the data to the calibration result. */
			cal->bias[X] = INT_TO_FP(idata[X]);
			cal->bias[Y] = INT_TO_FP(idata[Y]);
			cal->bias[Z] = INT_TO_FP(idata[Z]);
			has_new_calibration_values = true;
		} else {
			/* Possibly update the gyroscope calibration. */
			update_gyro_cal(sensor, fdata, timestamp);

			has_new_calibration_values = mag_cal_update(cal, idata);
		}

		if (has_new_calibration_values) {
			mutex_lock(&g_calib_cache_mutex);
			/* Copy the values */
			calib_data->cache[X] = cal->bias[X];
			calib_data->cache[Y] = cal->bias[Y];
			calib_data->cache[Z] = cal->bias[Z];
			/* Set valid and dirty. */
			sensor_calib_cache_valid_map |= BIT(sensor_num);
			sensor_calib_cache_dirty_map |= BIT(sensor_num);
			mutex_unlock(&g_calib_cache_mutex);
			/* Notify the AP. */
			mkbp_send_event(EC_MKBP_EVENT_ONLINE_CALIBRATION);
		}
		break;
	}
	case MOTIONSENSE_TYPE_GYRO: {
		if (is_spoofed) {
			/*
			 * Gyroscope uses fdata to store the calibration
			 * result, so there's no need to copy anything.
			 */
			has_new_calibration_values = true;
		} else {
			struct gyro_cal_data *gyro_cal_data =
				(struct gyro_cal_data *)
					calib_data->type_specific_data;
			struct gyro_cal *gyro_cal = &gyro_cal_data->gyro_cal;

			/* Temperature is required for gyro calibration. */
			rc = get_temperature(sensor, &temperature);
			if (rc != EC_SUCCESS)
				return rc;

			/* Update gyroscope calibration. */
			gyro_cal_update_gyro(gyro_cal, timestamp, fdata[X],
					     fdata[Y], fdata[Z], temperature);
			has_new_calibration_values =
				check_gyro_cal_new_bias(sensor, fdata);
		}

		if (has_new_calibration_values)
			set_gyro_cal_cache_values(sensor, fdata);
		break;
	}
	default:
		break;
	}

	return EC_SUCCESS;
}