2 * Copyright © 2021 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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_mmc5983.h>
25 struct ao_mmc5983_sample ao_mmc5983_current;
27 static uint8_t ao_mmc5983_configured;
30 #include <ao_i2c_bit.h>
33 ao_mmc5983_reg_write(uint8_t addr, uint8_t data)
40 ao_i2c_bit_start(MMC5983_I2C_ADDR);
41 ao_i2c_bit_send(d, 2);
46 ao_mmc5983_reg_read(uint8_t addr)
50 ao_i2c_bit_start(MMC5983_I2C_ADDR);
52 ao_i2c_bit_send(d, 1);
53 ao_i2c_bit_restart(MMC5983_I2C_ADDR | 1);
54 ao_i2c_bit_recv(d, 1);
60 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
62 struct ao_mmc5983_raw raw;
64 ao_i2c_bit_start(MMC5983_I2C_ADDR);
65 raw.addr = MMC5983_X_OUT_0;
66 ao_i2c_bit_send(&raw.addr, 1);
67 ao_i2c_bit_restart(MMC5983_I2C_ADDR | 1);
68 ao_i2c_bit_recv(&raw.x0, 7);
71 sample->x = raw.x0 << 10 | raw.x1 << 2 | ((raw.xyz2 >> 6) & 3);
72 sample->y = raw.y0 << 10 | raw.y1 << 2 | ((raw.xyz2 >> 4) & 3);
73 sample->z = raw.z0 << 10 | raw.z1 << 2 | ((raw.xyz2 >> 2) & 3);
77 #define AO_MMC5983_SPI_SPEED ao_spi_speed(2000000)
80 ao_mmc5983_start(void) {
81 ao_spi_get_bit(AO_MMC5983_SPI_CS_PORT,
82 AO_MMC5983_SPI_CS_PIN,
84 AO_MMC5983_SPI_SPEED);
88 ao_mmc5983_stop(void) {
89 ao_spi_put_bit(AO_MMC5983_SPI_CS_PORT,
90 AO_MMC5983_SPI_CS_PIN,
91 AO_MMC5983_SPI_INDEX);
96 ao_mmc5983_reg_write(uint8_t addr, uint8_t data)
103 ao_spi_send(d, 2, AO_MMC5983_SPI_INDEX);
108 ao_mmc5983_reg_read(uint8_t addr)
112 d[0] = addr | MMC5983_READ;
114 ao_spi_duplex(d, d, 2, AO_MMC5983_SPI_INDEX);
121 ao_mmc5983_duplex(uint8_t *dst, uint8_t len)
124 ao_spi_duplex(dst, dst, len, AO_MMC5983_SPI_INDEX);
129 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
131 struct ao_mmc5983_raw raw;
133 raw.addr = MMC5983_X_OUT_0 | MMC5983_READ;
134 ao_mmc5983_duplex((uint8_t *) &raw, sizeof (raw));
136 sample->x = raw.x0 << 10 | raw.x1 << 2 | ((raw.xyz2 >> 6) & 3);
137 sample->y = raw.y0 << 10 | raw.y1 << 2 | ((raw.xyz2 >> 4) & 3);
138 sample->z = raw.z0 << 10 | raw.z1 << 2 | ((raw.xyz2 >> 2) & 3);
142 static uint8_t product_id;
145 ao_mmc5983_setup(void)
148 if (ao_mmc5983_configured)
151 /* Delay for power up time (10ms) */
152 ao_delay(AO_MS_TO_TICKS(10));
154 ao_mmc5983_reg_write(MMC5983_CONTROL_1,
155 1 << MMC5983_CONTROL_1_SW_RST);
157 /* Delay for power up time (10ms) */
158 ao_delay(AO_MS_TO_TICKS(10));
160 /* Check product ID */
161 product_id = ao_mmc5983_reg_read(MMC5983_PRODUCT_ID);
162 if (product_id != MMC5983_PRODUCT_ID_PRODUCT_I2C &&
163 product_id != MMC5983_PRODUCT_ID_PRODUCT_SPI)
165 AO_SENSOR_ERROR(AO_DATA_MMC5983);
168 /* Set bandwidth to 200Hz */
169 ao_mmc5983_reg_write(MMC5983_CONTROL_1,
170 MMC5983_CONTROL_1_BW_200 << MMC5983_CONTROL_1_BW);
172 /* Measure at 200Hz so we get recent samples by just reading
175 ao_mmc5983_reg_write(MMC5983_CONTROL_2,
176 (1 << MMC5983_CONTROL_2_CMM_EN) |
177 (MMC5983_CONTROL_2_CM_FREQ_200HZ << MMC5983_CONTROL_2_CM_FREQ));
179 ao_mmc5983_configured = 1;
183 struct ao_mmc5983_sample ao_mmc5983_current;
188 struct ao_mmc5983_sample sample;
191 ao_mmc5983_sample(&sample);
192 ao_arch_block_interrupts();
193 ao_mmc5983_current = sample;
194 AO_DATA_PRESENT(AO_DATA_MMC5983);
196 ao_arch_release_interrupts();
200 static struct ao_task ao_mmc5983_task;
203 ao_mmc5983_show(void)
205 printf ("MMC5983: %d %d %d\n",
206 ao_mmc5983_along(&ao_mmc5983_current),
207 ao_mmc5983_across(&ao_mmc5983_current),
208 ao_mmc5983_through(&ao_mmc5983_current));
211 static const struct ao_cmds ao_mmc5983_cmds[] = {
212 { ao_mmc5983_show, "M\0Show MMC5983 status" },
217 ao_mmc5983_init(void)
219 ao_mmc5983_configured = 0;
222 ao_enable_output(AO_MMC5983_SPI_CS_PORT, AO_MMC5983_SPI_CS_PIN, 1);
224 ao_enable_input(AO_MMC5983_SPI_MISO_PORT,
225 AO_MMC5983_SPI_MISO_PIN,
226 AO_EXTI_MODE_PULL_NONE);
228 ao_enable_output(AO_MMC5983_SPI_CLK_PORT,
229 AO_MMC5983_SPI_CLK_PIN,
232 ao_enable_output(AO_MMC5983_SPI_MOSI_PORT,
233 AO_MMC5983_SPI_MOSI_PIN,
236 ao_spi_init_cs(AO_MMC5983_SPI_CS_PORT, (1 << AO_MMC5983_SPI_CS_PIN));
239 ao_add_task(&ao_mmc5983_task, ao_mmc5983, "mmc5983");
240 ao_cmd_register(&ao_mmc5983_cmds[0]);