blob: 2355c45b4b598fa1c142ca3e2f436930e33c900e [file] [log] [blame]
Bosch Sensorteca56bd002021-01-25 13:03:12 +01001/*
2 * Copyright (c) 2021 Bosch Sensortec GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
Gerard Marull-Paretas79e6b0e2022-08-25 09:58:46 +02007#include <zephyr/kernel.h>
Gerard Marull-Paretasc7b5b3c2022-05-05 16:03:03 +02008#include <zephyr/device.h>
9#include <zephyr/drivers/sensor.h>
Bosch Sensorteca56bd002021-01-25 13:03:12 +010010#include <stdio.h>
11
Keith Packard0b90fd52023-02-07 21:00:59 -080012int main(void)
Bosch Sensorteca56bd002021-01-25 13:03:12 +010013{
Gerard Marull-Paretasa2023412022-08-22 10:36:10 +020014 const struct device *const dev = DEVICE_DT_GET_ONE(bosch_bmi270);
Bosch Sensorteca56bd002021-01-25 13:03:12 +010015 struct sensor_value acc[3], gyr[3];
16 struct sensor_value full_scale, sampling_freq, oversampling;
17
Benjamin Björnsson3710af92022-06-16 20:33:31 +020018 if (!device_is_ready(dev)) {
19 printf("Device %s is not ready\n", dev->name);
Keith Packard0b90fd52023-02-07 21:00:59 -080020 return 0;
Bosch Sensorteca56bd002021-01-25 13:03:12 +010021 }
22
Benjamin Björnsson3710af92022-06-16 20:33:31 +020023 printf("Device %p name is %s\n", dev, dev->name);
Bosch Sensorteca56bd002021-01-25 13:03:12 +010024
25 /* Setting scale in G, due to loss of precision if the SI unit m/s^2
26 * is used
27 */
28 full_scale.val1 = 2; /* G */
29 full_scale.val2 = 0;
30 sampling_freq.val1 = 100; /* Hz. Performance mode */
31 sampling_freq.val2 = 0;
32 oversampling.val1 = 1; /* Normal mode */
33 oversampling.val2 = 0;
34
35 sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_FULL_SCALE,
36 &full_scale);
37 sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_OVERSAMPLING,
38 &oversampling);
39 /* Set sampling frequency last as this also sets the appropriate
40 * power mode. If already sampling, change to 0.0Hz before changing
41 * other attributes
42 */
43 sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ,
44 SENSOR_ATTR_SAMPLING_FREQUENCY,
45 &sampling_freq);
46
47
48 /* Setting scale in degrees/s to match the sensor scale */
49 full_scale.val1 = 500; /* dps */
50 full_scale.val2 = 0;
51 sampling_freq.val1 = 100; /* Hz. Performance mode */
52 sampling_freq.val2 = 0;
53 oversampling.val1 = 1; /* Normal mode */
54 oversampling.val2 = 0;
55
56 sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_FULL_SCALE,
57 &full_scale);
58 sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_OVERSAMPLING,
59 &oversampling);
60 /* Set sampling frequency last as this also sets the appropriate
61 * power mode. If already sampling, change sampling frequency to
62 * 0.0Hz before changing other attributes
63 */
64 sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ,
65 SENSOR_ATTR_SAMPLING_FREQUENCY,
66 &sampling_freq);
67
68 while (1) {
69 /* 10ms period, 100Hz Sampling frequency */
70 k_sleep(K_MSEC(10));
71
72 sensor_sample_fetch(dev);
73
74 sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ, acc);
75 sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ, gyr);
76
77 printf("AX: %d.%06d; AY: %d.%06d; AZ: %d.%06d; "
78 "GX: %d.%06d; GY: %d.%06d; GZ: %d.%06d;\n",
79 acc[0].val1, acc[0].val2,
80 acc[1].val1, acc[1].val2,
81 acc[2].val1, acc[2].val2,
82 gyr[0].val1, gyr[0].val2,
83 gyr[1].val1, gyr[1].val2,
84 gyr[2].val1, gyr[2].val2);
85 }
Keith Packard0b90fd52023-02-07 21:00:59 -080086 return 0;
Bosch Sensorteca56bd002021-01-25 13:03:12 +010087}