Skip to content

Commit 84c352d

Browse files
mhennerichMaureenHelm
authored andcommitted
samples: sensor: adxl372: Add ADXL372 sample application
This sample application produces slightly different outputs based on the chosen driver configuration mode. In Measuring Mode with trigger support, the acceleration on all three axis is printed in m/s^2 at the sampling rate (ODR). In polled Measuring Mode the instantaneous acceleration is polled every 2 seconds. In most high-g applications, a single (3-axis) acceleration sample at the peak of an impact event contains sufficient information about the event, and the full acceleration history is not required. In this Max Peak Detect Mode the device returns only the over threshold peak acceleration between two consecutive sample fetches or trigger events. Instead of printing the acceleration on all three axis, the sample application calculates the vector magnitude (root sum squared) and displays the result in g rather than in m/s^2, together with an bar graph. Signed-off-by: Michael Hennerich <[email protected]>
1 parent a3e7cea commit 84c352d

File tree

7 files changed

+277
-0
lines changed

7 files changed

+277
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Copyright (c) 2018 Analog Devices Inc.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
cmake_minimum_required(VERSION 3.8.2)
7+
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
8+
project(NONE)
9+
10+
FILE(GLOB app_sources src/*.c)
11+
target_sources(app PRIVATE ${app_sources})

samples/sensor/adxl372/README.rst

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
.. _adxl372:
2+
3+
ADXL372: Three Axis High-g I2C/SPI Accelerometer
4+
################################################
5+
6+
Description
7+
***********
8+
9+
This sample application produces slightly different outputs based on the chosen
10+
driver configuration mode:
11+
12+
* In **Measuring Mode with trigger support**,
13+
the acceleration on all three axis is printed in m/s^2 at the sampling rate (ODR).
14+
* In **Polled Measuring Mode**, the instantaneous acceleration is polled every 2 seconds.
15+
* In **Max Peak Detect Mode**, the device returns only the over-threshold
16+
peak acceleration between two consecutive sample fetches or trigger events.
17+
(In most high-g applications, a single 3-axis acceleration sample at the peak
18+
of an impact event contains sufficient information about the event, and the
19+
full acceleration history is not required.) Instead of printing the acceleration
20+
on all three axis, the sample application calculates the vector magnitude
21+
(root sum squared) and displays the result in g's rather than in m/s^2,
22+
together with an bar graph.
23+
24+
25+
References
26+
**********
27+
28+
- ADXL372: http://www.analog.com/adxl372
29+
30+
Wiring
31+
*******
32+
33+
This sample uses the ADXL372 sensor controlled either using the I2C or SPI interface.
34+
Connect supply **VDD**, **VS** and **GND**. The supply voltage can be in
35+
the 1.6V to 3.5V range.
36+
37+
I2C mode
38+
========
39+
40+
Connect Interface: **SDA**, **SCL** and optionally connect the **INT1** to a
41+
interrupt capable GPIO. It is a requirement that **SCLK** must be connected to
42+
**GND** in I2C mode. Depending on the baseboard used, the **SDA** and **SCL**
43+
lines require Pull-Up resistors. With the **MISO** pin low, the I2C address for
44+
the device is 0x1D, and an alternate I2C address of 0x53 can be chosen by
45+
pulling the **MISO** pin high.
46+
47+
I2C Address:
48+
49+
* **0x1D**: if MISO is pulled low
50+
* **0x53**: if MISO is pulled high
51+
52+
.. Note::
53+
When sharing an SDA bus, the ADXL372 Silicon Revision < 3 may prevent
54+
communication with other devices on that bus.
55+
56+
SPI mode
57+
========
58+
59+
Connect Interface: **SCLK**, **MISO**, **MOSI** and **/CS** and optionally
60+
connect the **INT1** to a interrupt capable GPIO.
61+
62+
63+
Building and Running
64+
********************
65+
66+
This project outputs sensor data to the console. It requires an ADXL372
67+
sensor. It should work with any platform featuring a I2C/SPI peripheral interface.
68+
It does not work on QEMU.
69+
In this example below the :ref:`nrf52_pca10040` board is used.
70+
71+
72+
.. zephyr-app-commands::
73+
:zephyr-app: samples/sensors/adxl372
74+
:board: nrf52_pca10040
75+
:goals: build flash
76+
77+
78+
Sample Output: Max Peak Detect Mode
79+
===================================
80+
81+
.. code-block:: console
82+
83+
Waiting for a threshold event
84+
23.94 g: ########################
85+
Waiting for a threshold event
86+
38.01 g: #######################################
87+
Waiting for a threshold event
88+
51.40 g: ####################################################
89+
Waiting for a threshold event
90+
63.63 g: ################################################################
91+
92+
Sample Output: Measurement Mode
93+
===============================
94+
95+
.. code-block:: console
96+
97+
AX= 2.94 AY= -5.88 AZ= 0.98 (m/s^2)
98+
AX= -4.90 AY= 6.86 AZ= -1.96 (m/s^2)
99+
AX= 2.94 AY= -2.94 AZ= 8.83 (m/s^2)
100+
AX= -0.98 AY= -6.86 AZ= -0.98 (m/s^2)
101+
AX= 6.86 AY= 2.94 AZ= 3.92 (m/s^2)
102+
AX= -0.98 AY= 4.90 AZ= -3.92 (m/s^2)
103+
104+
<repeats endlessly>

samples/sensor/adxl372/dts.fixup

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#if defined(CONFIG_HAS_DTS_I2C_DEVICE)
2+
3+
#ifndef CONFIG_ADXL372_DEV_NAME
4+
#define CONFIG_ADXL372_DEV_NAME ""
5+
#endif
6+
#ifndef CONFIG_ADXL372_I2C_ADDR
7+
#define CONFIG_ADXL372_I2C_ADDR 0
8+
#define CONFIG_ADXL372_I2C_MASTER_DEV_NAME ""
9+
#endif
10+
11+
#endif /* CONFIG_HAS_DTS_I2C_DEVICE */
12+
13+
#if defined(CONFIG_HAS_DTS_SPI_DEVICE)
14+
15+
#ifndef CONFIG_ADXL372_DEV_NAME
16+
#define CONFIG_ADXL372_DEV_NAME ""
17+
#endif
18+
#ifndef CONFIG_ADXL372_SPI_DEV_NAME
19+
#define CONFIG_ADXL372_SPI_DEV_NAME ""
20+
#define CONFIG_ADXL372_SPI_DEV_SLAVE 0
21+
#define CONFIG_ADXL372_SPI_BUS_FREQ 8000000
22+
#endif
23+
24+
#endif /* CONFIG_HAS_DTS_SPI_DEVICE */

samples/sensor/adxl372/prj.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_SYS_LOG=y
3+
CONFIG_SPI=y
4+
CONFIG_SENSOR=y
5+
CONFIG_ADXL372=y
6+
CONFIG_ADXL372_SPI=y
7+
CONFIG_SYS_LOG_SENSOR_LEVEL=3
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_STDOUT_CONSOLE=y
2+
CONFIG_SYS_LOG=y
3+
CONFIG_I2C=y
4+
CONFIG_SENSOR=y
5+
CONFIG_ADXL372=y
6+
CONFIG_ADXL372_I2C=y
7+
CONFIG_SYS_LOG_SENSOR_LEVEL=3
8+

samples/sensor/adxl372/sample.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sample:
2+
name: ADXL372 High-g Accelerometer Sensor
3+
tests:
4+
test:
5+
harness: sensor
6+
tags: samples sensor
7+
depends_on: spi
8+
test_i2c:
9+
harness: sensor
10+
tags: samples sensor
11+
depends_on: i2c
12+
extra_args: "CONF_FILE=prj_i2c.conf"

samples/sensor/adxl372/src/main.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright (c) 2018 Analog Devices Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <sensor.h>
9+
#include <stdio.h>
10+
11+
#define pow2(x) ((x) * (x))
12+
13+
static double sqrt(double value)
14+
{
15+
int i;
16+
double sqrt = value / 3;
17+
18+
if (value <= 0) {
19+
return 0;
20+
}
21+
22+
for (i = 0; i < 6; i++) {
23+
sqrt = (sqrt + value / sqrt) / 2;
24+
}
25+
26+
return sqrt;
27+
}
28+
29+
K_SEM_DEFINE(sem, 0, 1);
30+
31+
static void trigger_handler(struct device *dev, struct sensor_trigger *trigger)
32+
{
33+
ARG_UNUSED(trigger);
34+
35+
if (sensor_sample_fetch(dev)) {
36+
printf("sensor_sample_fetch failed\n");
37+
return;
38+
}
39+
40+
k_sem_give(&sem);
41+
}
42+
43+
void main(void)
44+
{
45+
struct sensor_value accel[3];
46+
double mag;
47+
int i;
48+
char meter[200];
49+
50+
struct device *dev = device_get_binding(CONFIG_ADXL372_DEV_NAME);
51+
52+
if (dev == NULL) {
53+
printf("Could not get %s device\n", CONFIG_ADXL372_DEV_NAME);
54+
return;
55+
}
56+
57+
struct sensor_trigger trig = {
58+
.type = SENSOR_TRIG_DATA_READY,
59+
.chan = SENSOR_CHAN_ACCEL_XYZ,
60+
};
61+
62+
if (IS_ENABLED(CONFIG_ADXL372_PEAK_DETECT_MODE)) {
63+
trig.type = SENSOR_TRIG_THRESHOLD;
64+
}
65+
66+
if (IS_ENABLED(CONFIG_ADXL372_TRIGGER)) {
67+
if (sensor_trigger_set(dev, &trig, trigger_handler)) {
68+
printf("Could not set trigger\n");
69+
return;
70+
}
71+
}
72+
73+
while (1) {
74+
if (IS_ENABLED(CONFIG_ADXL372_TRIGGER)) {
75+
if (IS_ENABLED(CONFIG_ADXL372_PEAK_DETECT_MODE)) {
76+
printf("Waiting for a threshold event\n");
77+
}
78+
k_sem_take(&sem, K_FOREVER);
79+
} else {
80+
if (sensor_sample_fetch(dev)) {
81+
printf("sensor_sample_fetch failed\n");
82+
}
83+
}
84+
85+
sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ, accel);
86+
87+
if (IS_ENABLED(CONFIG_ADXL372_PEAK_DETECT_MODE)) {
88+
mag = sqrt(pow2(sensor_ms2_to_g(&accel[0])) +
89+
pow2(sensor_ms2_to_g(&accel[1])) +
90+
pow2(sensor_ms2_to_g(&accel[2])));
91+
92+
for (i = 0; i <= mag && i < (sizeof(meter) - 1); i++) {
93+
meter[i] = '#';
94+
}
95+
96+
meter[i] = '\0';
97+
98+
printf("%6.2f g: %s\n", mag, meter);
99+
} else {
100+
printf("AX=%10.2f AY=%10.2f AZ=%10.2f (m/s^2)\n",
101+
sensor_value_to_double(&accel[0]),
102+
sensor_value_to_double(&accel[1]),
103+
sensor_value_to_double(&accel[2]));
104+
}
105+
106+
if (!IS_ENABLED(CONFIG_ADXL372_TRIGGER)) {
107+
k_sleep(2000);
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)