2 * Copyright © 2018 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.
16 #include "ao_adxl375.h"
24 #define PRINTD(l, ...) do { if (DEBUG & (l)) { printf ("\r%5lu %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } } while(0)
29 #define AO_ADXL375_SPI_SPEED ao_spi_speed(5000000)
31 struct ao_adxl375_sample ao_adxl375_current;
34 ao_adxl375_start(void) {
35 ao_spi_get_bit(AO_ADXL375_CS_PORT,
38 AO_ADXL375_SPI_SPEED);
42 ao_adxl375_stop(void) {
43 ao_spi_put_bit(AO_ADXL375_CS_PORT,
45 AO_ADXL375_SPI_INDEX);
49 ao_adxl375_reg_read(uint8_t addr)
53 d[0] = addr | AO_ADXL375_READ;
55 ao_spi_duplex(d, d, 2, AO_ADXL375_SPI_INDEX);
58 PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d[1]);
64 ao_adxl375_reg_write(uint8_t addr, uint8_t value)
68 PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
72 ao_spi_send(d, 2, AO_ADXL375_SPI_INDEX);
76 d[0] = addr | AO_ADXL375_READ;
79 ao_spi_duplex(d, d, 2, AO_ADXL375_SPI_INDEX);
81 PRINTD(DEBUG_LOW, "readback %x %x\n", d[0], d[1]);
86 ao_adxl375_value(struct ao_adxl375_sample *value)
90 d[0] = AO_ADXL375_DATAX0 | AO_ADXL375_READ | AO_ADXL375_MULTI_BYTE;
92 ao_spi_duplex(d, d, 7, AO_ADXL375_SPI_INDEX);
94 memcpy(value, &d[1], 6);
97 struct ao_adxl375_total {
103 #define AO_ADXL375_SELF_TEST_SAMPLES 10
104 #define AO_ADXL375_SELF_TEST_SETTLE 4
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
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))
115 ao_adxl375_total_value(struct ao_adxl375_total *total, int samples)
117 struct ao_adxl375_sample value;
119 *total = (struct ao_adxl375_total) { 0, 0, 0 };
120 for (int i = 0; i < samples; i++) {
121 ao_adxl375_value(&value);
125 ao_delay(AO_MS_TO_TICKS(10));
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) | \
133 (0 << AO_ADXL375_DATA_FORMAT_INT_INVERT) | \
134 (0 << AO_ADXL375_DATA_FORMAT_JUSTIFY))
136 static int32_t self_test_value;
139 ao_adxl375_setup(void)
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));
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);
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));
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);
160 /* Clear interrupts */
161 ao_adxl375_reg_write(AO_ADXL375_INT_ENABLE, 0);
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));
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));
177 /* Perform self-test */
179 struct ao_adxl375_total self_test_off, self_test_on;
181 /* Discard some samples to let it settle down */
182 ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SETTLE);
184 /* Get regular values */
185 ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SAMPLES);
187 /* Turn on self test */
188 ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
189 AO_ADXL375_DATA_FORMAT_SETTINGS(1));
191 /* Discard at least 4 samples to let the device settle */
192 ao_adxl375_total_value(&self_test_on, AO_ADXL375_SELF_TEST_SETTLE);
194 /* Read self test samples */
195 ao_adxl375_total_value(&self_test_on, AO_ADXL375_SELF_TEST_SAMPLES);
197 /* Reset back to normal mode */
199 ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
200 AO_ADXL375_DATA_FORMAT_SETTINGS(0));
202 /* Verify Z axis value is in range */
204 int32_t z_change = self_test_on.z - self_test_off.z;
206 self_test_value = z_change;
208 if (z_change < MIN_SELF_TEST)
209 AO_SENSOR_ERROR(AO_DATA_ADXL375);
211 /* This check is commented out as maximum self test is unreliable
213 if (z_change > MAX_SELF_TEST)
214 AO_SENSOR_ERROR(AO_DATA_ADXL375);
218 /* Discard some samples to let it settle down */
219 ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SETTLE);
227 ao_adxl375_value(&ao_adxl375_current);
229 AO_DATA_PRESENT(AO_DATA_ADXL375);
235 static struct ao_task ao_adxl375_task;
238 ao_adxl375_dump(void)
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);
249 const struct ao_cmds ao_adxl375_cmds[] = {
250 { ao_adxl375_dump, "A\0Display ADXL375 data" },
255 ao_adxl375_init(void)
257 ao_cmd_register(ao_adxl375_cmds);
258 ao_spi_init_cs(AO_ADXL375_CS_PORT, (1 << AO_ADXL375_CS_PIN));
260 ao_add_task(&ao_adxl375_task, ao_adxl375, "adxl375");