diff --git a/samples/lis2dw12_sample/Kconfig b/samples/lis2dw12_sample/Kconfig new file mode 100644 index 0000000..0a7c632 --- /dev/null +++ b/samples/lis2dw12_sample/Kconfig @@ -0,0 +1,13 @@ +# Copyright (c) 2023 T-Mobile USA, Inc. +# +# SPDX-License-Identifier: Apache-2.0 +# + +mainmenu "LIS2DW12 sample application" + +config LIS2DW12_SAMPLE_DRDY + bool "Enable the data read trigger" + depends on LIS2DW12_TRIGGER + default n + +source "Kconfig.zephyr" diff --git a/samples/lis2dw12_sample/prj.conf b/samples/lis2dw12_sample/prj.conf index a989d5f..013994a 100644 --- a/samples/lis2dw12_sample/prj.conf +++ b/samples/lis2dw12_sample/prj.conf @@ -4,7 +4,9 @@ CONFIG_SENSOR=y CONFIG_LIS2DW12=y CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD=y CONFIG_LIS2DW12_TAP=y +CONFIG_LIS2DW12_TAP_3D=y CONFIG_LIS2DW12_THRESHOLD=y +CONFIG_LIS2DW12_THRESHOLD_3D=y CONFIG_LIS2DW12_FREEFALL=y CONFIG_CBPRINTF_FP_SUPPORT=y CONFIG_ASSERT=y diff --git a/samples/lis2dw12_sample/src/main.c b/samples/lis2dw12_sample/src/main.c index f995403..6daa8fe 100644 --- a/samples/lis2dw12_sample/src/main.c +++ b/samples/lis2dw12_sample/src/main.c @@ -8,12 +8,6 @@ #include #include #include -#include - -#define DOUBLE_TAP_EVENT 0x10U -#define SINGLE_TAP_EVENT 0x08U -#define FREEFALL_EVENT 0x02U -#define DATAREADY_EVENT 0x01U bool sample_init_complete = false; @@ -21,9 +15,8 @@ bool sample_init_complete = false; uint8_t reg_value; struct sensor_value temperature; struct sensor_value sensor_attr_val; -struct sensor_trigger trig; -static void trigger_and_display(const struct device *sensor) +static void trigger_and_display(const struct device *sensor, const struct sensor_trigger *trig) { static unsigned int count; struct sensor_value accel[3]; @@ -56,66 +49,43 @@ static void trigger_and_display(const struct device *sensor) sensor_value_to_double(&accel[2])); } #endif - rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_STATUS, - (struct sensor_value *)&sensor_attr_val); - if (rc < 0) { - printf("\n\tERROR: Unable to read register 0X27 :%d\n", rc); - } else { - reg_value = (uint8_t)sensor_attr_val.val1; - if ((reg_value & SINGLE_TAP_EVENT) == SINGLE_TAP_EVENT) { - printf("\n\tSINGLE TAP was detected (%xh)\n", (reg_value)); - } else if ((reg_value & DOUBLE_TAP_EVENT) == DOUBLE_TAP_EVENT) { - printf("\n\tDOUBLE TAP was detected (%xh)\n", (reg_value)); - } else if ((reg_value & FREEFALL_EVENT) == FREEFALL_EVENT) { - printf("\n\tFREE FALL was detected (%xh)\n", (reg_value)); - } else if ((reg_value & DATAREADY_EVENT) == DATAREADY_EVENT) { - printf("\n\tDATAREADY was detected (%xh)\n", (reg_value)); + if (trig) { + if (trig->type == SENSOR_TRIG_TAP) { + printf("\nSINGLE TAP was detected\n"); + } else if (trig->type == SENSOR_TRIG_DOUBLE_TAP) { + printf("\nDOUBLE TAP was detected\n"); + } else if (trig->type == SENSOR_TRIG_FREEFALL) { + printf("\nFREE FALL was detected\n"); + } else if (trig->type == SENSOR_TRIG_DATA_READY) { + printf("\nDATAREADY was detected\n"); + } else if (trig->type == SENSOR_TRIG_THRESHOLD) { + printf("\nTHRESHOLD event was detected\n"); } else { - printf("\n\tUNKNOWN event was detected (%xh)\n", (reg_value)); + printf("\nUNKNOWN event was detected (%d)\n", (trig->type)); } } - uint8_t reg_array[5] = {0}; - rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_ALL_WAKE_UP_SRC, - (struct sensor_value *)®_array[0]); - if (rc < 0) { - printf("\n\tERROR: Unable to read register 0x38 :%d\n", rc); - } else { - if (reg_array[1]) { - if ((reg_array[1] & 0x20) == 0x20) { - printf("\tReg 38h - FREE FALL detected (%xh)\n", (reg_array[1])); - } - } else { - printf("\tReg 38h - No WAKE SRC detected (%xh)\n", (reg_array[1])); + if (trig->type == SENSOR_TRIG_THRESHOLD) { + if (trig->chan == SENSOR_CHAN_ACCEL_X) { + printf("\tTHRESHOLD X-Axis detected\n"); + } + if (trig->chan == SENSOR_CHAN_ACCEL_Y) { + printf("\tTHRESHOLD Y-Axis detected\n"); + } + if (trig->chan == SENSOR_CHAN_ACCEL_Z) { + printf("\tTHRESHOLD Z-Axis detected\n"); } } - reg_value = 0; - rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_TAP_SRC, - (struct sensor_value *)®_value); - if (rc < 0) { - printf("\n\tERROR: Unable to read register 0x39 :%d\n", rc); - } else { - - if (reg_value) { - if ((reg_value & 0x01) == 0x01) { - printf("\tReg 39h - TAP_EVENT Negative Accel detected (%xh)\n", - (reg_value)); - } else { - printf("\tReg 39h - TAP_EVENT Positive Accel detected (%xh)\n", - (reg_value)); - } - if ((reg_value & 0x04) == 0x04) { - printf("\tReg 39h - TAP_EVENT X-Axis detected\n"); - } - if ((reg_value & 0x02) == 0x02) { - printf("\tReg 39h - TAP_EVENT Y-Axis detected\n"); - } - if ((reg_value & 0x01) == 0x01) { - printf("\tReg 39h - TAP_EVENT Z-Axis detected\n"); - } - } else { - printf("\tReg 39h - No TAP_EVENT detected (%xh)\n", (reg_value)); + if (trig->type == SENSOR_TRIG_TAP || trig->type == SENSOR_TRIG_DOUBLE_TAP) { + if (trig->chan == SENSOR_CHAN_ACCEL_X) { + printf("\tTAP_EVENT X-Axis detected\n"); + } + if (trig->chan == SENSOR_CHAN_ACCEL_Y) { + printf("\tTAP_EVENT Y-Axis detected\n"); + } + if (trig->chan == SENSOR_CHAN_ACCEL_Z) { + printf("\tTAP_EVENT Z-Axis detected\n"); } } @@ -131,7 +101,7 @@ static void trigger_and_display(const struct device *sensor) #ifdef CONFIG_LIS2DW12_TRIGGER static void trigger_handler(const struct device *dev, const struct sensor_trigger *trig) { - trigger_and_display(dev); + trigger_and_display(dev, trig); } #endif @@ -166,71 +136,157 @@ void main(void) #if CONFIG_LIS2DW12_TRIGGER int rc; - trig.type = SENSOR_TRIG_DATA_READY; - trig.chan = SENSOR_CHAN_ACCEL_XYZ; + static struct sensor_trigger drdy_trig; + drdy_trig.type = SENSOR_TRIG_DATA_READY; + drdy_trig.chan = SENSOR_CHAN_ACCEL_XYZ; struct sensor_value odr = { - .val1 = 1, + .val1 = 2, }; - rc = sensor_attr_set(sensor, trig.chan, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr); + rc = sensor_attr_set(sensor, drdy_trig.chan, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr); if (rc != 0) { printf("\tFailed to set odr: %d\n", rc); return; } - trig.type = SENSOR_TRIG_DOUBLE_TAP; - trig.chan = SENSOR_CHAN_ACCEL_XYZ; +#if CONFIG_LIS2DW12_SAMPLE_DRDY + rc = sensor_trigger_set(sensor, &drdy_trig, trigger_handler); +#endif + + static struct sensor_trigger ff_trig; + ff_trig.type = SENSOR_TRIG_FREEFALL; + ff_trig.chan = SENSOR_CHAN_ACCEL_XYZ; + +#if CONFIG_LIS2DW12_TAP + rc = sensor_trigger_set(sensor, &ff_trig, trigger_handler); + + static struct sensor_trigger dtap_xyz_trig; + dtap_xyz_trig.type = SENSOR_TRIG_DOUBLE_TAP; + dtap_xyz_trig.chan = SENSOR_CHAN_ACCEL_XYZ; - rc = sensor_trigger_set(sensor, &trig, trigger_handler); + rc = sensor_trigger_set(sensor, &dtap_xyz_trig, trigger_handler); if (rc != 0) { printf("\tFailed to set Double Tap trigger: %d\n", rc); return; } - trig.type = SENSOR_TRIG_THRESHOLD; - trig.chan = SENSOR_CHAN_ACCEL_XYZ; + static struct sensor_trigger stap_xyz_trig; + stap_xyz_trig.type = SENSOR_TRIG_TAP; + stap_xyz_trig.chan = SENSOR_CHAN_ACCEL_XYZ; - rc = sensor_trigger_set(sensor, &trig, trigger_handler); + rc = sensor_trigger_set(sensor, &stap_xyz_trig, trigger_handler); if (rc != 0) { - printf("\tFailed to set Threshold trigger: %d\n", rc); + printf("\tFailed to set Single Tap trigger: %d\n", rc); return; } - struct sensor_value ctrl4_reg = { - .val1 = 0x58, - }; +#if CONFIG_LIS2DW12_TAP_3D + static struct sensor_trigger dtap_x_trig; + dtap_x_trig.type = SENSOR_TRIG_DOUBLE_TAP; + dtap_x_trig.chan = SENSOR_CHAN_ACCEL_X; - rc = sensor_attr_set(sensor, trig.chan, SENSOR_ATTR_ENABLE_EVENT_INTERRUPT, &ctrl4_reg); + rc = sensor_trigger_set(sensor, &dtap_x_trig, trigger_handler); if (rc != 0) { - printf("\tFailed to set odr: %d\n", rc); + printf("\tFailed to set Double Tap X trigger: %d\n", rc); return; } - rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_CHIP_ID, - (struct sensor_value *)&sensor_attr_val); + + static struct sensor_trigger dtap_y_trig; + dtap_y_trig.type = SENSOR_TRIG_DOUBLE_TAP; + dtap_y_trig.chan = SENSOR_CHAN_ACCEL_Y; + + rc = sensor_trigger_set(sensor, &dtap_y_trig, trigger_handler); if (rc != 0) { - printf("\tFailed to get status: %d\n", rc); + printf("\tFailed to set Double Tap Y trigger: %d\n", rc); return; } - if (sensor_attr_val.val1 == 0x44) { - printf("\tWHOAMI: (%xh)\n", (uint8_t)sensor_attr_val.val1); - } else { - printf("\tError : WHOAMI (%xh) doesnt match the LIS2DW12 ?\n", - (sensor_attr_val.val1)); + static struct sensor_trigger dtap_z_trig; + dtap_z_trig.type = SENSOR_TRIG_DOUBLE_TAP; + dtap_z_trig.chan = SENSOR_CHAN_ACCEL_Z; + + rc = sensor_trigger_set(sensor, &dtap_z_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Double Tap Z trigger: %d\n", rc); return; } - rc = sensor_attr_get(sensor, trig.chan, SENSOR_ATTR_STATUS, - (struct sensor_value *)&sensor_attr_val); + static struct sensor_trigger stap_x_trig; + stap_x_trig.type = SENSOR_TRIG_TAP; + stap_x_trig.chan = SENSOR_CHAN_ACCEL_X; + + rc = sensor_trigger_set(sensor, &stap_x_trig, trigger_handler); if (rc != 0) { - printf("\tFailed to get status: %d\n", rc); + printf("\tFailed to set Single Tap X trigger: %d\n", rc); return; } - printf("\tSENSOR_ATTR_STATUS: (%xh)\n", sensor_attr_val.val1); + static struct sensor_trigger stap_y_trig; + stap_y_trig.type = SENSOR_TRIG_TAP; + stap_y_trig.chan = SENSOR_CHAN_ACCEL_Y; + rc = sensor_trigger_set(sensor, &stap_y_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Single Tap Y trigger: %d\n", rc); + return; + } + + static struct sensor_trigger stap_z_trig; + stap_z_trig.type = SENSOR_TRIG_TAP; + stap_z_trig.chan = SENSOR_CHAN_ACCEL_Z; + + rc = sensor_trigger_set(sensor, &stap_z_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Single Tap Z trigger: %d\n", rc); + return; + } +#endif /* CONFIG_LIS2DW12_TAP_3D */ +#endif /* CONFIG_LIS2DW12_TAP */ +#if CONFIG_LIS2DW12_THRESHOLD + static struct sensor_trigger thresh_xyz_trig; + thresh_xyz_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_xyz_trig.chan = SENSOR_CHAN_ACCEL_XYZ; + + rc = sensor_trigger_set(sensor, &thresh_xyz_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold trigger: %d\n", rc); + return; + } + +#if CONFIG_LIS2DW12_THRESHOLD_3D + static struct sensor_trigger thresh_x_trig; + thresh_x_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_x_trig.chan = SENSOR_CHAN_ACCEL_X; + + rc = sensor_trigger_set(sensor, &thresh_x_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold X trigger: %d\n", rc); + return; + } + + static struct sensor_trigger thresh_y_trig; + thresh_y_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_y_trig.chan = SENSOR_CHAN_ACCEL_Y; + + rc = sensor_trigger_set(sensor, &thresh_y_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold Y trigger: %d\n", rc); + return; + } + + static struct sensor_trigger thresh_z_trig; + thresh_z_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_z_trig.chan = SENSOR_CHAN_ACCEL_Z; + + rc = sensor_trigger_set(sensor, &thresh_z_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold Z trigger: %d\n", rc); + return; + } +#endif /* CONFIG_LIS2DW12_THRESHOLD_3D */ +#endif /* CONFIG_LIS2DW12_THRESHOLD */ /* Let the interrupt handler know the sample is ready to run */ sample_init_complete = true; @@ -242,7 +298,7 @@ void main(void) #else /* CONFIG_LIS2DW12_TRIGGER */ while (true) { - trigger_and_display(sensor); + trigger_and_display(sensor, NULL); k_sleep(K_MSEC(2000)); } #endif /* CONFIG_LIS2DW12_TRIGGER */ diff --git a/samples/lis2dw12_sample_motion_nomotion/CMakeLists.txt b/samples/lis2dw12_sample_motion_nomotion/CMakeLists.txt new file mode 100644 index 0000000..de82968 --- /dev/null +++ b/samples/lis2dw12_sample_motion_nomotion/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2018, Yannis Damigos +# +# SPDX-License-Identifier: Apache-2.0 +# + +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(lis2dw12_sample) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/lis2dw12_sample_motion_nomotion/Kconfig b/samples/lis2dw12_sample_motion_nomotion/Kconfig new file mode 100644 index 0000000..0a7c632 --- /dev/null +++ b/samples/lis2dw12_sample_motion_nomotion/Kconfig @@ -0,0 +1,13 @@ +# Copyright (c) 2023 T-Mobile USA, Inc. +# +# SPDX-License-Identifier: Apache-2.0 +# + +mainmenu "LIS2DW12 sample application" + +config LIS2DW12_SAMPLE_DRDY + bool "Enable the data read trigger" + depends on LIS2DW12_TRIGGER + default n + +source "Kconfig.zephyr" diff --git a/samples/lis2dw12_sample_motion_nomotion/README.rst b/samples/lis2dw12_sample_motion_nomotion/README.rst new file mode 100644 index 0000000..bd644b5 --- /dev/null +++ b/samples/lis2dw12_sample_motion_nomotion/README.rst @@ -0,0 +1,68 @@ +.. _lis2dw12: + +LIS2DH: Motion Sensor Monitor +############################# + +Sample Overview +*************** + +This sample application periodically reads accelerometer data from the +LIS2DW12 sensor and displays the sensor data on the console. + +Device Overview +*************** + +The LIS2DW12 is a 3D digital accelerometer system-in-package with a digital I²C/SPI serial interface +standard output, performing at 90 μA in high-resolution mode and below 1 μA in low-power mode. + +The device has a dynamic user-selectable full-scale acceleration range of ±2/±4/±8/±16 g and is +capable of measuring accelerations with output data rates from 1.6 Hz to 1600 Hz. The LIS2DW12 can +be configured to generate interrupt signals by using hardware recognition of free-fall events, 6D +orientation, tap and double-tap sensing, activity or inactivity, and wake-up events. + +Requirements +************ + +This sample uses the LIS2DW12, ST MEMS system-in-package featuring a 3D +digital output motion sensor. + +References +********** + +For more information about the LIS2DW12 motion sensor see +http://www.st.com/en/mems-and-sensors/lis2dw12.html. + +Building and Running +******************** + +The LIS2DW12 or compatible sensors are available on a variety of boards +and shields supported by Zephyr, including: + +* :ref:`tmo_dev_edge` + +See the board documentation for detailed instructions on how to flash +and get access to the console where acceleration data is displayed. + +Building on tmo_dev_edge +======================== + +:ref:`tmo_dev_edge` includes an ST LIS2DW12 accelerometer which +supports the LIS2DW12 interface. + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/lis2dw12_sample + :board: actinius_icarus + :goals: build flash + :compact: + +Sample Output +============= + +.. code-block:: console + + Polling at 0.5 Hz + #1 @ 12 ms: x -5.387328 , y 5.578368 , z -5.463744 + #2 @ 2017 ms: x -5.310912 , y 5.654784 , z -5.501952 + #3 @ 4022 ms: x -5.349120 , y 5.692992 , z -5.463744 + + diff --git a/samples/lis2dw12_sample_motion_nomotion/prj.conf b/samples/lis2dw12_sample_motion_nomotion/prj.conf new file mode 100644 index 0000000..c6e24cc --- /dev/null +++ b/samples/lis2dw12_sample_motion_nomotion/prj.conf @@ -0,0 +1,23 @@ +CONFIG_STDOUT_CONSOLE=y +CONFIG_I2C=y +CONFIG_SENSOR=y +CONFIG_LIS2DW12=y +CONFIG_LIS2DW12_TRIGGER_GLOBAL_THREAD=y +CONFIG_LIS2DW12_TAP=n +CONFIG_LIS2DW12_TAP_3D=n +CONFIG_LIS2DW12_THRESHOLD=y +CONFIG_LIS2DW12_THRESHOLD_3D=y +CONFIG_LIS2DW12_FREEFALL=y +CONFIG_CBPRINTF_FP_SUPPORT=y +CONFIG_ASSERT=y + +#Power Managment +CONFIG_PM=y +CONFIG_PM_DEVICE=y + +CONFIG_ISR_STACK_SIZE=4096 + +CONFIG_GECKO_LETIMER=y +CONFIG_GECKO_LETIMER_USE_REP=y +CONFIG_CORTEX_M_SYSTICK=n +CONFIG_SYS_CLOCK_TICKS_PER_SEC=8192 diff --git a/samples/lis2dw12_sample_motion_nomotion/sample.yaml b/samples/lis2dw12_sample_motion_nomotion/sample.yaml new file mode 100644 index 0000000..0561376 --- /dev/null +++ b/samples/lis2dw12_sample_motion_nomotion/sample.yaml @@ -0,0 +1,13 @@ +sample: + name: LIS2DW12 Accelerometer Sample +tests: + sample.sensor.lis2dw12: + harness: console + tags: samples sensor + depends_on: i2c gpio + filter: dt_compat_enabled("st,lis2dw12") + harness_config: + type: multi_line + regex: + - "temperature is *.*C" + diff --git a/samples/lis2dw12_sample_motion_nomotion/src/main.c b/samples/lis2dw12_sample_motion_nomotion/src/main.c new file mode 100644 index 0000000..0408158 --- /dev/null +++ b/samples/lis2dw12_sample_motion_nomotion/src/main.c @@ -0,0 +1,221 @@ +/* + * Copyright (c) 2022 T-Mobile USA, Inc. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include + +bool sample_init_complete = false; + +void nomotion_fn(struct k_timer *timer_id) +{ + printf("\n\nNo motion detected in 10 seconds!\n\n"); + pm_state_force(0, &(struct pm_state_info){PM_STATE_STANDBY, 0, 0}); +} + +K_TIMER_DEFINE(nomotion_timer, nomotion_fn, NULL); + +uint8_t reg_value; +struct sensor_value temperature; +struct sensor_value sensor_attr_val; + +static void trigger_and_display(const struct device *sensor, const struct sensor_trigger *trig) +{ + int rc; +#if LIS2DW12_DEBUG + static unsigned int count; + struct sensor_value accel[3]; + const char *overrun = ""; + rc = sensor_sample_fetch(sensor); + + if (!sample_init_complete) { + return; + } + + ++count; + if (rc == -EBADMSG) { + /* Sample overrun. Ignore in polled mode. */ + if (IS_ENABLED(CONFIG_LIS2DW12_TRIGGER)) { + overrun = "[OVERRUN] "; + } + rc = 0; + } + + if (rc == 0) { + rc = sensor_channel_get(sensor, SENSOR_CHAN_ACCEL_XYZ, accel); + } + + if (rc < 0) { + printf("\tERROR: Update failed: %d\n", rc); + } else { + printf("\t#%u @ %u ms: %sx %f , y %f , z %f", count, k_uptime_get_32(), overrun, + sensor_value_to_double(&accel[0]), sensor_value_to_double(&accel[1]), + sensor_value_to_double(&accel[2])); + } +#endif + if (trig) { + if (trig->type == SENSOR_TRIG_THRESHOLD) { + printf("\n\nMotion Detected!\n\n"); + printf("\nTHRESHOLD event was detected\n"); + k_timer_start(&nomotion_timer, K_SECONDS(10), K_NO_WAIT); + } else { + printf("\nUNKNOWN event was detected (%d)\n", (trig->type)); + } + } + + if (trig->type == SENSOR_TRIG_THRESHOLD) { + if (trig->chan == SENSOR_CHAN_ACCEL_X) { + printf("\tTHRESHOLD X-Axis detected\n"); + } + if (trig->chan == SENSOR_CHAN_ACCEL_Y) { + printf("\tTHRESHOLD Y-Axis detected\n"); + } + if (trig->chan == SENSOR_CHAN_ACCEL_Z) { + printf("\tTHRESHOLD Z-Axis detected\n"); + } + } + + rc = sensor_channel_get(sensor, SENSOR_CHAN_AMBIENT_TEMP, &temperature); + if (rc < 0) { + printf("\n\tERROR: Unable to read ambient temperature :%d\n", rc); + } else { + + printf("\t\tAmbient Temperature: %.2f C\n", sensor_value_to_double(&temperature)); + } +} + +#ifdef CONFIG_LIS2DW12_TRIGGER +static void trigger_handler(const struct device *dev, const struct sensor_trigger *trig) +{ + trigger_and_display(dev, trig); + pm_state_force(0, &(struct pm_state_info){PM_STATE_STANDBY, 0, 0}); +} +#endif + +static void greeting() +{ + printf("\n\t\t\tWelcome to T-Mobile DevEdge!\n\n" + "This sample application demonstrates the integrated DevEdge based motion sensor\n" + "(STMicroelectronics LIS2DW12 MEMS accelerometer).\n" + "The motion sensor can detect single-tap, double-tap, and free-fall events.\n" + "Motion events detected by the LIS2DW12 result in interrupts that trigger\n" + "a handler function, which collects and displays specific information about each " + "event.\n\n" + "This sample simply determines between motion/stationary scenarious using these\n" + "interrupts.\n"); +} + +void suspend_lock_all(void) +{ + const struct device *dev; + size_t len = z_device_get_all_static(&dev); + const struct device *dev_end = dev + len; + + while (dev < dev_end) { + if (device_is_ready(dev) && dev->pm) { + pm_device_action_run(dev, PM_DEVICE_ACTION_SUSPEND); + pm_device_state_lock(dev); + } + dev++; + } +} + +void main(void) +{ + k_sleep(K_MSEC(2000)); + greeting(); + + k_sleep(K_MSEC(5000)); + const struct device *sensor = DEVICE_DT_GET_ANY(st_lis2dw12); + if (!sensor) { + return; + } + + pm_device_state_lock(sensor); + + /* Suspend and lock all other devices */ + suspend_lock_all(); + + int rc; + + struct sensor_value odr = { + .val1 = 2, + }; + + rc = sensor_attr_set(sensor, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, &odr); + if (rc != 0) { + printf("\tFailed to set odr: %d\n", rc); + return; + } + + struct sensor_value thresh = { + .val1 = 0, + .val2 = 153228, + }; + + rc = sensor_attr_set(sensor, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_LOWER_THRESH, &thresh); + if (rc != 0) { + printf("\tFailed to set threshold: %d\n", rc); + return; + } + + + static struct sensor_trigger thresh_xyz_trig; + thresh_xyz_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_xyz_trig.chan = SENSOR_CHAN_ACCEL_XYZ; + + rc = sensor_trigger_set(sensor, &thresh_xyz_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold trigger: %d\n", rc); + return; + } + +#if CONFIG_LIS2DW12_THRESHOLD_3D + static struct sensor_trigger thresh_x_trig; + thresh_x_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_x_trig.chan = SENSOR_CHAN_ACCEL_X; + + rc = sensor_trigger_set(sensor, &thresh_x_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold X trigger: %d\n", rc); + return; + } + + static struct sensor_trigger thresh_y_trig; + thresh_y_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_y_trig.chan = SENSOR_CHAN_ACCEL_Y; + + rc = sensor_trigger_set(sensor, &thresh_y_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold Y trigger: %d\n", rc); + return; + } + + static struct sensor_trigger thresh_z_trig; + thresh_z_trig.type = SENSOR_TRIG_THRESHOLD; + thresh_z_trig.chan = SENSOR_CHAN_ACCEL_Z; + + rc = sensor_trigger_set(sensor, &thresh_z_trig, trigger_handler); + if (rc != 0) { + printf("\tFailed to set Threshold Z trigger: %d\n", rc); + return; + } +#endif /* CONFIG_LIS2DW12_THRESHOLD_3D */ + /* Let the interrupt handler know the sample is ready to run */ + sample_init_complete = true; + + /* We should be spinning in this loop waiting for the user to trigger us.*/ + printf("\tWaiting here for a motion event trigger\n"); + k_timer_start(&nomotion_timer, K_SECONDS(10), K_NO_WAIT); + pm_state_force(0, &(struct pm_state_info){PM_STATE_STANDBY, 0, 0}); + while (true) { + k_sleep(K_MSEC(30000)); + } + +}