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_wake;
23 static uint8_t ao_hmc5883_configured;
28 ao_exti_disable(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN);
30 ao_wakeup(&ao_hmc5883_wake);
33 static uint8_t ao_hmc5883_addr_reg;
36 ao_hmc5883_update_addr (uint8_t len)
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;
47 ao_hmc5883_write(uint8_t addr, uint8_t *data, uint8_t len)
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);
52 ao_hmc5883_addr_reg = addr;
54 ao_i2c_send(data, len, AO_HMC5883_I2C_INDEX);
55 ao_hmc5883_update_addr(len);
57 ao_i2c_stop(AO_HMC5883_I2C_INDEX);
58 ao_i2c_put(AO_HMC5883_I2C_INDEX);
62 ao_hmc5883_read(uint8_t addr, uint8_t *data, uint8_t len)
64 ao_i2c_get(AO_HMC5883_I2C_INDEX);
65 if (addr != ao_hmc5883_addr_reg) {
66 ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_WRITE);
67 ao_i2c_send(&addr, 1, AO_HMC5883_I2C_INDEX);
68 ao_hmc5883_addr_reg = addr;
70 ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
72 ao_i2c_recv(data, len, AO_HMC5883_I2C_INDEX);
73 ao_hmc5883_update_addr(len);
75 ao_i2c_stop(AO_HMC5883_I2C_INDEX);
76 ao_i2c_put(AO_HMC5883_I2C_INDEX);
80 ao_hmc5883_setup(void)
83 if (ao_hmc5883_configured)
86 /* Enable the EXTI interrupt for the appropriate pin */
87 ao_enable_port(AO_HMC5883_INT_PORT);
88 ao_exti_setup(&AO_HMC5883_INT_PORT, AO_HMC5883_INT_PIN,
89 AO_EXTI_MODE_FALLING, ao_hmc5883_isr);
91 ao_hmc5883_addr_reg = 0xff;
93 ao_i2c_get(AO_HMC5883_I2C_INDEX);
94 present = ao_i2c_start(AO_HMC5883_I2C_INDEX, HMC5883_ADDR_READ);
95 ao_i2c_stop(AO_HMC5883_I2C_INDEX);
96 ao_i2c_put(AO_HMC5883_I2C_INDEX);
99 ao_hmc5883_configured = 1;
104 ao_hmc5883_show(void)
108 for (addr = 0x00; addr <= 0x7f; addr++)
110 ao_i2c_get(AO_HMC5883_I2C_INDEX);
111 data = ao_i2c_start(AO_HMC5883_I2C_INDEX, addr << 1);
112 ao_i2c_stop(AO_HMC5883_I2C_INDEX);
113 ao_i2c_put(AO_HMC5883_I2C_INDEX);
115 printf("address %02x responds\n", addr << 1);
117 if (!ao_hmc5883_setup()) {
118 printf("hmc5883 not present\n");
121 for (addr = 0; addr <= 12; addr++) {
122 ao_hmc5883_read(addr, &data, 1);
123 printf ("hmc5883 register %2d: %02x\n",
128 static const struct ao_cmds ao_hmc5883_cmds[] = {
129 { ao_hmc5883_show, "M\0Show HMC5883 status" },
134 ao_hmc5883_init(void)
136 ao_hmc5883_configured = 0;
138 ao_cmd_register(&ao_hmc5883_cmds[0]);