altos: Add mpu6000 and hmc5883 stubs
[fw/altos] / src / drivers / ao_hmc5883.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_hmc5883.h>
20 #include <ao_exti.h>
21
22 static uint8_t  ao_hmc5883_wake;
23 static uint8_t  ao_hmc5883_configured;
24
25 static void
26 ao_hmc5883_isr(void)
27 {
28         ao_exti_disable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
29         ao_hmc5883_wake = 1;
30         ao_wakeup(&ao_hmc5883_wake);
31 }
32
33 static uint8_t  ao_hmc5883_addr_reg;
34
35 static void
36 ao_hmc5883_update_addr (uint8_t len)
37 {
38         ao_hmc5883_addr_reg += len;
39         if (ao_hmc5883_addr_reg == 9)
40                 ao_hmc5883_addr_reg = 3;
41         else if (ao_hmc5883_addr_reg > 12)
42                 ao_hmc5883_addr_reg = 0;
43                     
44 }
45
46 static void
47 ao_hmc5883_write(uint8_t addr, uint8_t *data, uint8_t len)
48 {
49         ao_i2c_get(AO_HMC5883_I2C_INDEX);
50         ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_WRITE);
51         ao_i2c_send(&addr, 1, AO_HMC5883_I2C_INDEX);
52         ao_hmc5883_addr_reg = addr;
53         if (len) {
54                 ao_i2c_send(data, len, AO_HMC5883_I2C_INDEX);
55                 ao_hmc5883_update_addr(len);
56         }
57         ao_i2c_stop(AO_HMC5883_I2C_INDEX);
58         ao_i2c_put(AO_HMC5883_I2C_INDEX);
59 }
60
61 static void
62 ao_hmc5883_read(uint8_t addr, uint8_t *data, uint8_t len)
63 {
64         ao_i2c_get(AO_HMC5883_I2C_INDEX);
65         if (addr != ao_hmc5883_addr_reg) {
66                 ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_WRITE);
67                 ao_i2c_send(&addr, 1, AO_HMC5883_I2C_INDEX);
68                 ao_hmc5883_addr_reg = addr;
69         }
70         ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
71         if (len) {
72                 ao_i2c_recv(data, len, AO_HMC5883_I2C_INDEX);
73                 ao_hmc5883_update_addr(len);
74         }
75         ao_i2c_stop(AO_HMC5883_I2C_INDEX);
76         ao_i2c_put(AO_HMC5883_I2C_INDEX);
77 }
78
79 static uint8_t
80 ao_hmc5883_setup(void)
81 {
82         uint8_t present;
83         if (ao_hmc5883_configured)
84                 return 1;
85
86         /* Enable the EXTI interrupt for the appropriate pin */
87         ao_enable_port(AO_HMC5883_INT_PORT);
88         ao_exti_setup(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN,
89                       AO_EXTI_MODE_FALLING, ao_hmc5883_isr);
90
91         ao_hmc5883_addr_reg = 0xff;
92
93         ao_i2c_get(AO_HMC5883_I2C_INDEX);
94         present = ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
95         ao_i2c_stop(AO_HMC5883_I2C_INDEX);
96         ao_i2c_put(AO_HMC5883_I2C_INDEX);
97         if (!present)
98                 return 0;
99         ao_hmc5883_configured = 1;
100         return 1;
101 }
102
103 static void
104 ao_hmc5883_show(void)
105 {
106         uint8_t addr, data;
107
108         for (addr = 0x00; addr <= 0x7f; addr++)
109         {
110                 ao_i2c_get(AO_HMC5883_I2C_INDEX);
111                 data = ao_i2c_start(AO_HMC5883_I2C_INDEX, addr << 1);
112                 ao_i2c_stop(AO_HMC5883_I2C_INDEX);
113                 ao_i2c_put(AO_HMC5883_I2C_INDEX);
114                 if (data)
115                         printf("address %02x responds\n", addr << 1);
116         }
117         if (!ao_hmc5883_setup()) {
118                 printf("hmc5883 not present\n");
119                 return;
120         }
121         for (addr = 0; addr <= 12; addr++) {
122                 ao_hmc5883_read(addr, &data, 1);
123                 printf ("hmc5883 register %2d: %02x\n",
124                         addr, data);
125         }
126 }
127
128 static const struct ao_cmds ao_hmc5883_cmds[] = {
129         { ao_hmc5883_show,      "M\0Show HMC5883 status" },
130         { 0, NULL }
131 };
132
133 void
134 ao_hmc5883_init(void)
135 {
136         ao_hmc5883_configured = 0;
137
138         ao_cmd_register(&ao_hmc5883_cmds[0]);
139 }