43d04f70c5edae83f3e1861bfc5aa0dcc6cf1c7a
[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, FALSE);
52         ao_hmc5883_addr_reg = addr;
53         ao_i2c_send(data, len, AO_HMC5883_I2C_INDEX, TRUE);
54         ao_hmc5883_update_addr(len);
55         ao_i2c_put(AO_HMC5883_I2C_INDEX);
56 }
57
58 static void
59 ao_hmc5883_read(uint8_t addr, uint8_t *data, uint8_t len)
60 {
61         ao_i2c_get(AO_HMC5883_I2C_INDEX);
62         if (addr != ao_hmc5883_addr_reg) {
63                 ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_WRITE);
64                 ao_i2c_send(&addr, 1, AO_HMC5883_I2C_INDEX, FALSE);
65                 ao_hmc5883_addr_reg = addr;
66         }
67         ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
68         ao_i2c_recv(data, len, AO_HMC5883_I2C_INDEX, TRUE);
69         ao_hmc5883_update_addr(len);
70         ao_i2c_put(AO_HMC5883_I2C_INDEX);
71 }
72
73 static uint8_t
74 ao_hmc5883_setup(void)
75 {
76         uint8_t present;
77         if (ao_hmc5883_configured)
78                 return 1;
79
80         /* Enable the EXTI interrupt for the appropriate pin */
81         ao_enable_port(AO_HMC5883_INT_PORT);
82         ao_exti_setup(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN,
83                       AO_EXTI_MODE_FALLING, ao_hmc5883_isr);
84
85         ao_hmc5883_addr_reg = 0xff;
86
87         ao_i2c_get(AO_HMC5883_I2C_INDEX);
88         present = ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
89         ao_i2c_recv(NULL, 0, AO_HMC5883_I2C_INDEX, TRUE);
90         ao_i2c_put(AO_HMC5883_I2C_INDEX);
91         if (!present)
92                 return 0;
93         ao_hmc5883_configured = 1;
94         return 1;
95 }
96
97 static void
98 ao_hmc5883_show(void)
99 {
100         uint8_t addr, data;
101
102         for (addr = 0x00; addr <= 0x7f; addr++)
103         {
104                 ao_i2c_get(AO_HMC5883_I2C_INDEX);
105                 data = ao_i2c_start(AO_HMC5883_I2C_INDEX, addr << 1);
106                 ao_i2c_recv(NULL, 0, AO_HMC5883_I2C_INDEX, TRUE);
107                 ao_i2c_put(AO_HMC5883_I2C_INDEX);
108                 if (data)
109                         printf("address %02x responds\n", addr << 1);
110         }
111         if (!ao_hmc5883_setup()) {
112                 printf("hmc5883 not present\n");
113                 return;
114         }
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 }
121
122 static const struct ao_cmds ao_hmc5883_cmds[] = {
123         { ao_hmc5883_show,      "M\0Show HMC5883 status" },
124         { 0, NULL }
125 };
126
127 void
128 ao_hmc5883_init(void)
129 {
130         ao_hmc5883_configured = 0;
131
132         ao_cmd_register(&ao_hmc5883_cmds[0]);
133 }