lsm6dso.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* ST Microelectronics LSM6DSO 6-axis IMU sensor driver
  2. *
  3. * Copyright (c) 2019 STMicroelectronics
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Datasheet:
  8. * https://www.st.com/resource/en/datasheet/lsm6dso.pdf
  9. */
  10. #ifndef ZEPHYR_DRIVERS_SENSOR_LSM6DSO_LSM6DSO_H_
  11. #define ZEPHYR_DRIVERS_SENSOR_LSM6DSO_LSM6DSO_H_
  12. #include <drivers/sensor.h>
  13. #include <zephyr/types.h>
  14. #include <drivers/gpio.h>
  15. #include <drivers/spi.h>
  16. #include <sys/util.h>
  17. #include <stmemsc.h>
  18. #include "lsm6dso_reg.h"
  19. #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
  20. #include <drivers/spi.h>
  21. #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(spi) */
  22. #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
  23. #include <drivers/i2c.h>
  24. #endif /* DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c) */
  25. #define LSM6DSO_EN_BIT 0x01
  26. #define LSM6DSO_DIS_BIT 0x00
  27. /* Accel sensor sensitivity grain is 61 ug/LSB */
  28. #define GAIN_UNIT_XL (61LL)
  29. /* Gyro sensor sensitivity grain is 4.375 udps/LSB */
  30. #define GAIN_UNIT_G (4375LL)
  31. #define SENSOR_PI_DOUBLE (SENSOR_PI / 1000000.0)
  32. #define SENSOR_DEG2RAD_DOUBLE (SENSOR_PI_DOUBLE / 180)
  33. #define SENSOR_G_DOUBLE (SENSOR_G / 1000000.0)
  34. #if CONFIG_LSM6DSO_ACCEL_FS == 0
  35. #define LSM6DSO_ACCEL_FS_RUNTIME 1
  36. #define LSM6DSO_DEFAULT_ACCEL_FULLSCALE 0
  37. #define LSM6DSO_DEFAULT_ACCEL_SENSITIVITY GAIN_UNIT_XL
  38. #elif CONFIG_LSM6DSO_ACCEL_FS == 2
  39. #define LSM6DSO_DEFAULT_ACCEL_FULLSCALE 0
  40. #define LSM6DSO_DEFAULT_ACCEL_SENSITIVITY GAIN_UNIT_XL
  41. #elif CONFIG_LSM6DSO_ACCEL_FS == 4
  42. #define LSM6DSO_DEFAULT_ACCEL_FULLSCALE 2
  43. #define LSM6DSO_DEFAULT_ACCEL_SENSITIVITY (2.0 * GAIN_UNIT_XL)
  44. #elif CONFIG_LSM6DSO_ACCEL_FS == 8
  45. #define LSM6DSO_DEFAULT_ACCEL_FULLSCALE 3
  46. #define LSM6DSO_DEFAULT_ACCEL_SENSITIVITY (4.0 * GAIN_UNIT_XL)
  47. #elif CONFIG_LSM6DSO_ACCEL_FS == 16
  48. #define LSM6DSO_DEFAULT_ACCEL_FULLSCALE 1
  49. #define LSM6DSO_DEFAULT_ACCEL_SENSITIVITY (8.0 * GAIN_UNIT_XL)
  50. #endif
  51. #if (CONFIG_LSM6DSO_ACCEL_ODR == 0)
  52. #define LSM6DSO_ACCEL_ODR_RUNTIME 1
  53. #endif
  54. #define GYRO_FULLSCALE_125 4
  55. #if CONFIG_LSM6DSO_GYRO_FS == 0
  56. #define LSM6DSO_GYRO_FS_RUNTIME 1
  57. #define LSM6DSO_DEFAULT_GYRO_FULLSCALE 4
  58. #define LSM6DSO_DEFAULT_GYRO_SENSITIVITY GAIN_UNIT_G
  59. #elif CONFIG_LSM6DSO_GYRO_FS == 125
  60. #define LSM6DSO_DEFAULT_GYRO_FULLSCALE 4
  61. #define LSM6DSO_DEFAULT_GYRO_SENSITIVITY GAIN_UNIT_G
  62. #elif CONFIG_LSM6DSO_GYRO_FS == 250
  63. #define LSM6DSO_DEFAULT_GYRO_FULLSCALE 0
  64. #define LSM6DSO_DEFAULT_GYRO_SENSITIVITY (2.0 * GAIN_UNIT_G)
  65. #elif CONFIG_LSM6DSO_GYRO_FS == 500
  66. #define LSM6DSO_DEFAULT_GYRO_FULLSCALE 1
  67. #define LSM6DSO_DEFAULT_GYRO_SENSITIVITY (4.0 * GAIN_UNIT_G)
  68. #elif CONFIG_LSM6DSO_GYRO_FS == 1000
  69. #define LSM6DSO_DEFAULT_GYRO_FULLSCALE 2
  70. #define LSM6DSO_DEFAULT_GYRO_SENSITIVITY (8.0 * GAIN_UNIT_G)
  71. #elif CONFIG_LSM6DSO_GYRO_FS == 2000
  72. #define LSM6DSO_DEFAULT_GYRO_FULLSCALE 3
  73. #define LSM6DSO_DEFAULT_GYRO_SENSITIVITY (16.0 * GAIN_UNIT_G)
  74. #endif
  75. #if (CONFIG_LSM6DSO_GYRO_ODR == 0)
  76. #define LSM6DSO_GYRO_ODR_RUNTIME 1
  77. #endif
  78. struct lsm6dso_config {
  79. stmdev_ctx_t ctx;
  80. union {
  81. #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
  82. const struct stmemsc_cfg_i2c i2c;
  83. #endif
  84. #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
  85. const struct stmemsc_cfg_spi spi;
  86. #endif
  87. } stmemsc_cfg;
  88. #ifdef CONFIG_LSM6DSO_TRIGGER
  89. const struct gpio_dt_spec gpio_drdy;
  90. uint8_t int_pin;
  91. bool trig_enabled;
  92. #endif /* CONFIG_LSM6DSO_TRIGGER */
  93. };
  94. union samples {
  95. uint8_t raw[6];
  96. struct {
  97. int16_t axis[3];
  98. };
  99. } __aligned(2);
  100. #define LSM6DSO_SHUB_MAX_NUM_SLVS 2
  101. struct lsm6dso_data {
  102. const struct device *dev;
  103. int16_t acc[3];
  104. uint32_t acc_gain;
  105. int16_t gyro[3];
  106. uint32_t gyro_gain;
  107. #if defined(CONFIG_LSM6DSO_ENABLE_TEMP)
  108. int temp_sample;
  109. #endif
  110. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  111. uint8_t ext_data[2][6];
  112. uint16_t magn_gain;
  113. struct hts221_data {
  114. int16_t x0;
  115. int16_t x1;
  116. int16_t y0;
  117. int16_t y1;
  118. } hts221;
  119. bool shub_inited;
  120. uint8_t num_ext_dev;
  121. uint8_t shub_ext[LSM6DSO_SHUB_MAX_NUM_SLVS];
  122. #endif /* CONFIG_LSM6DSO_SENSORHUB */
  123. uint16_t accel_freq;
  124. uint8_t accel_fs;
  125. uint16_t gyro_freq;
  126. uint8_t gyro_fs;
  127. #ifdef CONFIG_LSM6DSO_TRIGGER
  128. struct gpio_callback gpio_cb;
  129. sensor_trigger_handler_t handler_drdy_acc;
  130. sensor_trigger_handler_t handler_drdy_gyr;
  131. sensor_trigger_handler_t handler_drdy_temp;
  132. #if defined(CONFIG_LSM6DSO_TRIGGER_OWN_THREAD)
  133. K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_LSM6DSO_THREAD_STACK_SIZE);
  134. struct k_thread thread;
  135. struct k_sem gpio_sem;
  136. #elif defined(CONFIG_LSM6DSO_TRIGGER_GLOBAL_THREAD)
  137. struct k_work work;
  138. #endif
  139. #endif /* CONFIG_LSM6DSO_TRIGGER */
  140. };
  141. #if defined(CONFIG_LSM6DSO_SENSORHUB)
  142. int lsm6dso_shub_init(const struct device *dev);
  143. int lsm6dso_shub_fetch_external_devs(const struct device *dev);
  144. int lsm6dso_shub_get_idx(const struct device *dev, enum sensor_channel type);
  145. int lsm6dso_shub_config(const struct device *dev, enum sensor_channel chan,
  146. enum sensor_attribute attr,
  147. const struct sensor_value *val);
  148. #endif /* CONFIG_LSM6DSO_SENSORHUB */
  149. #ifdef CONFIG_LSM6DSO_TRIGGER
  150. int lsm6dso_trigger_set(const struct device *dev,
  151. const struct sensor_trigger *trig,
  152. sensor_trigger_handler_t handler);
  153. int lsm6dso_init_interrupt(const struct device *dev);
  154. #endif
  155. #endif /* ZEPHYR_DRIVERS_SENSOR_LSM6DSO_LSM6DSO_H_ */