sensor.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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_sen_get_timestamp() time() /* API for the sensor to get the timestamp */
  19. #else
  20. #define rt_sen_get_timestamp() 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_SEN_FLAG_FIFO 0x200 /* Flag to use when the sensor is open by fifo mode */
  24. #define RT_SEN_MODULE_MAX (3) /* The maximum number of members of a sensor module */
  25. /* Sensor types */
  26. #define RT_SEN_CLASS_NONE (0)
  27. #define RT_SEN_CLASS_ACCE (1) /* Accelerometer */
  28. #define RT_SEN_CLASS_GYRO (2) /* Gyroscope */
  29. #define RT_SEN_CLASS_MAG (3) /* Magnetometer */
  30. #define RT_SEN_CLASS_TEMP (4) /* Temperature */
  31. #define RT_SEN_CLASS_HUMI (5) /* Relative Humidity */
  32. #define RT_SEN_CLASS_BARO (6) /* Barometer */
  33. #define RT_SEN_CLASS_LIGHT (7) /* Ambient light */
  34. #define RT_SEN_CLASS_PROXIMITY (8) /* Proximity */
  35. #define RT_SEN_CLASS_HR (9) /* Heart Rate */
  36. #define RT_SEN_CLASS_TVOC (10) /* TVOC Level */
  37. #define RT_SEN_CLASS_NOISE (11) /* Noise Loudness */
  38. #define RT_SEN_CLASS_STEP (12) /* Step sensor */
  39. /* Sensor vendor types */
  40. #define RT_SEN_VENDOR_UNKNOWN (0)
  41. #define RT_SEN_VENDOR_STM (1) /* STMicroelectronics */
  42. #define RT_SEN_VENDOR_BOSCH (2) /* Bosch */
  43. #define RT_SEN_VENDOR_INVENSENSE (3) /* Invensense */
  44. #define RT_SEN_VENDOR_SEMTECH (4) /* Semtech */
  45. #define RT_SEN_VENDOR_GOERTEK (5) /* Goertek */
  46. /* Sensor unit types */
  47. #define RT_SEN_UNIT_NONE (0)
  48. #define RT_SEN_UNIT_MG (1) /* Accelerometer unit: mG */
  49. #define RT_SEN_UNIT_MDPS (2) /* Gyroscope unit: mdps */
  50. #define RT_SEN_UNIT_MGAUSS (3) /* Magnetometer unit: mGauss */
  51. #define RT_SEN_UNIT_LUX (4) /* Ambient light unit: lux */
  52. #define RT_SEN_UNIT_CM (5) /* Distance unit: cm */
  53. #define RT_SEN_UNIT_PA (6) /* Barometer unit: pa */
  54. #define RT_SEN_UNIT_PERMILLAGE (7) /* Relative Humidity unit: permillage */
  55. #define RT_SEN_UNIT_DCELSIUS (8) /* Temperature unit: dCelsius */
  56. #define RT_SEN_UNIT_HZ (9) /* Frequency unit: HZ */
  57. #define RT_SEN_UNIT_ONE (10) /* Dimensionless quantity unit: 1 */
  58. /* Sensor communication interface types */
  59. #define RT_SEN_INTF_I2C (1 << 0)
  60. #define RT_SEN_INTF_SPI (1 << 1)
  61. #define RT_SEN_INTF_UART (1 << 2)
  62. #define RT_SEN_INTF_ONEWIRE (1 << 3)
  63. /* Sensor power mode types */
  64. #define RT_SEN_POWER_NONE (0)
  65. #define RT_SEN_POWER_DOWN (1) /* power down mode */
  66. #define RT_SEN_POWER_NORMAL (2) /* normal-power mode */
  67. #define RT_SEN_POWER_LOW (3) /* low-power mode */
  68. #define RT_SEN_POWER_HIGH (4) /* high-power mode */
  69. /* Sensor work mode types */
  70. #define RT_SEN_MODE_NONE (0)
  71. #define RT_SEN_MODE_POLLING (1) /* One shot only read a data */
  72. #define RT_SEN_MODE_INT (2) /* TODO: One shot interrupt only read a data */
  73. #define RT_SEN_MODE_FIFO (3) /* TODO: One shot interrupt read all fifo data */
  74. /* Sensor control cmd types */
  75. #define RT_SEN_CTRL_GET_ID (0) /* Get device id */
  76. #define RT_SEN_CTRL_GET_INFO (1) /* Get sensor info */
  77. #define RT_SEN_CTRL_SET_RANGE (2) /* Set the measure range of sensor. unit is info of sensor */
  78. #define RT_SEN_CTRL_SET_ODR (3) /* Set output date rate. unit is HZ */
  79. #define RT_SEN_CTRL_SET_MODE (4) /* Set sensor's work mode. ex. RT_SEN_MODE_POLLING,RT_SEN_MODE_INT */
  80. #define RT_SEN_CTRL_SET_POWER (5) /* Set power mode. args type of sensor power mode. ex. RT_SEN_POWER_DOWN,RT_SEN_POWER_NORMAL */
  81. #define RT_SEN_CTRL_SELF_TEST (6) /* Take a self test */
  82. struct rt_sensor_info
  83. {
  84. rt_uint8_t type; /* The sensor type */
  85. rt_uint8_t vendor; /* Vendor of sensors */
  86. const char *model; /* model name of sensor */
  87. rt_uint8_t unit; /* unit of measurement */
  88. rt_uint8_t intf_type; /* Communication interface type */
  89. rt_int32_t range_max; /* maximum range of this sensor's value. unit is 'unit' */
  90. rt_int32_t range_min; /* minimum range of this sensor's value. unit is 'unit' */
  91. rt_uint32_t period_min; /* Minimum measurement period,unit:ms. zero = not a constant rate */
  92. rt_uint8_t fifo_max;
  93. };
  94. struct rt_sensor_intf
  95. {
  96. char *dev_name; /* The name of the communication device */
  97. rt_uint8_t type; /* Communication interface type */
  98. void *user_data; /* Private data for the sensor. ex. i2c addr,spi cs,control I/O */
  99. };
  100. struct rt_sensor_config
  101. {
  102. struct rt_sensor_intf intf; /* sensor interface config */
  103. struct rt_device_pin_mode irq_pin; /* Interrupt pin, The purpose of this pin is to notification read data */
  104. rt_uint8_t mode; /* sensor work mode */
  105. rt_uint8_t power; /* sensor power mode */
  106. rt_uint16_t odr; /* sensor out data rate */
  107. rt_int32_t range; /* sensor range of measurement */
  108. };
  109. struct rt_sensor_device
  110. {
  111. struct rt_device parent; /* The standard device */
  112. struct rt_sensor_info info; /* The sensor info data */
  113. struct rt_sensor_config config; /* The sensor config data */
  114. void *data_buf; /* The buf of the data received */
  115. rt_size_t data_len; /* The size of the data received */
  116. struct rt_sensor_ops *ops; /* The sensor ops */
  117. struct rt_sensor_module *module; /* The sensor module */
  118. };
  119. typedef struct rt_sensor_device *rt_sensor_t;
  120. struct rt_sensor_module
  121. {
  122. rt_mutex_t lock; /* The module lock */
  123. rt_sensor_t sen[RT_SEN_MODULE_MAX]; /* The module contains a list of sensors */
  124. rt_uint8_t sen_num; /* Number of sensors contained in the module */
  125. };
  126. /* 3-axis Data Type */
  127. struct sensor_3_axis
  128. {
  129. rt_int32_t x;
  130. rt_int32_t y;
  131. rt_int32_t z;
  132. };
  133. struct rt_sensor_data
  134. {
  135. rt_uint32_t timestamp; /* The timestamp when the data was received */
  136. rt_uint8_t type; /* The sensor type of the data */
  137. union
  138. {
  139. struct sensor_3_axis acce; /* Accelerometer. unit: mG */
  140. struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */
  141. struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */
  142. rt_int32_t temp; /* Temperature. unit: dCelsius */
  143. rt_int32_t humi; /* Relative humidity. unit: permillage */
  144. rt_int32_t baro; /* Pressure. unit: pascal (Pa) */
  145. rt_int32_t light; /* Light. unit: lux */
  146. rt_int32_t proximity; /* Distance. unit: centimeters */
  147. rt_int32_t hr; /* Heat rate. unit: HZ */
  148. rt_int32_t tvoc; /* TVOC. unit: permillage */
  149. rt_int32_t noise; /* Noise Loudness. unit: HZ */
  150. rt_uint32_t step; /* Step sensor. unit: 1 */
  151. }data;
  152. };
  153. struct rt_sensor_ops
  154. {
  155. rt_size_t (*fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len);
  156. rt_err_t (*control)(struct rt_sensor_device *sensor, int cmd, void *arg);
  157. };
  158. int rt_hw_sensor_register(rt_sensor_t sensor,
  159. const char *name,
  160. rt_uint32_t flag,
  161. void *data);
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif /* __SENSOR_H__ */