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);
80 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
82 struct ao_mmc5983_raw raw;
84 raw.addr = MMC5983_X_OUT_0 | MMC5983_READ;
85 ao_mmc5983_duplex((uint8_t *) &raw, sizeof (raw));
87 sample->x = raw.x0 << 10 | raw.x1 << 2 | ((raw.xyz2 >> 6) & 3);
88 sample->y = raw.y0 << 10 | raw.y1 << 2 | ((raw.xyz2 >> 4) & 3);
89 sample->z = raw.z0 << 10 | raw.z1 << 2 | ((raw.xyz2 >> 2) & 3);
93 ao_mmc5983_setup(void)
97 if (ao_mmc5983_configured)
100 /* Place device in 3-wire mode */
101 ao_mmc5983_reg_write(MMC5983_CONTROL_3,
102 1 << MMC5983_CONTROL_3_SPI_3W);
104 /* Check product ID */
105 product_id = ao_mmc5983_reg_read(MMC5983_PRODUCT_ID);
106 if (product_id != MMC5983_PRODUCT_ID_PRODUCT)
107 AO_SENSOR_ERROR(AO_DATA_MMC5983);
109 /* Set bandwidth to 200Hz */
110 ao_mmc5983_reg_write(MMC5983_CONTROL_1,
111 MMC5983_CONTROL_1_BW_200 << MMC5983_CONTROL_1_BW);
113 /* Measure at 200Hz so we get recent samples by just reading
116 ao_mmc5983_reg_write(MMC5983_CONTROL_2,
117 (1 << MMC5983_CONTROL_2_CMM_EN) |
118 (MMC5983_CONTROL_2_CM_FREQ_200HZ << MMC5983_CONTROL_2_CM_FREQ));
120 ao_mmc5983_configured = 1;
124 struct ao_mmc5983_sample ao_mmc5983_current;
129 struct ao_mmc5983_sample sample;
132 ao_mmc5983_sample(&sample);
133 ao_arch_block_interrupts();
134 ao_mmc5983_current = sample;
135 AO_DATA_PRESENT(AO_DATA_MMC5983);
137 ao_arch_release_interrupts();
141 static struct ao_task ao_mmc5983_task;
144 ao_mmc5983_show(void)
146 printf ("X: %d Z: %d Y: %d\n",
147 ao_mmc5983_current.x, ao_mmc5983_current.z, ao_mmc5983_current.y);
150 static const struct ao_cmds ao_mmc5983_cmds[] = {
151 { ao_mmc5983_show, "M\0Show MMC5983 status" },
156 ao_mmc5983_init(void)
158 ao_mmc5983_configured = 0;
160 ao_spi_init_cs(AO_MMC5983_SPI_CS_PORT, (1 << AO_MMC5983_SPI_CS_PIN));
162 ao_add_task(&ao_mmc5983_task, ao_mmc5983, "mmc5983");
163 ao_cmd_register(&ao_mmc5983_cmds[0]);