summaryrefslogtreecommitdiff
path: root/include/motion_sense.h
blob: 4991fcaba400821755b04eadf873137842e9a741 (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
/* Copyright (c) 2014 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.
 */

/* Header for motion_sense.c */

#ifndef __CROS_EC_MOTION_SENSE_H
#define __CROS_EC_MOTION_SENSE_H

#include "gpio.h"
#include "math_util.h"

/* Anything outside of lid angle range [-180, 180] should work. */
#define LID_ANGLE_UNRELIABLE 500.0F

/**
 * This structure defines all of the data needed to specify the orientation
 * of the base and lid accelerometers in order to calculate the lid angle.
 */
struct accel_orientation {
	/*
	 * Rotation matrix to rotate the lid sensor into the same reference
	 * frame as the base sensor.
	 */
	matrix_3x3_t rot_align;

	/* Rotation matrix to rotate positive 90 degrees around the hinge. */
	matrix_3x3_t rot_hinge_90;

	/*
	 * Rotation matrix to rotate 180 degrees around the hinge. The value
	 * here should be rot_hinge_90 ^ 2.
	 */
	matrix_3x3_t rot_hinge_180;

	/*
	 * Rotation matrix to rotate base sensor into the standard reference
	 * frame.
	 */
	matrix_3x3_t rot_standard_ref;

	/* Vector pointing along hinge axis. */
	vector_3_t hinge_axis;
};

/* Link global structure for orientation. This must be defined in board.c. */
extern
#ifndef CONFIG_ACCEL_CALIBRATE
const
#endif
struct accel_orientation acc_orient;


/**
 * Get last calculated lid angle. Note, the lid angle calculated by the EC
 * is un-calibrated and is an approximate angle.
 *
 * @return lid angle in degrees in range [0, 360].
 */
int motion_get_lid_angle(void);


#ifdef CONFIG_ACCEL_CALIBRATE
/**
 * Get the last measured lid acceleration vector.
 *
 * @param v Pointer to location to store vector.
 * @param adjusted If false use the raw vector, if true use the adjusted vector.
 */
void motion_get_accel_lid(vector_3_t *v, int adjusted);

/**
 * Get the last measured base acceleration vector.
 *
 * @param v Pointer to location to store vector.
 */
void motion_get_accel_base(vector_3_t *v);
#endif

/**
 * Interrupt function for lid accelerometer.
 *
 * @param signal GPIO signal that caused interrupt
 */
void accel_int_lid(enum gpio_signal signal);

/**
 * Interrupt function for base accelerometer.
 *
 * @param signal GPIO signal that caused interrupt
 */
void accel_int_base(enum gpio_signal signal);

enum sensor_location_t {
	LOCATION_BASE = 0,
	LOCATION_LID  = 1,
};

enum sensor_type_t {
	SENSOR_ACCELEROMETER = 0x1,
	SENSOR_GYRO          = 0x2,
};

enum sensor_chip_t {
	SENSOR_CHIP_KXCJ9 = 0,
	SENSOR_CHIP_LSM6DS0 = 1,
};

enum sensor_state {
	SENSOR_NOT_INITIALIZED = 0,
	SENSOR_INITIALIZED = 1,
	SENSOR_INIT_ERROR = 2
};

enum sensor_power {
	SENSOR_POWER_OFF = 0,
	SENSOR_POWER_ON  = 1
};

struct motion_sensor_t {
	/* RO fields */
	char *name;
	enum sensor_chip_t chip;
	enum sensor_type_t type;
	enum sensor_location_t location;
	const struct accelgyro_drv *drv;
	struct mutex *mutex;
	void *drv_data;
	uint8_t i2c_addr;

	/* RW fields */
	enum sensor_state state;
	enum sensor_power power;
	vector_3_t raw_xyz;
	vector_3_t xyz;
};

/* Defined at board level. */
extern struct motion_sensor_t motion_sensors[];
extern const unsigned int motion_sensor_count;

#endif /* __CROS_EC_MOTION_SENSE_H */