sensor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. /* Sensor unit types */
  57. #define RT_SENSOR_UNIT_NONE (0)
  58. #define RT_SENSOR_UNIT_MG (1) /* Accelerometer unit: mG */
  59. #define RT_SENSOR_UNIT_MDPS (2) /* Gyroscope unit: mdps */
  60. #define RT_SENSOR_UNIT_MGAUSS (3) /* Magnetometer unit: mGauss */
  61. #define RT_SENSOR_UNIT_LUX (4) /* Ambient light unit: lux */
  62. #define RT_SENSOR_UNIT_CM (5) /* Distance unit: cm */
  63. #define RT_SENSOR_UNIT_PA (6) /* Barometer unit: pa */
  64. #define RT_SENSOR_UNIT_PERMILLAGE (7) /* Relative Humidity unit: permillage */
  65. #define RT_SENSOR_UNIT_DCELSIUS (8) /* Temperature unit: dCelsius */
  66. #define RT_SENSOR_UNIT_HZ (9) /* Frequency unit: HZ */
  67. #define RT_SENSOR_UNIT_ONE (10) /* Dimensionless quantity unit: 1 */
  68. #define RT_SENSOR_UNIT_BPM (11) /* Heart rate unit: bpm */
  69. #define RT_SENSOR_UNIT_MM (12) /* Distance unit: mm */
  70. #define RT_SENSOR_UNIT_MN (13) /* Force unit: mN */
  71. /* Sensor communication interface types */
  72. #define RT_SENSOR_INTF_I2C (1 << 0)
  73. #define RT_SENSOR_INTF_SPI (1 << 1)
  74. #define RT_SENSOR_INTF_UART (1 << 2)
  75. #define RT_SENSOR_INTF_ONEWIRE (1 << 3)
  76. /* Sensor power mode types */
  77. #define RT_SENSOR_POWER_NONE (0)
  78. #define RT_SENSOR_POWER_DOWN (1) /* power down mode */
  79. #define RT_SENSOR_POWER_NORMAL (2) /* normal-power mode */
  80. #define RT_SENSOR_POWER_LOW (3) /* low-power mode */
  81. #define RT_SENSOR_POWER_HIGH (4) /* high-power mode */
  82. /* Sensor work mode types */
  83. #define RT_SENSOR_MODE_NONE (0)
  84. #define RT_SENSOR_MODE_POLLING (1) /* One shot only read a data */
  85. #define RT_SENSOR_MODE_INT (2) /* TODO: One shot interrupt only read a data */
  86. #define RT_SENSOR_MODE_FIFO (3) /* TODO: One shot interrupt read all fifo data */
  87. /* Sensor control cmd types */
  88. #define RT_SENSOR_CTRL_GET_ID (0) /* Get device id */
  89. #define RT_SENSOR_CTRL_GET_INFO (1) /* Get sensor info */
  90. #define RT_SENSOR_CTRL_SET_RANGE (2) /* Set the measure range of sensor. unit is info of sensor */
  91. #define RT_SENSOR_CTRL_SET_ODR (3) /* Set output date rate. unit is HZ */
  92. #define RT_SENSOR_CTRL_SET_MODE (4) /* Set sensor's work mode. ex. RT_SENSOR_MODE_POLLING,RT_SENSOR_MODE_INT */
  93. #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 */
  94. #define RT_SENSOR_CTRL_SELF_TEST (6) /* Take a self test */
  95. #define RT_SENSOR_CTRL_USER_CMD_START 0x100 /* User commands should be greater than 0x100 */
  96. struct rt_sensor_info
  97. {
  98. rt_uint8_t type; /* The sensor type */
  99. rt_uint8_t vendor; /* Vendor of sensors */
  100. const char *model; /* model name of sensor */
  101. rt_uint8_t unit; /* unit of measurement */
  102. rt_uint8_t intf_type; /* Communication interface type */
  103. rt_int32_t range_max; /* maximum range of this sensor's value. unit is 'unit' */
  104. rt_int32_t range_min; /* minimum range of this sensor's value. unit is 'unit' */
  105. rt_uint32_t period_min; /* Minimum measurement period,unit:ms. zero = not a constant rate */
  106. rt_uint8_t fifo_max;
  107. };
  108. struct rt_sensor_intf
  109. {
  110. char *dev_name; /* The name of the communication device */
  111. rt_uint8_t type; /* Communication interface type */
  112. void *user_data; /* Private data for the sensor. ex. i2c addr,spi cs,control I/O */
  113. };
  114. struct rt_sensor_config
  115. {
  116. struct rt_sensor_intf intf; /* sensor interface config */
  117. struct rt_device_pin_mode irq_pin; /* Interrupt pin, The purpose of this pin is to notification read data */
  118. rt_uint8_t mode; /* sensor work mode */
  119. rt_uint8_t power; /* sensor power mode */
  120. rt_uint16_t odr; /* sensor out data rate */
  121. rt_int32_t range; /* sensor range of measurement */
  122. };
  123. typedef struct rt_sensor_device *rt_sensor_t;
  124. struct rt_sensor_device
  125. {
  126. struct rt_device parent; /* The standard device */
  127. struct rt_sensor_info info; /* The sensor info data */
  128. struct rt_sensor_config config; /* The sensor config data */
  129. void *data_buf; /* The buf of the data received */
  130. rt_size_t data_len; /* The size of the data received */
  131. const struct rt_sensor_ops *ops; /* The sensor ops */
  132. struct rt_sensor_module *module; /* The sensor module */
  133. rt_err_t (*irq_handle)(rt_sensor_t sensor); /* Called when an interrupt is generated, registered by the driver */
  134. };
  135. struct rt_sensor_module
  136. {
  137. rt_mutex_t lock; /* The module lock */
  138. rt_sensor_t sen[RT_SENSOR_MODULE_MAX]; /* The module contains a list of sensors */
  139. rt_uint8_t sen_num; /* Number of sensors contained in the module */
  140. };
  141. /* 3-axis Data Type */
  142. struct sensor_3_axis
  143. {
  144. rt_int32_t x;
  145. rt_int32_t y;
  146. rt_int32_t z;
  147. };
  148. struct rt_sensor_data
  149. {
  150. rt_uint32_t timestamp; /* The timestamp when the data was received */
  151. rt_uint8_t type; /* The sensor type of the data */
  152. union
  153. {
  154. struct sensor_3_axis acce; /* Accelerometer. unit: mG */
  155. struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */
  156. struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */
  157. rt_int32_t temp; /* Temperature. unit: dCelsius */
  158. rt_int32_t humi; /* Relative humidity. unit: permillage */
  159. rt_int32_t baro; /* Pressure. unit: pascal (Pa) */
  160. rt_int32_t light; /* Light. unit: lux */
  161. rt_int32_t proximity; /* Distance. unit: centimeters */
  162. rt_int32_t hr; /* Heart rate. unit: bpm */
  163. rt_int32_t tvoc; /* TVOC. unit: permillage */
  164. rt_int32_t noise; /* Noise Loudness. unit: HZ */
  165. rt_uint32_t step; /* Step sensor. unit: 1 */
  166. rt_int32_t force; /* Force sensor. unit: mN */
  167. rt_uint32_t dust; /* Dust sensor. unit: ug/m3 */
  168. rt_uint32_t eco2; /* eCO2 sensor. unit: ppm */
  169. } data;
  170. };
  171. struct rt_sensor_ops
  172. {
  173. rt_size_t (*fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len);
  174. rt_err_t (*control)(struct rt_sensor_device *sensor, int cmd, void *arg);
  175. };
  176. int rt_hw_sensor_register(rt_sensor_t sensor,
  177. const char *name,
  178. rt_uint32_t flag,
  179. void *data);
  180. #ifdef __cplusplus
  181. }
  182. #endif
  183. #endif /* __SENSOR_H__ */