81bf8f2ba9f50c4c64cf629c4f74c9d122760833
[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 void
80 ao_mmc5983_sample(struct ao_mmc5983_sample *sample)
81 {
82         struct ao_mmc5983_raw   raw;
83
84         raw.addr = MMC5983_X_OUT_0 | MMC5983_READ;
85         ao_mmc5983_duplex((uint8_t *) &raw, sizeof (raw));
86
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);
90 }
91
92 static uint8_t
93 ao_mmc5983_setup(void)
94 {
95         uint8_t product_id;
96
97         if (ao_mmc5983_configured)
98                 return 1;
99
100         /* Place device in 3-wire mode */
101         ao_mmc5983_reg_write(MMC5983_CONTROL_3,
102                              1 << MMC5983_CONTROL_3_SPI_3W);
103
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);
108
109         /* Set bandwidth to 200Hz */
110         ao_mmc5983_reg_write(MMC5983_CONTROL_1,
111                              MMC5983_CONTROL_1_BW_200 << MMC5983_CONTROL_1_BW);
112
113         /* Measure at 200Hz so we get recent samples by just reading
114          * the registers
115          */
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));
119
120         ao_mmc5983_configured = 1;
121         return 1;
122 }
123
124 struct ao_mmc5983_sample ao_mmc5983_current;
125
126 static void
127 ao_mmc5983(void)
128 {
129         struct ao_mmc5983_sample        sample;
130         ao_mmc5983_setup();
131         for (;;) {
132                 ao_mmc5983_sample(&sample);
133                 ao_arch_block_interrupts();
134                 ao_mmc5983_current = sample;
135                 AO_DATA_PRESENT(AO_DATA_MMC5983);
136                 AO_DATA_WAIT();
137                 ao_arch_release_interrupts();
138         }
139 }
140
141 static struct ao_task ao_mmc5983_task;
142
143 static void
144 ao_mmc5983_show(void)
145 {
146         printf ("X: %d Z: %d Y: %d\n",
147                 ao_mmc5983_current.x, ao_mmc5983_current.z, ao_mmc5983_current.y);
148 }
149
150 static const struct ao_cmds ao_mmc5983_cmds[] = {
151         { ao_mmc5983_show,      "M\0Show MMC5983 status" },
152         { 0, NULL }
153 };
154
155 void
156 ao_mmc5983_init(void)
157 {
158         ao_mmc5983_configured = 0;
159
160         ao_spi_init_cs(AO_MMC5983_SPI_CS_PORT, (1 << AO_MMC5983_SPI_CS_PIN));
161
162         ao_add_task(&ao_mmc5983_task, ao_mmc5983, "mmc5983");
163         ao_cmd_register(&ao_mmc5983_cmds[0]);
164 }
165
166 #endif