1 /*
2  * Copyright (c) 2018 STMicroelectronics
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 #ifdef CONFIG_LSM6DSL_TRIGGER
14 static int lsm6dsl_trig_cnt;
15 
lsm6dsl_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)16 static void lsm6dsl_trigger_handler(const struct device *dev,
17 				    const struct sensor_trigger *trig)
18 {
19 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ALL);
20 	lsm6dsl_trig_cnt++;
21 }
22 #endif
23 
main(void)24 int main(void)
25 {
26 	struct sensor_value temp1, temp2, hum, press;
27 	struct sensor_value accel1[3], accel2[3];
28 	struct sensor_value gyro[3];
29 	struct sensor_value magn[3];
30 	const struct device *const hts221 = DEVICE_DT_GET_ONE(st_hts221);
31 	const struct device *const lps22hb = DEVICE_DT_GET_ONE(st_lps22hb_press);
32 	const struct device *const lsm6dsl = DEVICE_DT_GET_ONE(st_lsm6dsl);
33 	const struct device *const lsm303agr_a = DEVICE_DT_GET_ONE(st_lis2dh);
34 	const struct device *const lsm303agr_m = DEVICE_DT_GET_ONE(st_lis2mdl);
35 #ifdef CONFIG_LSM6DSL_TRIGGER
36 	int cnt = 1;
37 #endif
38 
39 	if (!device_is_ready(hts221)) {
40 		printk("%s: device not ready.\n", hts221->name);
41 		return 0;
42 	}
43 	if (!device_is_ready(lps22hb)) {
44 		printk("%s: device not ready.\n", lps22hb->name);
45 		return 0;
46 	}
47 	if (!device_is_ready(lsm6dsl)) {
48 		printk("%s: device not ready.\n", lsm6dsl->name);
49 		return 0;
50 	}
51 	if (!device_is_ready(lsm303agr_a)) {
52 		printk("%s: device not ready.\n", lsm303agr_a->name);
53 		return 0;
54 	}
55 	if (!device_is_ready(lsm303agr_m)) {
56 		printk("%s: device not ready.\n", lsm303agr_m->name);
57 		return 0;
58 	}
59 
60 	/* set LSM6DSL accel/gyro sampling frequency to 104 Hz */
61 	struct sensor_value odr_attr;
62 
63 	odr_attr.val1 = 208;
64 	odr_attr.val2 = 0;
65 
66 	if (sensor_attr_set(lsm6dsl, SENSOR_CHAN_ACCEL_XYZ,
67 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
68 		printk("Cannot set sampling frequency for accelerometer.\n");
69 		return 0;
70 	}
71 
72 	if (sensor_attr_set(lsm6dsl, SENSOR_CHAN_GYRO_XYZ,
73 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
74 		printk("Cannot set sampling frequency for gyro.\n");
75 		return 0;
76 	}
77 
78 #ifdef CONFIG_LSM6DSL_TRIGGER
79 	struct sensor_trigger trig;
80 
81 	trig.type = SENSOR_TRIG_DATA_READY;
82 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
83 	sensor_trigger_set(lsm6dsl, &trig, lsm6dsl_trigger_handler);
84 #endif
85 
86 	while (1) {
87 		int ret;
88 
89 		/* Get sensor samples */
90 
91 		if (sensor_sample_fetch(hts221) < 0) {
92 			printf("HTS221 Sensor sample update error\n");
93 			return 0;
94 		}
95 		if (sensor_sample_fetch(lps22hb) < 0) {
96 			printf("LPS22HB Sensor sample update error\n");
97 			return 0;
98 		}
99 #ifndef CONFIG_LSM6DSL_TRIGGER
100 		if (sensor_sample_fetch(lsm6dsl) < 0) {
101 			printf("LSM6DSL Sensor sample update error\n");
102 			return 0;
103 		}
104 #endif
105 		ret = sensor_sample_fetch(lsm303agr_a);
106 		if (ret < 0 && ret != -EBADMSG) {
107 			printf("LSM303AGR Accel Sensor sample update error\n");
108 			return 0;
109 		}
110 		if (sensor_sample_fetch(lsm303agr_m) < 0) {
111 			printf("LSM303AGR Magn Sensor sample update error\n");
112 			return 0;
113 		}
114 
115 		/* Get sensor data */
116 
117 		sensor_channel_get(hts221, SENSOR_CHAN_AMBIENT_TEMP, &temp1);
118 		sensor_channel_get(hts221, SENSOR_CHAN_HUMIDITY, &hum);
119 		sensor_channel_get(lps22hb, SENSOR_CHAN_PRESS, &press);
120 		sensor_channel_get(lps22hb, SENSOR_CHAN_AMBIENT_TEMP, &temp2);
121 		sensor_channel_get(lsm6dsl, SENSOR_CHAN_ACCEL_XYZ, accel1);
122 		sensor_channel_get(lsm6dsl, SENSOR_CHAN_GYRO_XYZ, gyro);
123 		sensor_channel_get(lsm303agr_a, SENSOR_CHAN_ACCEL_XYZ, accel2);
124 		sensor_channel_get(lsm303agr_m, SENSOR_CHAN_MAGN_XYZ, magn);
125 
126 		/* Display sensor data */
127 
128 		/* Erase previous */
129 		printf("\0033\014");
130 
131 		printf("X-NUCLEO-IKS01A2 sensor dashboard\n\n");
132 
133 		/* temperature */
134 		printf("HTS221: Temperature: %.1f C\n",
135 		       sensor_value_to_double(&temp1));
136 
137 		/* humidity */
138 		printf("HTS221: Relative Humidity: %.1f%%\n",
139 		       sensor_value_to_double(&hum));
140 
141 		/* pressure */
142 		printf("LPS22HB: Pressure:%.3f kpa\n",
143 		       sensor_value_to_double(&press));
144 
145 		/* lps22hb temperature */
146 		printf("LPS22HB: Temperature: %.1f C\n",
147 		       sensor_value_to_double(&temp2));
148 
149 		/* lsm6dsl accel */
150 		printf("LSM6DSL: Accel (m.s-2): x: %.1f, y: %.1f, z: %.1f\n",
151 		       sensor_value_to_double(&accel1[0]),
152 		       sensor_value_to_double(&accel1[1]),
153 		       sensor_value_to_double(&accel1[2]));
154 
155 		/* lsm6dsl gyro */
156 		printf("LSM6DSL: Gyro (dps): x: %.3f, y: %.3f, z: %.3f\n",
157 		       sensor_value_to_double(&gyro[0]),
158 		       sensor_value_to_double(&gyro[1]),
159 		       sensor_value_to_double(&gyro[2]));
160 
161 #if defined(CONFIG_LSM6DSL_TRIGGER)
162 		printf("%d:: lsm6dsl trig %d\n", cnt++, lsm6dsl_trig_cnt);
163 #endif
164 
165 		/* lsm303agr accel */
166 		printf("LSM303AGR: Accel (m.s-2): x: %.1f, y: %.1f, z: %.1f\n",
167 		       sensor_value_to_double(&accel2[0]),
168 		       sensor_value_to_double(&accel2[1]),
169 		       sensor_value_to_double(&accel2[2]));
170 
171 		/* lsm303agr magn */
172 		printf("LSM303AGR: Magn (gauss): x: %.3f, y: %.3f, z: %.3f\n",
173 		       sensor_value_to_double(&magn[0]),
174 		       sensor_value_to_double(&magn[1]),
175 		       sensor_value_to_double(&magn[2]));
176 
177 		k_sleep(K_MSEC(2000));
178 	}
179 }
180