Skip to content

Commit 9f1c256

Browse files
aviscontifabiobaltieri
authored andcommitted
samples/shields: add samples to test x-nucleo-iks4a1 shield
Provide three basic examples to test the x-nucleo-iks4a1 shield: - Test shield in standard mode Acquire sensor data from shield with all MEMS sensors connected to micro-controller - Test shield in HUB1 mode Acquire sensor data from shield with lis2mdl and lps22df connected to LSM6DSV16X sensor hub - Test shield in HUB2 mode Acquire sensor data from shield with lis2mdl and lps22df connected to LSm6DSO16IS sensor hub Signed-off-by: Armando Visconti <[email protected]>
1 parent cc3040a commit 9f1c256

File tree

16 files changed

+1054
-0
lines changed

16 files changed

+1054
-0
lines changed

boards/shields/x_nucleo_iks4a1/doc/index.rst

+15
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ There are three predefined DT overlays in the board:
150150
This overlay describes sensor connections (and matching h/w configuration to be done)
151151
as explained in SHUB2 Mode (:ref:`x-nucleo-iks4a1-mode-2`)
152152

153+
Examples
154+
********
155+
156+
Three samples are provided as examples for ``x-nucleo-iks4a1`` shield, each one associated
157+
with one of the overlays described above:
158+
159+
- :ref:`x-nucleo-iks4a1-std-sample` application, to be used when the shield is configured
160+
in Standard Mode (Mode 1)
161+
- :ref:`x-nucleo-iks4a1-shub1-sample` application, to be used when the shield is configured
162+
in SHUB1 Mode (Mode 3)
163+
- :ref:`x-nucleo-iks4a1-shub2-sample` application, to be used when the shield is configured
164+
in SHUB2 Mode (Mode 2)
165+
166+
See also :ref:`shields` for more details.
167+
153168
.. _X-NUCLEO-IKS4A1 website:
154169
http://www.st.com/en/ecosystems/x-nucleo-iks4a1.html
155170

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2024 STMicroelectronics
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
cmake_minimum_required(VERSION 3.20.0)
6+
7+
# This sample is specific to x_nucleo_iks4a1 shield. Enforce -DSHIELD option
8+
set(SHIELD x_nucleo_iks4a1_shub1)
9+
10+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
11+
project(x_nucleo_iks4a1)
12+
13+
FILE(GLOB app_sources src/*.c)
14+
target_sources(app PRIVATE ${app_sources})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.. _x-nucleo-iks4a1-shub1-sample:
2+
3+
X-NUCLEO-IKS4A1 shield SHUB1 (Mode 3) sample
4+
############################################
5+
6+
Overview
7+
********
8+
This sample is provided as an example to test the X-NUCLEO-IKS4A1 shield
9+
configured in SHUB1 (Mode 3).
10+
Please refer to :ref:`x-nucleo-iks4a1-mode-3` for more info on this configuration.
11+
12+
This sample enables LSM6DSV16X IMU in sensorhub mode with LIS2MDL magnetometer and
13+
LPS22DF pressure and temperature sensor.
14+
15+
Then sensor data are displayed periodically
16+
17+
- LSM6DSV16X 6-Axis acceleration and angular velocity
18+
- LSM6DSV16X (from LIS2MDL) 3-Axis magnetic field intensity
19+
- LSM6DSV16X (from LPS22DF) ambient temperature and atmospheric pressure
20+
21+
Requirements
22+
************
23+
24+
This sample communicates over I2C with the X-NUCLEO-IKS4A1 shield
25+
stacked on a board with an Arduino connector, e.g. the
26+
:ref:`nucleo_f411re_board` board.
27+
28+
Building and Running
29+
********************
30+
31+
This sample runs with X-NUCLEO-IKS4A1 stacked on any board with a matching
32+
Arduino connector. For this example, we use a :ref:`nucleo_f411re_board` board.
33+
34+
.. zephyr-app-commands::
35+
:zephyr-app: samples/shields/x_nucleo_iks4a1/sensorhub1/
36+
:host-os: unix
37+
:board: nucleo_f411re
38+
:goals: build flash
39+
:compact:
40+
41+
Sample Output
42+
=============
43+
44+
.. code-block:: console
45+
46+
X-NUCLEO-IKS01A4 sensor dashboard
47+
48+
LSM6DSV16X: Accel (m.s-2): x: 0.081, y: -0.177, z: 9.945
49+
LSM6DSV16X: GYro (dps): x: 0.001, y: -0.000, z: 0.004
50+
LSM6DSV16X: Magn (gauss): x: 0.217, y: 0.015, z: -0.415
51+
LSM6DSV16X: Temperature: 19.8 C
52+
LSM6DSV16X: Pressure:99.655 kpa
53+
16:: lsm6dso16is acc trig 6432
54+
55+
<updated endlessly every 2 seconds>
56+
57+
References
58+
**********
59+
60+
:ref:`x-nucleo-iks4a1`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CONFIG_LOG=y
2+
CONFIG_STDOUT_CONSOLE=y
3+
CONFIG_I2C=y
4+
CONFIG_SENSOR=y
5+
CONFIG_SENSOR_LOG_LEVEL_DBG=y
6+
CONFIG_LSM6DSV16X_TRIGGER_OWN_THREAD=y
7+
CONFIG_LSM6DSV16X_SENSORHUB=y
8+
CONFIG_LSM6DSV16X_EXT_LIS2MDL=y
9+
CONFIG_LSM6DSV16X_EXT_LPS22DF=y
10+
CONFIG_CBPRINTF_FP_SUPPORT=y
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sample:
2+
name: X-NUCLEO-IKS01A4 sensor shield
3+
tests:
4+
sample.shields.x_nucleo_iks4a1.sensorhub1:
5+
harness: shield
6+
tags: shield
7+
depends_on: arduino_i2c arduino_gpio
8+
platform_exclude:
9+
- disco_l475_iot1
10+
- lpcxpresso55s16
11+
- mimxrt1010_evk
12+
- stm32mp157c_dk2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* Copyright (c) 2019 STMicroelectronics
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/device.h>
9+
#include <zephyr/drivers/sensor.h>
10+
#include <stdio.h>
11+
#include <zephyr/sys/util.h>
12+
13+
#ifdef CONFIG_LSM6DSV16X_TRIGGER
14+
static int lsm6dsv16x_acc_trig_cnt;
15+
16+
static void lsm6dsv16x_acc_trig_handler(const struct device *dev,
17+
const struct sensor_trigger *trig)
18+
{
19+
sensor_sample_fetch_chan(dev, SENSOR_CHAN_ALL);
20+
lsm6dsv16x_acc_trig_cnt++;
21+
}
22+
#endif
23+
24+
static void lsm6dsv16x_config(const struct device *lsm6dsv16x)
25+
{
26+
struct sensor_value odr_attr, fs_attr;
27+
28+
/* set LSM6DSV16X accel sampling frequency to 208 Hz */
29+
odr_attr.val1 = 208;
30+
odr_attr.val2 = 0;
31+
32+
if (sensor_attr_set(lsm6dsv16x, SENSOR_CHAN_ACCEL_XYZ,
33+
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
34+
printk("Cannot set sampling frequency for LSM6DSV16X accel\n");
35+
return;
36+
}
37+
38+
sensor_g_to_ms2(16, &fs_attr);
39+
40+
if (sensor_attr_set(lsm6dsv16x, SENSOR_CHAN_ACCEL_XYZ,
41+
SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
42+
printk("Cannot set full scale for LSM6DSV16X accel\n");
43+
return;
44+
}
45+
46+
/* set LSM6DSV16X gyro sampling frequency to 208 Hz */
47+
odr_attr.val1 = 208;
48+
odr_attr.val2 = 0;
49+
50+
if (sensor_attr_set(lsm6dsv16x, SENSOR_CHAN_GYRO_XYZ,
51+
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
52+
printk("Cannot set sampling frequency for LSM6DSV16X gyro\n");
53+
return;
54+
}
55+
56+
sensor_degrees_to_rad(250, &fs_attr);
57+
58+
if (sensor_attr_set(lsm6dsv16x, SENSOR_CHAN_GYRO_XYZ,
59+
SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
60+
printk("Cannot set full scale for LSM6DSV16X gyro\n");
61+
return;
62+
}
63+
64+
/* set LSM6DSV16X external magn sampling frequency to 100 Hz */
65+
odr_attr.val1 = 100;
66+
odr_attr.val2 = 0;
67+
68+
#ifdef CONFIG_LSM6DSV16X_EXT_LIS2MDL
69+
if (sensor_attr_set(lsm6dsv16x, SENSOR_CHAN_MAGN_XYZ,
70+
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
71+
printk("Cannot set sampling frequency for LSM6DSV16X ext magn\n");
72+
}
73+
#endif
74+
75+
#ifdef CONFIG_LSM6DSV16X_EXT_LPS22DF
76+
if (sensor_attr_set(lsm6dsv16x, SENSOR_CHAN_PRESS,
77+
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
78+
printk("Cannot set sampling frequency for LSM6DSV16X ext pressure\n");
79+
}
80+
#endif
81+
82+
#ifdef CONFIG_LSM6DSV16X_EXT_HTS221
83+
odr_attr.val1 = 12;
84+
if (sensor_attr_set(lsm6dsv16x, SENSOR_CHAN_HUMIDITY,
85+
SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
86+
printk("Cannot set sampling frequency for LSM6DSV16X ext humidity\n");
87+
}
88+
#endif
89+
90+
#ifdef CONFIG_LSM6DSV16X_TRIGGER
91+
struct sensor_trigger trig;
92+
93+
trig.type = SENSOR_TRIG_DATA_READY;
94+
trig.chan = SENSOR_CHAN_ACCEL_XYZ;
95+
sensor_trigger_set(lsm6dsv16x, &trig, lsm6dsv16x_acc_trig_handler);
96+
#endif
97+
}
98+
99+
int main(void)
100+
{
101+
struct sensor_value lsm6dsv16x_xl[3], lsm6dsv16x_gy[3];
102+
#ifdef CONFIG_LSM6DSV16X_ENABLE_TEMP
103+
struct sensor_value lsm6dsv16x_temp;
104+
#endif
105+
#ifdef CONFIG_LSM6DSV16X_EXT_LIS2MDL
106+
struct sensor_value lis2mdl_magn[3];
107+
#endif
108+
#ifdef CONFIG_LSM6DSV16X_EXT_LPS22DF
109+
struct sensor_value lps22df_press;
110+
struct sensor_value lps22df_temp;
111+
#endif
112+
const struct device *const lsm6dsv16x = DEVICE_DT_GET_ONE(st_lsm6dsv16x);
113+
int cnt = 1;
114+
115+
if (!device_is_ready(lsm6dsv16x)) {
116+
printk("%s: device not ready.\n", lsm6dsv16x->name);
117+
return 0;
118+
}
119+
120+
lsm6dsv16x_config(lsm6dsv16x);
121+
122+
while (1) {
123+
/* Get sensor samples */
124+
#ifndef CONFIG_LSM6DSV16X_TRIGGER
125+
if (sensor_sample_fetch(lsm6dsv16x) < 0) {
126+
printf("LSM6DSV16X Sensor sample update error\n");
127+
return 0;
128+
}
129+
#endif
130+
131+
/* Get sensor data */
132+
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_ACCEL_XYZ, lsm6dsv16x_xl);
133+
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_GYRO_XYZ, lsm6dsv16x_gy);
134+
#ifdef CONFIG_LSM6DSV16X_ENABLE_TEMP
135+
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_DIE_TEMP, &lsm6dsv16x_temp);
136+
#endif
137+
#ifdef CONFIG_LSM6DSV16X_EXT_LIS2MDL
138+
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_MAGN_XYZ, lis2mdl_magn);
139+
#endif
140+
#ifdef CONFIG_LSM6DSV16X_EXT_LPS22DF
141+
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_AMBIENT_TEMP, &lps22df_temp);
142+
sensor_channel_get(lsm6dsv16x, SENSOR_CHAN_PRESS, &lps22df_press);
143+
#endif
144+
145+
/* Display sensor data */
146+
147+
/* Erase previous */
148+
printf("\0033\014");
149+
150+
printf("X-NUCLEO-IKS01A4 sensor dashboard\n\n");
151+
152+
printf("LSM6DSV16X: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
153+
sensor_value_to_double(&lsm6dsv16x_xl[0]),
154+
sensor_value_to_double(&lsm6dsv16x_xl[1]),
155+
sensor_value_to_double(&lsm6dsv16x_xl[2]));
156+
157+
printf("LSM6DSV16X: Gyro (dps): x: %.3f, y: %.3f, z: %.3f\n",
158+
sensor_value_to_double(&lsm6dsv16x_gy[0]),
159+
sensor_value_to_double(&lsm6dsv16x_gy[1]),
160+
sensor_value_to_double(&lsm6dsv16x_gy[2]));
161+
162+
#ifdef CONFIG_LSM6DSV16X_ENABLE_TEMP
163+
/* temperature */
164+
printf("LSM6DSV16X: Temperature: %.1f C\n",
165+
sensor_value_to_double(&lsm6dsv16x_temp));
166+
#endif
167+
168+
#ifdef CONFIG_LSM6DSV16X_EXT_LIS2MDL
169+
printf("LSM6DSV16X: Magn (gauss): x: %.3f, y: %.3f, z: %.3f\n",
170+
sensor_value_to_double(&lis2mdl_magn[0]),
171+
sensor_value_to_double(&lis2mdl_magn[1]),
172+
sensor_value_to_double(&lis2mdl_magn[2]));
173+
#endif
174+
175+
#ifdef CONFIG_LSM6DSV16X_EXT_LPS22DF
176+
printf("LSM6DSV16X: Temperature: %.1f C\n",
177+
sensor_value_to_double(&lps22df_temp));
178+
179+
printf("LSM6DSV16X: Pressure:%.3f kpa\n",
180+
sensor_value_to_double(&lps22df_press));
181+
#endif
182+
183+
#ifdef CONFIG_LSM6DSV16X_TRIGGER
184+
printk("%d: lsm6dsv16x acc trig %d\n", cnt, lsm6dsv16x_acc_trig_cnt);
185+
#endif
186+
187+
cnt++;
188+
k_sleep(K_MSEC(2000));
189+
}
190+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright (c) 2024 STMicroelectronics
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
cmake_minimum_required(VERSION 3.20.0)
6+
7+
# This sample is specific to x_nucleo_iks4a1 shield. Enforce -DSHIELD option
8+
set(SHIELD x_nucleo_iks4a1_shub2)
9+
10+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
11+
project(x_nucleo_iks4a1)
12+
13+
FILE(GLOB app_sources src/*.c)
14+
target_sources(app PRIVATE ${app_sources})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
.. _x-nucleo-iks4a1-shub2-sample:
2+
3+
X-NUCLEO-IKS4A1: shield SHUB2 (Mode 2) sample
4+
#############################################
5+
6+
Overview
7+
********
8+
This sample is provided as an example to test the X-NUCLEO-IKS4A1 shield
9+
configured in Sensor Hub mode (Mode 2).
10+
Please refer to :ref:`x-nucleo-iks4a1-mode-2` for more info on this configuration.
11+
12+
This sample enables LSM6DSO16IS IMU in sensorhub mode with LIS2MDL magnetometer and
13+
LPS22DF pressure and temperature sensor.
14+
15+
Then sensor data are displayed periodically
16+
17+
- LSM6DSO16IS 6-Axis acceleration and angular velocity
18+
- LSM6DSO16IS (from LIS2MDL) 3-Axis magnetic field intensity
19+
- LSM6DSO16IS (from LPS22DF) ambient temperature and atmospheric pressure
20+
21+
Requirements
22+
************
23+
24+
This sample communicates over I2C with the X-NUCLEO-IKS4A1 shield
25+
stacked on a board with an Arduino connector, e.g. the
26+
:ref:`nucleo_f411re_board` board.
27+
28+
Building and Running
29+
********************
30+
31+
This sample runs with X-NUCLEO-IKS4A1 stacked on any board with a matching
32+
Arduino connector. For this example, we use a :ref:`nucleo_f411re_board` board.
33+
34+
.. zephyr-app-commands::
35+
:zephyr-app: samples/shields/x_nucleo_iks4a1/sensorhub2/
36+
:host-os: unix
37+
:board: nucleo_f411re
38+
:goals: build flash
39+
:compact:
40+
41+
Sample Output
42+
=============
43+
44+
.. code-block:: console
45+
46+
X-NUCLEO-IKS01A4 sensor dashboard
47+
48+
LSM6DSO16IS: Accel (m.s-2): x: 0.081, y: -0.177, z: 9.945
49+
LSM6DSO16IS: GYro (dps): x: 0.001, y: -0.000, z: 0.004
50+
LSM6DSO16IS: Magn (gauss): x: 0.217, y: 0.015, z: -0.415
51+
LSM6DSO16IS: Temperature: 20.8 C
52+
LSM6DSO16IS: Pressure:99.756 kpa
53+
736:: lsm6dso16is acc trig 314944
54+
55+
<updated endlessly every 2 seconds>
56+
57+
References
58+
**********
59+
60+
:ref:`x-nucleo-iks4a1`

0 commit comments

Comments
 (0)