samples: sensors: add sample for i3g4250d gyro

Adds a sample for the ST I3G4250D gyroscope.

Signed-off-by: Jonathan Hahn <Jonathan.Hahn@t-online.de>
This commit is contained in:
Jonathan Hahn 2021-08-11 01:10:49 +02:00 committed by Christopher Friedt
commit a133bf8821
5 changed files with 146 additions and 0 deletions

View file

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

View file

@ -0,0 +1,47 @@
.. _i3g4250d:
I3G4250D: 3-axis digital output gyroscope
#########################################
Description
***********
This sample application configures the gyroscope with a fixed
sampling rate and polls the measurements with the same rate and
displays the measurements along with a timestamp since startup.
Requirements
************
This sample application uses the I3G4250D sensor connected via
SPI interface. This is the case for example for stm32f3_disco@E
board devicetree.
Building and Running
********************
To build the application a board with I3G4250D on SPI interface
has to be chosen, or a custom devicetree overlay has to be provided.
Here STM32F3 discovery board is used.
.. zephyr-app-commands::
:zephyr-app: samples/sensor/i3g4250d
:board: stm32f3_disco@E
:goals: build flash
Sample Output
*************
.. code-block:: console
*** Booting Zephyr OS build zephyr-v2.6.0-1897-ga09838064f26 ***
Set sensor sampling frequency to 200.000000 Hz.
Polling at 200.000000 Hz
12 ms: x 2454.031430 , y 2336.015410 , z -1800.030108
22 ms: x -248.003106 , y -268.979704 , z 6.018390
32 ms: x -214.989906 , y -237.023468 , z 6.013926
41 ms: x -193.978308 , y -205.032232 , z 2.000356
51 ms: x -158.986716 , y -171.014568 , z 3.969998
60 ms: x -138.979582 , y -153.003326 , z 3.978748
70 ms: x -120.981554 , y -129.982800 , z -13.971422
80 ms: x -106.984060 , y -112.967272 , z -80.006572

View file

@ -0,0 +1,3 @@
CONFIG_SPI=y
CONFIG_SENSOR=y
CONFIG_I3G4250D=y

View file

@ -0,0 +1,12 @@
#
# Copyright (c) 2021 Jonathan Hahn
#
# SPDX-License-Identifier: Apache-2.0
#
sample:
name: I3G4250D Sensor Sample
tests:
sample.sensor.i3g4250d:
platform_allow: stm32f3_disco@E
tags: sensors

View file

@ -0,0 +1,72 @@
/*
* Copyright (c) 2021 Jonathan Hahn
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <zephyr.h>
#include <device.h>
#include <drivers/sensor.h>
#define SAMPLING_INTERVAL_MS 10
#define DISPLAY_INTERVAL_MS 50
static int set_sampling_frequency(const struct device *sensor, double sampling_frequency)
{
struct sensor_value setting;
sensor_value_from_double(&setting, sampling_frequency);
return sensor_attr_set(sensor,
SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, &setting);
}
static void fetch_and_display(const struct device *sensor)
{
struct sensor_value gyro[3];
int rc = sensor_sample_fetch(sensor);
if (rc != 0) {
printf("ERROR: Failed fetching gyro values: %d.\n", rc);
return;
}
rc = sensor_channel_get(sensor,
SENSOR_CHAN_GYRO_XYZ,
gyro);
if (rc != 0) {
printf("ERROR: Failed getting gyro values: %d\n", rc);
return;
}
printf("%u ms: x %f , y %f , z %f\n",
k_uptime_get_32(),
sensor_value_to_double(&gyro[0]),
sensor_value_to_double(&gyro[1]),
sensor_value_to_double(&gyro[2]));
}
void main(void)
{
const double sampling_frequency = 1000.0 / SAMPLING_INTERVAL_MS;
const struct device *sensor = DEVICE_DT_GET_ONE(st_i3g4250d);
if (!device_is_ready(sensor)) {
printf("Sensor %s is not ready\n", sensor->name);
return;
}
printf("Set sensor sampling frequency to %f Hz.\n", sampling_frequency);
set_sampling_frequency(sensor, sampling_frequency);
printf("Start polling with an interval of %d ms\n", DISPLAY_INTERVAL_MS);
while (true) {
fetch_and_display(sensor);
/* Wait some time before printing the next value */
k_sleep(K_MSEC(DISPLAY_INTERVAL_MS));
}
}