altos: Add mpu6000 and hmc5883 stubs
[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);
39         if (len)
40                 ao_i2c_send(data, len, AO_MPU6000_I2C_INDEX);
41         ao_i2c_stop(AO_MPU6000_I2C_INDEX);
42         ao_i2c_put(AO_MPU6000_I2C_INDEX);
43 }
44
45 static void
46 ao_mpu6000_read(uint8_t addr, uint8_t *data, uint8_t len)
47 {
48         ao_i2c_get(AO_MPU6000_I2C_INDEX);
49         ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_WRITE);
50         ao_i2c_send(&addr, 1, AO_MPU6000_I2C_INDEX);
51         ao_i2c_start(AO_MPU6000_I2C_INDEX, MPU6000_ADDR_READ);
52         if (len)
53                 ao_i2c_recv(data, len, AO_MPU6000_I2C_INDEX);
54         ao_i2c_stop(AO_MPU6000_I2C_INDEX);
55         ao_i2c_put(AO_MPU6000_I2C_INDEX);
56 }
57
58 static void
59 ao_mpu6000_setup(void)
60 {
61         if (ao_mpu6000_configured)
62                 return;
63
64         /* Enable the EXTI interrupt for the appropriate pin */
65         ao_enable_port(AO_MPU6000_INT_PORT);
66         ao_exti_setup(&AO_MPU6000_INT_PORT, AO_MPU6000_INT_PIN,
67                       AO_EXTI_MODE_FALLING, ao_mpu6000_isr);
68
69         ao_mpu6000_configured = 1;
70 }
71
72 static void
73 ao_mpu6000_show(void)
74 {
75         uint8_t addr;
76         uint8_t data[14];
77         uint8_t i;
78
79         ao_mpu6000_read(MPU6000_WHO_AM_I, data, 1);
80         printf ("mpu6000 WHO_AM_I: %02x\n", data[0]);
81         ao_mpu6000_read(MPU6000_ACCEL_XOUT_H, data, 1);
82         for (i = 0; i < 14; i++)
83                 printf ("reg %02x: %02x\n", i + MPU6000_ACCEL_XOUT_H, data[i]);
84 }
85
86 static const struct ao_cmds ao_mpu6000_cmds[] = {
87         { ao_mpu6000_show,      "I\0Show MPU6000 status" },
88         { 0, NULL }
89 };
90
91 void
92 ao_mpu6000_init(void)
93 {
94         ao_mpu6000_configured = 0;
95
96         ao_cmd_register(&ao_mpu6000_cmds[0]);
97 }