RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
sensor.h
浏览该文件的文档.
1/*
2 * Copyright (c) 2006-2021, 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
11#ifndef __SENSOR_H__
12#define __SENSOR_H__
13
14#include <rtthread.h>
15#include "dev_pin.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#ifdef RT_USING_RTC
22#define rt_sensor_get_ts() time(RT_NULL) /* API for the sensor to get the timestamp */
23#else
24#define rt_sensor_get_ts() rt_tick_get() /* API for the sensor to get the timestamp */
25#endif
26
27#define RT_PIN_NONE 0xFFFF /* RT PIN NONE */
28#define RT_DEVICE_FLAG_FIFO_RX 0x200 /* Flag to use when the sensor is open by fifo mode */
29
30#define RT_SENSOR_MODULE_MAX (3) /* The maximum number of members of a sensor module */
31
32/* Sensor types */
33
34#define RT_SENSOR_CLASS_NONE (0)
35#define RT_SENSOR_CLASS_ACCE (1) /* Accelerometer */
36#define RT_SENSOR_CLASS_GYRO (2) /* Gyroscope */
37#define RT_SENSOR_CLASS_MAG (3) /* Magnetometer */
38#define RT_SENSOR_CLASS_TEMP (4) /* Temperature */
39#define RT_SENSOR_CLASS_HUMI (5) /* Relative Humidity */
40#define RT_SENSOR_CLASS_BARO (6) /* Barometer */
41#define RT_SENSOR_CLASS_LIGHT (7) /* Ambient light */
42#define RT_SENSOR_CLASS_PROXIMITY (8) /* Proximity */
43#define RT_SENSOR_CLASS_HR (9) /* Heart Rate */
44#define RT_SENSOR_CLASS_TVOC (10) /* TVOC Level */
45#define RT_SENSOR_CLASS_NOISE (11) /* Noise Loudness */
46#define RT_SENSOR_CLASS_STEP (12) /* Step sensor */
47#define RT_SENSOR_CLASS_FORCE (13) /* Force sensor */
48#define RT_SENSOR_CLASS_DUST (14) /* Dust sensor */
49#define RT_SENSOR_CLASS_ECO2 (15) /* eCO2 sensor */
50#define RT_SENSOR_CLASS_GNSS (16) /* GPS/GNSS sensor */
51#define RT_SENSOR_CLASS_TOF (17) /* TOF sensor */
52#define RT_SENSOR_CLASS_SPO2 (18) /* SpO2 sensor */
53#define RT_SENSOR_CLASS_IAQ (19) /* IAQ sensor. */
54#define RT_SENSOR_CLASS_ETOH (20) /* EtOH sensor. */
55#define RT_SENSOR_CLASS_BP (21) /* Blood Pressure */
56#define RT_SENSOR_CLASS_VOLTAGE (22) /* Voltage sensor */
57#define RT_SENSOR_CLASS_CURRENT (23) /* Current sensor */
58#define RT_SENSOR_CLASS_POWER (24) /* Power sensor */
59
60/* Sensor vendor types */
61
62#define RT_SENSOR_VENDOR_UNKNOWN (0)
63#define RT_SENSOR_VENDOR_STM (1) /* STMicroelectronics */
64#define RT_SENSOR_VENDOR_BOSCH (2) /* Bosch */
65#define RT_SENSOR_VENDOR_INVENSENSE (3) /* Invensense */
66#define RT_SENSOR_VENDOR_SEMTECH (4) /* Semtech */
67#define RT_SENSOR_VENDOR_GOERTEK (5) /* Goertek */
68#define RT_SENSOR_VENDOR_MIRAMEMS (6) /* MiraMEMS */
69#define RT_SENSOR_VENDOR_DALLAS (7) /* Dallas */
70#define RT_SENSOR_VENDOR_ASAIR (8) /* Aosong */
71#define RT_SENSOR_VENDOR_SHARP (9) /* Sharp */
72#define RT_SENSOR_VENDOR_SENSIRION (10) /* Sensirion */
73#define RT_SENSOR_VENDOR_TI (11) /* Texas Instruments */
74#define RT_SENSOR_VENDOR_PLANTOWER (12) /* Plantower */
75#define RT_SENSOR_VENDOR_AMS (13) /* ams AG */
76#define RT_SENSOR_VENDOR_MAXIM (14) /* Maxim Integrated */
77#define RT_SENSOR_VENDOR_MELEXIS (15) /* Melexis */
78
79/* Sensor unit types */
80
81#define RT_SENSOR_UNIT_NONE (0)
82#define RT_SENSOR_UNIT_MG (1) /* Accelerometer unit: mG */
83#define RT_SENSOR_UNIT_MDPS (2) /* Gyroscope unit: mdps */
84#define RT_SENSOR_UNIT_MGAUSS (3) /* Magnetometer unit: mGauss */
85#define RT_SENSOR_UNIT_LUX (4) /* Ambient light unit: lux */
86#define RT_SENSOR_UNIT_CM (5) /* Distance unit: cm */
87#define RT_SENSOR_UNIT_PA (6) /* Barometer unit: pa */
88#define RT_SENSOR_UNIT_PERMILLAGE (7) /* Relative Humidity unit: permillage */
89#define RT_SENSOR_UNIT_DCELSIUS (8) /* Temperature unit: dCelsius */
90#define RT_SENSOR_UNIT_HZ (9) /* Frequency unit: HZ */
91#define RT_SENSOR_UNIT_ONE (10) /* Dimensionless quantity unit: 1 */
92#define RT_SENSOR_UNIT_BPM (11) /* Heart rate unit: bpm */
93#define RT_SENSOR_UNIT_MM (12) /* Distance unit: mm */
94#define RT_SENSOR_UNIT_MN (13) /* Force unit: mN */
95#define RT_SENSOR_UNIT_PPM (14) /* Concentration unit: ppm */
96#define RT_SENSOR_UNIT_PPB (15) /* Concentration unit: ppb */
97#define RT_SENSOR_UNIT_DMS (16) /* Coordinates unit: DMS */
98#define RT_SENSOR_UNIT_DD (17) /* Coordinates unit: DD */
99#define RT_SENSOR_UNIT_MGM3 (18) /* Concentration unit: mg/m3 */
100#define RT_SENSOR_UNIT_MMHG (19) /* Blood Pressure unit: mmHg */
101#define RT_SENSOR_UNIT_MV (20) /* Voltage unit: mV */
102#define RT_SENSOR_UNIT_MA (21) /* Current unit: mA */
103#define RT_SENSOR_UNIT_MW (22) /* Power unit: mW */
104/* Sensor communication interface types */
105
106#define RT_SENSOR_INTF_I2C (1 << 0)
107#define RT_SENSOR_INTF_SPI (1 << 1)
108#define RT_SENSOR_INTF_UART (1 << 2)
109#define RT_SENSOR_INTF_ONEWIRE (1 << 3)
110
111/* Sensor power mode types */
112
113#define RT_SENSOR_POWER_NONE (0)
114#define RT_SENSOR_POWER_DOWN (1) /* power down mode */
115#define RT_SENSOR_POWER_NORMAL (2) /* normal-power mode */
116#define RT_SENSOR_POWER_LOW (3) /* low-power mode */
117#define RT_SENSOR_POWER_HIGH (4) /* high-power mode */
118
119/* Sensor work mode types */
120
121#define RT_SENSOR_MODE_NONE (0)
122#define RT_SENSOR_MODE_POLLING (1) /* One shot only read a data */
123#define RT_SENSOR_MODE_INT (2) /* TODO: One shot interrupt only read a data */
124#define RT_SENSOR_MODE_FIFO (3) /* TODO: One shot interrupt read all fifo data */
125
126/* Sensor control cmd types */
127
128#define RT_SENSOR_CTRL_GET_ID (RT_DEVICE_CTRL_BASE(Sensor) + 0) /* Get device id */
129#define RT_SENSOR_CTRL_GET_INFO (RT_DEVICE_CTRL_BASE(Sensor) + 1) /* Get sensor info */
130#define RT_SENSOR_CTRL_SET_RANGE (RT_DEVICE_CTRL_BASE(Sensor) + 2) /* Set the measure range of sensor. unit is info of sensor */
131#define RT_SENSOR_CTRL_SET_ODR (RT_DEVICE_CTRL_BASE(Sensor) + 3) /* Set output date rate. unit is HZ */
132#define RT_SENSOR_CTRL_SET_MODE (RT_DEVICE_CTRL_BASE(Sensor) + 4) /* Set sensor's work mode. ex. RT_SENSOR_MODE_POLLING,RT_SENSOR_MODE_INT */
133#define RT_SENSOR_CTRL_SET_POWER (RT_DEVICE_CTRL_BASE(Sensor) + 5) /* Set power mode. args type of sensor power mode. ex. RT_SENSOR_POWER_DOWN,RT_SENSOR_POWER_NORMAL */
134#define RT_SENSOR_CTRL_SELF_TEST (RT_DEVICE_CTRL_BASE(Sensor) + 6) /* Take a self test */
135
136#define RT_SENSOR_CTRL_USER_CMD_START 0x100 /* User commands should be greater than 0x100 */
137
139{
140 rt_uint8_t type; /* The sensor type */
141 rt_uint8_t vendor; /* Vendor of sensors */
142 const char *model; /* model name of sensor */
143 rt_uint8_t unit; /* unit of measurement */
144 rt_uint8_t intf_type; /* Communication interface type */
145 rt_int32_t range_max; /* maximum range of this sensor's value. unit is 'unit' */
146 rt_int32_t range_min; /* minimum range of this sensor's value. unit is 'unit' */
147 rt_uint32_t period_min; /* Minimum measurement period,unit:ms. zero = not a constant rate */
149};
150
152{
153 char *dev_name; /* The name of the communication device */
154 rt_uint8_t type; /* Communication interface type */
155 void *user_data; /* Private data for the sensor. ex. i2c addr,spi cs,control I/O */
156};
157
159{
160 struct rt_sensor_intf intf; /* sensor interface config */
161 struct rt_device_pin_mode irq_pin; /* Interrupt pin, The purpose of this pin is to notification read data */
162 rt_uint8_t mode; /* sensor work mode */
163 rt_uint8_t power; /* sensor power mode */
164 rt_uint16_t odr; /* sensor out data rate */
165 rt_int32_t range; /* sensor range of measurement */
166};
167
169
171{
172 struct rt_device parent; /* The standard device */
173
174 struct rt_sensor_info info; /* The sensor info data */
175 struct rt_sensor_config config; /* The sensor config data */
176
177 void *data_buf; /* The buf of the data received */
178 rt_size_t data_len; /* The size of the data received */
179
180 const struct rt_sensor_ops *ops; /* The sensor ops */
181
182 struct rt_sensor_module *module; /* The sensor module */
183
184 rt_err_t (*irq_handle)(rt_sensor_t sensor); /* Called when an interrupt is generated, registered by the driver */
185};
186
188{
189 rt_mutex_t lock; /* The module lock */
190
191 rt_sensor_t sen[RT_SENSOR_MODULE_MAX]; /* The module contains a list of sensors */
192 rt_uint8_t sen_num; /* Number of sensors contained in the module */
193};
194
195/* 3-axis Data Type */
202
203/* Blood Pressure Data Type */
205{
206 rt_int32_t sbp; /* SBP : systolic pressure */
207 rt_int32_t dbp; /* DBP : diastolic pressure */
208};
209
211{
212 double longitude;
213 double latitude;
214};
215
217{
218 rt_uint32_t timestamp; /* The timestamp when the data was received */
219 rt_uint8_t type; /* The sensor type of the data */
220 union
221 {
222 struct sensor_3_axis acce; /* Accelerometer. unit: mG */
223 struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */
224 struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */
225 struct coordinates coord; /* Coordinates unit: degrees */
226 rt_int32_t temp; /* Temperature. unit: dCelsius */
227 rt_int32_t humi; /* Relative humidity. unit: permillage */
228 rt_int32_t baro; /* Pressure. unit: pascal (Pa) */
229 rt_int32_t light; /* Light. unit: lux */
230 rt_int32_t proximity; /* Distance. unit: centimeters */
231 rt_int32_t hr; /* Heart rate. unit: bpm */
232 rt_int32_t tvoc; /* TVOC. unit: permillage */
233 rt_int32_t noise; /* Noise Loudness. unit: HZ */
234 rt_uint32_t step; /* Step sensor. unit: 1 */
235 rt_int32_t force; /* Force sensor. unit: mN */
236 rt_uint32_t dust; /* Dust sensor. unit: ug/m3 */
237 rt_uint32_t eco2; /* eCO2 sensor. unit: ppm */
238 rt_uint32_t spo2; /* SpO2 sensor. unit: permillage */
239 rt_uint32_t iaq; /* IAQ sensor. unit: 1 */
240 rt_uint32_t etoh; /* EtOH sensor. unit: ppm */
241 struct sensor_bp bp; /* BloodPressure. unit: mmHg */
242 float mv; /* Voltage sensor. unit: mv */
243 float ma; /* Current sensor. unit: ma */
244 float mw; /* Power sensor. unit: mw */
246};
247
249{
250 rt_ssize_t (*fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len);
251 rt_err_t (*control)(struct rt_sensor_device *sensor, int cmd, void *arg);
252};
253
255 const char *name,
256 rt_uint32_t flag,
257 void *data);
258
259#ifdef __cplusplus
260}
261#endif
262
263#endif /* __SENSOR_H__ */
struct rt_mutex * rt_mutex_t
rt_base_t rt_ssize_t
rt_base_t rt_err_t
unsigned char rt_uint8_t
unsigned short rt_uint16_t
rt_ubase_t rt_size_t
unsigned int rt_uint32_t
signed int rt_int32_t
int rt_hw_sensor_register(rt_sensor_t sensor, const char *name, rt_uint32_t flag, void *data)
struct rt_sensor_device * rt_sensor_t
#define RT_SENSOR_MODULE_MAX
struct rt_sensor_device * rt_sensor_t
double latitude
double longitude
pin mode structure
struct rt_sensor_intf intf
rt_uint16_t odr
struct rt_device_pin_mode irq_pin
rt_int32_t baro
struct sensor_3_axis mag
rt_int32_t hr
struct coordinates coord
union rt_sensor_data::@022165123134134131071120365022351202312371364346 data
rt_uint32_t spo2
rt_uint32_t eco2
rt_uint8_t type
struct sensor_bp bp
rt_uint32_t timestamp
struct sensor_3_axis gyro
rt_uint32_t iaq
rt_int32_t tvoc
rt_uint32_t step
rt_int32_t force
rt_int32_t noise
struct sensor_3_axis acce
rt_int32_t humi
rt_uint32_t etoh
rt_int32_t light
rt_int32_t proximity
rt_uint32_t dust
rt_int32_t temp
struct rt_device parent
struct rt_sensor_module * module
const struct rt_sensor_ops * ops
struct rt_sensor_info info
struct rt_sensor_config config
rt_err_t(* irq_handle)(rt_sensor_t sensor)
rt_int32_t range_min
rt_uint32_t period_min
const char * model
rt_uint8_t vendor
rt_uint8_t fifo_max
rt_uint8_t type
rt_uint8_t unit
rt_int32_t range_max
rt_uint8_t intf_type
rt_uint8_t type
rt_sensor_t sen[RT_SENSOR_MODULE_MAX]
rt_uint8_t sen_num
rt_ssize_t(* fetch_data)(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
rt_err_t(* control)(struct rt_sensor_device *sensor, int cmd, void *arg)
rt_int32_t x
rt_int32_t y
rt_int32_t z
rt_int32_t dbp
rt_int32_t sbp