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;
25 ao_hmc5883_reg_write(uint8_t addr, uint8_t 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);
38 ao_hmc5883_read(uint8_t addr, uint8_t *data, uint8_t len)
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);
48 static uint8_t ao_hmc5883_done;
53 ao_exti_disable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
55 ao_wakeup(&ao_hmc5883_done);
59 ao_hmc5883_sample(struct ao_hmc5883_sample *sample)
61 uint16_t *d = (uint16_t *) sample;
62 int i = sizeof (*sample) / 2;
63 uint8_t single = HMC5883_MODE_SINGLE;
66 ao_exti_enable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
67 ao_hmc5883_reg_write(HMC5883_MODE, HMC5883_MODE_SINGLE);
69 while (!ao_hmc5883_done)
70 ao_sleep(&ao_hmc5883_done);
72 ao_hmc5883_read(HMC5883_X_MSB, (uint8_t *) sample, sizeof (struct ao_hmc5883_sample));
73 #if __BYTE_ORDER == __LITTLE_ENDIAN
77 *d++ = (t >> 8) | (t << 8);
83 ao_hmc5883_setup(void)
86 if (ao_hmc5883_configured)
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);
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);
100 ao_hmc5883_configured = 1;
105 ao_hmc5883_show(void)
108 struct ao_hmc5883_sample sample;
110 if (!ao_hmc5883_setup()) {
111 printf("hmc5883 not present\n");
115 for (addr = 0; addr <= 12; addr++) {
116 ao_hmc5883_read(addr, &data, 1);
117 printf ("hmc5883 register %2d: %02x\n",
121 ao_hmc5883_sample(&sample);
122 printf ("X: %d Y: %d Z: %d\n", sample.x, sample.y, sample.z);
125 static const struct ao_cmds ao_hmc5883_cmds[] = {
126 { ao_hmc5883_show, "M\0Show HMC5883 status" },
131 ao_hmc5883_init(void)
133 ao_hmc5883_configured = 0;
135 ao_exti_setup(&AO_HMC5883_INT_PORT,
137 AO_EXTI_MODE_FALLING,
140 ao_cmd_register(&ao_hmc5883_cmds[0]);