altos: Support normalized axes in mpu6000 and mmc5983
[fw/altos] / src / drivers / ao_mmc5983.c
1 /*
2  * Copyright © 2021 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_mmc5983.h>
21 #include <ao_exti.h>
22
23 #if HAS_MMC5983
24
25 struct ao_mmc5983_sample        ao_mmc5983_current;
26
27 static uint8_t  ao_mmc5983_configured;
28
29 #ifdef MMC5983_I2C
30 #include <ao_i2c_bit.h>
31
32 static void
33 ao_mmc5983_reg_write(uint8_t addr, uint8_t data)
34 {
35         uint8_t d[2];
36
37         d[0] = addr;
38         d[1] = data;
39
40         ao_i2c_bit_start(MMC5983_I2C_ADDR);
41         ao_i2c_bit_send(d, 2);
42         ao_i2c_bit_stop();
43 }
44
45 static uint8_t
46 ao_mmc5983_reg_read(uint8_t addr)
47 {
48         uint8_t d[1];
49
50         ao_i2c_bit_start(MMC5983_I2C_ADDR);
51         d[0] = addr;
52         ao_i2c_bit_send(d, 1);
53         ao_i2c_bit_restart(MMC5983_I2C_ADDR | 1);
54         ao_i2c_bit_recv(d, 1);
55         ao_i2c_bit_stop();
56         return d[0];
57 }
58
59 static void
60 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
61 {
62         struct ao_mmc5983_raw   raw;
63
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);
69         ao_i2c_bit_stop();
70
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);
74 }
75
76 #else
77 #define AO_MMC5983_SPI_SPEED    ao_spi_speed(2000000)
78
79 static void
80 ao_mmc5983_start(void) {
81         ao_spi_get_bit(AO_MMC5983_SPI_CS_PORT,
82                        AO_MMC5983_SPI_CS_PIN,
83                        AO_MMC5983_SPI_INDEX,
84                        AO_MMC5983_SPI_SPEED);
85 }
86
87 static void
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);
92 }
93
94
95 static void
96 ao_mmc5983_reg_write(uint8_t addr, uint8_t data)
97 {
98         uint8_t d[2];
99
100         d[0] = addr;
101         d[1] = data;
102         ao_mmc5983_start();
103         ao_spi_send(d, 2, AO_MMC5983_SPI_INDEX);
104         ao_mmc5983_stop();
105 }
106
107 static uint8_t
108 ao_mmc5983_reg_read(uint8_t addr)
109 {
110         uint8_t d[2];
111
112         d[0] = addr | MMC5983_READ;
113         ao_mmc5983_start();
114         ao_spi_duplex(d, d, 2, AO_MMC5983_SPI_INDEX);
115         ao_mmc5983_stop();
116
117         return d[1];
118 }
119
120 static void
121 ao_mmc5983_duplex(uint8_t *dst, uint8_t len)
122 {
123         ao_mmc5983_start();
124         ao_spi_duplex(dst, dst, len, AO_MMC5983_SPI_INDEX);
125         ao_mmc5983_stop();
126 }
127
128 static void
129 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
130 {
131         struct ao_mmc5983_raw   raw;
132
133         raw.addr = MMC5983_X_OUT_0 | MMC5983_READ;
134         ao_mmc5983_duplex((uint8_t *) &raw, sizeof (raw));
135
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);
139 }
140 #endif
141
142 static uint8_t product_id;
143
144 static uint8_t
145 ao_mmc5983_setup(void)
146 {
147
148         if (ao_mmc5983_configured)
149                 return 1;
150
151         /* Delay for power up time (10ms) */
152         ao_delay(AO_MS_TO_TICKS(10));
153
154         ao_mmc5983_reg_write(MMC5983_CONTROL_1,
155                              1 << MMC5983_CONTROL_1_SW_RST);
156
157         /* Delay for power up time (10ms) */
158         ao_delay(AO_MS_TO_TICKS(10));
159
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)
164         {
165                 AO_SENSOR_ERROR(AO_DATA_MMC5983);
166         }
167
168         /* Set bandwidth to 200Hz */
169         ao_mmc5983_reg_write(MMC5983_CONTROL_1,
170                              MMC5983_CONTROL_1_BW_200 << MMC5983_CONTROL_1_BW);
171
172         /* Measure at 200Hz so we get recent samples by just reading
173          * the registers
174          */
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));
178
179         ao_mmc5983_configured = 1;
180         return 1;
181 }
182
183 struct ao_mmc5983_sample ao_mmc5983_current;
184
185 static void
186 ao_mmc5983(void)
187 {
188         struct ao_mmc5983_sample        sample;
189         ao_mmc5983_setup();
190         for (;;) {
191                 ao_mmc5983_sample(&sample);
192                 ao_arch_block_interrupts();
193                 ao_mmc5983_current = sample;
194                 AO_DATA_PRESENT(AO_DATA_MMC5983);
195                 AO_DATA_WAIT();
196                 ao_arch_release_interrupts();
197         }
198 }
199
200 static struct ao_task ao_mmc5983_task;
201
202 static void
203 ao_mmc5983_show(void)
204 {
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));
209 }
210
211 static const struct ao_cmds ao_mmc5983_cmds[] = {
212         { ao_mmc5983_show,      "M\0Show MMC5983 status" },
213         { 0, NULL }
214 };
215
216 void
217 ao_mmc5983_init(void)
218 {
219         ao_mmc5983_configured = 0;
220
221 #ifdef MMC5983_I2C
222         ao_enable_output(AO_MMC5983_SPI_CS_PORT, AO_MMC5983_SPI_CS_PIN, 1);
223 #else
224         ao_enable_input(AO_MMC5983_SPI_MISO_PORT,
225                         AO_MMC5983_SPI_MISO_PIN,
226                         AO_EXTI_MODE_PULL_NONE);
227
228         ao_enable_output(AO_MMC5983_SPI_CLK_PORT,
229                          AO_MMC5983_SPI_CLK_PIN,
230                          1);
231
232         ao_enable_output(AO_MMC5983_SPI_MOSI_PORT,
233                          AO_MMC5983_SPI_MOSI_PIN,
234                          0);
235
236         ao_spi_init_cs(AO_MMC5983_SPI_CS_PORT, (1 << AO_MMC5983_SPI_CS_PIN));
237 #endif
238
239         ao_add_task(&ao_mmc5983_task, ao_mmc5983, "mmc5983");
240         ao_cmd_register(&ao_mmc5983_cmds[0]);
241 }
242
243 #endif