2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include <ao_hmc5883.h>
22 static uint8_t ao_hmc5883_configured;
24 static uint8_t ao_hmc5883_addr;
27 ao_hmc5883_reg_write(uint8_t addr, uint8_t 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;
41 ao_hmc5883_read(uint8_t addr, uint8_t *data, uint8_t len)
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);
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;
54 static uint8_t ao_hmc5883_done;
59 ao_exti_disable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
61 ao_wakeup(&ao_hmc5883_done);
65 ao_hmc5883_sample(struct ao_hmc5883_sample *sample)
67 uint16_t *d = (uint16_t *) sample;
68 int i = sizeof (*sample) / 2;
69 uint8_t single = HMC5883_MODE_SINGLE;
72 ao_exti_enable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
73 ao_hmc5883_reg_write(HMC5883_MODE, HMC5883_MODE_SINGLE);
76 while (!ao_hmc5883_done)
77 ao_sleep(&ao_hmc5883_done);
80 printf ("done %d\n", ao_hmc5883_done);
82 ao_hmc5883_read(HMC5883_X_MSB, (uint8_t *) sample, sizeof (struct ao_hmc5883_sample));
83 #if __BYTE_ORDER == __LITTLE_ENDIAN
87 *d++ = (t >> 8) | (t << 8);
93 ao_hmc5883_setup(void)
97 if (ao_hmc5883_configured)
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);
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));
113 ao_hmc5883_reg_write(HMC5883_CONFIG_B,
114 (HMC5883_CONFIG_B_GN_1_3 << HMC5883_CONFIG_B_GN));
116 ao_hmc5883_configured = 1;
121 ao_hmc5883_show(void)
124 struct ao_hmc5883_sample sample;
126 if (!ao_hmc5883_setup()) {
127 printf("hmc5883 not present\n");
131 for (addr = 0; addr <= 12; addr++) {
132 ao_hmc5883_read(addr, &data, 1);
133 printf ("hmc5883 register %2d: %02x\n",
137 ao_hmc5883_sample(&sample);
138 printf ("X: %d Y: %d Z: %d\n", sample.x, sample.y, sample.z);
141 static const struct ao_cmds ao_hmc5883_cmds[] = {
142 { ao_hmc5883_show, "M\0Show HMC5883 status" },
147 ao_hmc5883_init(void)
149 ao_hmc5883_configured = 0;
151 ao_enable_port(AO_HMC5883_INT_PORT);
152 ao_exti_setup(&AO_HMC5883_INT_PORT,
154 AO_EXTI_MODE_FALLING | AO_EXTI_MODE_PULL_UP,
157 ao_cmd_register(&ao_hmc5883_cmds[0]);