9fc3334177b1208ad82f9e256f801fdf87177b32
[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         uint16_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 struct ao_bmm150_trim {
50         int8_t          dig_x1;
51         int8_t          dig_y1;
52         int8_t          dig_x2;
53         int8_t          dig_y2;
54         uint16_t        dig_z1;
55         int16_t         dig_z2;
56         int16_t         dig_z3;
57         int16_t         dig_z4;
58         uint8_t         dig_xy1;
59         int8_t          dig_xy2;
60         uint16_t        dig_xyz1;
61 };
62
63 void
64 ao_bmx160_init(void);
65
66 #define BMX160_CHIPID                   0x00
67 #define  BMX160_CHIPID_BMX160                   0xd8
68 #define BMX160_ERR_REG                  0x02
69 #define BMX160_PMU_STATUS               0x03
70 #define  BMX160_PMU_STATUS_MAG_IF_PMU_STATUS    0
71 #define   BMX160_PMU_STATUS_MAG_IF_PMU_STATUS_SUSPEND           0
72 #define   BMX160_PMU_STATUS_MAG_IF_PMU_STATUS_NORMAL            1
73 #define   BMX160_PMU_STATUS_MAG_IF_PMU_STATUS_LOW_POWER         2
74 #define  BMX160_PMU_STATUS_GYR_PMU_STATUS       2
75 #define   BMX160_PMU_STATUS_GYR_PMU_STATUS_SUSPEND              0
76 #define   BMX160_PMU_STATUS_GYR_PMU_STATUS_NORMAL               1
77 #define   BMX160_PMU_STATUS_GYR_PMU_STATUS_FAST_START_UP        3
78 #define   BMX160_PMU_STATUS_GYR_PMU_STATUS_MASK                 3
79 #define  BMX160_PMU_STATUS_ACC_PMU_STATUS       4
80 #define   BMX160_PMU_STATUS_ACC_PMU_STATUS_SUSPEND              0
81 #define   BMX160_PMU_STATUS_ACC_PMU_STATUS_NORMAL               1
82 #define   BMX160_PMU_STATUS_ACC_PMU_STATUS_LOW_POWER            2
83 #define   BMX160_PMU_STATUS_ACC_PMU_STATUS_MASK                 3
84 #define BMX160_DATA_0                   0x04
85 #define BMX160_MAG_X_0_7                0x04
86 #define BMX160_MAG_X_8_15               0x05
87 #define BMX160_MAG_Y_0_7                0x06
88 #define BMX160_MAG_Y_8_15               0x07
89 #define BMX160_MAG_Z_0_7                0x08
90 #define BMX160_MAG_Z_8_15               0x09
91 #define BMX160_RHALL_0_7                0x0A
92 #define BMX160_RHALL_8_15               0x0B
93 #define BMX160_GYRO_X_0_7               0x0C
94 #define BMX160_GYRO_X_8_15              0x0D
95 #define BMX160_GYRO_Y_0_7               0x0E
96 #define BMX160_GYRO_Y_8_15              0x0F
97 #define BMX160_GYRO_Z_0_7               0x10
98 #define BMX160_GYRO_Z_8_15              0x11
99 #define BMX160_ACCEL_X_0_7              0x12
100 #define BMX160_ACCEL_X_8_15             0x13
101 #define BMX160_ACCEL_Y_0_7              0x14
102 #define BMX160_ACCEL_Y_8_15             0x15
103 #define BMX160_ACCEL_Z_0_7              0x16
104 #define BMX160_ACCEL_Z_8_15             0x17
105 #define BMX160_SENSORTIME_0_7           0x18
106 #define BMX160_SENSORTIME_8_15          0x19
107 #define BMX160_SENSORTIME_16_23         0x1A
108 #define BMX160_STATUS                   0x1B
109 #define  BMX160_STATUS_GYR_SELF_TEST_OK         1
110 #define  BMX160_STATUS_MAG_MAN_OP               2
111 #define  BMX160_STATUS_FOC_RDY                  3
112 #define  BMX160_STATUS_NVM_RDY                  4
113 #define  BMX160_STATUS_DRDY_MAG                 5
114 #define  BMX160_STATUS_DRDY_GYR                 6
115 #define  BMX160_STATUS_DRDY_ACC                 7
116 #define BMX160_INT_STATUS_0             0x1C-0x1F
117 #define BMX160_INT_STATUS_1             0x1D
118 #define BMX160_INT_STATUS_2             0x1E
119 #define BMX160_INT_STATUS_3             0x1F
120 #define BMX160_TEMPERATURE_0_7          0x20
121 #define BMX160_TEMPERATURE_8_15         0x21
122 #define BMX160_FIFO_LENGTH_0_7          0x22
123 #define BMX160_FIFO_LENGTH_8_15         0x23
124 #define BMX160_FIFO_DATA                0x24
125 #define BMX160_ACC_CONF                 0x40
126 #define  BMX160_ACC_CONF_ACC_ODR                0
127 #define   BMX160_ACC_CONF_ACC_ODR_25_32                 0x1
128 #define   BMX160_ACC_CONF_ACC_ODR_25_16                 0x2
129 #define   BMX160_ACC_CONF_ACC_ODR_25_8                  0x3
130 #define   BMX160_ACC_CONF_ACC_ODR_25_4                  0x4
131 #define   BMX160_ACC_CONF_ACC_ODR_25_2                  0x5
132 #define   BMX160_ACC_CONF_ACC_ODR_25                    0x6
133 #define   BMX160_ACC_CONF_ACC_ODR_50                    0x7
134 #define   BMX160_ACC_CONF_ACC_ODR_100                   0x8
135 #define   BMX160_ACC_CONF_ACC_ODR_200                   0x9
136 #define   BMX160_ACC_CONF_ACC_ODR_400                   0xa
137 #define   BMX160_ACC_CONF_ACC_ODR_800                   0xb
138 #define   BMX160_ACC_CONF_ACC_ODR_1600                  0xc
139 #define  BMX160_ACC_CONF_ACC_BWP                4
140 #define   BMX160_ACC_CONF_ACC_BWP_NORMAL                        0x2
141 #define   BMX160_ACC_CONF_ACC_BWP_OSR2                          0x1
142 #define   BMX160_ACC_CONF_ACC_BWP_OSR4                          0x0
143 #define  BMX160_ACC_CONF_ACC_US                 7
144 #define BMX160_ACC_RANGE                0x41
145 #define  BMX160_ACC_RANGE_2G                    0x3
146 #define  BMX160_ACC_RANGE_4G                    0x5
147 #define  BMX160_ACC_RANGE_8G                    0x8
148 #define  BMX160_ACC_RANGE_16G                   0xc
149 #define BMX160_GYR_CONF                 0x42
150 #define  BMX160_GYR_CONF_GYR_ODR                0
151 #define   BMX160_GYR_CONF_GYR_ODR_25                    0x6
152 #define   BMX160_GYR_CONF_GYR_ODR_50                    0x7
153 #define   BMX160_GYR_CONF_GYR_ODR_100                   0x8
154 #define   BMX160_GYR_CONF_GYR_ODR_200                   0x9
155 #define   BMX160_GYR_CONF_GYR_ODR_400                   0xa
156 #define   BMX160_GYR_CONF_GYR_ODR_800                   0xb
157 #define   BMX160_GYR_CONF_GYR_ODR_1600                  0xc
158 #define   BMX160_GYR_CONF_GYR_ODR_3200                  0xd
159 #define  BMX160_GYR_CONF_GYR_BWP                4
160 #define   BMX160_GYR_CONF_GYR_BWP_NORMAL                        0x2
161 #define   BMX160_GYR_CONF_GYR_BWP_OSR2                          0x1
162 #define   BMX160_GYR_CONF_GYR_BWP_OSR4                          0x0
163 #define BMX160_GYR_RANGE                0x43
164 #define  BMX160_GYR_RANGE_2000                                  0x0
165 #define  BMX160_GYR_RANGE_1000                                  0x1
166 #define  BMX160_GYR_RANGE_500                                   0x2
167 #define  BMX160_GYR_RANGE_250                                   0x3
168 #define  BMX160_GYR_RANGE_125                                   0x4
169 #define BMX160_MAG_CONF                 0x44
170 #define  BMX160_MAG_CONF_MAG_ODR                        0
171 #define   BMX160_MAG_CONF_MAG_ODR_25_32                         0x1
172 #define   BMX160_MAG_CONF_MAG_ODR_25_16                         0x2
173 #define   BMX160_MAG_CONF_MAG_ODR_25_8                          0x3
174 #define   BMX160_MAG_CONF_MAG_ODR_25_4                          0x4
175 #define   BMX160_MAG_CONF_MAG_ODR_25_2                          0x5
176 #define   BMX160_MAG_CONF_MAG_ODR_25                            0x6
177 #define   BMX160_MAG_CONF_MAG_ODR_50                            0x7
178 #define   BMX160_MAG_CONF_MAG_ODR_100                           0x8
179 #define   BMX160_MAG_CONF_MAG_ODR_200                           0x9
180 #define   BMX160_MAG_CONF_MAG_ODR_400                           0xa
181 #define   BMX160_MAG_CONF_MAG_ODR_800                           0xb
182 #define BMX160_FIFO_DOWNS               0x45
183 #define BMX160_FIFO_CONFIG_0            0x46
184 #define BMX160_FIFO_CONFIG_1            0x47
185 #define BMX160_MAG_IF_0                 0x4C
186 #define  BMX160_MAG_IF_0_MAG_RD_BURST           0
187 #define  BMX160_MAG_IF_0_MAG_OFFSET             2
188 #define  BMX160_MAG_IF_0_MAG_MANUAL_EN          7
189 #define BMX160_MAG_IF_1                 0x4D
190 #define BMX160_MAG_IF_2                 0x4E
191 #define BMX160_MAG_IF_3                 0x4F
192 #define BMX160_INT_EN                   0x50-0x52
193 #define BMX160_INT_OUT_CTRL             0x53
194 #define BMX160_INT_LATCH                0x54
195 #define BMX160_INT_MAP                  0x55-0x57
196 #define BMX160_INT_DATA                 0x58-0x59
197 #define BMX160_INT_LOWHIGH              0x5A-0x5E
198 #define BMX160_INT_MOTION               0x5F-0x62
199 #define BMX160_INT_TAP                  0x63-0x64
200 #define BMX160_INT_ORIENT               0x65-0x66
201 #define BMX160_INT_FLAT                 0x67-0x68
202 #define BMX160_FOC_CONF                 0x69
203 #define BMX160_CONF                     0x6A
204 #define BMX160_IF_CONF                  0x6B
205 #define  BMX160_IF_CONF_IF_MODE                 4
206 #define  BMX160_IF_CONF_IF_MODE_AUTO_MAG                0x02
207 #define BMX160_PMU_TRIGGER              0x6C
208 #define BMX160_SELF_TEST                0x6D
209 #define BMX160_NV_CONF                  0x70
210 #define  BMX160_NV_CONF_SPI_EN                  0
211 #define  BMX160_NV_CONF_I2C_WDT_SEL             1
212 #define  BMX160_NV_CONF_I2C_WDT_EN              2
213 #define BMX160_OFFSET                   0x71-0x77
214 #define BMX160_STEP_CNT                 0x78-0x79
215 #define BMX160_STEP_CONF                0x7A-0x7B
216 #define BMX160_CMD                      0x7E
217 #define  BMX160_CMD_START_FOC                   0x03
218 #define  BMX160_CMD_ACC_SET_PMU_MODE(n)         (0x10 | (n))
219 #define  BMX160_CMD_GYR_SET_PMU_MODE(n)         (0x14 | (n))
220 #define  BMX160_CMD_MAG_IF_SET_PMU_MODE(n)      (0x18 | (n))
221 #define  BMX160_CMD_PROG_NVM                    0xa0
222 #define  BMX160_CMD_FIFO_FLUSH                  0xb0
223 #define  BMX160_CMD_INT_RESET                   0xb1
224 #define  BMX160_CMD_SOFTRESET                   0xb6
225 #define  BMX160_CMD_STEP_CNT_CLR                0xb2
226
227 #define BMM150_CHIP_ID                          0x40
228 #define BMM150_DATA_X_0_4                       0x42
229 #define BMM150_DATA_X_5_12                      0x43
230 #define BMM150_DATA_Y_0_4                       0x44
231 #define BMM150_DATA_Y_5_12                      0x45
232 #define BMM150_DATA_Z_0_6                       0x46
233 #define BMM150_DATA_Z_7_14                      0x47
234 #define BMM150_RHALL_0_5                        0x48
235 #define BMM150_RHALL_6_13                       0x49
236 #define BMM150_INT_STATUS                       0x4a
237 #define BMM150_POWER_MODE                       0x4b
238 #define  BMM150_POWER_MODE_SOFT_RESET_HI                7
239 #define  BMM150_POWER_MODE_SPI3EN                       2
240 #define  BMM150_POWER_MODE_SOFT_RESET_LO                1
241 #define  BMM150_POWER_MODE_POWER_CONTROL                0
242 #define BMM150_CONTROL                          0x4c
243 #define  BMM150_CONTROL_ADV_ST_1                        7
244 #define  BMM150_CONTROL_ADV_ST_0                        6
245 #define  BMM150_CONTROL_DATA_RATE                       3
246 #define   BMM150_CONTROL_DATA_RATE_10                           0
247 #define   BMM150_CONTROL_DATA_RATE_2                            1
248 #define   BMM150_CONTROL_DATA_RATE_6                            2
249 #define   BMM150_CONTROL_DATA_RATE_8                            3
250 #define   BMM150_CONTROL_DATA_RATE_15                           4
251 #define   BMM150_CONTROL_DATA_RATE_20                           5
252 #define   BMM150_CONTROL_DATA_RATE_25                           6
253 #define   BMM150_CONTROL_DATA_RATE_30                           7
254 #define  BMM150_CONTROL_OP_MODE                         1
255 #define   BMM150_CONTROL_OP_MODE_NORMAL                         0
256 #define   BMM150_CONTROL_OP_MODE_FORCED                         1
257 #define   BMM150_CONTROL_OP_MODE_SLEEP                          3
258 #define  BMM150_CONTROL_SELF_TEST                       0
259 #define BMM150_INT_EN                           0x4d
260 #define BMM150_INT_CONF                         0x4e
261 #define  BMM150_INT_CONF_X_DISABLE                      3
262 #define  BMM150_INT_CONF_Y_DISABLE                      4
263 #define  BMM150_INT_CONF_Z_DISABLE                      5
264 #define BMM150_LOW_THRESHOLD                    0x4f
265 #define BMM150_HIGH_THRESHOLD                   0x50
266 #define BMM150_REPXY                            0x51
267 #define  BMM150_REPXY_VALUE(n)                          (((n)-1) >> 1)
268 #define BMM150_REPZ                             0x52
269 #define  BMM150_REPZ_VALUE(n)                           ((n) -1)
270
271 /* Trim Extended Registers */
272 #define BMM150_DIG_X1                        0x5D
273 #define BMM150_DIG_Y1                        0x5E
274 #define BMM150_DIG_Z4_LSB                    0x62
275 #define BMM150_DIG_Z4_MSB                    0x63
276 #define BMM150_DIG_X2                        0x64
277 #define BMM150_DIG_Y2                        0x65
278 #define BMM150_DIG_Z2_LSB                    0x68
279 #define BMM150_DIG_Z2_MSB                    0x69
280 #define BMM150_DIG_Z1_LSB                    0x6A
281 #define BMM150_DIG_Z1_MSB                    0x6B
282 #define BMM150_DIG_XYZ1_LSB                  0x6C
283 #define BMM150_DIG_XYZ1_MSB                  0x6D
284 #define BMM150_DIG_Z3_LSB                    0x6E
285 #define BMM150_DIG_Z3_MSB                    0x6F
286 #define BMM150_DIG_XY2                       0x70
287 #define BMM150_DIG_XY1                       0x71
288
289 #define BMM150_XYAXES_FLIP_OVERFLOW_ADCVAL   -4096
290 #define BMM150_ZAXIS_HALL_OVERFLOW_ADCVAL    -16384
291 #define BMM150_OVERFLOW_OUTPUT               -32768
292 #define BMM150_NEGATIVE_SATURATION_Z         -32767
293 #define BMM150_POSITIVE_SATURATION_Z         32767
294
295 #define BMX160_GYRO_FULLSCALE   ((float) 2000 * M_PI/180.0)
296
297 static inline float
298 ao_bmx160_gyro(float sensor) {
299         return sensor * ((float) (BMX160_GYRO_FULLSCALE / 32767.0));
300 }
301
302 #define BMX160_ACCEL_FULLSCALE  16
303
304 static inline float
305 ao_bmx160_accel(int16_t sensor) {
306         return (float) sensor * ((float) (BMX160_ACCEL_FULLSCALE * GRAVITY / 32767.0));
307 }
308
309 #endif /* _BMX160_H_ */