samples: sensor: added general samples for triggers

Added samples that show off trigger functionality, removed fxos8700 driver
specific sample

Signed-off-by: Krystof Sadlik <krystof.sadlik@nxp.com>
This commit is contained in:
Krystof Sadlik 2024-08-20 15:48:15 +02:00 committed by Carles Cufí
parent 5eb5f3d6a5
commit 3978693d47
25 changed files with 259 additions and 186 deletions

View file

@ -43,8 +43,8 @@ Pin Assignment of the FRDM-STBC-AGM01 Shield
For more information about the FXOS8700, FXAS21002, and FRDM-STBC-AGM01
board:
- :zephyr:code-sample:`fxos8700`
- :zephyr:code-sample:`fxas21002`
- `FXOS8700 Website`_
- `FRDM-STBC-AGM01 Website`_
- `FRDM-STBC-AGM01 Quick Reference Card`_
- `FRDM-STBC-AGM01 Schematics`_
@ -68,3 +68,6 @@ Set ``--shield frdm_stbc_agm01`` when you invoke ``west build``. For example:
.. _FRDM-STBC-AGM01 Schematics:
https://www.nxp.com/downloads/en/schematics/FRDM-STBC-AGM01-SCH.pdf
.. _FXOS8700 Website:
https://www.nxp.com/products/sensors/accelerometers/digital-motion-sensor-3d-accelerometer-2g-4g-8g-plus-3d-magnetometer:FXOS8700CQ

View file

@ -0,0 +1,9 @@
# Copyright (c) 2024 NXP
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(accel_trig)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,45 @@
.. zephyr:code-sample:: accel_trig
:name: Accelerometer trigger
Test and debug accelerometer with interrupts.
Overview
********
This sample application demonstrates how to use 3-Axis accelerometers with triggers.
Building and Running
********************
.. code-block:: devicetree
/ {
aliases {
accel0 = &fxos8700;
};
};
Make sure the aliases are in devicetree, then build and run with:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/accel_trig
:board: <board to use>
:goals: build flash
:compact:
Sample Output
=============
.. code-block:: console
fxos8700@1d [m/s^2]: ( -0.153229, -0.057461, 9.931148)
fxos8700@1d [m/s^2]: ( -0.153229, -0.057461, 9.931148)
fxos8700@1d [m/s^2]: ( -0.143653, -0.057461, 9.921571)
fxos8700@1d [m/s^2]: ( -0.153229, -0.067038, 9.931148)
fxos8700@1d [m/s^2]: ( -0.143653, -0.067038, 9.921571)
fxos8700@1d [m/s^2]: ( -0.134076, -0.047885, 9.931148)
fxos8700@1d [m/s^2]: ( -0.105345, -0.038308, 9.940725)
fxos8700@1d [m/s^2]: ( -0.105345, -0.019154, 9.931148)
fxos8700@1d [m/s^2]: ( -0.105345, -0.028731, 9.921571)
fxos8700@1d [m/s^2]: ( -0.095769, -0.028731, 9.931148)
fxos8700@1d [m/s^2]: ( -0.095769, -0.009577, 9.940725)

View file

@ -0,0 +1,2 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=y
CONFIG_FXOS8700_MODE_ACCEL=y

View file

@ -0,0 +1,3 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_SENSOR=y
CONFIG_CBPRINTF_FP_SUPPORT=y

View file

@ -0,0 +1,14 @@
sample:
name: Accelerometer trigger sample
tests:
sample.sensor.accel_trig:
harness: console
tags: sensors
filter: dt_alias_exists("accel0")
harness_config:
type: one_line
regex:
- "^\\s*[0-9A-Za-z_,+-.]*@[0-9A-Fa-f]* \\[m\/s\\^2\\]: \
\\(\\s*-?[0-9\\.]*,\\s*-?[0-9\\.]*,\\s*-?[0-9\\.]*\\)$"
integration_platforms:
- frdm_k64f # fxos8700

View file

@ -0,0 +1,58 @@
/*
* Copyright 2024 NXP
* Copyright (c) 2018 Phytec Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <stdio.h>
K_SEM_DEFINE(sem, 0, 1); /* starts off "not available" */
static void trigger_handler(const struct device *dev, const struct sensor_trigger *trigger)
{
ARG_UNUSED(trigger);
/* Always fetch the sample to clear the data ready interrupt in the
* sensor.
*/
if (sensor_sample_fetch(dev)) {
printf("sensor_sample_fetch failed\n");
return;
}
k_sem_give(&sem);
}
int main(void)
{
struct sensor_value data[3];
const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(accel0));
struct sensor_trigger trig = {
.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_ACCEL_XYZ,
};
if (!device_is_ready(dev)) {
printf("Device %s is not ready\n", dev->name);
return 0;
}
if (sensor_trigger_set(dev, &trig, trigger_handler)) {
printf("Could not set trigger\n");
return 0;
}
while (1) {
k_sem_take(&sem, K_FOREVER);
sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ, data);
/* Print accel x,y,z data */
printf("%16s [m/s^2]: (%12.6f, %12.6f, %12.6f)\n", dev->name,
sensor_value_to_double(&data[0]), sensor_value_to_double(&data[1]),
sensor_value_to_double(&data[2]));
}
}

View file

@ -1,2 +0,0 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=n
CONFIG_FXOS8700_TRIGGER_NONE=y

View file

@ -1,2 +0,0 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=n
CONFIG_FXOS8700_TRIGGER_NONE=y

View file

@ -1,2 +0,0 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=n
CONFIG_FXOS8700_TRIGGER_NONE=y

View file

@ -1,2 +0,0 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=n
CONFIG_FXOS8700_TRIGGER_NONE=y

View file

@ -1,2 +0,0 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=n
CONFIG_FXOS8700_TRIGGER_NONE=y

View file

@ -1,2 +0,0 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=n
CONFIG_FXOS8700_TRIGGER_NONE=y

View file

@ -1,2 +0,0 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=n
CONFIG_FXOS8700_TRIGGER_NONE=y

View file

@ -1 +0,0 @@
CONFIG_FXOS8700_MOTION=y

View file

@ -1,9 +0,0 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_LOG=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_SENSOR_LOG_LEVEL_DBG=y
CONFIG_FXOS8700_MODE_HYBRID=y
CONFIG_FXOS8700_TEMP=y
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=y
CONFIG_CBPRINTF_FP_SUPPORT=y

View file

@ -1,8 +0,0 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_LOG=y
CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_SENSOR_LOG_LEVEL_DBG=y
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_FXOS8700_MODE_ACCEL=y
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=y

View file

@ -1,41 +0,0 @@
common:
harness: sensor
tags: sensors
depends_on:
- i2c
- gpio
sample:
name: FXOS8700 Accelerometer/Magnetometer Sensor
tests:
sample.sensor.fxos8700.hybrid:
platform_allow:
- frdm_k64f
- hexiwear/mk64f12
- warp7/mcimx7d/m4
- frdm_kw41z
- rv32m1_vega/openisa_rv32m1/ri5cy
- twr_ke18f
- lpcxpresso55s16
- mimxrt685_evk/mimxrt685s/cm33
- frdm_k22f
- mimxrt1024_evk
- mimxrt595_evk/mimxrt595s/cm33
integration_platforms:
- frdm_k64f
extra_configs:
- CONFIG_FXOS8700_PULSE=y
- CONFIG_FXOS8700_PULSE_INT1=y
- CONFIG_FXOS8700_MOTION=y
- CONFIG_FXOS8700_MOTION_INT1=y
- CONFIG_FXOS8700_MAG_VECM=y
sample.sensor.fxos8700.accel:
platform_allow:
- frdm_kl25z
- bbc_microbit
- lpcxpresso55s69/lpc55s69/cpu0
- reel_board
- mimxrt685_evk/mimxrt685s/cm33
- mimxrt595_evk/mimxrt595s/cm33
integration_platforms:
- bbc_microbit
extra_args: CONF_FILE=prj_accel.conf

View file

@ -1,111 +0,0 @@
/*
* Copyright (c) 2016 Freescale Semiconductor, Inc.
* Copyright (c) 2018 Phytec Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <stdio.h>
K_SEM_DEFINE(sem, 0, 1); /* starts off "not available" */
#if !defined(CONFIG_FXOS8700_TRIGGER_NONE)
static void trigger_handler(const struct device *dev,
const struct sensor_trigger *trigger)
{
ARG_UNUSED(trigger);
/* Always fetch the sample to clear the data ready interrupt in the
* sensor.
*/
if (sensor_sample_fetch(dev)) {
printf("sensor_sample_fetch failed\n");
return;
}
k_sem_give(&sem);
}
#endif /* !CONFIG_FXOS8700_TRIGGER_NONE */
int main(void)
{
struct sensor_value accel[3];
const struct device *const dev = DEVICE_DT_GET_ONE(nxp_fxos8700);
if (!device_is_ready(dev)) {
printf("Device %s is not ready\n", dev->name);
return 0;
}
struct sensor_value attr = {
.val1 = 6,
.val2 = 250000,
};
if (sensor_attr_set(dev, SENSOR_CHAN_ALL,
SENSOR_ATTR_SAMPLING_FREQUENCY, &attr)) {
printk("Could not set sampling frequency\n");
return 0;
}
#ifdef CONFIG_FXOS8700_MOTION
attr.val1 = 10;
attr.val2 = 600000;
if (sensor_attr_set(dev, SENSOR_CHAN_ALL,
SENSOR_ATTR_SLOPE_TH, &attr)) {
printk("Could not set slope threshold\n");
return 0;
}
#endif
#if !defined(CONFIG_FXOS8700_TRIGGER_NONE)
struct sensor_trigger trig = {
#ifdef CONFIG_FXOS8700_MOTION
.type = SENSOR_TRIG_DELTA,
#else
.type = SENSOR_TRIG_DATA_READY,
#endif
.chan = SENSOR_CHAN_ACCEL_XYZ,
};
if (sensor_trigger_set(dev, &trig, trigger_handler)) {
printf("Could not set trigger\n");
return 0;
}
#endif /* !CONFIG_FXOS8700_TRIGGER_NONE */
while (1) {
#ifdef CONFIG_FXOS8700_TRIGGER_NONE
k_sleep(K_MSEC(160));
sensor_sample_fetch(dev);
#else
k_sem_take(&sem, K_FOREVER);
#endif
sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ, accel);
/* Print accel x,y,z data */
printf("AX=%10.6f AY=%10.6f AZ=%10.6f ",
sensor_value_to_double(&accel[0]),
sensor_value_to_double(&accel[1]),
sensor_value_to_double(&accel[2]));
#if defined(CONFIG_FXOS8700_MODE_MAGN) || defined(CONFIG_FXOS8700_MODE_HYBRID)
struct sensor_value magn[3];
sensor_channel_get(dev, SENSOR_CHAN_MAGN_XYZ, magn);
/* Print mag x,y,z data */
printf("MX=%10.6f MY=%10.6f MZ=%10.6f ",
sensor_value_to_double(&magn[0]),
sensor_value_to_double(&magn[1]),
sensor_value_to_double(&magn[2]));
#endif
#ifdef CONFIG_FXOS8700_TEMP
struct sensor_value temp;
sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP, &temp);
/* Print accel x,y,z and mag x,y,z data */
printf("T=%10.6f", sensor_value_to_double(&temp));
#endif
printf("\n");
}
}

View file

@ -1,8 +1,9 @@
# Copyright (c) 2024 NXP
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(fxos8700)
project(magn_trig)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})

View file

@ -0,0 +1,46 @@
.. zephyr:Code-sample:: magn-trig
:name: Magnetometer trigger
Test and debug magnetometer with interrupts
Overview
********
Sample application that reads magnetometer (X, Y, Z) data from
the available device that implements SENSOR_TRIG_DATA_READY and SENSOR_CHAN_MAGN_*.
Building and Running
********************
.. code-block:: devicetree
/ {
aliases {
magn0 = &fxos8700;
};
};
Make sure the aliases are in devicetree, then build and run with:
.. zephyr-app-commands::
:zephyr-app: samples/sensor/magn_trig
:board: <board to use>
:goals: build flash
:compact:
Sample Output
=============
.. code-block:: console
fxos8700@1d (x, y, z): ( -0.107000, 0.118000, -1.026000)
fxos8700@1d (x, y, z): ( -0.132000, 0.083000, -0.981000)
fxos8700@1d (x, y, z): ( -0.143000, 0.102000, -0.931000)
fxos8700@1d (x, y, z): ( -0.153000, 0.126000, -0.843000)
fxos8700@1d (x, y, z): ( -0.145000, 0.152000, -0.802000)
fxos8700@1d (x, y, z): ( -0.143000, 0.125000, -0.740000)
fxos8700@1d (x, y, z): ( -0.133000, 0.130000, -0.736000)
fxos8700@1d (x, y, z): ( -0.133000, 0.124000, -0.776000)
fxos8700@1d (x, y, z): ( -0.120000, 0.123000, -0.776000)
fxos8700@1d (x, y, z): ( -0.135000, 0.120000, -0.782000)
fxos8700@1d (x, y, z): ( -0.129000, 0.116000, -0.787000)

View file

@ -0,0 +1 @@
CONFIG_FXOS8700_TRIGGER_OWN_THREAD=y

View file

@ -0,0 +1,3 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_SENSOR=y
CONFIG_CBPRINTF_FP_SUPPORT=y

View file

@ -0,0 +1,14 @@
sample:
name: Magnetometer trigger sample
tests:
sample.sensor.magn_trigger:
harness: console
tags: sensors
filter: dt_alias_exists("magn0")
harness_config:
type: one_line
regex:
- "^\\s*[0-9A-Za-z_,+-.]*@[0-9A-Fa-f]* \\(x, y, z\\): \
\\(\\s*-?[0-9\\.]*,\\s*-?[0-9\\.]*,\\s*-?[0-9\\.]*\\)$"
integration_platforms:
- frdm_k64f # fxos8700

View file

@ -0,0 +1,58 @@
/*
* Copyright 2024 NXP
* Copyright (c) 2018 Phytec Messtechnik GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/drivers/sensor.h>
#include <stdio.h>
K_SEM_DEFINE(sem, 0, 1); /* starts off "not available" */
static void trigger_handler(const struct device *dev, const struct sensor_trigger *trigger)
{
ARG_UNUSED(trigger);
/* Always fetch the sample to clear the data ready interrupt in the
* sensor.
*/
if (sensor_sample_fetch(dev)) {
printf("sensor_sample_fetch failed\n");
return;
}
k_sem_give(&sem);
}
int main(void)
{
struct sensor_value data[3];
const struct device *const dev = DEVICE_DT_GET(DT_ALIAS(magn0));
struct sensor_trigger trig = {
.type = SENSOR_TRIG_DATA_READY,
.chan = SENSOR_CHAN_MAGN_XYZ,
};
if (!device_is_ready(dev)) {
printf("Device %s is not ready\n", dev->name);
return 0;
}
if (sensor_trigger_set(dev, &trig, trigger_handler)) {
printf("Could not set trigger\n");
return 0;
}
while (1) {
k_sem_take(&sem, K_FOREVER);
sensor_channel_get(dev, SENSOR_CHAN_MAGN_XYZ, data);
/* Print magn x,y,z data */
printf("%16s (x, y, z): (%12.6f, %12.6f, %12.6f)\n", dev->name,
sensor_value_to_double(&data[0]), sensor_value_to_double(&data[1]),
sensor_value_to_double(&data[2]));
}
}