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