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