d7f67d6ecb61bdfa6791424dcd77e0d34bab33f6
[fw/altos] / src / drivers / ao_mpu6000.c
1 /*
2  * Copyright © 2012 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19 #include <ao_mpu6000.h>
20 #include <ao_exti.h>
21
22 static uint8_t  ao_mpu6000_wake;
23 static uint8_t  ao_mpu6000_configured;
24
25 static void
26 ao_mpu6000_isr(void)
27 {
28         ao_exti_disable(&AO_MPU6000_INT_PORT, AO_MPU6000_INT_PIN);
29         ao_mpu6000_wake = 1;
30         ao_wakeup(&ao_mpu6000_wake);
31 }
32
33 static void
34 ao_mpu6000_write(uint8_t addr, uint8_t *data, uint8_t len)
35 {
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);
41 }
42
43 static void
44 ao_mpu6000_read(uint8_t addr, uint8_t *data, uint8_t len)
45 {
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);
52 }
53
54 static void
55 ao_mpu6000_setup(void)
56 {
57         if (ao_mpu6000_configured)
58                 return;
59
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);
64
65         ao_mpu6000_configured = 1;
66 }
67
68 static void
69 ao_mpu6000_show(void)
70 {
71         uint8_t addr;
72         uint8_t data[14];
73         uint8_t i;
74
75         ao_mpu6000_read(MPU6000_WHO_AM_I, data, 1);
76         printf ("mpu6000 WHO_AM_I: %02x\n", data[0]);
77 #if 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]);
81 #endif
82 }
83
84 static const struct ao_cmds ao_mpu6000_cmds[] = {
85         { ao_mpu6000_show,      "I\0Show MPU6000 status" },
86         { 0, NULL }
87 };
88
89 void
90 ao_mpu6000_init(void)
91 {
92         ao_mpu6000_configured = 0;
93
94         ao_cmd_register(&ao_mpu6000_cmds[0]);
95 }