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, 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);
59 ao_hmc5883_read(uint8_t addr, uint8_t *data, uint8_t len)
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;
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);
74 ao_hmc5883_setup(void)
77 if (ao_hmc5883_configured)
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);
85 ao_hmc5883_addr_reg = 0xff;
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);
93 ao_hmc5883_configured = 1;
102 for (addr = 0x00; addr <= 0x7f; addr++)
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);
109 printf("address %02x responds\n", addr << 1);
111 if (!ao_hmc5883_setup()) {
112 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",
122 static const struct ao_cmds ao_hmc5883_cmds[] = {
123 { ao_hmc5883_show, "M\0Show HMC5883 status" },
128 ao_hmc5883_init(void)
130 ao_hmc5883_configured = 0;
132 ao_cmd_register(&ao_hmc5883_cmds[0]);