2 * Copyright © 2012 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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_mpu6000.h>
22 static uint8_t ao_mpu6000_wake;
23 static uint8_t ao_mpu6000_configured;
28 ao_exti_disable(&AO_MPU6000_INT_PORT, AO_MPU6000_INT_PIN);
30 ao_wakeup(&ao_mpu6000_wake);
34 ao_mpu6000_write(uint8_t addr, uint8_t *data, uint8_t len)
36 ao_i2c_get(AO_MPU6000_I2C_INDEX);
37 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
38 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
39 ao_i2c_send(data, len, AO_MPU6000_I2C_INDEX, TRUE);
40 ao_i2c_put(AO_MPU6000_I2C_INDEX);
44 ao_mpu6000_read(uint8_t addr, uint8_t *data, uint8_t len)
46 ao_i2c_get(AO_MPU6000_I2C_INDEX);
47 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
48 ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX, FALSE);
49 ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
50 ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX, TRUE);
51 ao_i2c_put(AO_MPU6000_I2C_INDEX);
55 ao_mpu6000_setup(void)
57 if (ao_mpu6000_configured)
60 /* Enable the EXTI interrupt for the appropriate pin */
61 ao_enable_port(AO_MPU6000_INT_PORT);
62 ao_exti_setup(&AO_MPU6000_INT_PORT, AO_MPU6000_INT_PIN,
63 AO_EXTI_MODE_FALLING, ao_mpu6000_isr);
65 ao_mpu6000_configured = 1;
75 ao_mpu6000_read(MPU6000_WHO_AM_I, data, 1);
76 printf ("mpu6000 WHO_AM_I: %02x\n", data[0]);
78 ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, data, 14);
79 for (i = 0; i < 14; i++)
80 printf ("reg %02x: %02x\n", i + MPU6000_ACCEL_XOUT_H, data[i]);
84 static const struct ao_cmds ao_mpu6000_cmds[] = {
85 { ao_mpu6000_show, "I\0Show MPU6000 status" },
92 ao_mpu6000_configured = 0;
94 ao_cmd_register(&ao_mpu6000_cmds[0]);