Skip to content

Commit 6582f88

Browse files
samples: sensor: Add sample for ST lsm6dsr driver
This sample demonstrates the usage for ST lsm6dsr inertial measurement unit (IMU) driver. Origin: Original Signed-off-by: Marco Schuler <[email protected]>
1 parent 0a73550 commit 6582f88

File tree

7 files changed

+245
-0
lines changed

7 files changed

+245
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) 2025 Helbling Technik AG
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
cmake_minimum_required(VERSION 3.20.0)
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
project(lsm6dsr)
8+
9+
FILE(GLOB app_sources src/*.c)
10+
target_sources(app PRIVATE ${app_sources})

samples/sensor/lsm6dsr/README.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.. zephyr:code-sample:: lsmd6dsr
2+
:name: LSM6DSR IMU sensor
3+
:relevant-api: sensor_interface
4+
5+
Get accelerometer and gyroscope data from an LSM6DSR sensor (polling).
6+
7+
Overview
8+
********
9+
10+
This sample sets the LSM6DSR accelerometer and gyroscope to 104Hz and reads the
11+
values (polling) in a loop. It displays the values for accelerometer and
12+
gyroscope on the console.
13+
14+
15+
Requirements
16+
************
17+
18+
This sample uses the LSM6DSR sensor controlled using the I2C or SPI interface.
19+
It has been tested on the nRF52840 DK board with an STEVAL-MKI194V1 attached.
20+
21+
References
22+
**********
23+
24+
- LSM6DSR https://www.st.com/en/mems-and-sensors/lsm6dsr.html
25+
- STEVAL-MKI194V1 https://www.st.com/en/evaluation-tools/steval-mki194v1.html
26+
- nRF52840 DK https://www.nordicsemi.com/Products/Development-hardware/nRF52840-DK
27+
28+
Building and Running
29+
********************
30+
31+
This project outputs sensor data to the console. It requires an LSM6DSR
32+
sensor attached to the nRF52840 DK.
33+
34+
There are two devicetree overlays to either chose building for I2C or SPI
35+
36+
- i2c.overlay
37+
- spi.overlay
38+
39+
Use DEXTRA_DTC_OVERLAY_FILE to specify which one to use.
40+
41+
Building on nRF52840 board
42+
==========================
43+
44+
.. zephyr-app-commands::
45+
:zephyr-app: samples/sensor/lsm6dsr
46+
:host-os: unix
47+
:board: nRF52840
48+
:goals: build
49+
:compact:
50+
51+
Sample Output
52+
=============
53+
54+
.. code-block:: console
55+
56+
LSM6DSR sensor samples:
57+
58+
accel x:4.705485 ms/2 y:-8.873782 ms/2 z:0.023928 ms/2
59+
gyro x:0.006490 dps y:-0.007559 dps z:-0.002977 dps
60+
loop:4 trig_cnt:339
61+
62+
<repeats endlessly every 1 seconds>

samples/sensor/lsm6dsr/i2c.overlay

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright (c) 2025 Helbling Technik AG
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&i2c0 {
8+
lsm6dsr: lsm6dsr@6b {
9+
compatible = "st,lsm6dsr";
10+
reg = <0x6b>;
11+
irq-gpios = <&gpio1 7 GPIO_ACTIVE_HIGH>;
12+
};
13+
};

samples/sensor/lsm6dsr/prj.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2025 Helbling Technik AG
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
CONFIG_STDOUT_CONSOLE=y
6+
CONFIG_I2C=y
7+
CONFIG_SPI=y
8+
CONFIG_SENSOR=y
9+
CONFIG_CBPRINTF_FP_SUPPORT=y
10+
11+
CONFIG_LSM6DSR_TRIGGER_OWN_THREAD=y

samples/sensor/lsm6dsr/sample.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
sample:
2+
name: LSM6DSR accelerometer and gyrometer sensor
3+
tests:
4+
sample.sensor.lsm6dsr:
5+
harness: console
6+
tags: sensors
7+
depends_on: lsm6dsr
8+
timeout: 15
9+
harness_config:
10+
type: multi_line
11+
ordered: true
12+
regex:
13+
- "accel x:[-.0-9]* ms/2 y:[-.0-9]* ms/2 z:[-.0-9]* ms/2"
14+
- "gyro x:[-.0-9]* dps y:[-.0-9]* dps z:[-.0-9]* dps"
15+
- "loop:[0-9]* trig_cnt:[0-9]*"

samples/sensor/lsm6dsr/spi.overlay

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright (c) 2025 Helbling Technik AG
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&spi1 {
8+
cs-gpios = <&gpio1 4 GPIO_ACTIVE_LOW>;
9+
lsm6dsr: lsm6dsr@0 {
10+
compatible = "st,lsm6dsr";
11+
reg = <0>;
12+
irq-gpios = <&gpio1 7 GPIO_ACTIVE_HIGH>;
13+
spi-max-frequency = <4000000>;
14+
};
15+
};

samples/sensor/lsm6dsr/src/main.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2025 Helbling Technik AG
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+
static int print_samples;
14+
static int lsm6dsr_trig_cnt;
15+
16+
static struct sensor_value accel_x_out, accel_y_out, accel_z_out;
17+
static struct sensor_value gyro_x_out, gyro_y_out, gyro_z_out;
18+
19+
#ifdef CONFIG_LSM6DSR_TRIGGER
20+
static void lsm6dsr_trigger_handler(const struct device *dev, const struct sensor_trigger *trig)
21+
{
22+
static struct sensor_value accel_x, accel_y, accel_z;
23+
static struct sensor_value gyro_x, gyro_y, gyro_z;
24+
25+
lsm6dsr_trig_cnt++;
26+
27+
sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
28+
sensor_channel_get(dev, SENSOR_CHAN_ACCEL_X, &accel_x);
29+
sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Y, &accel_y);
30+
sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Z, &accel_z);
31+
32+
/* lsm6dsr gyro */
33+
sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
34+
sensor_channel_get(dev, SENSOR_CHAN_GYRO_X, &gyro_x);
35+
sensor_channel_get(dev, SENSOR_CHAN_GYRO_Y, &gyro_y);
36+
sensor_channel_get(dev, SENSOR_CHAN_GYRO_Z, &gyro_z);
37+
38+
if (print_samples) {
39+
print_samples = 0;
40+
41+
accel_x_out = accel_x;
42+
accel_y_out = accel_y;
43+
accel_z_out = accel_z;
44+
45+
gyro_x_out = gyro_x;
46+
gyro_y_out = gyro_y;
47+
gyro_z_out = gyro_z;
48+
}
49+
}
50+
#endif
51+
52+
int main(void)
53+
{
54+
int cnt = 0;
55+
char out_str[64];
56+
struct sensor_value odr_attr;
57+
const struct device *const lsm6dsr_dev = DEVICE_DT_GET_ONE(st_lsm6dsr);
58+
59+
if (!device_is_ready(lsm6dsr_dev)) {
60+
printk("sensor: device not ready.\n");
61+
return 0;
62+
}
63+
64+
/* set accel/gyro sampling frequency to 104 Hz */
65+
odr_attr.val1 = 104;
66+
odr_attr.val2 = 0;
67+
68+
if (sensor_attr_set(lsm6dsr_dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY,
69+
&odr_attr) < 0) {
70+
printk("Cannot set sampling frequency for accelerometer.\n");
71+
return 0;
72+
}
73+
74+
if (sensor_attr_set(lsm6dsr_dev, SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY,
75+
&odr_attr) < 0) {
76+
printk("Cannot set sampling frequency for gyro.\n");
77+
return 0;
78+
}
79+
80+
#ifdef CONFIG_LSM6DSR_TRIGGER
81+
struct sensor_trigger trig;
82+
83+
trig.type = SENSOR_TRIG_DATA_READY;
84+
trig.chan = SENSOR_CHAN_ACCEL_XYZ;
85+
86+
if (sensor_trigger_set(lsm6dsr_dev, &trig, lsm6dsr_trigger_handler) != 0) {
87+
printk("Could not set sensor type and channel\n");
88+
return 0;
89+
}
90+
#endif
91+
92+
if (sensor_sample_fetch(lsm6dsr_dev) < 0) {
93+
printk("Sensor sample update error\n");
94+
return 0;
95+
}
96+
97+
while (1) {
98+
/* Erase previous */
99+
printk("\0033\014");
100+
printf("LSM6DSR sensor samples:\n\n");
101+
102+
/* lsm6dsr accel */
103+
sprintf(out_str, "accel x:%f ms/2 y:%f ms/2 z:%f ms/2",
104+
sensor_value_to_double(&accel_x_out), sensor_value_to_double(&accel_y_out),
105+
sensor_value_to_double(&accel_z_out));
106+
printk("%s\n", out_str);
107+
108+
/* lsm6dsr gyro */
109+
sprintf(out_str, "gyro x:%f dps y:%f dps z:%f dps",
110+
sensor_value_to_double(&gyro_x_out), sensor_value_to_double(&gyro_y_out),
111+
sensor_value_to_double(&gyro_z_out));
112+
printk("%s\n", out_str);
113+
114+
printk("loop:%d trig_cnt:%d\n\n", ++cnt, lsm6dsr_trig_cnt);
115+
116+
print_samples = 1;
117+
k_sleep(K_MSEC(1000));
118+
}
119+
}

0 commit comments

Comments
 (0)