altos: Change adxl375 self test parameters
[fw/altos] / src / drivers / ao_adxl375.c
1 /*
2  * Copyright © 2018 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
15 #include <ao.h>
16 #include "ao_adxl375.h"
17
18 #define DEBUG_LOW       1
19 #define DEBUG_HIGH      2
20
21 #define DEBUG           0
22
23 #if DEBUG
24 #define PRINTD(l, ...) do { if (DEBUG & (l)) { printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } } while(0)
25 #else
26 #define PRINTD(l,...)
27 #endif
28
29 #define AO_ADXL375_SPI_SPEED    ao_spi_speed(5000000)
30
31 struct ao_adxl375_sample        ao_adxl375_current;
32
33 static void
34 ao_adxl375_start(void) {
35         ao_spi_get_bit(AO_ADXL375_CS_PORT,
36                        AO_ADXL375_CS_PIN,
37                        AO_ADXL375_SPI_INDEX,
38                        AO_ADXL375_SPI_SPEED);
39 }
40
41 static void
42 ao_adxl375_stop(void) {
43         ao_spi_put_bit(AO_ADXL375_CS_PORT,
44                        AO_ADXL375_CS_PIN,
45                        AO_ADXL375_SPI_INDEX);
46 }
47
48 static uint8_t
49 ao_adxl375_reg_read(uint8_t addr)
50 {
51         uint8_t d[2];
52
53         d[0] = addr | AO_ADXL375_READ;
54         ao_adxl375_start();
55         ao_spi_duplex(d, d, 2, AO_ADXL375_SPI_INDEX);
56         ao_adxl375_stop();
57
58         PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d);
59
60         return d[1];
61 }
62
63 static void
64 ao_adxl375_reg_write(uint8_t addr, uint8_t value)
65 {
66         uint8_t d[2];
67
68         PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
69         d[0] = addr;
70         d[1] = value;
71         ao_adxl375_start();
72         ao_spi_send(d, 2, AO_ADXL375_SPI_INDEX);
73         ao_adxl375_stop();
74
75 #if DEBUG & DEBUG_LOW
76         d[0] = addr | AO_ADXL375_READ
77         d[1] = 0;
78         ao_adxl375_start();
79         ao_spi_duplex(d, d, 2, AO_ADXL375_SPI_INDEX);
80         ao_adxl375_stop();
81         PRINTD(DEBUG_LOW, "readback %x %x\n", d[0], d[1]);
82 #endif
83 }
84
85 static void
86 ao_adxl375_value(struct ao_adxl375_sample *value)
87 {
88         uint8_t d[7];
89
90         d[0] = AO_ADXL375_DATAX0 | AO_ADXL375_READ | AO_ADXL375_MULTI_BYTE;
91         ao_adxl375_start();
92         ao_spi_duplex(d, d, 7, AO_ADXL375_SPI_INDEX);
93         ao_adxl375_stop();
94         memcpy(value, &d[1], 6);
95 }
96
97 struct ao_adxl375_total {
98         int32_t x;
99         int32_t y;
100         int32_t z;
101 };
102
103 #define AO_ADXL375_SELF_TEST_SAMPLES    10
104 #define AO_ADXL375_SELF_TEST_SETTLE     4
105
106 #define MIN_LSB_G       18.4
107 #define MAX_LSB_G       22.6
108 #define SELF_TEST_MIN_G 5.0
109 #define SELF_TEST_MAX_G 6.8
110
111 #define MIN_SELF_TEST   ((int32_t) (MIN_LSB_G * SELF_TEST_MIN_G * AO_ADXL375_SELF_TEST_SAMPLES + 0.5))
112 #define MAX_SELF_TEST   ((int32_t) (MAX_LSB_G * SELF_TEST_MAX_G * AO_ADXL375_SELF_TEST_SAMPLES + 0.5))
113
114 static void
115 ao_adxl375_total_value(struct ao_adxl375_total *total, int samples)
116 {
117         struct ao_adxl375_sample        value;
118
119         *total = (struct ao_adxl375_total) { 0, 0, 0 };
120         for (int i = 0; i < samples; i++) {
121                 ao_adxl375_value(&value);
122                 total->x += value.x;
123                 total->y += value.y;
124                 total->z += value.z;
125                 ao_delay(AO_MS_TO_TICKS(10));
126         }
127 }
128
129 #define AO_ADXL375_DATA_FORMAT_SETTINGS(self_test) (                    \
130                 AO_ADXL375_DATA_FORMAT_FIXED |                          \
131                 (self_test << AO_ADXL375_DATA_FORMAT_SELF_TEST) |       \
132                 (AO_ADXL375_DATA_FORMAT_SPI_4_WIRE << AO_ADXL375_DATA_FORMAT_SPI_4_WIRE) | \
133                 (0 << AO_ADXL375_DATA_FORMAT_INT_INVERT) |              \
134                 (0 << AO_ADXL375_DATA_FORMAT_JUSTIFY))
135
136 static int32_t  self_test_value;
137
138 static void
139 ao_adxl375_setup(void)
140 {
141         /* Get the device into 4-wire SPI mode before proceeding */
142         ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
143                              AO_ADXL375_DATA_FORMAT_SETTINGS(0));
144
145
146         uint8_t devid = ao_adxl375_reg_read(AO_ADXL375_DEVID);
147         if (devid != AO_ADXL375_DEVID_ID)
148                 AO_SENSOR_ERROR(AO_DATA_ADXL375);
149
150         /* Set the data rate */
151         ao_adxl375_reg_write(AO_ADXL375_BW_RATE,
152                              (0 << AO_ADXL375_BW_RATE_LOW_POWER) |
153                              (AO_ADXL375_BW_RATE_RATE_200 << AO_ADXL375_BW_RATE_RATE));
154
155         /* Set the offsets all to zero */
156         ao_adxl375_reg_write(AO_ADXL375_OFSX, 0);
157         ao_adxl375_reg_write(AO_ADXL375_OFSY, 0);
158         ao_adxl375_reg_write(AO_ADXL375_OFSZ, 0);
159
160         /* Clear interrupts */
161         ao_adxl375_reg_write(AO_ADXL375_INT_ENABLE, 0);
162
163         /* Configure FIFO (disable) */
164         ao_adxl375_reg_write(AO_ADXL375_FIFO_CTL,
165                              (AO_ADXL375_FIFO_CTL_FIFO_MODE_BYPASS << AO_ADXL375_FIFO_CTL_FIFO_MODE) |
166                              (0 << AO_ADXL375_FIFO_CTL_TRIGGER) |
167                              (0 << AO_ADXL375_FIFO_CTL_SAMPLES));
168
169         /* Place part in measurement mode */
170         ao_adxl375_reg_write(AO_ADXL375_POWER_CTL,
171                              (0 << AO_ADXL375_POWER_CTL_LINK) |
172                              (0 << AO_ADXL375_POWER_CTL_AUTO_SLEEP) |
173                              (1 << AO_ADXL375_POWER_CTL_MEASURE) |
174                              (0 << AO_ADXL375_POWER_CTL_SLEEP) |
175                              (AO_ADXL375_POWER_CTL_WAKEUP_8 << AO_ADXL375_POWER_CTL_WAKEUP));
176
177         /* Perform self-test */
178
179         struct ao_adxl375_total self_test_off, self_test_on;
180
181         /* Discard some samples to let it settle down */
182         ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SETTLE);
183
184         /* Get regular values */
185         ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SAMPLES);
186
187         /* Turn on self test */
188         ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
189                              AO_ADXL375_DATA_FORMAT_SETTINGS(1));
190
191         /* Discard at least 4 samples to let the device settle */
192         ao_adxl375_total_value(&self_test_on, AO_ADXL375_SELF_TEST_SETTLE);
193
194         /* Read self test samples */
195         ao_adxl375_total_value(&self_test_on, AO_ADXL375_SELF_TEST_SAMPLES);
196
197         /* Reset back to normal mode */
198
199         ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
200                              AO_ADXL375_DATA_FORMAT_SETTINGS(0));
201
202         /* Verify Z axis value is in range */
203
204         int32_t z_change = self_test_on.z - self_test_off.z;
205
206         self_test_value = z_change;
207
208         if (z_change < MIN_SELF_TEST)
209                 AO_SENSOR_ERROR(AO_DATA_ADXL375);
210
211         /* This check is commented out as maximum self test is unreliable
212
213            if (z_change > MAX_SELF_TEST)
214                 AO_SENSOR_ERROR(AO_DATA_ADXL375);
215
216         */
217
218         /* Discard some samples to let it settle down */
219         ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SETTLE);
220 }
221
222 static void
223 ao_adxl375(void)
224 {
225         ao_adxl375_setup();
226         for (;;) {
227                 ao_adxl375_value(&ao_adxl375_current);
228                 ao_arch_critical(
229                         AO_DATA_PRESENT(AO_DATA_ADXL375);
230                         AO_DATA_WAIT();
231                         );
232         }
233 }
234
235 static struct ao_task ao_adxl375_task;
236
237 static void
238 ao_adxl375_dump(void)
239 {
240         printf ("ADXL375 value %d %d %d self test %ld min %ld max %ld\n",
241                 ao_adxl375_current.x,
242                 ao_adxl375_current.y,
243                 ao_adxl375_current.z,
244                 (long) self_test_value,
245                 (long) MIN_SELF_TEST,
246                 (long) MAX_SELF_TEST);
247 }
248
249 const struct ao_cmds ao_adxl375_cmds[] = {
250         { ao_adxl375_dump,      "A\0Display ADXL375 data" },
251         { 0, NULL },
252 };
253
254 void
255 ao_adxl375_init(void)
256 {
257         ao_cmd_register(ao_adxl375_cmds);
258         ao_spi_init_cs(AO_ADXL375_CS_PORT, (1 << AO_ADXL375_CS_PIN));
259
260         ao_add_task(&ao_adxl375_task, ao_adxl375, "adxl375");
261 }