1 /*
2  * Copyright (c) 2024 STMicroelectronics
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/drivers/led.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/drivers/spi.h>
14 #include <zephyr/drivers/sensor.h>
15 
16 #include <stdio.h>
17 
18 #ifdef CONFIG_STTS22H_TRIGGER
19 static int stts22h_trig_cnt;
20 
stts22h_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)21 static void stts22h_trigger_handler(const struct device *dev,
22 				    const struct sensor_trigger *trig)
23 {
24 	stts22h_trig_cnt++;
25 }
26 #endif
27 
28 #ifdef CONFIG_IIS2DLPC_TRIGGER
29 static int iis2dlpc_trig_cnt;
30 
iis2dlpc_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)31 static void iis2dlpc_trigger_handler(const struct device *dev,
32 				     const struct sensor_trigger *trig)
33 {
34 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
35 	iis2dlpc_trig_cnt++;
36 }
37 #endif
38 
39 #ifdef CONFIG_IIS2MDC_TRIGGER
40 static int iis2mdc_trig_cnt;
41 
iis2mdc_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)42 static void iis2mdc_trigger_handler(const struct device *dev,
43 				    const struct sensor_trigger *trig)
44 {
45 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ALL);
46 	iis2mdc_trig_cnt++;
47 }
48 #endif
49 
50 #ifdef CONFIG_ISM330DHCX_TRIGGER
51 static int ism330dhcx_acc_trig_cnt;
52 static int ism330dhcx_gyr_trig_cnt;
53 
ism330dhcx_acc_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)54 static void ism330dhcx_acc_trigger_handler(const struct device *dev,
55 					   const struct sensor_trigger *trig)
56 {
57 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
58 	ism330dhcx_acc_trig_cnt++;
59 }
60 
ism330dhcx_gyr_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)61 static void ism330dhcx_gyr_trigger_handler(const struct device *dev,
62 					   const struct sensor_trigger *trig)
63 {
64 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
65 	ism330dhcx_gyr_trig_cnt++;
66 }
67 #endif
68 
69 #ifdef CONFIG_IIS2ICLX_TRIGGER
70 static int iis2iclx_trig_cnt;
71 
iis2iclx_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)72 static void iis2iclx_trigger_handler(const struct device *dev,
73 				     const struct sensor_trigger *trig)
74 {
75 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
76 	iis2iclx_trig_cnt++;
77 }
78 #endif
79 
stts22h_config(const struct device * stts22h)80 static void stts22h_config(const struct device *stts22h)
81 {
82 	struct sensor_value odr_attr;
83 
84 	/* set STTS22H sampling frequency to 100 Hz */
85 	odr_attr.val1 = 100;
86 	odr_attr.val2 = 0;
87 
88 	if (sensor_attr_set(stts22h, SENSOR_CHAN_AMBIENT_TEMP,
89 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
90 		printk("Cannot set sampling frequency for STTS22H\n");
91 		return;
92 	}
93 
94 #ifdef CONFIG_STTS22H_TRIGGER
95 	struct sensor_trigger trig;
96 
97 	trig.type = SENSOR_TRIG_DATA_READY;
98 	trig.chan = SENSOR_CHAN_AMBIENT_TEMP;
99 	sensor_trigger_set(stts22h, &trig, stts22h_trigger_handler);
100 #endif
101 }
102 
iis2dlpc_config(const struct device * iis2dlpc)103 static void iis2dlpc_config(const struct device *iis2dlpc)
104 {
105 	struct sensor_value odr_attr, fs_attr;
106 
107 	/* set IIS2DLPC accel/gyro sampling frequency to 100 Hz */
108 	odr_attr.val1 = 200;
109 	odr_attr.val2 = 0;
110 
111 	if (sensor_attr_set(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ,
112 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
113 		printk("Cannot set sampling frequency for IIS2DLPC accel\n");
114 		return;
115 	}
116 
117 	sensor_g_to_ms2(16, &fs_attr);
118 
119 	if (sensor_attr_set(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ,
120 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
121 		printk("Cannot set full scale for IIS2DLPC accel\n");
122 		return;
123 	}
124 
125 #ifdef CONFIG_IIS2DLPC_TRIGGER
126 	struct sensor_trigger trig;
127 
128 	trig.type = SENSOR_TRIG_DATA_READY;
129 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
130 	sensor_trigger_set(iis2dlpc, &trig, iis2dlpc_trigger_handler);
131 #endif
132 }
133 
iis2iclx_config(const struct device * iis2iclx)134 static void iis2iclx_config(const struct device *iis2iclx)
135 {
136 	struct sensor_value odr_attr, fs_attr;
137 
138 	/* set IIS2ICLX accel/gyro sampling frequency to 200 Hz */
139 	odr_attr.val1 = 200;
140 	odr_attr.val2 = 0;
141 
142 	if (sensor_attr_set(iis2iclx, SENSOR_CHAN_ACCEL_XYZ,
143 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
144 		printk("Cannot set sampling frequency for IIS2ICLX accel\n");
145 		return;
146 	}
147 
148 	sensor_g_to_ms2(2, &fs_attr);
149 
150 	if (sensor_attr_set(iis2iclx, SENSOR_CHAN_ACCEL_XYZ,
151 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
152 		printk("Cannot set full scale for IIS2ICLX accel\n");
153 		return;
154 	}
155 
156 #ifdef CONFIG_IIS2ICLX_TRIGGER
157 	struct sensor_trigger trig;
158 
159 	trig.type = SENSOR_TRIG_DATA_READY;
160 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
161 	sensor_trigger_set(iis2iclx, &trig, iis2iclx_trigger_handler);
162 #endif
163 }
164 
iis2mdc_config(const struct device * iis2mdc)165 static void iis2mdc_config(const struct device *iis2mdc)
166 {
167 	struct sensor_value odr_attr;
168 
169 	/* set IIS2MDC sampling frequency to 100 Hz */
170 	odr_attr.val1 = 100;
171 	odr_attr.val2 = 0;
172 
173 	if (sensor_attr_set(iis2mdc, SENSOR_CHAN_ALL,
174 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
175 		printk("Cannot set sampling frequency for IIS2MDC\n");
176 		return;
177 	}
178 
179 #ifdef CONFIG_IIS2MDC_TRIGGER
180 	struct sensor_trigger trig;
181 
182 	trig.type = SENSOR_TRIG_DATA_READY;
183 	trig.chan = SENSOR_CHAN_MAGN_XYZ;
184 	sensor_trigger_set(iis2mdc, &trig, iis2mdc_trigger_handler);
185 #endif
186 }
187 
ism330dhcx_config(const struct device * ism330dhcx)188 static void ism330dhcx_config(const struct device *ism330dhcx)
189 {
190 	struct sensor_value odr_attr, fs_attr;
191 
192 	/* set ISM330DHCX sampling frequency to 416 Hz */
193 	odr_attr.val1 = 416;
194 	odr_attr.val2 = 0;
195 
196 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_ACCEL_XYZ,
197 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
198 		printk("Cannot set sampling frequency for ISM330DHCX accel\n");
199 		return;
200 	}
201 
202 	sensor_g_to_ms2(16, &fs_attr);
203 
204 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_ACCEL_XYZ,
205 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
206 		printk("Cannot set sampling frequency for ISM330DHCX accel\n");
207 		return;
208 	}
209 
210 	/* set ISM330DHCX gyro sampling frequency to 208 Hz */
211 	odr_attr.val1 = 208;
212 	odr_attr.val2 = 0;
213 
214 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_GYRO_XYZ,
215 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
216 		printk("Cannot set sampling frequency for ISM330DHCX gyro\n");
217 		return;
218 	}
219 
220 	sensor_degrees_to_rad(250, &fs_attr);
221 
222 	if (sensor_attr_set(ism330dhcx, SENSOR_CHAN_GYRO_XYZ,
223 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
224 		printk("Cannot set fs for ISM330DHCX gyro\n");
225 		return;
226 	}
227 
228 #ifdef CONFIG_ISM330DHCX_TRIGGER
229 	struct sensor_trigger trig;
230 
231 	trig.type = SENSOR_TRIG_DATA_READY;
232 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
233 	sensor_trigger_set(ism330dhcx, &trig, ism330dhcx_acc_trigger_handler);
234 
235 	trig.type = SENSOR_TRIG_DATA_READY;
236 	trig.chan = SENSOR_CHAN_GYRO_XYZ;
237 	sensor_trigger_set(ism330dhcx, &trig, ism330dhcx_gyr_trigger_handler);
238 #endif
239 }
240 
ilps22qs_config(const struct device * ilps22qs)241 static void ilps22qs_config(const struct device *ilps22qs)
242 {
243 	struct sensor_value odr_attr;
244 
245 	/* set ILPS22QS sampling frequency to 50 Hz */
246 	odr_attr.val1 = 50;
247 	odr_attr.val2 = 0;
248 
249 	if (sensor_attr_set(ilps22qs, SENSOR_CHAN_ALL,
250 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
251 		printk("Cannot set sampling frequency for ILPS22QS\n");
252 		return;
253 	}
254 }
255 
led_pattern_out(void)256 static int led_pattern_out(void)
257 {
258 	const struct gpio_dt_spec led0_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
259 	const struct gpio_dt_spec led1_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
260 	int i;
261 
262 	/* led 0 */
263 	if (!gpio_is_ready_dt(&led0_gpio)) {
264 		printk("%s: device not ready.\n", led0_gpio.port->name);
265 		return -1;
266 	}
267 	gpio_pin_configure_dt(&led0_gpio, GPIO_OUTPUT_INACTIVE);
268 
269 	/* led 1 */
270 	if (!gpio_is_ready_dt(&led1_gpio)) {
271 		printk("%s: device not ready.\n", led1_gpio.port->name);
272 		return -1;
273 	}
274 	gpio_pin_configure_dt(&led1_gpio, GPIO_OUTPUT_INACTIVE);
275 
276 	/* output led alternate pattern */
277 	for (i = 0; i < 8; i++) {
278 		gpio_pin_set_dt(&led0_gpio, ((i % 2) == 0) ? 1 : 0);
279 		gpio_pin_set_dt(&led1_gpio, ((i % 2) == 1) ? 1 : 0);
280 		k_msleep(100);
281 	}
282 
283 	/* output led in sync pattern */
284 	for (i = 0; i < 6; i++) {
285 		gpio_pin_set_dt(&led0_gpio, ((i % 2) == 0) ? 1 : 0);
286 		gpio_pin_set_dt(&led1_gpio, ((i % 2) == 0) ? 1 : 0);
287 		k_msleep(250);
288 	}
289 
290 	/* turn all leds off */
291 	gpio_pin_set_dt(&led0_gpio, 0);
292 	gpio_pin_set_dt(&led1_gpio, 0);
293 
294 	return 0;
295 }
296 
main(void)297 int main(void)
298 {
299 	int cnt = 1;
300 
301 	/* signal that sample is started */
302 	if (led_pattern_out() < 0) {
303 		return -1;
304 	}
305 
306 	printk("STWIN.box board sensor test\n");
307 
308 	const struct device *const stts22h = DEVICE_DT_GET_ONE(st_stts22h);
309 	const struct device *const iis2mdc = DEVICE_DT_GET_ONE(st_iis2mdc);
310 	const struct device *const ism330dhcx = DEVICE_DT_GET_ONE(st_ism330dhcx);
311 	const struct device *const iis2dlpc = DEVICE_DT_GET_ONE(st_iis2dlpc);
312 	const struct device *const iis2iclx = DEVICE_DT_GET_ONE(st_iis2iclx);
313 	const struct device *const ilps22qs = DEVICE_DT_GET_ONE(st_ilps22qs);
314 
315 	if (!device_is_ready(stts22h)) {
316 		printk("%s: device not ready.\n", stts22h->name);
317 		return 0;
318 	}
319 	if (!device_is_ready(iis2mdc)) {
320 		printk("%s: device not ready.\n", iis2mdc->name);
321 		return 0;
322 	}
323 	if (!device_is_ready(ism330dhcx)) {
324 		printk("%s: device not ready.\n", ism330dhcx->name);
325 		return 0;
326 	}
327 	if (!device_is_ready(iis2dlpc)) {
328 		printk("%s: device not ready.\n", iis2dlpc->name);
329 		return 0;
330 	}
331 	if (!device_is_ready(iis2iclx)) {
332 		printk("%s: device not ready.\n", iis2iclx->name);
333 		return 0;
334 	}
335 	if (!device_is_ready(ilps22qs)) {
336 		printk("%s: device not ready.\n", ilps22qs->name);
337 		return 0;
338 	}
339 
340 	stts22h_config(stts22h);
341 	iis2mdc_config(iis2mdc);
342 	ism330dhcx_config(ism330dhcx);
343 	iis2dlpc_config(iis2dlpc);
344 	iis2iclx_config(iis2iclx);
345 	ilps22qs_config(ilps22qs);
346 
347 	while (1) {
348 		struct sensor_value stts22h_temp;
349 		struct sensor_value iis2dlpc_accel[3];
350 		struct sensor_value iis2mdc_magn[3];
351 		struct sensor_value iis2mdc_temp;
352 		struct sensor_value ism330dhcx_accel[3];
353 		struct sensor_value ism330dhcx_gyro[3];
354 		struct sensor_value iis2iclx_accel[2];
355 		struct sensor_value ilps22qs_press, ilps22qs_temp;
356 
357 #ifndef CONFIG_STTS22H_TRIGGER
358 		if (sensor_sample_fetch(stts22h) < 0) {
359 			printf("STTS22H Sensor sample update error\n");
360 			return 0;
361 		}
362 #endif
363 #ifndef CONFIG_IIS2MDC_TRIGGER
364 		if (sensor_sample_fetch(iis2mdc) < 0) {
365 			printf("IIS2MDC Magn Sensor sample update error\n");
366 			return 0;
367 		}
368 #endif
369 #ifndef CONFIG_ISM330DHCX_TRIGGER
370 		if (sensor_sample_fetch(ism330dhcx) < 0) {
371 			printf("ISM330DHCX IMU Sensor sample update error\n");
372 			return 0;
373 		}
374 #endif
375 #ifndef CONFIG_IIS2DLPC_TRIGGER
376 		if (sensor_sample_fetch(iis2dlpc) < 0) {
377 			printf("IIS2DLPC Sensor sample update error\n");
378 			return 0;
379 		}
380 #endif
381 #ifndef CONFIG_IIS2ICLX_TRIGGER
382 		if (sensor_sample_fetch(iis2iclx) < 0) {
383 			printf("IIS2ICLX Sensor sample update error\n");
384 			return 0;
385 		}
386 #endif
387 
388 		if (sensor_sample_fetch(ilps22qs) < 0) {
389 			printf("ILPS22QS Sensor sample update error\n");
390 			return 0;
391 		}
392 
393 		sensor_channel_get(stts22h, SENSOR_CHAN_AMBIENT_TEMP, &stts22h_temp);
394 		sensor_channel_get(iis2dlpc, SENSOR_CHAN_ACCEL_XYZ, iis2dlpc_accel);
395 		sensor_channel_get(iis2mdc, SENSOR_CHAN_MAGN_XYZ, iis2mdc_magn);
396 		sensor_channel_get(iis2mdc, SENSOR_CHAN_DIE_TEMP, &iis2mdc_temp);
397 		sensor_channel_get(ism330dhcx, SENSOR_CHAN_ACCEL_XYZ, ism330dhcx_accel);
398 		sensor_channel_get(ism330dhcx, SENSOR_CHAN_GYRO_XYZ, ism330dhcx_gyro);
399 		sensor_channel_get(iis2iclx, SENSOR_CHAN_ACCEL_XYZ, iis2iclx_accel);
400 		sensor_channel_get(ilps22qs, SENSOR_CHAN_AMBIENT_TEMP, &ilps22qs_temp);
401 		sensor_channel_get(ilps22qs, SENSOR_CHAN_PRESS, &ilps22qs_press);
402 
403 		/* Display sensor data */
404 
405 		/*  Clear terminal (ANSI ESC-C) */
406 		printf("\0033\014");
407 
408 		printf("STWIN.box dashboard\n\n");
409 
410 		/* STTS22H temperature */
411 		printf("STTS22H: Temperature: %.1f C\n",
412 		       sensor_value_to_double(&stts22h_temp));
413 
414 		/* iis2dlpc */
415 		printf("IIS2DLPC: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
416 			sensor_value_to_double(&iis2dlpc_accel[0]),
417 			sensor_value_to_double(&iis2dlpc_accel[1]),
418 			sensor_value_to_double(&iis2dlpc_accel[2]));
419 
420 		/* iis2mdc */
421 		printf("IIS2MDC: Magn (gauss): x: %.3f, y: %.3f, z: %.3f\n",
422 		       sensor_value_to_double(&iis2mdc_magn[0]),
423 		       sensor_value_to_double(&iis2mdc_magn[1]),
424 		       sensor_value_to_double(&iis2mdc_magn[2]));
425 
426 		printf("IIS2MDC: Temperature: %.1f C\n",
427 		       sensor_value_to_double(&iis2mdc_temp));
428 
429 		/* ism330dhcx */
430 		printf("ISM330DHCX: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
431 			sensor_value_to_double(&ism330dhcx_accel[0]),
432 			sensor_value_to_double(&ism330dhcx_accel[1]),
433 			sensor_value_to_double(&ism330dhcx_accel[2]));
434 
435 		printf("ISM330DHCX: Gyro (dps): x: %.3f, y: %.3f, z: %.3f\n",
436 			sensor_value_to_double(&ism330dhcx_gyro[0]),
437 			sensor_value_to_double(&ism330dhcx_gyro[1]),
438 			sensor_value_to_double(&ism330dhcx_gyro[2]));
439 
440 		/* iis2iclx */
441 		printf("IIS2ICLX: Accel (m.s-2): x: %.3f, y: %.3f\n",
442 			sensor_value_to_double(&iis2iclx_accel[0]),
443 			sensor_value_to_double(&iis2iclx_accel[1]));
444 
445 		/* temperature */
446 		printf("ILPS22QS: Temperature: %.1f C\n",
447 		       sensor_value_to_double(&ilps22qs_temp));
448 
449 		/* pressure */
450 		printf("ILPS22QS: Pressure: %.3f kpa\n",
451 		       sensor_value_to_double(&ilps22qs_press));
452 
453 #ifdef CONFIG_STTS22H_TRIGGER
454 		printk("%d:: stts22h trig %d\n", cnt, stts22h_trig_cnt);
455 #endif
456 
457 #ifdef CONFIG_IIS2DLPC_TRIGGER
458 		printk("%d:: iis2dlpc trig %d\n", cnt, iis2dlpc_trig_cnt);
459 #endif
460 
461 #if defined(CONFIG_IIS2MDC_TRIGGER)
462 		printk("%d:: iis2mdc trig %d\n", cnt, iis2mdc_trig_cnt);
463 #endif
464 
465 #ifdef CONFIG_ISM330DHCX_TRIGGER
466 		printk("%d:: ism330dhcx acc trig %d\n", cnt, ism330dhcx_acc_trig_cnt);
467 		printk("%d:: ism330dhcx gyr trig %d\n", cnt, ism330dhcx_gyr_trig_cnt);
468 #endif
469 
470 #ifdef CONFIG_IIS2ICLX_TRIGGER
471 		printk("%d:: iis2iclx trig %d\n", cnt, iis2iclx_trig_cnt);
472 #endif
473 
474 		k_msleep(2000);
475 	}
476 }
477