e62f172da9bfe8711f23a32b26a96b20c8c91b0a
[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_GYR_CONF                 0x42
134 #define  BMX160_GYR_CONF_GYR_ODR                0
135 #define   BMX160_GYR_CONF_GYR_ODR_25                    0x6
136 #define   BMX160_GYR_CONF_GYR_ODR_50                    0x7
137 #define   BMX160_GYR_CONF_GYR_ODR_100                   0x8
138 #define   BMX160_GYR_CONF_GYR_ODR_200                   0x9
139 #define   BMX160_GYR_CONF_GYR_ODR_400                   0xa
140 #define   BMX160_GYR_CONF_GYR_ODR_800                   0xb
141 #define   BMX160_GYR_CONF_GYR_ODR_1600                  0xc
142 #define   BMX160_GYR_CONF_GYR_ODR_3200                  0xd
143 #define  BMX160_GYR_CONF_GYR_BWP                4
144 #define   BMX160_GYR_CONF_GYR_BWP_NORMAL                        0x2
145 #define   BMX160_GYR_CONF_GYR_BWP_OSR2                          0x1
146 #define   BMX160_GYR_CONF_GYR_BWP_OSR4                          0x0
147 #define BMX160_GYR_RANGE                0x43
148 #define  BMX160_GYR_RANGE_2000                                  0x0
149 #define  BMX160_GYR_RANGE_1000                                  0x1
150 #define  BMX160_GYR_RANGE_500                                   0x2
151 #define  BMX160_GYR_RANGE_250                                   0x3
152 #define  BMX160_GYR_RANGE_125                                   0x4
153 #define BMX160_MAG_CONF                 0x44
154 #define  BMX160_MAG_CONF_MAG_ODR                        0
155 #define   BMX160_MAG_CONF_MAG_ODR_25_32                         0x1
156 #define   BMX160_MAG_CONF_MAG_ODR_25_16                         0x2
157 #define   BMX160_MAG_CONF_MAG_ODR_25_8                          0x3
158 #define   BMX160_MAG_CONF_MAG_ODR_25_4                          0x4
159 #define   BMX160_MAG_CONF_MAG_ODR_25_2                          0x5
160 #define   BMX160_MAG_CONF_MAG_ODR_25                            0x6
161 #define   BMX160_MAG_CONF_MAG_ODR_50                            0x7
162 #define   BMX160_MAG_CONF_MAG_ODR_100                           0x8
163 #define   BMX160_MAG_CONF_MAG_ODR_200                           0x9
164 #define   BMX160_MAG_CONF_MAG_ODR_400                           0xa
165 #define   BMX160_MAG_CONF_MAG_ODR_800                           0xb
166 #define BMX160_FIFO_DOWNS               0x45
167 #define BMX160_FIFO_CONFIG_0            0x46
168 #define BMX160_FIFO_CONFIG_1            0x47
169 #define BMX160_MAG_IF_0                 0x4C
170 #define  BMX160_MAG_IF_0_MAG_RD_BURST           0
171 #define  BMX160_MAG_IF_0_MAG_OFFSET             2
172 #define  BMX160_MAG_IF_0_MAG_MANUAL_EN          7
173 #define BMX160_MAG_IF_1                 0x4D
174 #define BMX160_MAG_IF_2                 0x4E
175 #define BMX160_MAG_IF_3                 0x4F
176 #define BMX160_INT_EN                   0x50-0x52
177 #define BMX160_INT_OUT_CTRL             0x53
178 #define BMX160_INT_LATCH                0x54
179 #define BMX160_INT_MAP                  0x55-0x57
180 #define BMX160_INT_DATA                 0x58-0x59
181 #define BMX160_INT_LOWHIGH              0x5A-0x5E
182 #define BMX160_INT_MOTION               0x5F-0x62
183 #define BMX160_INT_TAP                  0x63-0x64
184 #define BMX160_INT_ORIENT               0x65-0x66
185 #define BMX160_INT_FLAT                 0x67-0x68
186 #define BMX160_FOC_CONF                 0x69
187 #define BMX160_CONF                     0x6A
188 #define BMX160_IF_CONF                  0x6B
189 #define BMX160_PMU_TRIGGER              0x6C
190 #define BMX160_SELF_TEST                0x6D
191 #define BMX160_NV_CONF                  0x70
192 #define  BMX160_NV_CONF_SPI_EN                  0
193 #define  BMX160_NV_CONF_I2C_WDT_SEL             1
194 #define  BMX160_NV_CONF_I2C_WDT_EN              2
195 #define BMX160_OFFSET                   0x71-0x77
196 #define BMX160_STEP_CNT                 0x78-0x79
197 #define BMX160_STEP_CONF                0x7A-0x7B
198 #define BMX160_CMD                      0x7E
199 #define  BMX160_CMD_START_FOC                   0x03
200 #define  BMX160_CMD_ACC_SET_PMU_MODE(n)         (0x10 | (n))
201 #define  BMX160_CMD_GYR_SET_PMU_MODE(n)         (0x14 | (n))
202 #define  BMX160_CMD_MAG_IF_SET_PMU_MODE(n)      (0x18 | (n))
203 #define  BMX160_CMD_PROG_NVM                    0xa0
204 #define  BMX160_CMD_FIFO_FLUSH                  0xb0
205 #define  BMX160_CMD_INT_RESET                   0xb1
206 #define  BMX160_CMD_SOFTRESET                   0xb6
207 #define  BMX160_CMD_STEP_CNT_CLR                0xb2
208
209 #define BMM150_CHIP_ID                          0x40
210 #define BMM150_DATA_X_0_4                       0x42
211 #define BMM150_DATA_X_5_12                      0x43
212 #define BMM150_DATA_Y_0_4                       0x44
213 #define BMM150_DATA_Y_5_12                      0x45
214 #define BMM150_DATA_Z_0_6                       0x46
215 #define BMM150_DATA_Z_7_14                      0x47
216 #define BMM150_RHALL_0_5                        0x48
217 #define BMM150_RHALL_6_13                       0x49
218 #define BMM150_INT_STATUS                       0x4a
219 #define BMM150_POWER_MODE                       0x4b
220 #define  BMM150_POWER_MODE_SOFT_RESET_HI                7
221 #define  BMM150_POWER_MODE_SPI3EN                       2
222 #define  BMM150_POWER_MODE_SOFT_RESET_LO                1
223 #define  BMM150_POWER_MODE_POWER_CONTROL                0
224 #define BMM150_CONTROL                          0x4c
225 #define  BMM150_CONTROL_ADV_ST_1                        7
226 #define  BMM150_CONTROL_ADV_ST_0                        6
227 #define  BMM150_CONTROL_DATA_RATE                       3
228 #define   BMM150_CONTROL_DATA_RATE_10                           0
229 #define   BMM150_CONTROL_DATA_RATE_2                            1
230 #define   BMM150_CONTROL_DATA_RATE_6                            2
231 #define   BMM150_CONTROL_DATA_RATE_8                            3
232 #define   BMM150_CONTROL_DATA_RATE_15                           4
233 #define   BMM150_CONTROL_DATA_RATE_20                           5
234 #define   BMM150_CONTROL_DATA_RATE_25                           6
235 #define   BMM150_CONTROL_DATA_RATE_30                           7
236 #define  BMM150_CONTROL_OP_MODE                         1
237 #define   BMM150_CONTROL_OP_MODE_NORMAL                         0
238 #define   BMM150_CONTROL_OP_MODE_FORCED                         1
239 #define   BMM150_CONTROL_OP_MODE_SLEEP                          3
240 #define  BMM150_CONTROL_SELF_TEST                       0
241 #define BMM150_INT_EN                           0x4d
242 #define BMM150_INT_CONF                         0x4e
243 #define  BMM150_INT_CONF_X_DISABLE                      3
244 #define  BMM150_INT_CONF_Y_DISABLE                      4
245 #define  BMM150_INT_CONF_Z_DISABLE                      5
246 #define BMM150_LOW_THRESHOLD                    0x4f
247 #define BMM150_HIGH_THRESHOLD                   0x50
248 #define BMM150_REPXY                            0x51
249 #define  BMM150_REPXY_VALUE(n)                          (((n)-1) >> 1)
250 #define BMM150_REPZ                             0x52
251 #define  BMM150_REPZ_VALUE(n)                           ((n) -1)
252
253 #define BMX160_GYRO_FULLSCALE   ((float) 2000 * M_PI/180.0)
254
255 static inline float
256 ao_bmx160_gyro(float sensor) {
257         return sensor * ((float) (BMX160_GYRO_FULLSCALE / 32767.0));
258 }
259
260 #define BMX160_ACCEL_FULLSCALE  16
261
262 static inline float
263 ao_bmx160_accel(int16_t sensor) {
264         return (float) sensor * ((float) (BMX160_ACCEL_FULLSCALE * GRAVITY / 32767.0));
265 }
266
267 #endif /* _BMX160_H_ */