2 * Copyright © 2019 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #include <ao_bmx160.h>
23 static uint8_t ao_bmx160_configured;
25 #define ao_bmx160_spi_get() ao_spi_get(AO_BMX160_SPI_BUS, AO_SPI_SPEED_8MHz)
26 #define ao_bmx160_spi_put() ao_spi_put(AO_BMX160_SPI_BUS)
28 #define ao_bmx160_spi_start() ao_spi_set_cs(AO_BMX160_SPI_CS_PORT, \
29 (1 << AO_BMX160_SPI_CS_PIN))
31 #define ao_bmx160_spi_end() ao_spi_clr_cs(AO_BMX160_SPI_CS_PORT, \
32 (1 << AO_BMX160_SPI_CS_PIN))
35 _ao_bmx160_reg_write(uint8_t addr, uint8_t value)
37 uint8_t d[2] = { addr, value };
38 ao_bmx160_spi_start();
39 ao_spi_send(d, 2, AO_BMX160_SPI_BUS);
44 _ao_bmx160_read(uint8_t addr, void *data, uint8_t len)
47 ao_bmx160_spi_start();
48 ao_spi_send(&addr, 1, AO_BMX160_SPI_BUS);
49 ao_spi_recv(data, len, AO_BMX160_SPI_BUS);
54 _ao_bmx160_reg_read(uint8_t addr)
58 ao_bmx160_spi_start();
59 ao_spi_send(&addr, 1, AO_BMX160_SPI_BUS);
60 ao_spi_recv(&value, 1, AO_BMX160_SPI_BUS);
66 _ao_bmx160_cmd(uint8_t cmd)
68 _ao_bmx160_reg_write(BMX160_CMD, cmd);
69 ao_delay(AO_MS_TO_TICKS(100));
73 _ao_bmx160_mag_setup(void)
75 _ao_bmx160_reg_write(BMX160_MAG_IF_0, 0x80);
79 _ao_bmm150_wait_manual(void)
81 while (_ao_bmx160_reg_read(BMX160_STATUS) & (1 << BMX160_STATUS_MAG_MAN_OP))
86 _ao_bmm150_reg_write(uint8_t addr, uint8_t data)
88 _ao_bmx160_reg_write(BMX160_MAG_IF_3, data);
89 _ao_bmx160_reg_write(BMX160_MAG_IF_2, addr);
90 _ao_bmm150_wait_manual();
95 _ao_bmm150_reg_read(uint8_t addr)
97 _ao_bmx160_reg_write(BMX160_MAG_IF_1, addr);
98 _ao_bmm150_wait_manual();
99 return _ao_bmx160_reg_read(BMX160_DATA_0);
104 _ao_bmx160_sample(struct ao_bmx160_sample *sample)
106 _ao_bmx160_read(BMX160_MAG_X_0_7, sample, sizeof (*sample));
107 #if __BYTE_ORDER != __LITTLE_ENDIAN
108 int i = sizeof (*sample) / 2;
109 uint16_t *d = (uint16_t *) sample;
114 *d++ = (t >> 8) | (t << 8);
119 #define G 981 /* in cm/s² */
122 static int16_t /* cm/s² */
123 ao_bmx160_accel(int16_t v)
125 return (int16_t) ((v * (int32_t) (16.0 * 980.665 + 0.5)) / 32767);
128 static int16_t /* deg*10/s */
129 ao_bmx160_gyro(int16_t v)
131 return (int16_t) ((v * (int32_t) 20000) / 32767);
135 ao_bmx160_accel_check(int16_t normal, int16_t test)
137 int16_t diff = test - normal;
139 if (diff < BMX160_ST_ACCEL(16) / 4) {
142 if (diff > BMX160_ST_ACCEL(16) * 4) {
149 ao_bmx160_gyro_check(int16_t normal, int16_t test)
151 int16_t diff = test - normal;
155 if (diff < BMX160_ST_GYRO(2000) / 4) {
158 if (diff > BMX160_ST_GYRO(2000) * 4) {
166 _ao_bmx160_wait_alive(void)
170 /* Wait for the chip to wake up */
171 for (i = 0; i < 30; i++) {
172 ao_delay(AO_MS_TO_TICKS(100));
173 if (_ao_bmx160_reg_read(BMX160_CHIPID) == BMX160_CHIPID_BMX160)
177 ao_panic(AO_PANIC_SELF_TEST_BMX160);
184 _ao_bmx160_setup(void)
188 if (ao_bmx160_configured)
191 /* Make sure the chip is responding */
192 _ao_bmx160_wait_alive();
195 _ao_bmx160_reg_write(BMX160_NV_CONF, 1 << BMX160_NV_CONF_SPI_EN);
197 /* Enable acc and gyr
200 _ao_bmx160_cmd(BMX160_CMD_ACC_SET_PMU_MODE(BMX160_PMU_STATUS_ACC_PMU_STATUS_NORMAL));
202 for (r = 0; r < 20; r++) {
203 ao_delay(AO_MS_TO_TICKS(100));
204 if (((_ao_bmx160_reg_read(BMX160_PMU_STATUS)
205 >> BMX160_PMU_STATUS_ACC_PMU_STATUS)
206 & BMX160_PMU_STATUS_ACC_PMU_STATUS_MASK)
207 == BMX160_PMU_STATUS_ACC_PMU_STATUS_NORMAL)
214 AO_SENSOR_ERROR(AO_DATA_BMX160);
216 _ao_bmx160_cmd(BMX160_CMD_GYR_SET_PMU_MODE(BMX160_PMU_STATUS_GYR_PMU_STATUS_NORMAL));
218 for (r = 0; r < 20; r++) {
219 ao_delay(AO_MS_TO_TICKS(100));
220 if (((_ao_bmx160_reg_read(BMX160_PMU_STATUS)
221 >> BMX160_PMU_STATUS_GYR_PMU_STATUS)
222 & BMX160_PMU_STATUS_GYR_PMU_STATUS_MASK)
223 == BMX160_PMU_STATUS_GYR_PMU_STATUS_NORMAL)
230 AO_SENSOR_ERROR(AO_DATA_BMX160);
232 /* Configure accelerometer:
234 * undersampling disabled
236 * 200Hz sampling rate
239 * This yields a 3dB cutoff frequency of 80Hz
241 _ao_bmx160_reg_write(BMX160_ACC_CONF,
242 (0 << BMX160_ACC_CONF_ACC_US) |
243 (BMX160_ACC_CONF_ACC_BWP_NORMAL << BMX160_ACC_CONF_ACC_BWP) |
244 (BMX160_ACC_CONF_ACC_ODR_200 << BMX160_ACC_CONF_ACC_ODR));
245 _ao_bmx160_reg_write(BMX160_ACC_RANGE,
246 BMX160_ACC_RANGE_16G);
248 for (r = 0x3; r <= 0x1b; r++)
249 (void) _ao_bmx160_reg_read(r);
253 * 200Hz sampling rate
257 _ao_bmx160_reg_write(BMX160_GYR_CONF,
258 (BMX160_GYR_CONF_GYR_BWP_NORMAL << BMX160_GYR_CONF_GYR_BWP) |
259 (BMX160_GYR_CONF_GYR_ODR_200 << BMX160_GYR_CONF_GYR_ODR));
260 _ao_bmx160_reg_write(BMX160_GYR_RANGE,
261 BMX160_GYR_RANGE_2000);
264 /* Configure magnetometer:
270 _ao_bmx160_cmd(BMX160_CMD_MAG_IF_SET_PMU_MODE(BMX160_PMU_STATUS_MAG_IF_PMU_STATUS_NORMAL));
272 /* Enter setup mode */
273 _ao_bmx160_mag_setup();
275 /* Place in suspend mode to reboot the chip */
276 _ao_bmm150_reg_write(BMM150_POWER_MODE,
277 (0 << BMM150_POWER_MODE_POWER_CONTROL));
280 _ao_bmm150_reg_write(BMM150_POWER_MODE,
281 (1 << BMM150_POWER_MODE_POWER_CONTROL));
283 /* Set data rate and place in sleep mode */
284 _ao_bmm150_reg_write(BMM150_CONTROL,
285 (BMM150_CONTROL_DATA_RATE_30 << BMM150_CONTROL_DATA_RATE) |
286 (BMM150_CONTROL_OP_MODE_SLEEP << BMM150_CONTROL_OP_MODE));
288 /* enable all axes (should already be enabled) */
289 _ao_bmm150_reg_write(BMM150_INT_CONF,
290 (0 << BMM150_INT_CONF_X_DISABLE) |
291 (0 << BMM150_INT_CONF_Y_DISABLE) |
292 (0 << BMM150_INT_CONF_Z_DISABLE));
294 /* Set repetition values (?) */
295 _ao_bmm150_reg_write(BMM150_REPXY, BMM150_REPXY_VALUE(9));
296 _ao_bmm150_reg_write(BMM150_REPZ, BMM150_REPZ_VALUE(15));
298 /* To get data out of the magnetometer, set the control op mode to 'forced', then read
299 * from the data registers
301 _ao_bmx160_reg_write(BMX160_MAG_IF_3, (BMM150_CONTROL_OP_MODE_FORCED << BMM150_CONTROL_OP_MODE));
302 _ao_bmx160_reg_write(BMX160_MAG_IF_2, BMM150_CONTROL);
303 _ao_bmx160_reg_write(BMX160_MAG_IF_1, BMM150_DATA_X_0_4);
305 /* Set data rate to 200Hz */
306 _ao_bmx160_reg_write(BMX160_MAG_CONF,
307 (BMX160_MAG_CONF_MAG_ODR_200 << BMX160_MAG_CONF_MAG_ODR));
309 /* Put magnetometer interface back into 'normal mode'
311 _ao_bmx160_reg_write(BMX160_MAG_IF_0,
312 (0 << BMX160_MAG_IF_0_MAG_MANUAL_EN) |
313 (0 << BMX160_MAG_IF_0_MAG_OFFSET) |
314 (0 << BMX160_MAG_IF_0_MAG_RD_BURST));
316 ao_bmx160_configured = 1;
319 struct ao_bmx160_sample ao_bmx160_current;
324 struct ao_bmx160_sample sample;
326 /* ao_bmx160_init already grabbed the SPI bus and mutex */
332 _ao_bmx160_sample(&sample);
334 ao_arch_block_interrupts();
335 ao_bmx160_current = sample;
336 AO_DATA_PRESENT(AO_DATA_BMX160);
338 ao_arch_release_interrupts();
342 static struct ao_task ao_bmx160_task;
347 printf ("Accel: %7d %7d %7d Gyro: %7d %7d %7d Mag: %7d %7d %7d\n",
348 ao_bmx160_current.acc_x,
349 ao_bmx160_current.acc_y,
350 ao_bmx160_current.acc_z,
351 ao_bmx160_current.gyr_x,
352 ao_bmx160_current.gyr_y,
353 ao_bmx160_current.gyr_z,
354 ao_bmx160_current.mag_x,
355 ao_bmx160_current.mag_y,
356 ao_bmx160_current.mag_z);
368 if (ao_cmd_status != ao_cmd_success)
371 val = _ao_bmx160_reg_read(addr);
373 printf("Addr %02x val %02x\n", addr, val);
377 ao_bmx160_write(void)
383 if (ao_cmd_status != ao_cmd_success)
386 if (ao_cmd_status != ao_cmd_success)
388 printf("Addr %02x val %02x\n", addr, val);
390 _ao_bmx160_reg_write(addr, val);
401 if (ao_cmd_status != ao_cmd_success)
404 val = _ao_bmm150_reg_read(addr);
406 printf("Addr %02x val %02x\n", addr, val);
410 ao_bmm150_write(void)
416 if (ao_cmd_status != ao_cmd_success)
419 if (ao_cmd_status != ao_cmd_success)
421 printf("Addr %02x val %02x\n", addr, val);
423 _ao_bmm150_reg_write(addr, val);
427 #endif /* BMX160_TEST */
429 static const struct ao_cmds ao_bmx160_cmds[] = {
430 { ao_bmx160_show, "I\0Show BMX160 status" },
432 { ao_bmx160_read, "R <addr>\0Read BMX160 register" },
433 { ao_bmx160_write, "W <addr> <val>\0Write BMX160 register" },
434 { ao_bmm150_read, "M <addr>\0Read BMM150 register" },
435 { ao_bmm150_write, "N <addr> <val>\0Write BMM150 register" },
443 ao_add_task(&ao_bmx160_task, ao_bmx160, "bmx160");
445 ao_spi_init_cs(AO_BMX160_SPI_CS_PORT, (1 << AO_BMX160_SPI_CS_PIN));
447 /* Pretend to be the bmx160 task. Grab the SPI bus right away and
448 * hold it for the task so that nothing else uses the SPI bus before
449 * we get the I2C mode disabled in the chip
452 ao_cur_task = &ao_bmx160_task;
455 ao_cmd_register(&ao_bmx160_cmds[0]);