diff --git a/samples/sensor/lis2dw12/CMakeLists.txt b/samples/sensor/lis2dw12/CMakeLists.txt new file mode 100644 index 000000000000..851e7f11c4a3 --- /dev/null +++ b/samples/sensor/lis2dw12/CMakeLists.txt @@ -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}) diff --git a/samples/sensor/lis2dw12/README.rst b/samples/sensor/lis2dw12/README.rst new file mode 100644 index 000000000000..52be596f7882 --- /dev/null +++ b/samples/sensor/lis2dw12/README.rst @@ -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 + + diff --git a/samples/sensor/lis2dw12/boards/stm32f3_disco.overlay b/samples/sensor/lis2dw12/boards/stm32f3_disco.overlay new file mode 100644 index 000000000000..f70b26e140d9 --- /dev/null +++ b/samples/sensor/lis2dw12/boards/stm32f3_disco.overlay @@ -0,0 +1,27 @@ +#include + +/ { + aliases { + accel1 = &lis2dw12_accel; + }; +}; + +&i2c2 { + pinctrl-0 = <&i2c2_scl_pa9 &i2c2_sda_pa10>; + pinctrl-names = "default"; + status = "okay"; + clock-frequency = ; + + lis2dw12_accel: lis2dw12@19 { + compatible = "st,lis2dw12"; + reg = <0x19>; + odr = <400>; + tap-mode = ; + tap-threshold = <12>, <12>, <12>; + power-mode = ; + tap-shock = <0x03>; + tap-quiet = <0x03>; + tap-latency = <0x03>; + irq-gpios = <&gpioe 6 GPIO_ACTIVE_HIGH>; + }; +}; diff --git a/samples/sensor/lis2dw12/prj.conf b/samples/sensor/lis2dw12/prj.conf new file mode 100644 index 000000000000..b43ae1ba06dc --- /dev/null +++ b/samples/sensor/lis2dw12/prj.conf @@ -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 diff --git a/samples/sensor/lis2dw12/sample.yaml b/samples/sensor/lis2dw12/sample.yaml new file mode 100644 index 000000000000..309d4261cd3f --- /dev/null +++ b/samples/sensor/lis2dw12/sample.yaml @@ -0,0 +1,11 @@ +sample: + name: LIS2DW12 Accelerometer Monitor +tests: + sample.sensor.lis2dw12: + harness: sensor + tags: + - samples + - sensor + depends_on: + - i2c + - lis2dw12 diff --git a/samples/sensor/lis2dw12/src/main.c b/samples/sensor/lis2dw12/src/main.c new file mode 100644 index 000000000000..71a6c2881a59 --- /dev/null +++ b/samples/sensor/lis2dw12/src/main.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2024 Pierrick Curt + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +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 */ +}