Skip to content

Commit

Permalink
samples: sensor: lis2dw12: add sample with double tap detection.
Browse files Browse the repository at this point in the history
This example configures a lis2dw12 accelerometer to detect a double
tap with a basic configuration. It can also read the sensor in
polling mode. The example also reads the internal temperature
sensor.

Signed-off-by: Pierrick Curt <pierrickcurt@gmail.com>
  • Loading branch information
pcurt committed Jan 10, 2025
1 parent f07063d commit 16f1526
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
12 changes: 12 additions & 0 deletions samples/sensor/lis2dw12/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# Copyright (c) 2024, Pierrick Curt
#
# SPDX-License-Identifier: Apache-2.0
#

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(lis2dw12)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
52 changes: 52 additions & 0 deletions samples/sensor/lis2dw12/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.. zephyr:code-sample:: lis2dw12
:name: LIS2DW12 Motion Sensor
:relevant-api: sensor_interface

Get accelerometer data from an LIS2DW12 sensor (polling & trigger mode with a double tap).

Overview
********

By default this example uses the trigger mode with a double tap detection.
It also reads the internal temperature sensor and print sensor data to the console.

Requirements
************

This sample uses the LIS2DW12, ST MEMS system-in-package featuring a 3D
digital output motion sensor.

References
**********

For more information about LIS2DW12 motion sensor see
https://www.st.com/en/mems-and-sensors/lis2dw12.html.

Building and Running
********************

The LIS2DW12 is available on a dedicated adapter baord
https://www.st.com/en/evaluation-tools/steval-mki190v1.html.

On this example the LIS2DW12 is connected to the I2C2 bus of the board.
The interrupt pin (INT1) is connected to the GPIOE_6 pin of the board.

Building on stm32f3_disco
===========================

.. zephyr-app-commands::
:zephyr-app: samples/sensor/lis2dw12
:board: stm32f3_disco
:goals: build flash
:compact:

Sample Output
=============

.. code-block:: console
#1 @ 12 ms: x -5.387328 , y 5.578368 , z -5.463744, t 21.437500
#2 @ 2017 ms: x -5.310912 , y 5.654784 , z -5.501952, t 21.750000
#3 @ 4022 ms: x -5.349120 , y 5.692992 , z -5.463744, t 21.937500
<repeats endlessly every double tap on the sensor>
27 changes: 27 additions & 0 deletions samples/sensor/lis2dw12/boards/stm32f3_disco.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <zephyr/dt-bindings/sensor/lis2dw12.h>

/ {
aliases {
accel1 = &lis2dw12_accel;
};
};

&i2c2 {
pinctrl-0 = <&i2c2_scl_pa9 &i2c2_sda_pa10>;
pinctrl-names = "default";
status = "okay";
clock-frequency = <I2C_BITRATE_FAST>;

lis2dw12_accel: lis2dw12@19 {
compatible = "st,lis2dw12";
reg = <0x19>;
odr = <400>;
tap-mode = <LIS2DW12_DT_SINGLE_DOUBLE_TAP>;
tap-threshold = <12>, <12>, <12>;
power-mode = <LIS2DW12_DT_HP_MODE>;
tap-shock = <0x03>;
tap-quiet = <0x03>;
tap-latency = <0x03>;
irq-gpios = <&gpioe 6 GPIO_ACTIVE_HIGH>;
};
};
8 changes: 8 additions & 0 deletions samples/sensor/lis2dw12/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_LOG=y
CONFIG_SENSOR_LOG_LEVEL_DBG=y
CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD=y
CONFIG_LIS2DW12_TAP=y
11 changes: 11 additions & 0 deletions samples/sensor/lis2dw12/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sample:
name: LIS2DW12 Accelerometer Monitor
tests:
sample.sensor.lis2dw12:
harness: sensor
tags:
- samples
- sensor
depends_on:
- i2c
- lis2dw12
83 changes: 83 additions & 0 deletions samples/sensor/lis2dw12/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2024 Pierrick Curt
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/sensor.h>

static void fetch_and_display(const struct device *sensor)
{
static unsigned int count;
struct sensor_value accel[3];
struct sensor_value temperature;
int rc;

rc = sensor_sample_fetch_chan(sensor, SENSOR_CHAN_ACCEL_XYZ);
if (rc < 0) {
printf("ERROR: SENSOR_CHAN_ACCEL_XYZ fetch failed: %d\n", rc);
}
rc = sensor_sample_fetch_chan(sensor, SENSOR_CHAN_DIE_TEMP);
if (rc < 0) {
printf("ERROR: SENSOR_CHAN_DIE_TEMP fetch failed: %d\n", rc);
}

++count;
if (rc == 0) {
rc = sensor_channel_get(sensor, SENSOR_CHAN_ACCEL_XYZ, accel);
if (rc == 0) {
rc = sensor_channel_get(sensor, SENSOR_CHAN_DIE_TEMP, &temperature);
}
}
if (rc < 0) {
printf("ERROR: Update failed: %d\n", rc);
} else {
printf("#%u @ %u ms: x %f , y %f , z %f, t %f\n", count, k_uptime_get_32(),
sensor_value_to_double(&accel[0]), sensor_value_to_double(&accel[1]),
sensor_value_to_double(&accel[2]), sensor_value_to_double(&temperature));
}
}

#ifdef CONFIG_LIS2DW12_TRIGGER

static void lis2dw12_trigger_handler(const struct device *dev, const struct sensor_trigger *trig)
{
fetch_and_display(dev);
}
#endif

int main(void)
{
const struct device *const sensor = DEVICE_DT_GET_ANY(st_lis2dw12);

if (sensor == NULL) {
printf("No device found\n");
return 0;
}
if (!device_is_ready(sensor)) {
printf("Device %s is not ready\n", sensor->name);
return 0;
}

#ifdef CONFIG_LIS2DW12_TRIGGER
struct sensor_trigger trig;

trig.type = SENSOR_TRIG_DOUBLE_TAP;
trig.chan = SENSOR_CHAN_ACCEL_XYZ;
sensor_trigger_set(sensor, &trig, lis2dw12_trigger_handler);

while (true) {
k_sleep(K_MSEC(2000));
}

#else /* CONFIG_LIS2DW12_TRIGGER */
printf("Polling at 0.5 Hz\n");
while (true) {
fetch_and_display(sensor);
k_sleep(K_MSEC(2000));
}
#endif /* CONFIG_LIS2DW12_TRIGGER */
}

0 comments on commit 16f1526

Please sign in to comment.