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