1 /*
2  * Copyright (c) 2021 Bosch Sensortec GmbH
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 
main(void)12 int main(void)
13 {
14 	const struct device *const dev = DEVICE_DT_GET_ONE(bosch_bmi270);
15 	struct sensor_value acc[3], gyr[3];
16 	struct sensor_value full_scale, sampling_freq, oversampling;
17 
18 	if (!device_is_ready(dev)) {
19 		printf("Device %s is not ready\n", dev->name);
20 		return 0;
21 	}
22 
23 	printf("Device %p name is %s\n", dev, dev->name);
24 
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 	}
86 	return 0;
87 }
88