From a8722a9e65f6622a3b2270722d59c04760e10a07 Mon Sep 17 00:00:00 2001 From: JuHyun Kim Date: Sun, 1 Nov 2020 05:15:00 -0800 Subject: [PATCH] samples: sensor: sample app of ICM42605 motion sensor Sample appication of TDK Invensense ICM42605 motion sensor. Signed-off-by: JuHyun Kim --- samples/sensor/icm42605/CMakeLists.txt | 12 ++ samples/sensor/icm42605/README.rst | 57 +++++++ .../icm42605/boards/nrf52dk_nrf52832.overlay | 21 +++ samples/sensor/icm42605/prj.conf | 10 ++ samples/sensor/icm42605/sample.yaml | 13 ++ samples/sensor/icm42605/src/main.c | 143 ++++++++++++++++++ 6 files changed, 256 insertions(+) create mode 100644 samples/sensor/icm42605/CMakeLists.txt create mode 100644 samples/sensor/icm42605/README.rst create mode 100644 samples/sensor/icm42605/boards/nrf52dk_nrf52832.overlay create mode 100644 samples/sensor/icm42605/prj.conf create mode 100644 samples/sensor/icm42605/sample.yaml create mode 100644 samples/sensor/icm42605/src/main.c diff --git a/samples/sensor/icm42605/CMakeLists.txt b/samples/sensor/icm42605/CMakeLists.txt new file mode 100644 index 0000000000..43629dec9e --- /dev/null +++ b/samples/sensor/icm42605/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2019 TDK Invensense +# +# SPDX-License-Identifier: Apache-2.0 +# + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(icm42605) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/samples/sensor/icm42605/README.rst b/samples/sensor/icm42605/README.rst new file mode 100644 index 0000000000..a438f0dfbf --- /dev/null +++ b/samples/sensor/icm42605/README.rst @@ -0,0 +1,57 @@ +.. _icm42605: + +MPU6050: Invensense Motion Tracking Device +########################################## + +Description +*********** + +This sample application periodically (10 Hz) measures the sensor +temperature, acceleration, and angular velocity, tap, double tap +displaying the values on the console along with a timestamp since +startup. + +Wiring +******* + +This sample uses an external breakout for the sensor. A devicetree +overlay must be provided to identify the SPI bus and GPIO used to +control the sensor. + +Building and Running +******************** + +After providing a devicetree overlay that specifies the sensor location, +build this sample app using: + +.. zephyr-app-commands:: + :zephyr-app: samples/sensor/icm42605 + :board: nrf52dk_nrf52832 + :goals: build flash + +Sample Output +============= + +.. code-block:: console + + *** Booting Zephyr OS build zephyr-v2.1.0-576-g4b38659b0661 *** + [0:00:00.008]:23.6359 Cel + accel -5.882554 -6.485893 5.868188 m/s/s + gyro 0.014522 0.002264 -0.036905 rad/s + [0:00:02.020]:23.6359 Cel + accel -5.841853 -6.435615 5.911283 m/s/s + gyro 0.017852 0.001199 -0.034640 rad/s + [0:00:04.032]:23.6829 Cel + accel -5.930438 -6.461951 6.009446 m/s/s + gyro 0.012923 0.002131 -0.037171 rad/s + [0:00:06.044]:23.6359 Cel + accel -5.884948 -6.524200 5.961562 m/s/s + gyro 0.012390 -0.001732 -0.045964 rad/s + [0:00:08.056]:35.7712 Cel + accel -5.863400 -12.872426 -0.154427 m/s/s + gyro -0.034373 -0.034373 -0.034373 rad/s + [0:00:10.068]:23.6829 Cel + accel -5.906496 -6.461951 5.899312 m/s/s + gyro 0.015321 -0.000399 -0.039169 rad/s + + diff --git a/samples/sensor/icm42605/boards/nrf52dk_nrf52832.overlay b/samples/sensor/icm42605/boards/nrf52dk_nrf52832.overlay new file mode 100644 index 0000000000..50ec697cca --- /dev/null +++ b/samples/sensor/icm42605/boards/nrf52dk_nrf52832.overlay @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2020 TDK Invensense + * + * SPDX-License-Identifier: Apache-2.0 + */ + +&spi0 { + compatible = "nordic,nrf-spi"; + status = "okay"; + sck-pin = <27>; + mosi-pin = <26>; + miso-pin = <23>; + cs-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>; + icm42605@0 { + compatible = "invensense,icm42605"; + spi-max-frequency = <24000000>; + reg = <0>; + label = "ICM42605"; + int-gpios = <&gpio0 18 GPIO_ACTIVE_HIGH>; + }; +}; diff --git a/samples/sensor/icm42605/prj.conf b/samples/sensor/icm42605/prj.conf new file mode 100644 index 0000000000..17c239eab6 --- /dev/null +++ b/samples/sensor/icm42605/prj.conf @@ -0,0 +1,10 @@ +# +# Copyright (c) 2019 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# + +CONFIG_SPI=y +CONFIG_SENSOR=y +CONFIG_ICM42605=y +CONFIG_ICM42605_TRIGGER_OWN_THREAD=y diff --git a/samples/sensor/icm42605/sample.yaml b/samples/sensor/icm42605/sample.yaml new file mode 100644 index 0000000000..bee8ff0807 --- /dev/null +++ b/samples/sensor/icm42605/sample.yaml @@ -0,0 +1,13 @@ +# +# Copyright (c) 2020 TDK Invensense +# +# SPDX-License-Identifier: Apache-2.0 +# + +sample: + name: ICM42605 Sensor Sample +tests: + sample.sensor.icm42605: + build_only: true + platform_allow: nrf52dk_nrf52832 + tags: sensors diff --git a/samples/sensor/icm42605/src/main.c b/samples/sensor/icm42605/src/main.c new file mode 100644 index 0000000000..105794c63e --- /dev/null +++ b/samples/sensor/icm42605/src/main.c @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2020 TDK Invensense + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +static const char *now_str(void) +{ + static char buf[16]; /* ...HH:MM:SS.MMM */ + uint32_t now = k_uptime_get_32(); + unsigned int ms = now % MSEC_PER_SEC; + unsigned int s; + unsigned int min; + unsigned int h; + + now /= MSEC_PER_SEC; + s = now % 60U; + now /= 60U; + min = now % 60U; + now /= 60U; + h = now; + + snprintf(buf, sizeof(buf), "%u:%02u:%02u.%03u", + h, min, s, ms); + return buf; +} + +static int process_icm42605(const struct device *dev) +{ + struct sensor_value temperature; + struct sensor_value accel[3]; + struct sensor_value gyro[3]; + int rc = sensor_sample_fetch(dev); + + if (rc == 0) { + rc = sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ, + accel); + } + if (rc == 0) { + rc = sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ, + gyro); + } + if (rc == 0) { + rc = sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP, + &temperature); + } + if (rc == 0) { + printf("[%s]:% g Cel\n" + " accel % f % f % f m/s/s\n" + " gyro % f % f % f rad/s\n", + now_str(), + sensor_value_to_double(&temperature), + sensor_value_to_double(&accel[0]), + sensor_value_to_double(&accel[1]), + sensor_value_to_double(&accel[2]), + sensor_value_to_double(&gyro[0]), + sensor_value_to_double(&gyro[1]), + sensor_value_to_double(&gyro[2])); + } else { + printf("sample fetch/get failed: %d\n", rc); + } + + return rc; +} + +static struct sensor_trigger data_trigger; +static struct sensor_trigger tap_trigger; +static struct sensor_trigger double_tap_trigger; + +static void handle_icm42605_drdy(const struct device *dev, + struct sensor_trigger *trig) +{ + int rc = process_icm42605(dev); + + if (rc != 0) { + printf("cancelling trigger due to failure: %d\n", rc); + (void)sensor_trigger_set(dev, trig, NULL); + return; + } +} + +static void handle_icm42605_tap(const struct device *dev, + struct sensor_trigger *trig) +{ + printf("Tap Detected!\n"); +} + +static void handle_icm42605_double_tap(const struct device *dev, + struct sensor_trigger *trig) +{ + printf("Double Tap detected!\n"); +} + +void main(void) +{ + const char *const label = DT_LABEL(DT_INST(0, invensense_icm42605)); + const struct device *icm42605 = device_get_binding(label); + + if (!icm42605) { + printf("Failed to find sensor %s\n", label); + return; + } + + tap_trigger = (struct sensor_trigger) { + .type = SENSOR_TRIG_TAP, + .chan = SENSOR_CHAN_ALL, + }; + + if (sensor_trigger_set(icm42605, &tap_trigger, + handle_icm42605_tap) < 0) { + printf("Cannot configure tap trigger!!!\n"); + return; + } + + double_tap_trigger = (struct sensor_trigger) { + .type = SENSOR_TRIG_DOUBLE_TAP, + .chan = SENSOR_CHAN_ALL, + }; + + if (sensor_trigger_set(icm42605, &double_tap_trigger, + handle_icm42605_double_tap) < 0) { + printf("Cannot configure double tap trigger!!!\n"); + return; + } + + data_trigger = (struct sensor_trigger) { + .type = SENSOR_TRIG_DATA_READY, + .chan = SENSOR_CHAN_ALL, + }; + + if (sensor_trigger_set(icm42605, &data_trigger, + handle_icm42605_drdy) < 0) { + printf("Cannot configure data trigger!!!\n"); + return; + } + + printf("Configured for triggered sampling.\n"); +}