altos: Integrate BMX160 into data code
[fw/altos] / src / drivers / ao_bmx160.h
1 /*
2  * Copyright © 2019 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #ifndef _AO_BMX160_H_
20 #define _AO_BMX160_H_
21
22 #include <math.h>
23
24 struct ao_bmx160_sample {
25         int16_t         mag_x;
26         int16_t         mag_y;
27         int16_t         mag_z;
28         int16_t         rhall;
29         int16_t         gyr_x;
30         int16_t         gyr_y;
31         int16_t         gyr_z;
32         int16_t         acc_x;
33         int16_t         acc_y;
34         int16_t         acc_z;
35 };
36
37 extern struct ao_bmx160_sample  ao_bmx160_current;
38
39 struct ao_bmx160_offset {
40         int8_t          off_acc_x;
41         int8_t          off_acc_y;
42         int8_t          off_acc_z;
43         int8_t          off_gyr_x;
44         int8_t          off_gyr_y;
45         int8_t          off_gyr_z;
46         uint8_t         offset_6;
47 };
48
49 void
50 ao_bmx160_init(void);
51
52 #define BMX160_CHIPID                   0x00
53 #define  BMX160_CHIPID_BMX160                   0xd8
54 #define BMX160_ERR_REG                  0x02
55 #define BMX160_PMU_STATUS               0x03
56 #define  BMX160_PMU_STATUS_MAG_IF_PMU_STATUS    0
57 #define   BMX160_PMU_STATUS_MAG_IF_PMU_STATUS_SUSPEND           0
58 #define   BMX160_PMU_STATUS_MAG_IF_PMU_STATUS_NORMAL            1
59 #define   BMX160_PMU_STATUS_MAG_IF_PMU_STATUS_LOW_POWER         2
60 #define  BMX160_PMU_STATUS_GYR_PMU_STATUS       2
61 #define   BMX160_PMU_STATUS_GYR_PMU_STATUS_SUSPEND              0
62 #define   BMX160_PMU_STATUS_GYR_PMU_STATUS_NORMAL               1
63 #define   BMX160_PMU_STATUS_GYR_PMU_STATUS_FAST_START_UP        3
64 #define  BMX160_PMU_STATUS_ACC_PMU_STATUS       4
65 #define   BMX160_PMU_STATUS_ACC_PMU_STATUS_SUSPEND              0
66 #define   BMX160_PMU_STATUS_ACC_PMU_STATUS_NORMAL               1
67 #define   BMX160_PMU_STATUS_ACC_PMU_STATUS_LOW_POWER            2
68 #define BMX160_DATA_0                   0x04
69 #define BMX160_MAG_X_0_7                0x04
70 #define BMX160_MAG_X_8_15               0x05
71 #define BMX160_MAG_Y_0_7                0x06
72 #define BMX160_MAG_Y_8_15               0x07
73 #define BMX160_MAG_Z_0_7                0x08
74 #define BMX160_MAG_Z_8_15               0x09
75 #define BMX160_RHALL_0_7                0x0A
76 #define BMX160_RHALL_8_15               0x0B
77 #define BMX160_GYRO_X_0_7               0x0C
78 #define BMX160_GYRO_X_8_15              0x0D
79 #define BMX160_GYRO_Y_0_7               0x0E
80 #define BMX160_GYRO_Y_8_15              0x0F
81 #define BMX160_GYRO_Z_0_7               0x10
82 #define BMX160_GYRO_Z_8_15              0x11
83 #define BMX160_ACCEL_X_0_7              0x12
84 #define BMX160_ACCEL_X_8_15             0x13
85 #define BMX160_ACCEL_Y_0_7              0x14
86 #define BMX160_ACCEL_Y_8_15             0x15
87 #define BMX160_ACCEL_Z_0_7              0x16
88 #define BMX160_ACCEL_Z_8_15             0x17
89 #define BMX160_SENSORTIME_0_7           0x18
90 #define BMX160_SENSORTIME_8_15          0x19
91 #define BMX160_SENSORTIME_16_23         0x1A
92 #define BMX160_STATUS                   0x1B
93 #define  BMX160_STATUS_GYR_SELF_TEST_OK         1
94 #define  BMX160_STATUS_MAG_MAN_OP               2
95 #define  BMX160_STATUS_FOC_RDY                  3
96 #define  BMX160_STATUS_NVM_RDY                  4
97 #define  BMX160_STATUS_DRDY_MAG                 5
98 #define  BMX160_STATUS_DRDY_GYR                 6
99 #define  BMX160_STATUS_DRDY_ACC                 7
100 #define BMX160_INT_STATUS_0             0x1C-0x1F
101 #define BMX160_INT_STATUS_1             0x1D
102 #define BMX160_INT_STATUS_2             0x1E
103 #define BMX160_INT_STATUS_3             0x1F
104 #define BMX160_TEMPERATURE_0_7          0x20
105 #define BMX160_TEMPERATURE_8_15         0x21
106 #define BMX160_FIFO_LENGTH_0_7          0x22
107 #define BMX160_FIFO_LENGTH_8_15         0x23
108 #define BMX160_FIFO_DATA                0x24
109 #define BMX160_ACC_CONF                 0x40
110 #define  BMX160_ACC_CONF_ACC_ODR                0
111 #define   BMX160_ACC_CONF_ACC_ODR_25_32                 0x1
112 #define   BMX160_ACC_CONF_ACC_ODR_25_16                 0x2
113 #define   BMX160_ACC_CONF_ACC_ODR_25_8                  0x3
114 #define   BMX160_ACC_CONF_ACC_ODR_25_4                  0x4
115 #define   BMX160_ACC_CONF_ACC_ODR_25_2                  0x5
116 #define   BMX160_ACC_CONF_ACC_ODR_25                    0x6
117 #define   BMX160_ACC_CONF_ACC_ODR_50                    0x7
118 #define   BMX160_ACC_CONF_ACC_ODR_100                   0x8
119 #define   BMX160_ACC_CONF_ACC_ODR_200                   0x9
120 #define   BMX160_ACC_CONF_ACC_ODR_400                   0xa
121 #define   BMX160_ACC_CONF_ACC_ODR_800                   0xb
122 #define   BMX160_ACC_CONF_ACC_ODR_1600                  0xc
123 #define  BMX160_ACC_CONF_ACC_BWP                4
124 #define   BMX160_ACC_CONF_ACC_BWP_NORMAL                        0x2
125 #define   BMX160_ACC_CONF_ACC_BWP_OSR2                          0x1
126 #define   BMX160_ACC_CONF_ACC_BWP_OSR4                          0x0
127 #define  BMX160_ACC_CONF_ACC_US                 7
128 #define BMX160_ACC_RANGE                0x41
129 #define  BMX160_ACC_RANGE_2G                    0x3
130 #define  BMX160_ACC_RANGE_4G                    0x5
131 #define  BMX160_ACC_RANGE_8G                    0x8
132 #define  BMX160_ACC_RANGE_16G                   0xc
133 #define  BMX160_ACC_RANGE_
134 #define  BMX160_ACC_RANGE_
135 #define BMX160_GYR_CONF                 0x42
136 #define  BMX160_GYR_CONF_GYR_ODR                0
137 #define   BMX160_GYR_CONF_GYR_ODR_25                    0x6
138 #define   BMX160_GYR_CONF_GYR_ODR_50                    0x7
139 #define   BMX160_GYR_CONF_GYR_ODR_100                   0x8
140 #define   BMX160_GYR_CONF_GYR_ODR_200                   0x9
141 #define   BMX160_GYR_CONF_GYR_ODR_400                   0xa
142 #define   BMX160_GYR_CONF_GYR_ODR_800                   0xb
143 #define   BMX160_GYR_CONF_GYR_ODR_1600                  0xc
144 #define   BMX160_GYR_CONF_GYR_ODR_3200                  0xd
145 #define  BMX160_GYR_CONF_GYR_BWP                4
146 #define   BMX160_GYR_CONF_GYR_BWP_NORMAL                        0x2
147 #define   BMX160_GYR_CONF_GYR_BWP_OSR2                          0x1
148 #define   BMX160_GYR_CONF_GYR_BWP_OSR4                          0x0
149 #define BMX160_GYR_RANGE                0x43
150 #define  BMX160_GYR_RANGE_2000                                  0x0
151 #define  BMX160_GYR_RANGE_1000                                  0x1
152 #define  BMX160_GYR_RANGE_500                                   0x2
153 #define  BMX160_GYR_RANGE_250                                   0x3
154 #define  BMX160_GYR_RANGE_125                                   0x4
155 #define BMX160_MAG_CONF                 0x44
156 #define  BMX160_MAG_CONF_MAG_ODR                        0
157 #define   BMX160_MAG_CONF_MAG_ODR_25_32                         0x1
158 #define   BMX160_MAG_CONF_MAG_ODR_25_16                         0x2
159 #define   BMX160_MAG_CONF_MAG_ODR_25_8                          0x3
160 #define   BMX160_MAG_CONF_MAG_ODR_25_4                          0x4
161 #define   BMX160_MAG_CONF_MAG_ODR_25_2                          0x5
162 #define   BMX160_MAG_CONF_MAG_ODR_25                            0x6
163 #define   BMX160_MAG_CONF_MAG_ODR_50                            0x7
164 #define   BMX160_MAG_CONF_MAG_ODR_100                           0x8
165 #define   BMX160_MAG_CONF_MAG_ODR_200                           0x9
166 #define   BMX160_MAG_CONF_MAG_ODR_400                           0xa
167 #define   BMX160_MAG_CONF_MAG_ODR_800                           0xb
168 #define BMX160_FIFO_DOWNS               0x45
169 #define BMX160_FIFO_CONFIG_0            0x46
170 #define BMX160_FIFO_CONFIG_1            0x47
171 #define BMX160_MAG_IF_0                 0x4C
172 #define  BMX160_MAG_IF_0_MAG_RD_BURST           0
173 #define  BMX160_MAG_IF_0_MAG_OFFSET             2
174 #define  BMX160_MAG_IF_0_MAG_MANUAL_EN          7
175 #define BMX160_MAG_IF_1                 0x4D
176 #define BMX160_MAG_IF_2                 0x4E
177 #define BMX160_MAG_IF_3                 0x4F
178 #define BMX160_INT_EN                   0x50-0x52
179 #define BMX160_INT_OUT_CTRL             0x53
180 #define BMX160_INT_LATCH                0x54
181 #define BMX160_INT_MAP                  0x55-0x57
182 #define BMX160_INT_DATA                 0x58-0x59
183 #define BMX160_INT_LOWHIGH              0x5A-0x5E
184 #define BMX160_INT_MOTION               0x5F-0x62
185 #define BMX160_INT_TAP                  0x63-0x64
186 #define BMX160_INT_ORIENT               0x65-0x66
187 #define BMX160_INT_FLAT                 0x67-0x68
188 #define BMX160_FOC_CONF                 0x69
189 #define BMX160_CONF                     0x6A
190 #define BMX160_IF_CONF                  0x6B
191 #define BMX160_PMU_TRIGGER              0x6C
192 #define BMX160_SELF_TEST                0x6D
193 #define BMX160_NV_CONF                  0x70
194 #define  BMX160_NV_CONF_SPI_EN                  0
195 #define  BMX160_NV_CONF_I2C_WDT_SEL             1
196 #define  BMX160_NV_CONF_I2C_WDT_EN              2
197 #define BMX160_OFFSET                   0x71-0x77
198 #define BMX160_STEP_CNT                 0x78-0x79
199 #define BMX160_STEP_CONF                0x7A-0x7B
200 #define BMX160_CMD                      0x7E
201 #define  BMX160_CMD_START_FOC                   0x03
202 #define  BMX160_CMD_ACC_SET_PMU_MODE(n)         (0x10 | (n))
203 #define  BMX160_CMD_GYR_SET_PMU_MODE(n)         (0x14 | (n))
204 #define  BMX160_CMD_MAG_IF_SET_PMU_MODE(n)      (0x18 | (n))
205 #define  BMX160_CMD_PROG_NVM                    0xa0
206 #define  BMX160_CMD_FIFO_FLUSH                  0xb0
207 #define  BMX160_CMD_INT_RESET                   0xb1
208 #define  BMX160_CMD_SOFTRESET                   0xb6
209 #define  BMX160_CMD_STEP_CNT_CLR                0xb2
210
211 #define BMM150_CHIP_ID                          0x40
212 #define BMM150_DATA_X_0_4                       0x42
213 #define BMM150_DATA_X_5_12                      0x43
214 #define BMM150_DATA_Y_0_4                       0x44
215 #define BMM150_DATA_Y_5_12                      0x45
216 #define BMM150_DATA_Z_0_6                       0x46
217 #define BMM150_DATA_Z_7_14                      0x47
218 #define BMM150_RHALL_0_5                        0x48
219 #define BMM150_RHALL_6_13                       0x49
220 #define BMM150_INT_STATUS                       0x4a
221 #define BMM150_POWER_MODE                       0x4b
222 #define  BMM150_POWER_MODE_SOFT_RESET_HI                7
223 #define  BMM150_POWER_MODE_SPI3EN                       2
224 #define  BMM150_POWER_MODE_SOFT_RESET_LO                1
225 #define  BMM150_POWER_MODE_POWER_CONTROL                0
226 #define BMM150_CONTROL                          0x4c
227 #define  BMM150_CONTROL_ADV_ST_1                        7
228 #define  BMM150_CONTROL_ADV_ST_0                        6
229 #define  BMM150_CONTROL_DATA_RATE                       3
230 #define   BMM150_CONTROL_DATA_RATE_10                           0
231 #define   BMM150_CONTROL_DATA_RATE_2                            1
232 #define   BMM150_CONTROL_DATA_RATE_6                            2
233 #define   BMM150_CONTROL_DATA_RATE_8                            3
234 #define   BMM150_CONTROL_DATA_RATE_15                           4
235 #define   BMM150_CONTROL_DATA_RATE_20                           5
236 #define   BMM150_CONTROL_DATA_RATE_25                           6
237 #define   BMM150_CONTROL_DATA_RATE_30                           7
238 #define  BMM150_CONTROL_OP_MODE                         1
239 #define   BMM150_CONTROL_OP_MODE_NORMAL                         0
240 #define   BMM150_CONTROL_OP_MODE_FORCED                         1
241 #define   BMM150_CONTROL_OP_MODE_SLEEP                          3
242 #define  BMM150_CONTROL_SELF_TEST                       0
243 #define BMM150_INT_EN                           0x4d
244 #define BMM150_INT_CONF                         0x4e
245 #define  BMM150_INT_CONF_X_DISABLE                      3
246 #define  BMM150_INT_CONF_Y_DISABLE                      4
247 #define  BMM150_INT_CONF_Z_DISABLE                      5
248 #define BMM150_LOW_THRESHOLD                    0x4f
249 #define BMM150_HIGH_THRESHOLD                   0x50
250 #define BMM150_REPXY                            0x51
251 #define  BMM150_REPXY_VALUE(n)                          (((n)-1) >> 1)
252 #define BMM150_REPZ                             0x52
253 #define  BMM150_REPZ_VALUE(n)                           ((n) -1)
254
255 #define BMX160_GYRO_FULLSCALE   ((float) 2000 * M_PI/180.0)
256
257 static inline float
258 ao_bmx160_gyro(float sensor) {
259         return sensor * ((float) (BMX160_GYRO_FULLSCALE / 32767.0));
260 }
261
262 #define BMX160_ACCEL_FULLSCALE  16
263
264 static inline float
265 ao_bmx160_accel(int16_t sensor) {
266         return (float) sensor * ((float) (BMX160_ACCEL_FULLSCALE * GRAVITY / 32767.0));
267 }
268
269 #endif /* _BMX160_H_ */