altos: Add MMC5983 driver
[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 #define AO_MMC5983_SPI_SPEED    ao_spi_speed(10000000)
26
27 static void
28 ao_mmc5983_start(void) {
29         ao_spi_get_bit(AO_MMC5983_SPI_CS_PORT,
30                        AO_MMC5983_SPI_CS_PIN,
31                        AO_MMC5983_SPI_INDEX,
32                        AO_MMC5983_SPI_SPEED);
33 }
34
35 static void
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);
40 }
41
42 struct ao_mmc5983_sample        ao_mmc5983_current;
43
44 static uint8_t  ao_mmc5983_configured;
45
46 static void
47 ao_mmc5983_reg_write(uint8_t addr, uint8_t data)
48 {
49         uint8_t d[2];
50
51         d[0] = addr;
52         d[1] = data;
53         ao_mmc5983_start();
54         ao_spi_send(d, 2, AO_MMC5983_SPI_INDEX);
55         ao_mmc5983_stop();
56 }
57
58 static uint8_t
59 ao_mmc5983_reg_read(uint8_t addr)
60 {
61         uint8_t d[2];
62
63         d[0] = addr | MMC5983_READ;
64         ao_mmc5983_start();
65         ao_spi_duplex(d, d, 2, AO_MMC5983_SPI_INDEX);
66         ao_mmc5983_stop();
67
68         return d[1];
69 }
70
71 static void
72 ao_mmc5983_duplex(uint8_t *dst, uint8_t len)
73 {
74         ao_mmc5983_start();
75         ao_spi_duplex(dst, dst, len, AO_MMC5983_SPI_INDEX);
76         ao_mmc5983_stop();
77 }
78
79 static uint8_t ao_mmc5983_done;
80
81 static void
82 ao_mmc5983_isr(void)
83 {
84         ao_exti_disable(AO_MMC5983_INT_PORT, AO_MMC5983_INT_PIN);
85         ao_mmc5983_done = 1;
86         ao_wakeup(&ao_mmc5983_done);
87 }
88
89 static uint32_t ao_mmc5983_missed_irq;
90
91 static void
92 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
93 {
94         struct ao_mmc5983_raw   raw;
95
96         ao_mmc5983_done = 0;
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));
108
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);
112 }
113
114 static uint8_t
115 ao_mmc5983_setup(void)
116 {
117         uint8_t product_id;
118
119         if (ao_mmc5983_configured)
120                 return 1;
121
122         /* Place device in 3-wire mode */
123         ao_mmc5983_reg_write(MMC5983_CONTROL_3,
124                              1 << MMC5983_CONTROL_3_SPI_3W);
125
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);
130
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);
134
135         /* Clear automatic measurement and 'set' operation */
136         ao_mmc5983_reg_write(MMC5983_CONTROL_2,
137                              0);
138
139         ao_mmc5983_configured = 1;
140         return 1;
141 }
142
143 struct ao_mmc5983_sample ao_mmc5983_current;
144
145 static void
146 ao_mmc5983(void)
147 {
148         struct ao_mmc5983_sample        sample;
149         ao_mmc5983_setup();
150         for (;;) {
151                 ao_mmc5983_sample(&sample);
152                 ao_arch_block_interrupts();
153                 ao_mmc5983_current = sample;
154                 AO_DATA_PRESENT(AO_DATA_MMC5983);
155                 AO_DATA_WAIT();
156                 ao_arch_release_interrupts();
157         }
158 }
159
160 static struct ao_task ao_mmc5983_task;
161
162 static void
163 ao_mmc5983_show(void)
164 {
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);
167 }
168
169 static const struct ao_cmds ao_mmc5983_cmds[] = {
170         { ao_mmc5983_show,      "M\0Show MMC5983 status" },
171         { 0, NULL }
172 };
173
174 void
175 ao_mmc5983_init(void)
176 {
177         ao_mmc5983_configured = 0;
178
179         ao_spi_init_cs(AO_MMC5983_SPI_CS_PORT, (1 << AO_MMC5983_SPI_CS_PIN));
180
181         ao_enable_port(AO_MMC5983_INT_PORT);
182         ao_exti_setup(AO_MMC5983_INT_PORT,
183                       AO_MMC5983_INT_PIN,
184                       AO_EXTI_MODE_RISING | AO_EXTI_MODE_PULL_NONE,
185                       ao_mmc5983_isr);
186
187         ao_add_task(&ao_mmc5983_task, ao_mmc5983, "mmc5983");
188         ao_cmd_register(&ao_mmc5983_cmds[0]);
189 }
190
191 #endif