| /* |
| * Copyright (c) 2021 Bosch Sensortec GmbH |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| #include <zephyr/zephyr.h> |
| #include <zephyr/device.h> |
| #include <zephyr/drivers/sensor.h> |
| #include <stdio.h> |
| |
| void main(void) |
| { |
| const struct device *dev; |
| struct sensor_value acc[3], gyr[3]; |
| struct sensor_value full_scale, sampling_freq, oversampling; |
| |
| dev = device_get_binding(DT_LABEL(DT_INST(0, bosch_bmi270))); |
| if (dev == NULL) { |
| printf("Could not get %s device\n", |
| DT_LABEL(DT_INST(0, bosch_bmi270))); |
| return; |
| } |
| |
| printf("Device %p name is %s\n", dev, |
| DT_LABEL(DT_INST(0, bosch_bmi270))); |
| |
| /* Setting scale in G, due to loss of precision if the SI unit m/s^2 |
| * is used |
| */ |
| full_scale.val1 = 2; /* G */ |
| full_scale.val2 = 0; |
| sampling_freq.val1 = 100; /* Hz. Performance mode */ |
| sampling_freq.val2 = 0; |
| oversampling.val1 = 1; /* Normal mode */ |
| oversampling.val2 = 0; |
| |
| sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_FULL_SCALE, |
| &full_scale); |
| sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_OVERSAMPLING, |
| &oversampling); |
| /* Set sampling frequency last as this also sets the appropriate |
| * power mode. If already sampling, change to 0.0Hz before changing |
| * other attributes |
| */ |
| sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ, |
| SENSOR_ATTR_SAMPLING_FREQUENCY, |
| &sampling_freq); |
| |
| |
| /* Setting scale in degrees/s to match the sensor scale */ |
| full_scale.val1 = 500; /* dps */ |
| full_scale.val2 = 0; |
| sampling_freq.val1 = 100; /* Hz. Performance mode */ |
| sampling_freq.val2 = 0; |
| oversampling.val1 = 1; /* Normal mode */ |
| oversampling.val2 = 0; |
| |
| sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_FULL_SCALE, |
| &full_scale); |
| sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_OVERSAMPLING, |
| &oversampling); |
| /* Set sampling frequency last as this also sets the appropriate |
| * power mode. If already sampling, change sampling frequency to |
| * 0.0Hz before changing other attributes |
| */ |
| sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ, |
| SENSOR_ATTR_SAMPLING_FREQUENCY, |
| &sampling_freq); |
| |
| while (1) { |
| /* 10ms period, 100Hz Sampling frequency */ |
| k_sleep(K_MSEC(10)); |
| |
| sensor_sample_fetch(dev); |
| |
| sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ, acc); |
| sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ, gyr); |
| |
| printf("AX: %d.%06d; AY: %d.%06d; AZ: %d.%06d; " |
| "GX: %d.%06d; GY: %d.%06d; GZ: %d.%06d;\n", |
| acc[0].val1, acc[0].val2, |
| acc[1].val1, acc[1].val2, |
| acc[2].val1, acc[2].val2, |
| gyr[0].val1, gyr[0].val2, |
| gyr[1].val1, gyr[1].val2, |
| gyr[2].val1, gyr[2].val2); |
| } |
| } |