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%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } } while(0)
29 struct ao_adxl375_sample ao_adxl375_current;
32 ao_adxl375_start(void) {
33 ao_spi_get_bit(AO_ADXL375_CS_PORT,
36 AO_ADXL375_SPI_SPEED);
40 ao_adxl375_stop(void) {
41 ao_spi_put_bit(AO_ADXL375_CS_PORT,
43 AO_ADXL375_SPI_INDEX);
47 ao_adxl375_reg_read(uint8_t addr)
51 d[0] = addr | AO_ADXL375_READ;
53 ao_spi_duplex(d, d, 2, AO_ADXL375_SPI_INDEX);
56 PRINTD(DEBUG_LOW, "read %x = %x\n", addr, d);
62 ao_adxl375_reg_write(uint8_t addr, uint8_t value)
66 PRINTD(DEBUG_LOW, "write %x %x\n", addr, value);
70 ao_spi_send(d, 2, AO_ADXL375_SPI_INDEX);
74 d[0] = addr | AO_ADXL375_READ
77 ao_spi_duplex(d, d, 2, AO_ADXL375_SPI_INDEX);
79 PRINTD(DEBUG_LOW, "readback %x %x\n", d[0], d[1]);
84 ao_adxl375_value(struct ao_adxl375_sample *value)
88 d[0] = AO_ADXL375_DATAX0 | AO_ADXL375_READ | AO_ADXL375_MULTI_BYTE;
90 ao_spi_duplex(d, d, 7, AO_ADXL375_SPI_INDEX);
92 memcpy(value, &d[1], 6);
95 struct ao_adxl375_total {
101 #define AO_ADXL375_SELF_TEST_SAMPLES 10
102 #define AO_ADXL375_SELF_TEST_SETTLE 4
104 #define MIN_LSB_G 18.4
105 #define MAX_LSB_G 22.6
106 #define SELF_TEST_MIN_G 6.0
107 #define SELF_TEST_MAX_G 6.8
109 #define MIN_SELF_TEST ((int32_t) (MIN_LSB_G * SELF_TEST_MIN_G * AO_ADXL375_SELF_TEST_SAMPLES + 0.5))
110 #define MAX_SELF_TEST ((int32_t) (MAX_LSB_G * SELF_TEST_MAX_G * AO_ADXL375_SELF_TEST_SAMPLES + 0.5))
113 ao_adxl375_total_value(struct ao_adxl375_total *total, int samples)
115 struct ao_adxl375_sample value;
117 *total = (struct ao_adxl375_total) { 0, 0, 0 };
118 for (int i = 0; i < samples; i++) {
119 ao_adxl375_value(&value);
123 ao_delay(AO_MS_TO_TICKS(10));
127 #define AO_ADXL375_DATA_FORMAT_SETTINGS(self_test) ( \
128 AO_ADXL375_DATA_FORMAT_FIXED | \
129 (self_test << AO_ADXL375_DATA_FORMAT_SELF_TEST) | \
130 (AO_ADXL375_DATA_FORMAT_SPI_4_WIRE << AO_ADXL375_DATA_FORMAT_SPI_4_WIRE) | \
131 (0 << AO_ADXL375_DATA_FORMAT_INT_INVERT) | \
132 (0 << AO_ADXL375_DATA_FORMAT_JUSTIFY))
134 static int32_t self_test_value;
137 ao_adxl375_setup(void)
139 /* Get the device into 4-wire SPI mode before proceeding */
140 ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
141 AO_ADXL375_DATA_FORMAT_SETTINGS(0));
144 uint8_t devid = ao_adxl375_reg_read(AO_ADXL375_DEVID);
145 if (devid != AO_ADXL375_DEVID_ID)
146 AO_SENSOR_ERROR(AO_DATA_ADXL375);
148 /* Set the data rate */
149 ao_adxl375_reg_write(AO_ADXL375_BW_RATE,
150 (0 << AO_ADXL375_BW_RATE_LOW_POWER) |
151 (AO_ADXL375_BW_RATE_RATE_200 << AO_ADXL375_BW_RATE_RATE));
153 /* Set the offsets all to zero */
154 ao_adxl375_reg_write(AO_ADXL375_OFSX, 0);
155 ao_adxl375_reg_write(AO_ADXL375_OFSY, 0);
156 ao_adxl375_reg_write(AO_ADXL375_OFSZ, 0);
158 /* Clear interrupts */
159 ao_adxl375_reg_write(AO_ADXL375_INT_ENABLE, 0);
161 /* Configure FIFO (disable) */
162 ao_adxl375_reg_write(AO_ADXL375_FIFO_CTL,
163 (AO_ADXL375_FIFO_CTL_FIFO_MODE_BYPASS << AO_ADXL375_FIFO_CTL_FIFO_MODE) |
164 (0 << AO_ADXL375_FIFO_CTL_TRIGGER) |
165 (0 << AO_ADXL375_FIFO_CTL_SAMPLES));
167 /* Place part in measurement mode */
168 ao_adxl375_reg_write(AO_ADXL375_POWER_CTL,
169 (0 << AO_ADXL375_POWER_CTL_LINK) |
170 (0 << AO_ADXL375_POWER_CTL_AUTO_SLEEP) |
171 (1 << AO_ADXL375_POWER_CTL_MEASURE) |
172 (0 << AO_ADXL375_POWER_CTL_SLEEP) |
173 (AO_ADXL375_POWER_CTL_WAKEUP_8 << AO_ADXL375_POWER_CTL_WAKEUP));
175 /* Perform self-test */
177 struct ao_adxl375_total self_test_off, self_test_on;
179 /* Discard some samples to let it settle down */
180 ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SETTLE);
182 /* Get regular values */
183 ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SAMPLES);
185 /* Turn on self test */
186 ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
187 AO_ADXL375_DATA_FORMAT_SETTINGS(1));
189 /* Discard at least 4 samples to let the device settle */
190 ao_adxl375_total_value(&self_test_on, AO_ADXL375_SELF_TEST_SETTLE);
192 /* Read self test samples */
193 ao_adxl375_total_value(&self_test_on, AO_ADXL375_SELF_TEST_SAMPLES);
195 /* Reset back to normal mode */
197 ao_adxl375_reg_write(AO_ADXL375_DATA_FORMAT,
198 AO_ADXL375_DATA_FORMAT_SETTINGS(0));
200 /* Verify Z axis value is in range */
202 int32_t z_change = self_test_on.z - self_test_off.z;
204 self_test_value = z_change;
206 if (z_change < MIN_SELF_TEST)
207 AO_SENSOR_ERROR(AO_DATA_ADXL375);
209 /* This check is commented out as maximum self test is unreliable
211 if (z_change > MAX_SELF_TEST)
212 AO_SENSOR_ERROR(AO_DATA_ADXL375);
216 /* Discard some samples to let it settle down */
217 ao_adxl375_total_value(&self_test_off, AO_ADXL375_SELF_TEST_SETTLE);
225 ao_adxl375_value(&ao_adxl375_current);
227 AO_DATA_PRESENT(AO_DATA_ADXL375);
233 static struct ao_task ao_adxl375_task;
236 ao_adxl375_dump(void)
238 printf ("ADXL375 value %d %d %d self test %d min %d max %d\n",
239 ao_adxl375_current.x,
240 ao_adxl375_current.y,
241 ao_adxl375_current.z,
247 const struct ao_cmds ao_adxl375_cmds[] = {
248 { ao_adxl375_dump, "A\0Display ADXL375 data" },
253 ao_adxl375_init(void)
255 ao_cmd_register(ao_adxl375_cmds);
256 ao_spi_init_cs(AO_ADXL375_CS_PORT, (1 << AO_ADXL375_CS_PIN));
258 ao_add_task(&ao_adxl375_task, ao_adxl375, "adxl375");