sensor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-01-31 flybreak first version
  9. */
  10. #ifndef __SENSOR_H__
  11. #define __SENSOR_H__
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #ifdef RT_USING_RTC
  18. #define rt_sensor_get_ts() time(RT_NULL) /* API for the sensor to get the timestamp */
  19. #else
  20. #define rt_sensor_get_ts() rt_tick_get() /* API for the sensor to get the timestamp */
  21. #endif
  22. #define RT_PIN_NONE 0xFFFF /* RT PIN NONE */
  23. #define RT_DEVICE_FLAG_FIFO_RX 0x200 /* Flag to use when the sensor is open by fifo mode */
  24. #define RT_SENSOR_MODULE_MAX (3) /* The maximum number of members of a sensor module */
  25. /* Sensor types */
  26. #define RT_SENSOR_CLASS_NONE (0)
  27. #define RT_SENSOR_CLASS_ACCE (1) /* Accelerometer */
  28. #define RT_SENSOR_CLASS_GYRO (2) /* Gyroscope */
  29. #define RT_SENSOR_CLASS_MAG (3) /* Magnetometer */
  30. #define RT_SENSOR_CLASS_TEMP (4) /* Temperature */
  31. #define RT_SENSOR_CLASS_HUMI (5) /* Relative Humidity */
  32. #define RT_SENSOR_CLASS_BARO (6) /* Barometer */
  33. #define RT_SENSOR_CLASS_LIGHT (7) /* Ambient light */
  34. #define RT_SENSOR_CLASS_PROXIMITY (8) /* Proximity */
  35. #define RT_SENSOR_CLASS_HR (9) /* Heart Rate */
  36. #define RT_SENSOR_CLASS_TVOC (10) /* TVOC Level */
  37. #define RT_SENSOR_CLASS_NOISE (11) /* Noise Loudness */
  38. #define RT_SENSOR_CLASS_STEP (12) /* Step sensor */
  39. #define RT_SENSOR_CLASS_FORCE (13) /* Force sensor */
  40. #define RT_SENSOR_CLASS_DUST (14) /* Dust sensor */
  41. #define RT_SENSOR_CLASS_ECO2 (15) /* eCO2 sensor */
  42. /* Sensor vendor types */
  43. #define RT_SENSOR_VENDOR_UNKNOWN (0)
  44. #define RT_SENSOR_VENDOR_STM (1) /* STMicroelectronics */
  45. #define RT_SENSOR_VENDOR_BOSCH (2) /* Bosch */
  46. #define RT_SENSOR_VENDOR_INVENSENSE (3) /* Invensense */
  47. #define RT_SENSOR_VENDOR_SEMTECH (4) /* Semtech */
  48. #define RT_SENSOR_VENDOR_GOERTEK (5) /* Goertek */
  49. #define RT_SENSOR_VENDOR_MIRAMEMS (6) /* MiraMEMS */
  50. #define RT_SENSOR_VENDOR_DALLAS (7) /* Dallas */
  51. #define RT_SENSOR_VENDOR_ASAIR (8) /* Aosong */
  52. #define RT_SENSOR_VENDOR_SHARP (9) /* Sharp */
  53. #define RT_SENSOR_VENDOR_SENSIRION (10) /* Sensirion */
  54. #define RT_SENSOR_VENDOR_TI (11) /* Texas Instruments */
  55. #define RT_SENSOR_VENDOR_PLANTOWER (12) /* Plantower */
  56. #define RT_SENSOR_VENDOR_AMS (13) /* ams AG */
  57. /* Sensor unit types */
  58. #define RT_SENSOR_UNIT_NONE (0)
  59. #define RT_SENSOR_UNIT_MG (1) /* Accelerometer unit: mG */
  60. #define RT_SENSOR_UNIT_MDPS (2) /* Gyroscope unit: mdps */
  61. #define RT_SENSOR_UNIT_MGAUSS (3) /* Magnetometer unit: mGauss */
  62. #define RT_SENSOR_UNIT_LUX (4) /* Ambient light unit: lux */
  63. #define RT_SENSOR_UNIT_CM (5) /* Distance unit: cm */
  64. #define RT_SENSOR_UNIT_PA (6) /* Barometer unit: pa */
  65. #define RT_SENSOR_UNIT_PERMILLAGE (7) /* Relative Humidity unit: permillage */
  66. #define RT_SENSOR_UNIT_DCELSIUS (8) /* Temperature unit: dCelsius */
  67. #define RT_SENSOR_UNIT_HZ (9) /* Frequency unit: HZ */
  68. #define RT_SENSOR_UNIT_ONE (10) /* Dimensionless quantity unit: 1 */
  69. #define RT_SENSOR_UNIT_BPM (11) /* Heart rate unit: bpm */
  70. #define RT_SENSOR_UNIT_MM (12) /* Distance unit: mm */
  71. #define RT_SENSOR_UNIT_MN (13) /* Force unit: mN */
  72. #define RT_SENSOR_UNIT_PPM (14) /* Concentration unit: ppm */
  73. #define RT_SENSOR_UNIT_PPB (15) /* Concentration unit: ppb */
  74. /* Sensor communication interface types */
  75. #define RT_SENSOR_INTF_I2C (1 << 0)
  76. #define RT_SENSOR_INTF_SPI (1 << 1)
  77. #define RT_SENSOR_INTF_UART (1 << 2)
  78. #define RT_SENSOR_INTF_ONEWIRE (1 << 3)
  79. /* Sensor power mode types */
  80. #define RT_SENSOR_POWER_NONE (0)
  81. #define RT_SENSOR_POWER_DOWN (1) /* power down mode */
  82. #define RT_SENSOR_POWER_NORMAL (2) /* normal-power mode */
  83. #define RT_SENSOR_POWER_LOW (3) /* low-power mode */
  84. #define RT_SENSOR_POWER_HIGH (4) /* high-power mode */
  85. /* Sensor work mode types */
  86. #define RT_SENSOR_MODE_NONE (0)
  87. #define RT_SENSOR_MODE_POLLING (1) /* One shot only read a data */
  88. #define RT_SENSOR_MODE_INT (2) /* TODO: One shot interrupt only read a data */
  89. #define RT_SENSOR_MODE_FIFO (3) /* TODO: One shot interrupt read all fifo data */
  90. /* Sensor control cmd types */
  91. #define RT_SENSOR_CTRL_GET_ID (0) /* Get device id */
  92. #define RT_SENSOR_CTRL_GET_INFO (1) /* Get sensor info */
  93. #define RT_SENSOR_CTRL_SET_RANGE (2) /* Set the measure range of sensor. unit is info of sensor */
  94. #define RT_SENSOR_CTRL_SET_ODR (3) /* Set output date rate. unit is HZ */
  95. #define RT_SENSOR_CTRL_SET_MODE (4) /* Set sensor's work mode. ex. RT_SENSOR_MODE_POLLING,RT_SENSOR_MODE_INT */
  96. #define RT_SENSOR_CTRL_SET_POWER (5) /* Set power mode. args type of sensor power mode. ex. RT_SENSOR_POWER_DOWN,RT_SENSOR_POWER_NORMAL */
  97. #define RT_SENSOR_CTRL_SELF_TEST (6) /* Take a self test */
  98. #define RT_SENSOR_CTRL_USER_CMD_START 0x100 /* User commands should be greater than 0x100 */
  99. struct rt_sensor_info
  100. {
  101. rt_uint8_t type; /* The sensor type */
  102. rt_uint8_t vendor; /* Vendor of sensors */
  103. const char *model; /* model name of sensor */
  104. rt_uint8_t unit; /* unit of measurement */
  105. rt_uint8_t intf_type; /* Communication interface type */
  106. rt_int32_t range_max; /* maximum range of this sensor's value. unit is 'unit' */
  107. rt_int32_t range_min; /* minimum range of this sensor's value. unit is 'unit' */
  108. rt_uint32_t period_min; /* Minimum measurement period,unit:ms. zero = not a constant rate */
  109. rt_uint8_t fifo_max;
  110. };
  111. struct rt_sensor_intf
  112. {
  113. char *dev_name; /* The name of the communication device */
  114. rt_uint8_t type; /* Communication interface type */
  115. void *user_data; /* Private data for the sensor. ex. i2c addr,spi cs,control I/O */
  116. };
  117. struct rt_sensor_config
  118. {
  119. struct rt_sensor_intf intf; /* sensor interface config */
  120. struct rt_device_pin_mode irq_pin; /* Interrupt pin, The purpose of this pin is to notification read data */
  121. rt_uint8_t mode; /* sensor work mode */
  122. rt_uint8_t power; /* sensor power mode */
  123. rt_uint16_t odr; /* sensor out data rate */
  124. rt_int32_t range; /* sensor range of measurement */
  125. };
  126. typedef struct rt_sensor_device *rt_sensor_t;
  127. struct rt_sensor_device
  128. {
  129. struct rt_device parent; /* The standard device */
  130. struct rt_sensor_info info; /* The sensor info data */
  131. struct rt_sensor_config config; /* The sensor config data */
  132. void *data_buf; /* The buf of the data received */
  133. rt_size_t data_len; /* The size of the data received */
  134. const struct rt_sensor_ops *ops; /* The sensor ops */
  135. struct rt_sensor_module *module; /* The sensor module */
  136. rt_err_t (*irq_handle)(rt_sensor_t sensor); /* Called when an interrupt is generated, registered by the driver */
  137. };
  138. struct rt_sensor_module
  139. {
  140. rt_mutex_t lock; /* The module lock */
  141. rt_sensor_t sen[RT_SENSOR_MODULE_MAX]; /* The module contains a list of sensors */
  142. rt_uint8_t sen_num; /* Number of sensors contained in the module */
  143. };
  144. /* 3-axis Data Type */
  145. struct sensor_3_axis
  146. {
  147. rt_int32_t x;
  148. rt_int32_t y;
  149. rt_int32_t z;
  150. };
  151. struct rt_sensor_data
  152. {
  153. rt_uint32_t timestamp; /* The timestamp when the data was received */
  154. rt_uint8_t type; /* The sensor type of the data */
  155. union
  156. {
  157. struct sensor_3_axis acce; /* Accelerometer. unit: mG */
  158. struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */
  159. struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */
  160. rt_int32_t temp; /* Temperature. unit: dCelsius */
  161. rt_int32_t humi; /* Relative humidity. unit: permillage */
  162. rt_int32_t baro; /* Pressure. unit: pascal (Pa) */
  163. rt_int32_t light; /* Light. unit: lux */
  164. rt_int32_t proximity; /* Distance. unit: centimeters */
  165. rt_int32_t hr; /* Heart rate. unit: bpm */
  166. rt_int32_t tvoc; /* TVOC. unit: permillage */
  167. rt_int32_t noise; /* Noise Loudness. unit: HZ */
  168. rt_uint32_t step; /* Step sensor. unit: 1 */
  169. rt_int32_t force; /* Force sensor. unit: mN */
  170. rt_uint32_t dust; /* Dust sensor. unit: ug/m3 */
  171. rt_uint32_t eco2; /* eCO2 sensor. unit: ppm */
  172. } data;
  173. };
  174. struct rt_sensor_ops
  175. {
  176. rt_size_t (*fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len);
  177. rt_err_t (*control)(struct rt_sensor_device *sensor, int cmd, void *arg);
  178. };
  179. int rt_hw_sensor_register(rt_sensor_t sensor,
  180. const char *name,
  181. rt_uint32_t flag,
  182. void *data);
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif /* __SENSOR_H__ */