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 #define AO_MMC5983_SPI_SPEED ao_spi_speed(10000000)
28 ao_mmc5983_start(void) {
29 ao_spi_get_bit(AO_MMC5983_SPI_CS_PORT,
30 AO_MMC5983_SPI_CS_PIN,
32 AO_MMC5983_SPI_SPEED);
36 ao_mmc5983_stop(void) {
37 ao_spi_put_bit(AO_MMC5983_SPI_CS_PORT,
38 AO_MMC5983_SPI_CS_PIN,
39 AO_MMC5983_SPI_INDEX);
42 struct ao_mmc5983_sample ao_mmc5983_current;
44 static uint8_t ao_mmc5983_configured;
47 ao_mmc5983_reg_write(uint8_t addr, uint8_t data)
54 ao_spi_send(d, 2, AO_MMC5983_SPI_INDEX);
59 ao_mmc5983_reg_read(uint8_t addr)
63 d[0] = addr | MMC5983_READ;
65 ao_spi_duplex(d, d, 2, AO_MMC5983_SPI_INDEX);
72 ao_mmc5983_duplex(uint8_t *dst, uint8_t len)
75 ao_spi_duplex(dst, dst, len, AO_MMC5983_SPI_INDEX);
79 static uint8_t ao_mmc5983_done;
84 ao_exti_disable(AO_MMC5983_INT_PORT, AO_MMC5983_INT_PIN);
86 ao_wakeup(&ao_mmc5983_done);
89 static uint32_t ao_mmc5983_missed_irq;
92 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
94 struct ao_mmc5983_raw raw;
97 ao_exti_enable(AO_MMC5983_INT_PORT, AO_MMC5983_INT_PIN);
98 ao_mmc5983_reg_write(MMC5983_CONTROL_0,
99 (1 << MMC5983_CONTROL_0_INT_MEAS_DONE_EN) |
100 (1 << MMC5983_CONTROL_0_TM_M));
101 ao_arch_block_interrupts();
102 while (!ao_mmc5983_done)
103 if (ao_sleep_for(&ao_mmc5983_done, AO_MS_TO_TICKS(10)))
104 ++ao_mmc5983_missed_irq;
105 ao_arch_release_interrupts();
106 raw.addr = MMC5983_X_OUT_0 | MMC5983_READ;
107 ao_mmc5983_duplex((uint8_t *) &raw, sizeof (raw));
109 sample->x = raw.x0 << 10 | raw.x1 << 2 | ((raw.xyz2 >> 6) & 3);
110 sample->y = raw.y0 << 10 | raw.y1 << 2 | ((raw.xyz2 >> 4) & 3);
111 sample->z = raw.z0 << 10 | raw.z1 << 2 | ((raw.xyz2 >> 2) & 3);
115 ao_mmc5983_setup(void)
119 if (ao_mmc5983_configured)
122 /* Place device in 3-wire mode */
123 ao_mmc5983_reg_write(MMC5983_CONTROL_3,
124 1 << MMC5983_CONTROL_3_SPI_3W);
126 /* Check product ID */
127 product_id = ao_mmc5983_reg_read(MMC5983_PRODUCT_ID);
128 if (product_id != MMC5983_PRODUCT_ID_PRODUCT)
129 AO_SENSOR_ERROR(AO_DATA_MMC5983);
131 /* Set high bandwidth to reduce sample collection time */
132 ao_mmc5983_reg_write(MMC5983_CONTROL_1,
133 MMC5983_CONTROL_1_BW_800 << MMC5983_CONTROL_1_BW);
135 /* Clear automatic measurement and 'set' operation */
136 ao_mmc5983_reg_write(MMC5983_CONTROL_2,
139 ao_mmc5983_configured = 1;
143 struct ao_mmc5983_sample ao_mmc5983_current;
148 struct ao_mmc5983_sample sample;
151 ao_mmc5983_sample(&sample);
152 ao_arch_block_interrupts();
153 ao_mmc5983_current = sample;
154 AO_DATA_PRESENT(AO_DATA_MMC5983);
156 ao_arch_release_interrupts();
160 static struct ao_task ao_mmc5983_task;
163 ao_mmc5983_show(void)
165 printf ("X: %d Z: %d Y: %d missed irq: %lu\n",
166 ao_mmc5983_current.x, ao_mmc5983_current.z, ao_mmc5983_current.y, ao_mmc5983_missed_irq);
169 static const struct ao_cmds ao_mmc5983_cmds[] = {
170 { ao_mmc5983_show, "M\0Show MMC5983 status" },
175 ao_mmc5983_init(void)
177 ao_mmc5983_configured = 0;
179 ao_spi_init_cs(AO_MMC5983_SPI_CS_PORT, (1 << AO_MMC5983_SPI_CS_PIN));
181 ao_enable_port(AO_MMC5983_INT_PORT);
182 ao_exti_setup(AO_MMC5983_INT_PORT,
184 AO_EXTI_MODE_RISING | AO_EXTI_MODE_PULL_NONE,
187 ao_add_task(&ao_mmc5983_task, ao_mmc5983, "mmc5983");
188 ao_cmd_register(&ao_mmc5983_cmds[0]);