altos: Get HMC5883 driver limping along
[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_configured;
23
24 static uint8_t  ao_hmc5883_addr;
25
26 static void
27 ao_hmc5883_reg_write(uint8_t addr, uint8_t data)
28 {
29         uint8_t d[2];
30
31         d[0] = addr;
32         d[1] = data;
33         ao_i2c_get(AO_HMC5883_I2C_INDEX);
34         ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_WRITE);
35         ao_i2c_send(d, 2, AO_HMC5883_I2C_INDEX, TRUE);
36         ao_i2c_put(AO_HMC5883_I2C_INDEX);
37         ao_hmc5883_addr = addr + 1;
38 }
39
40 static void
41 ao_hmc5883_read(uint8_t addr, uint8_t *data, uint8_t len)
42 {
43         ao_i2c_get(AO_HMC5883_I2C_INDEX);
44         if (addr != ao_hmc5883_addr) {
45                 ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_WRITE);
46                 ao_i2c_send(&addr, 1, AO_HMC5883_I2C_INDEX, FALSE);
47         }
48         ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
49         ao_i2c_recv(data, len, AO_HMC5883_I2C_INDEX, TRUE);
50         ao_i2c_put(AO_HMC5883_I2C_INDEX);
51         ao_hmc5883_addr = 0xff;
52 }
53
54 static uint8_t ao_hmc5883_done;
55
56 static void
57 ao_hmc5883_isr(void)
58 {
59         ao_exti_disable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
60         ++ao_hmc5883_done;
61         ao_wakeup(&ao_hmc5883_done);
62 }
63
64 void
65 ao_hmc5883_sample(struct ao_hmc5883_sample *sample)
66 {
67         uint16_t        *d = (uint16_t *) sample;
68         int             i = sizeof (*sample) / 2;
69         uint8_t         single = HMC5883_MODE_SINGLE;
70
71         ao_hmc5883_done = 0;
72         ao_exti_enable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
73         ao_hmc5883_reg_write(HMC5883_MODE, HMC5883_MODE_SINGLE);
74
75         cli();
76         while (!ao_hmc5883_done)
77                 ao_sleep(&ao_hmc5883_done);
78         sei();
79
80         printf ("done %d\n", ao_hmc5883_done);
81
82         ao_hmc5883_read(HMC5883_X_MSB, (uint8_t *) sample, sizeof (struct ao_hmc5883_sample));
83 #if __BYTE_ORDER == __LITTLE_ENDIAN
84         /* byte swap */
85         while (i--) {
86                 uint16_t        t = *d;
87                 *d++ = (t >> 8) | (t << 8);
88         }
89 #endif
90 }
91
92 static uint8_t
93 ao_hmc5883_setup(void)
94 {
95         uint8_t d;
96         uint8_t present;
97         if (ao_hmc5883_configured)
98                 return 1;
99
100         ao_i2c_get(AO_HMC5883_I2C_INDEX);
101         present = ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
102         ao_i2c_recv(&d, 1, AO_HMC5883_I2C_INDEX, TRUE);
103         ao_i2c_put(AO_HMC5883_I2C_INDEX);
104
105         if (!present)
106                 return 0;
107
108         ao_hmc5883_reg_write(HMC5883_CONFIG_A,
109                              (HMC5883_CONFIG_A_MA_8 << HMC5883_CONFIG_A_MA) |
110                              (HMC5883_CONFIG_A_DO_15 << HMC5883_CONFIG_A_DO) |
111                              (HMC5883_CONFIG_A_MS_NORMAL << HMC5883_CONFIG_A_MS));
112
113         ao_hmc5883_reg_write(HMC5883_CONFIG_B,
114                              (HMC5883_CONFIG_B_GN_1_3 << HMC5883_CONFIG_B_GN));
115
116         ao_hmc5883_configured = 1;
117         return 1;
118 }
119
120 static void
121 ao_hmc5883_show(void)
122 {
123         uint8_t addr, data;
124         struct ao_hmc5883_sample sample;
125
126         if (!ao_hmc5883_setup()) {
127                 printf("hmc5883 not present\n");
128                 return;
129         }
130 #if 0
131         for (addr = 0; addr <= 12; addr++) {
132                 ao_hmc5883_read(addr, &data, 1);
133                 printf ("hmc5883 register %2d: %02x\n",
134                         addr, data);
135         }
136 #endif
137         ao_hmc5883_sample(&sample);
138         printf ("X: %d Y: %d Z: %d\n", sample.x, sample.y, sample.z);
139 }
140
141 static const struct ao_cmds ao_hmc5883_cmds[] = {
142         { ao_hmc5883_show,      "M\0Show HMC5883 status" },
143         { 0, NULL }
144 };
145
146 void
147 ao_hmc5883_init(void)
148 {
149         ao_hmc5883_configured = 0;
150
151         ao_enable_port(AO_HMC5883_INT_PORT);
152         ao_exti_setup(&AO_HMC5883_INT_PORT,
153                       AO_HMC5883_INT_PIN,
154                       AO_EXTI_MODE_FALLING | AO_EXTI_MODE_PULL_UP,
155                       ao_hmc5883_isr);
156
157         ao_cmd_register(&ao_hmc5883_cmds[0]);
158 }