summaryrefslogtreecommitdiff
path: root/test/motion_lid.c
blob: d734959fbed3faf5ecb16722e3677da82a216e51 (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
395
396
397
398
399
400
401
402
403
404
405
406
/* Copyright 2014 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test motion sense code.
 */

#include "accelgyro.h"
#include "common.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "motion_lid.h"
#include "motion_sense.h"
#include "tablet_mode.h"
#include "task.h"
#include "test_util.h"
#include "timer.h"
#include "util.h"

#include <math.h>
#include <stdio.h>

extern enum chipset_state_mask sensor_active;
extern int wait_us;

/*
 * Period in us for the motion task period.
 * The task will read the vectors at that interval
 */
#define TEST_LID_EC_RATE (10 * MSEC)

/*
 * Time in us to wait for the task to read the vectors.
 */
#define TEST_LID_SLEEP_RATE (TEST_LID_EC_RATE / 5)
#define ONE_G_MEASURED (1 << 14)

/*****************************************************************************/
/* Mock functions */
static int accel_init(struct motion_sensor_t *s)
{
	return EC_SUCCESS;
}

static int accel_read(const struct motion_sensor_t *s, intv3_t v)
{
	rotate(s->xyz, *s->rot_standard_ref, v);
	return EC_SUCCESS;
}

static int accel_set_range(struct motion_sensor_t *s, const int range,
			   const int rnd)
{
	s->current_range = range;
	return EC_SUCCESS;
}

static int accel_get_resolution(const struct motion_sensor_t *s)
{
	return 0;
}

int test_data_rate[2] = { 0 };

static int accel_set_data_rate(const struct motion_sensor_t *s, const int rate,
			       const int rnd)
{
	test_data_rate[s - motion_sensors] = rate;
	return EC_SUCCESS;
}

static int accel_get_data_rate(const struct motion_sensor_t *s)
{
	return test_data_rate[s - motion_sensors];
}

const struct accelgyro_drv test_motion_sense = {
	.init = accel_init,
	.read = accel_read,
	.set_range = accel_set_range,
	.get_resolution = accel_get_resolution,
	.set_data_rate = accel_set_data_rate,
	.get_data_rate = accel_get_data_rate,
};

struct motion_sensor_t motion_sensors[] = {
	[BASE] = {
		.name = "base",
		.active_mask = SENSOR_ACTIVE_S0_S3_S5,
		.chip = MOTIONSENSE_CHIP_LSM6DS0,
		.type = MOTIONSENSE_TYPE_ACCEL,
		.location = MOTIONSENSE_LOC_BASE,
		.drv = &test_motion_sense,
		.rot_standard_ref = NULL,
		.default_range = MOTION_SCALING_FACTOR / ONE_G_MEASURED,
		.config = {
			/* EC use accel for angle detection */
			[SENSOR_CONFIG_EC_S0] = {
				.odr = 119000 | ROUND_UP_FLAG,
				.ec_rate = TEST_LID_EC_RATE
			},
			/* Used for double tap */
			[SENSOR_CONFIG_EC_S3] = {
				.odr = 119000 | ROUND_UP_FLAG,
				.ec_rate = TEST_LID_EC_RATE * 100,
			},
		},
	},
	[LID] = {
		.name = "lid",
		.active_mask = SENSOR_ACTIVE_S0_S3,
		.chip = MOTIONSENSE_CHIP_KXCJ9,
		.type = MOTIONSENSE_TYPE_ACCEL,
		.location = MOTIONSENSE_LOC_LID,
		.drv = &test_motion_sense,
		.rot_standard_ref = NULL,
		.default_range = MOTION_SCALING_FACTOR / ONE_G_MEASURED,
		.config = {
			/* EC use accel for angle detection */
			[SENSOR_CONFIG_EC_S0] = {
				.odr = 119000 | ROUND_UP_FLAG,
				.ec_rate = TEST_LID_EC_RATE,
			},
			/* Used for double tap */
			[SENSOR_CONFIG_EC_S3] = {
				.odr = 200000 | ROUND_UP_FLAG,
				.ec_rate = TEST_LID_EC_RATE * 100,
			},
		},
	},
};
const unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors);

/*****************************************************************************/
/* Test utilities */
static void wait_for_valid_sample(void)
{
	uint8_t *lpc_status = host_get_memmap(EC_MEMMAP_ACC_STATUS);
	uint8_t sample;
	int i;

	for (i = 0; i < 2 * TABLET_MODE_DEBOUNCE_COUNT; i++) {
		sample = *lpc_status & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK;
		usleep(TEST_LID_EC_RATE);
		while ((*lpc_status & EC_MEMMAP_ACC_STATUS_SAMPLE_ID_MASK) ==
		       sample)
			usleep(TEST_LID_SLEEP_RATE);
	}
}

static int tablet_hook_count;

static void tablet_mode_change_hook(void)
{
	tablet_hook_count++;
}
DECLARE_HOOK(HOOK_TABLET_MODE_CHANGE, tablet_mode_change_hook,
	     HOOK_PRIO_DEFAULT);

void before_test(void)
{
	/* Make sure the device lid is in a consistent state (close). */
	gpio_set_level(GPIO_TABLET_MODE_L, 1);
	msleep(50);
	gpio_set_level(GPIO_LID_OPEN, 0);
	msleep(50);
	tablet_hook_count = 1;
}

/*
 * The device lid is closed from before_test,
 * Initialize the EC, set the sensors to match the lid angle (0 degree)
 * and go through several lid angles.
 * When lid angle are close to 0 or 360, activate the GMRs GPIO or not
 * and observe their on lid angle data quality and the tablet mode state.
 */
static int test_lid_angle(void)
{
	struct motion_sensor_t *base =
		&motion_sensors[CONFIG_LID_ANGLE_SENSOR_BASE];
	struct motion_sensor_t *lid =
		&motion_sensors[CONFIG_LID_ANGLE_SENSOR_LID];
	int lid_angle;

	/* We don't have TASK_CHIP so simulate init ourselves */
	hook_notify(HOOK_CHIPSET_SHUTDOWN);
	/* Wait for the sensor task to start */
	msleep(50);
	TEST_ASSERT(sensor_active == SENSOR_ACTIVE_S5);
	TEST_ASSERT(accel_get_data_rate(lid) == 0);
	TEST_ASSERT(base->collection_rate == 0);
	TEST_ASSERT(lid->collection_rate == 0);
	TEST_ASSERT(wait_us == -1);

	/* Go to S0 state */
	hook_notify(HOOK_CHIPSET_SUSPEND);
	hook_notify(HOOK_CHIPSET_RESUME);
	msleep(50);
	TEST_ASSERT(sensor_active == SENSOR_ACTIVE_S0);
	TEST_ASSERT(accel_get_data_rate(lid) == 119000);
	TEST_ASSERT(base->collection_rate != 0);
	TEST_ASSERT(lid->collection_rate != 0);
	TEST_ASSERT(wait_us > 0);

	/* Check we are in clamshell mode initially. */
	TEST_ASSERT(tablet_hook_count == 1);
	TEST_ASSERT(!tablet_get_mode());

	/*
	 * Set the base accelerometer as if it were sitting flat on a desk
	 * and set the lid to closed.
	 */
	base->xyz[X] = 0;
	base->xyz[Y] = 0;
	base->xyz[Z] = ONE_G_MEASURED;
	lid->xyz[X] = 0;
	lid->xyz[Y] = 0;
	lid->xyz[Z] = -ONE_G_MEASURED;

	/* Check we are still in clamshell mode, no event */
	TEST_ASSERT(tablet_hook_count == 1);
	TEST_ASSERT(!tablet_get_mode());

	wait_for_valid_sample();
	lid_angle = motion_lid_get_angle();
	cprints(CC_ACCEL, "LID(%d, %d, %d)/BASE(%d, %d, %d): %d", lid->xyz[X],
		lid->xyz[Y], lid->xyz[Z], base->xyz[X], base->xyz[Y],
		base->xyz[Z], lid_angle);
	TEST_ASSERT(lid_angle == 0);

	/* Set lid open to 90 degrees. */
	lid->xyz[X] = 0;
	lid->xyz[Y] = ONE_G_MEASURED;
	lid->xyz[Z] = 0;
	gpio_set_level(GPIO_LID_OPEN, 1);
	msleep(100);
	wait_for_valid_sample();

	TEST_ASSERT(motion_lid_get_angle() == 90);
	TEST_ASSERT(tablet_hook_count == 1);
	TEST_ASSERT(!tablet_get_mode());

	/* Set lid open to 225. */
	lid->xyz[X] = 0;
	lid->xyz[Y] = -1 * ONE_G_MEASURED * 0.707106;
	lid->xyz[Z] = ONE_G_MEASURED * 0.707106;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == 225);

	/* We are now in tablet mode */
	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/* Set lid open to 350 */
	lid->xyz[X] = 0;
	lid->xyz[Y] = -1 * ONE_G_MEASURED * 0.1736;
	lid->xyz[Z] = -1 * ONE_G_MEASURED * 0.9848;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == 350);

	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/* Assert tablet GMT sensor, no change. */
	gpio_set_level(GPIO_TABLET_MODE_L, 0);
	msleep(50);
	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/*
	 * Set lid open to 10.  Since the lid switch still indicates that it's
	 * open, we should be getting an unreliable reading.
	 * We are still in tablet mode.
	 */
	gpio_set_level(GPIO_TABLET_MODE_L, 1);
	lid->xyz[X] = 0;
	lid->xyz[Y] = ONE_G_MEASURED * 0.1736;
	lid->xyz[Z] = -1 * ONE_G_MEASURED * 0.9848;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == LID_ANGLE_UNRELIABLE);
	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/* Rotate back to 180 and then 10 */
	lid->xyz[X] = 0;
	lid->xyz[Y] = 0;
	lid->xyz[Z] = ONE_G_MEASURED;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == 180);
	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/*
	 * Again, since the lid isn't closed, the angle should be unreliable.
	 * See SMALL_LID_ANGLE_RANGE.
	 */
	lid->xyz[X] = 0;
	lid->xyz[Y] = ONE_G_MEASURED * 0.1736;
	lid->xyz[Z] = -1 * ONE_G_MEASURED * 0.9848;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == LID_ANGLE_UNRELIABLE);
	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/*
	 * Align base with hinge and make sure it returns unreliable for angle.
	 * In this test it doesn't matter what the lid acceleration vector is.
	 */
	base->xyz[X] = ONE_G_MEASURED;
	base->xyz[Y] = 0;
	base->xyz[Z] = 0;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == LID_ANGLE_UNRELIABLE);
	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/*
	 * Use all three axes and set lid to negative base and make sure
	 * angle is 180.
	 */
	base->xyz[X] = 5296;
	base->xyz[Y] = 7856;
	base->xyz[Z] = 13712;
	lid->xyz[X] = 5296;
	lid->xyz[Y] = 7856;
	lid->xyz[Z] = 13712;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == 180);
	/* Still in tablet mode */
	TEST_ASSERT(tablet_hook_count == 2);
	TEST_ASSERT(tablet_get_mode());

	/*
	 * Close the lid and set the angle to 0.
	 */
	base->xyz[X] = 0;
	base->xyz[Y] = 0;
	base->xyz[Z] = ONE_G_MEASURED;
	lid->xyz[X] = 0;
	lid->xyz[Y] = 0;
	lid->xyz[Z] = -1 * ONE_G_MEASURED;
	gpio_set_level(GPIO_LID_OPEN, 0);
	msleep(100);
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == 0);
	TEST_ASSERT(tablet_hook_count == 3);
	TEST_ASSERT(!tablet_get_mode());

	/*
	 * Make the angle large, but since the lid is closed, the angle should
	 * be regarded as unreliable.
	 */
	lid->xyz[X] = 0;
	lid->xyz[Y] = -1 * ONE_G_MEASURED * 0.1736;
	lid->xyz[Z] = -1 * ONE_G_MEASURED * 0.9848;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == LID_ANGLE_UNRELIABLE);
	TEST_ASSERT(tablet_hook_count == 3);
	TEST_ASSERT(!tablet_get_mode());

	/* Open the lid, the large angle is now valid */
	gpio_set_level(GPIO_LID_OPEN, 1);
	msleep(100);
	TEST_ASSERT(motion_lid_get_angle() == 350);
	TEST_ASSERT(tablet_hook_count == 4);
	TEST_ASSERT(tablet_get_mode());

	/*
	 * Close the lid and set the angle
	 * to 10. The reading of small angle shouldn't be corrected.
	 */
	msleep(100);
	gpio_set_level(GPIO_LID_OPEN, 0);
	msleep(100);
	lid->xyz[X] = 0;
	lid->xyz[Y] = ONE_G_MEASURED * 0.1736;
	lid->xyz[Z] = -1 * ONE_G_MEASURED * 0.9848;
	wait_for_valid_sample();
	TEST_ASSERT(motion_lid_get_angle() == 10);
	TEST_ASSERT(tablet_hook_count == 5);
	TEST_ASSERT(!tablet_get_mode());

	/* Shutdown in place, same mode. */
	hook_notify(HOOK_CHIPSET_SHUTDOWN);
	msleep(1000);
	TEST_ASSERT(sensor_active == SENSOR_ACTIVE_S5);
	/* Base ODR is 0, collection rate is 0. */
	TEST_ASSERT(base->collection_rate == 0);
	/* Lid is powered off, collection rate is 0. */
	TEST_ASSERT(lid->collection_rate == 0);
	TEST_ASSERT(wait_us == -1);
	TEST_ASSERT(tablet_hook_count == 5);
	TEST_ASSERT(!tablet_get_mode());

	return EC_SUCCESS;
}

void run_test(int argc, const char **argv)
{
	test_reset();

	RUN_TEST(test_lid_angle);

	test_print_result();
}