1 /*
2  * Copyright (c) 2018 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 #include <zephyr/usb/usb_device.h>
16 #include <zephyr/drivers/uart.h>
17 
18 #include <stdio.h>
19 
20 #define WHOAMI_REG      0x0F
21 #define WHOAMI_ALT_REG  0x4F
22 
23 #ifdef CONFIG_LPS22HH_TRIGGER
24 static int lps22hh_trig_cnt;
25 
lps22hh_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)26 static void lps22hh_trigger_handler(const struct device *dev,
27 				    const struct sensor_trigger *trig)
28 {
29 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_PRESS);
30 	lps22hh_trig_cnt++;
31 }
32 #endif
33 
34 #ifdef CONFIG_LIS2DW12_TRIGGER
35 static int lis2dw12_trig_cnt;
36 
lis2dw12_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)37 static void lis2dw12_trigger_handler(const struct device *dev,
38 				     const struct sensor_trigger *trig)
39 {
40 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
41 	lis2dw12_trig_cnt++;
42 }
43 #endif
44 
45 #ifdef CONFIG_LSM6DSO_TRIGGER
46 static int lsm6dso_acc_trig_cnt;
47 static int lsm6dso_gyr_trig_cnt;
48 static int lsm6dso_temp_trig_cnt;
49 
lsm6dso_acc_trig_handler(const struct device * dev,const struct sensor_trigger * trig)50 static void lsm6dso_acc_trig_handler(const struct device *dev,
51 				     const struct sensor_trigger *trig)
52 {
53 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
54 	lsm6dso_acc_trig_cnt++;
55 }
56 
lsm6dso_gyr_trig_handler(const struct device * dev,const struct sensor_trigger * trig)57 static void lsm6dso_gyr_trig_handler(const struct device *dev,
58 				     const struct sensor_trigger *trig)
59 {
60 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
61 	lsm6dso_gyr_trig_cnt++;
62 }
63 
lsm6dso_temp_trig_handler(const struct device * dev,const struct sensor_trigger * trig)64 static void lsm6dso_temp_trig_handler(const struct device *dev,
65 				      const struct sensor_trigger *trig)
66 {
67 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_DIE_TEMP);
68 	lsm6dso_temp_trig_cnt++;
69 }
70 #endif
71 
72 #ifdef CONFIG_STTS751_TRIGGER
73 static int stts751_trig_cnt;
74 
stts751_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)75 static void stts751_trigger_handler(const struct device *dev,
76 				    const struct sensor_trigger *trig)
77 {
78 	stts751_trig_cnt++;
79 }
80 #endif
81 
82 #ifdef CONFIG_IIS3DHHC_TRIGGER
83 static int iis3dhhc_trig_cnt;
84 
iis3dhhc_trigger_handler(const struct device * dev,const struct sensor_trigger * trig)85 static void iis3dhhc_trigger_handler(const struct device *dev,
86 				     const struct sensor_trigger *trig)
87 {
88 	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
89 	iis3dhhc_trig_cnt++;
90 }
91 #endif
92 
lps22hh_config(const struct device * lps22hh)93 static void lps22hh_config(const struct device *lps22hh)
94 {
95 	struct sensor_value odr_attr;
96 
97 	/* set LPS22HH sampling frequency to 50 Hz */
98 	odr_attr.val1 = 50;
99 	odr_attr.val2 = 0;
100 
101 	if (sensor_attr_set(lps22hh, SENSOR_CHAN_ALL,
102 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
103 		printk("Cannot set sampling frequency for LPS22HH\n");
104 		return;
105 	}
106 
107 #ifdef CONFIG_LPS22HH_TRIGGER
108 	struct sensor_trigger trig;
109 
110 	trig.type = SENSOR_TRIG_DATA_READY;
111 	trig.chan = SENSOR_CHAN_ALL;
112 	sensor_trigger_set(lps22hh, &trig, lps22hh_trigger_handler);
113 #endif
114 }
115 
lis2dw12_config(const struct device * lis2dw12)116 static void lis2dw12_config(const struct device *lis2dw12)
117 {
118 	struct sensor_value odr_attr, fs_attr;
119 
120 	/* set LIS2DW12 accel/gyro sampling frequency to 100 Hz */
121 	odr_attr.val1 = 100;
122 	odr_attr.val2 = 0;
123 
124 	if (sensor_attr_set(lis2dw12, SENSOR_CHAN_ACCEL_XYZ,
125 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
126 		printk("Cannot set sampling frequency for LIS2DW12 accel\n");
127 		return;
128 	}
129 
130 	sensor_g_to_ms2(16, &fs_attr);
131 
132 	if (sensor_attr_set(lis2dw12, SENSOR_CHAN_ACCEL_XYZ,
133 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
134 		printk("Cannot set sampling frequency for LIS2DW12 gyro\n");
135 		return;
136 	}
137 
138 #ifdef CONFIG_LIS2DW12_TRIGGER
139 	struct sensor_trigger trig;
140 
141 	trig.type = SENSOR_TRIG_DATA_READY;
142 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
143 	sensor_trigger_set(lis2dw12, &trig, lis2dw12_trigger_handler);
144 #endif
145 }
146 
lsm6dso_config(const struct device * lsm6dso)147 static void lsm6dso_config(const struct device *lsm6dso)
148 {
149 	struct sensor_value odr_attr, fs_attr;
150 
151 	/* set LSM6DSO accel sampling frequency to 208 Hz */
152 	odr_attr.val1 = 208;
153 	odr_attr.val2 = 0;
154 
155 	if (sensor_attr_set(lsm6dso, SENSOR_CHAN_ACCEL_XYZ,
156 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
157 		printk("Cannot set sampling frequency for LSM6DSO accel\n");
158 		return;
159 	}
160 
161 	sensor_g_to_ms2(16, &fs_attr);
162 
163 	if (sensor_attr_set(lsm6dso, SENSOR_CHAN_ACCEL_XYZ,
164 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
165 		printk("Cannot set fs for LSM6DSO accel\n");
166 		return;
167 	}
168 
169 	/* set LSM6DSO gyro sampling frequency to 208 Hz */
170 	odr_attr.val1 = 208;
171 	odr_attr.val2 = 0;
172 
173 	if (sensor_attr_set(lsm6dso, SENSOR_CHAN_GYRO_XYZ,
174 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
175 		printk("Cannot set sampling frequency for LSM6DSO gyro\n");
176 		return;
177 	}
178 
179 	sensor_degrees_to_rad(250, &fs_attr);
180 
181 	if (sensor_attr_set(lsm6dso, SENSOR_CHAN_GYRO_XYZ,
182 			    SENSOR_ATTR_FULL_SCALE, &fs_attr) < 0) {
183 		printk("Cannot set fs for LSM6DSO gyro\n");
184 		return;
185 	}
186 
187 #ifdef CONFIG_LSM6DSO_TRIGGER
188 	struct sensor_trigger trig;
189 
190 	trig.type = SENSOR_TRIG_DATA_READY;
191 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
192 	sensor_trigger_set(lsm6dso, &trig, lsm6dso_acc_trig_handler);
193 
194 	trig.type = SENSOR_TRIG_DATA_READY;
195 	trig.chan = SENSOR_CHAN_GYRO_XYZ;
196 	sensor_trigger_set(lsm6dso, &trig, lsm6dso_gyr_trig_handler);
197 
198 	trig.type = SENSOR_TRIG_DATA_READY;
199 	trig.chan = SENSOR_CHAN_DIE_TEMP;
200 	sensor_trigger_set(lsm6dso, &trig, lsm6dso_temp_trig_handler);
201 #endif
202 }
203 
stts751_config(const struct device * stts751)204 static void stts751_config(const struct device *stts751)
205 {
206 	struct sensor_value odr_attr;
207 
208 	/* set STTS751 conversion rate to 16 Hz */
209 	odr_attr.val1 = 16;
210 	odr_attr.val2 = 0;
211 
212 	if (sensor_attr_set(stts751, SENSOR_CHAN_ALL,
213 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
214 		printk("Cannot set sampling frequency for STTS751\n");
215 		return;
216 	}
217 
218 #ifdef CONFIG_STTS751_TRIGGER
219 	struct sensor_trigger trig;
220 
221 	trig.type = SENSOR_TRIG_THRESHOLD;
222 	trig.chan = SENSOR_CHAN_ALL;
223 	sensor_trigger_set(stts751, &trig, stts751_trigger_handler);
224 #endif
225 }
226 
iis3dhhc_config(const struct device * iis3dhhc)227 static void iis3dhhc_config(const struct device *iis3dhhc)
228 {
229 	struct sensor_value odr_attr;
230 
231 	/* enable IIS3DHHC conversion */
232 	odr_attr.val1 = 1000; /* enable sensor at 1KHz */
233 	odr_attr.val2 = 0;
234 
235 	if (sensor_attr_set(iis3dhhc, SENSOR_CHAN_ALL,
236 			    SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr) < 0) {
237 		printk("Cannot set sampling frequency for IIS3DHHC\n");
238 		return;
239 	}
240 
241 #ifdef CONFIG_IIS3DHHC_TRIGGER
242 	struct sensor_trigger trig;
243 
244 	trig.type = SENSOR_TRIG_DATA_READY;
245 	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
246 	sensor_trigger_set(iis3dhhc, &trig, iis3dhhc_trigger_handler);
247 #endif
248 }
249 
main(void)250 int main(void)
251 {
252 	static const struct gpio_dt_spec led0_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
253 	static const struct gpio_dt_spec led1_gpio = GPIO_DT_SPEC_GET(DT_ALIAS(led1), gpios);
254 	const struct device *const dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
255 	int i, on = 1;
256 	int cnt = 1;
257 	uint32_t dtr = 0;
258 
259 	/* Application must enable USB by itself */
260 	if (!device_is_ready(dev) || usb_enable(NULL)) {
261 		return 0;
262 	}
263 
264 	/* Poll if the DTR flag was set, optional */
265 	while (!dtr) {
266 		uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
267 	}
268 
269 	if (!gpio_is_ready_dt(&led0_gpio)) {
270 		printk("%s: device not ready.\n", led0_gpio.port->name);
271 		return 0;
272 	}
273 	gpio_pin_configure_dt(&led0_gpio, GPIO_OUTPUT_ACTIVE);
274 
275 	if (!gpio_is_ready_dt(&led1_gpio)) {
276 		printk("%s: device not ready.\n", led1_gpio.port->name);
277 		return 0;
278 	}
279 	gpio_pin_configure_dt(&led1_gpio, GPIO_OUTPUT_INACTIVE);
280 
281 	for (i = 0; i < 6; i++) {
282 		gpio_pin_set_dt(&led0_gpio, on);
283 		gpio_pin_set_dt(&led1_gpio, !on);
284 		k_sleep(K_MSEC(100));
285 		on = (on == 1) ? 0 : 1;
286 	}
287 
288 	gpio_pin_set_dt(&led0_gpio, 0);
289 	gpio_pin_set_dt(&led1_gpio, 1);
290 
291 	printk("SensorTile.box test!!\n");
292 
293 	const struct device *const hts221 = DEVICE_DT_GET_ONE(st_hts221);
294 	const struct device *const lis2dw12 = DEVICE_DT_GET_ONE(st_lis2dw12);
295 	const struct device *const lps22hh = DEVICE_DT_GET_ONE(st_lps22hh);
296 	const struct device *const lsm6dso = DEVICE_DT_GET_ONE(st_lsm6dso);
297 	const struct device *const stts751 = DEVICE_DT_GET_ONE(st_stts751);
298 	const struct device *const iis3dhhc = DEVICE_DT_GET_ONE(st_iis3dhhc);
299 	const struct device *const lis2mdl = DEVICE_DT_GET_ONE(st_lis2mdl);
300 
301 	if (!device_is_ready(hts221)) {
302 		printk("%s: device not ready.\n", hts221->name);
303 		return 0;
304 	}
305 	if (!device_is_ready(lis2dw12)) {
306 		printk("%s: device not ready.\n", lis2dw12->name);
307 		return 0;
308 	}
309 	if (!device_is_ready(lps22hh)) {
310 		printk("%s: device not ready.\n", lps22hh->name);
311 		return 0;
312 	}
313 	if (!device_is_ready(lsm6dso)) {
314 		printk("%s: device not ready.\n", lsm6dso->name);
315 		return 0;
316 	}
317 	if (!device_is_ready(stts751)) {
318 		printk("%s: device not ready.\n", stts751->name);
319 		return 0;
320 	}
321 	if (!device_is_ready(iis3dhhc)) {
322 		printk("%s: device not ready.\n", iis3dhhc->name);
323 		return 0;
324 	}
325 	if (!device_is_ready(lis2mdl)) {
326 		printk("%s: device not ready.\n", lis2mdl->name);
327 		return 0;
328 	}
329 
330 	lis2dw12_config(lis2dw12);
331 	lps22hh_config(lps22hh);
332 	lsm6dso_config(lsm6dso);
333 	stts751_config(stts751);
334 	iis3dhhc_config(iis3dhhc);
335 
336 	while (1) {
337 		struct sensor_value hts221_hum, hts221_temp;
338 		struct sensor_value lps22hh_press, lps22hh_temp;
339 		struct sensor_value lis2dw12_accel[3];
340 		struct sensor_value iis3dhhc_accel[3];
341 		struct sensor_value lsm6dso_accel[3], lsm6dso_gyro[3];
342 		struct sensor_value stts751_temp;
343 		struct sensor_value magn[3];
344 
345 		/* handle HTS221 sensor */
346 		if (sensor_sample_fetch(hts221) < 0) {
347 			printf("HTS221 Sensor sample update error\n");
348 			return 0;
349 		}
350 
351 #ifndef CONFIG_LIS2DW12_TRIGGER
352 		/* handle LIS2DW12 sensor */
353 		if (sensor_sample_fetch(lis2dw12) < 0) {
354 			printf("LIS2DW12 Sensor sample update error\n");
355 			return 0;
356 		}
357 #endif
358 
359 #ifndef CONFIG_LSM6DSO_TRIGGER
360 		if (sensor_sample_fetch(lsm6dso) < 0) {
361 			printf("LSM6DSO Sensor sample update error\n");
362 			return 0;
363 		}
364 #endif
365 
366 #ifndef CONFIG_LPS22HH_TRIGGER
367 		if (sensor_sample_fetch(lps22hh) < 0) {
368 			printf("LPS22HH Sensor sample update error\n");
369 			return 0;
370 		}
371 #endif
372 
373 #ifndef CONFIG_STTS751_TRIGGER
374 		if (sensor_sample_fetch(stts751) < 0) {
375 			printf("STTS751 Sensor sample update error\n");
376 			return 0;
377 		}
378 #endif
379 
380 #ifndef CONFIG_IIS3DHHC_TRIGGER
381 		if (sensor_sample_fetch(iis3dhhc) < 0) {
382 			printf("IIS3DHHC Sensor sample update error\n");
383 			return 0;
384 		}
385 #endif
386 
387 		if (sensor_sample_fetch(lis2mdl) < 0) {
388 			printf("LIS2MDL Sensor sample update error\n");
389 			return 0;
390 		}
391 
392 		sensor_channel_get(hts221, SENSOR_CHAN_HUMIDITY, &hts221_hum);
393 		sensor_channel_get(hts221, SENSOR_CHAN_AMBIENT_TEMP, &hts221_temp);
394 		sensor_channel_get(lis2dw12, SENSOR_CHAN_ACCEL_XYZ, lis2dw12_accel);
395 		sensor_channel_get(lps22hh, SENSOR_CHAN_AMBIENT_TEMP, &lps22hh_temp);
396 		sensor_channel_get(lps22hh, SENSOR_CHAN_PRESS, &lps22hh_press);
397 		sensor_channel_get(lsm6dso, SENSOR_CHAN_ACCEL_XYZ, lsm6dso_accel);
398 		sensor_channel_get(lsm6dso, SENSOR_CHAN_GYRO_XYZ, lsm6dso_gyro);
399 		sensor_channel_get(stts751, SENSOR_CHAN_AMBIENT_TEMP, &stts751_temp);
400 		sensor_channel_get(iis3dhhc, SENSOR_CHAN_ACCEL_XYZ, iis3dhhc_accel);
401 		sensor_channel_get(lis2mdl, SENSOR_CHAN_MAGN_XYZ, magn);
402 
403 		/* Display sensor data */
404 
405 		/* Erase previous */
406 		printf("\0033\014");
407 
408 		printf("SensorTile.box dashboard\n\n");
409 
410 		/* HTS221 temperature */
411 		printf("HTS221: Temperature: %.1f C\n",
412 		       sensor_value_to_double(&hts221_temp));
413 
414 		/* HTS221 humidity */
415 		printf("HTS221: Relative Humidity: %.1f%%\n",
416 		       sensor_value_to_double(&hts221_hum));
417 
418 		/* temperature */
419 		printf("LPS22HH: Temperature: %.1f C\n",
420 		       sensor_value_to_double(&lps22hh_temp));
421 
422 		/* pressure */
423 		printf("LPS22HH: Pressure:%.3f kpa\n",
424 		       sensor_value_to_double(&lps22hh_press));
425 
426 		printf("LIS2DW12: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
427 			sensor_value_to_double(&lis2dw12_accel[0]),
428 			sensor_value_to_double(&lis2dw12_accel[1]),
429 			sensor_value_to_double(&lis2dw12_accel[2]));
430 
431 		printf("IIS3DHHC: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
432 			sensor_value_to_double(&iis3dhhc_accel[0]),
433 			sensor_value_to_double(&iis3dhhc_accel[1]),
434 			sensor_value_to_double(&iis3dhhc_accel[2]));
435 
436 		printf("LSM6DSOX: Accel (m.s-2): x: %.3f, y: %.3f, z: %.3f\n",
437 			sensor_value_to_double(&lsm6dso_accel[0]),
438 			sensor_value_to_double(&lsm6dso_accel[1]),
439 			sensor_value_to_double(&lsm6dso_accel[2]));
440 
441 		printf("LSM6DSOX: GYro (dps): x: %.3f, y: %.3f, z: %.3f\n",
442 			sensor_value_to_double(&lsm6dso_gyro[0]),
443 			sensor_value_to_double(&lsm6dso_gyro[1]),
444 			sensor_value_to_double(&lsm6dso_gyro[2]));
445 
446 		/* temperature */
447 		printf("STTS751: Temperature: %.1f C\n",
448 		       sensor_value_to_double(&stts751_temp));
449 
450 		printf("LIS2MDL: Magn (Gauss): x: %.3f, y: %.3f, z: %.3f\n",
451 			sensor_value_to_double(&magn[0]),
452 			sensor_value_to_double(&magn[1]),
453 			sensor_value_to_double(&magn[2]));
454 
455 #if defined(CONFIG_LPS22HH_TRIGGER)
456 		printk("%d:: lps22hh trig %d\n", cnt, lps22hh_trig_cnt);
457 #endif
458 
459 #ifdef CONFIG_LIS2DW12_TRIGGER
460 		printk("%d:: lis2dw12 trig %d\n", cnt, lis2dw12_trig_cnt);
461 #endif
462 
463 #ifdef CONFIG_LSM6DSO_TRIGGER
464 		printk("%d:: lsm6dsox acc trig %d\n", cnt, lsm6dso_acc_trig_cnt);
465 		printk("%d:: lsm6dsox gyr trig %d\n", cnt, lsm6dso_gyr_trig_cnt);
466 #endif
467 
468 #if defined(CONFIG_STTS751_TRIGGER)
469 		printk("%d:: stts751 trig %d\n", cnt, stts751_trig_cnt);
470 #endif
471 
472 #if defined(CONFIG_IIS3DHHC_TRIGGER)
473 		printk("%d:: iis3dhhc trig %d\n", cnt, iis3dhhc_trig_cnt);
474 #endif
475 
476 		k_sleep(K_MSEC(2000));
477 	}
478 }
479