Skip to content

Commit a8722a9

Browse files
JuHyun Kimgalak
authored andcommitted
samples: sensor: sample app of ICM42605 motion sensor
Sample appication of TDK Invensense ICM42605 motion sensor. Signed-off-by: JuHyun Kim <[email protected]>
1 parent cc56fb5 commit a8722a9

File tree

6 files changed

+256
-0
lines changed

6 files changed

+256
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2019 TDK Invensense
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
cmake_minimum_required(VERSION 3.13.1)
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(icm42605)
10+
11+
FILE(GLOB app_sources src/*.c)
12+
target_sources(app PRIVATE ${app_sources})

samples/sensor/icm42605/README.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
.. _icm42605:
2+
3+
MPU6050: Invensense Motion Tracking Device
4+
##########################################
5+
6+
Description
7+
***********
8+
9+
This sample application periodically (10 Hz) measures the sensor
10+
temperature, acceleration, and angular velocity, tap, double tap
11+
displaying the values on the console along with a timestamp since
12+
startup.
13+
14+
Wiring
15+
*******
16+
17+
This sample uses an external breakout for the sensor. A devicetree
18+
overlay must be provided to identify the SPI bus and GPIO used to
19+
control the sensor.
20+
21+
Building and Running
22+
********************
23+
24+
After providing a devicetree overlay that specifies the sensor location,
25+
build this sample app using:
26+
27+
.. zephyr-app-commands::
28+
:zephyr-app: samples/sensor/icm42605
29+
:board: nrf52dk_nrf52832
30+
:goals: build flash
31+
32+
Sample Output
33+
=============
34+
35+
.. code-block:: console
36+
37+
*** Booting Zephyr OS build zephyr-v2.1.0-576-g4b38659b0661 ***
38+
[0:00:00.008]:23.6359 Cel
39+
accel -5.882554 -6.485893 5.868188 m/s/s
40+
gyro 0.014522 0.002264 -0.036905 rad/s
41+
[0:00:02.020]:23.6359 Cel
42+
accel -5.841853 -6.435615 5.911283 m/s/s
43+
gyro 0.017852 0.001199 -0.034640 rad/s
44+
[0:00:04.032]:23.6829 Cel
45+
accel -5.930438 -6.461951 6.009446 m/s/s
46+
gyro 0.012923 0.002131 -0.037171 rad/s
47+
[0:00:06.044]:23.6359 Cel
48+
accel -5.884948 -6.524200 5.961562 m/s/s
49+
gyro 0.012390 -0.001732 -0.045964 rad/s
50+
[0:00:08.056]:35.7712 Cel
51+
accel -5.863400 -12.872426 -0.154427 m/s/s
52+
gyro -0.034373 -0.034373 -0.034373 rad/s
53+
[0:00:10.068]:23.6829 Cel
54+
accel -5.906496 -6.461951 5.899312 m/s/s
55+
gyro 0.015321 -0.000399 -0.039169 rad/s
56+
57+
<repeats endlessly>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2020 TDK Invensense
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
&spi0 {
8+
compatible = "nordic,nrf-spi";
9+
status = "okay";
10+
sck-pin = <27>;
11+
mosi-pin = <26>;
12+
miso-pin = <23>;
13+
cs-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>;
14+
icm42605@0 {
15+
compatible = "invensense,icm42605";
16+
spi-max-frequency = <24000000>;
17+
reg = <0>;
18+
label = "ICM42605";
19+
int-gpios = <&gpio0 18 GPIO_ACTIVE_HIGH>;
20+
};
21+
};

samples/sensor/icm42605/prj.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# Copyright (c) 2019 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
CONFIG_SPI=y
8+
CONFIG_SENSOR=y
9+
CONFIG_ICM42605=y
10+
CONFIG_ICM42605_TRIGGER_OWN_THREAD=y
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Copyright (c) 2020 TDK Invensense
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
sample:
8+
name: ICM42605 Sensor Sample
9+
tests:
10+
sample.sensor.icm42605:
11+
build_only: true
12+
platform_allow: nrf52dk_nrf52832
13+
tags: sensors

samples/sensor/icm42605/src/main.c

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (c) 2020 TDK Invensense
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr.h>
8+
#include <device.h>
9+
#include <drivers/sensor.h>
10+
#include <stdio.h>
11+
12+
static const char *now_str(void)
13+
{
14+
static char buf[16]; /* ...HH:MM:SS.MMM */
15+
uint32_t now = k_uptime_get_32();
16+
unsigned int ms = now % MSEC_PER_SEC;
17+
unsigned int s;
18+
unsigned int min;
19+
unsigned int h;
20+
21+
now /= MSEC_PER_SEC;
22+
s = now % 60U;
23+
now /= 60U;
24+
min = now % 60U;
25+
now /= 60U;
26+
h = now;
27+
28+
snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u",
29+
h, min, s, ms);
30+
return buf;
31+
}
32+
33+
static int process_icm42605(const struct device *dev)
34+
{
35+
struct sensor_value temperature;
36+
struct sensor_value accel[3];
37+
struct sensor_value gyro[3];
38+
int rc = sensor_sample_fetch(dev);
39+
40+
if (rc == 0) {
41+
rc = sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ,
42+
accel);
43+
}
44+
if (rc == 0) {
45+
rc = sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ,
46+
gyro);
47+
}
48+
if (rc == 0) {
49+
rc = sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP,
50+
&temperature);
51+
}
52+
if (rc == 0) {
53+
printf("[%s]:% g Cel\n"
54+
" accel % f % f % f m/s/s\n"
55+
" gyro % f % f % f rad/s\n",
56+
now_str(),
57+
sensor_value_to_double(&temperature),
58+
sensor_value_to_double(&accel[0]),
59+
sensor_value_to_double(&accel[1]),
60+
sensor_value_to_double(&accel[2]),
61+
sensor_value_to_double(&gyro[0]),
62+
sensor_value_to_double(&gyro[1]),
63+
sensor_value_to_double(&gyro[2]));
64+
} else {
65+
printf("sample fetch/get failed: %d\n", rc);
66+
}
67+
68+
return rc;
69+
}
70+
71+
static struct sensor_trigger data_trigger;
72+
static struct sensor_trigger tap_trigger;
73+
static struct sensor_trigger double_tap_trigger;
74+
75+
static void handle_icm42605_drdy(const struct device *dev,
76+
struct sensor_trigger *trig)
77+
{
78+
int rc = process_icm42605(dev);
79+
80+
if (rc != 0) {
81+
printf("cancelling trigger due to failure: %d\n", rc);
82+
(void)sensor_trigger_set(dev, trig, NULL);
83+
return;
84+
}
85+
}
86+
87+
static void handle_icm42605_tap(const struct device *dev,
88+
struct sensor_trigger *trig)
89+
{
90+
printf("Tap Detected!\n");
91+
}
92+
93+
static void handle_icm42605_double_tap(const struct device *dev,
94+
struct sensor_trigger *trig)
95+
{
96+
printf("Double Tap detected!\n");
97+
}
98+
99+
void main(void)
100+
{
101+
const char *const label = DT_LABEL(DT_INST(0, invensense_icm42605));
102+
const struct device *icm42605 = device_get_binding(label);
103+
104+
if (!icm42605) {
105+
printf("Failed to find sensor %s\n", label);
106+
return;
107+
}
108+
109+
tap_trigger = (struct sensor_trigger) {
110+
.type = SENSOR_TRIG_TAP,
111+
.chan = SENSOR_CHAN_ALL,
112+
};
113+
114+
if (sensor_trigger_set(icm42605, &tap_trigger,
115+
handle_icm42605_tap) < 0) {
116+
printf("Cannot configure tap trigger!!!\n");
117+
return;
118+
}
119+
120+
double_tap_trigger = (struct sensor_trigger) {
121+
.type = SENSOR_TRIG_DOUBLE_TAP,
122+
.chan = SENSOR_CHAN_ALL,
123+
};
124+
125+
if (sensor_trigger_set(icm42605, &double_tap_trigger,
126+
handle_icm42605_double_tap) < 0) {
127+
printf("Cannot configure double tap trigger!!!\n");
128+
return;
129+
}
130+
131+
data_trigger = (struct sensor_trigger) {
132+
.type = SENSOR_TRIG_DATA_READY,
133+
.chan = SENSOR_CHAN_ALL,
134+
};
135+
136+
if (sensor_trigger_set(icm42605, &data_trigger,
137+
handle_icm42605_drdy) < 0) {
138+
printf("Cannot configure data trigger!!!\n");
139+
return;
140+
}
141+
142+
printf("Configured for triggered sampling.\n");
143+
}

0 commit comments

Comments
 (0)