|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Helbling Technik AG |
| 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 | +#include <zephyr/sys/util.h> |
| 12 | + |
| 13 | +static int print_samples; |
| 14 | +static int lsm6dsr_trig_cnt; |
| 15 | + |
| 16 | +static struct sensor_value accel_x_out, accel_y_out, accel_z_out; |
| 17 | +static struct sensor_value gyro_x_out, gyro_y_out, gyro_z_out; |
| 18 | + |
| 19 | +#ifdef CONFIG_LSM6DSR_TRIGGER |
| 20 | +static void lsm6dsr_trigger_handler(const struct device *dev, const struct sensor_trigger *trig) |
| 21 | +{ |
| 22 | + static struct sensor_value accel_x, accel_y, accel_z; |
| 23 | + static struct sensor_value gyro_x, gyro_y, gyro_z; |
| 24 | + |
| 25 | + lsm6dsr_trig_cnt++; |
| 26 | + |
| 27 | + sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ); |
| 28 | + sensor_channel_get(dev, SENSOR_CHAN_ACCEL_X, &accel_x); |
| 29 | + sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Y, &accel_y); |
| 30 | + sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Z, &accel_z); |
| 31 | + |
| 32 | + /* lsm6dsr gyro */ |
| 33 | + sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ); |
| 34 | + sensor_channel_get(dev, SENSOR_CHAN_GYRO_X, &gyro_x); |
| 35 | + sensor_channel_get(dev, SENSOR_CHAN_GYRO_Y, &gyro_y); |
| 36 | + sensor_channel_get(dev, SENSOR_CHAN_GYRO_Z, &gyro_z); |
| 37 | + |
| 38 | + if (print_samples) { |
| 39 | + print_samples = 0; |
| 40 | + |
| 41 | + accel_x_out = accel_x; |
| 42 | + accel_y_out = accel_y; |
| 43 | + accel_z_out = accel_z; |
| 44 | + |
| 45 | + gyro_x_out = gyro_x; |
| 46 | + gyro_y_out = gyro_y; |
| 47 | + gyro_z_out = gyro_z; |
| 48 | + } |
| 49 | +} |
| 50 | +#endif |
| 51 | + |
| 52 | +int main(void) |
| 53 | +{ |
| 54 | + int cnt = 0; |
| 55 | + char out_str[64]; |
| 56 | + struct sensor_value odr_attr; |
| 57 | + const struct device *const lsm6dsr_dev = DEVICE_DT_GET_ONE(st_lsm6dsr); |
| 58 | + |
| 59 | + if (!device_is_ready(lsm6dsr_dev)) { |
| 60 | + printk("sensor: device not ready.\n"); |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + /* set accel/gyro sampling frequency to 104 Hz */ |
| 65 | + odr_attr.val1 = 104; |
| 66 | + odr_attr.val2 = 0; |
| 67 | + |
| 68 | + if (sensor_attr_set(lsm6dsr_dev, SENSOR_CHAN_ACCEL_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, |
| 69 | + &odr_attr) < 0) { |
| 70 | + printk("Cannot set sampling frequency for accelerometer.\n"); |
| 71 | + return 0; |
| 72 | + } |
| 73 | + |
| 74 | + if (sensor_attr_set(lsm6dsr_dev, SENSOR_CHAN_GYRO_XYZ, SENSOR_ATTR_SAMPLING_FREQUENCY, |
| 75 | + &odr_attr) < 0) { |
| 76 | + printk("Cannot set sampling frequency for gyro.\n"); |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | +#ifdef CONFIG_LSM6DSR_TRIGGER |
| 81 | + struct sensor_trigger trig; |
| 82 | + |
| 83 | + trig.type = SENSOR_TRIG_DATA_READY; |
| 84 | + trig.chan = SENSOR_CHAN_ACCEL_XYZ; |
| 85 | + |
| 86 | + if (sensor_trigger_set(lsm6dsr_dev, &trig, lsm6dsr_trigger_handler) != 0) { |
| 87 | + printk("Could not set sensor type and channel\n"); |
| 88 | + return 0; |
| 89 | + } |
| 90 | +#endif |
| 91 | + |
| 92 | + if (sensor_sample_fetch(lsm6dsr_dev) < 0) { |
| 93 | + printk("Sensor sample update error\n"); |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + while (1) { |
| 98 | + /* Erase previous */ |
| 99 | + printk("\0033\014"); |
| 100 | + printf("LSM6DSR sensor samples:\n\n"); |
| 101 | + |
| 102 | + /* lsm6dsr accel */ |
| 103 | + sprintf(out_str, "accel x:%f ms/2 y:%f ms/2 z:%f ms/2", |
| 104 | + sensor_value_to_double(&accel_x_out), sensor_value_to_double(&accel_y_out), |
| 105 | + sensor_value_to_double(&accel_z_out)); |
| 106 | + printk("%s\n", out_str); |
| 107 | + |
| 108 | + /* lsm6dsr gyro */ |
| 109 | + sprintf(out_str, "gyro x:%f dps y:%f dps z:%f dps", |
| 110 | + sensor_value_to_double(&gyro_x_out), sensor_value_to_double(&gyro_y_out), |
| 111 | + sensor_value_to_double(&gyro_z_out)); |
| 112 | + printk("%s\n", out_str); |
| 113 | + |
| 114 | + printk("loop:%d trig_cnt:%d\n\n", ++cnt, lsm6dsr_trig_cnt); |
| 115 | + |
| 116 | + print_samples = 1; |
| 117 | + k_sleep(K_MSEC(1000)); |
| 118 | + } |
| 119 | +} |
0 commit comments