sensor.h 9.9 KB
Newer Older
B
Bernard Xiong 已提交
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
B
Bernard Xiong 已提交
5 6 7
 *
 * Change Logs:
 * Date           Author       Notes
G
guozhanxin 已提交
8
 * 2019-01-31     flybreak     first version
B
Bernard Xiong 已提交
9
 */
G
guozhanxin 已提交
10

G
guozhanxin 已提交
11 12
#ifndef __SENSOR_H__
#define __SENSOR_H__
B
Bernard Xiong 已提交
13

G
guozhanxin 已提交
14
#include <rtthread.h>
B
Bernard Xiong 已提交
15 16
#include <rtdevice.h>

G
guozhanxin 已提交
17 18 19 20
#ifdef __cplusplus
extern "C" {
#endif

G
guozhanxin 已提交
21
#ifdef RT_USING_RTC
G
guozhanxin 已提交
22
#define  rt_sensor_get_ts()  time(RT_NULL)   /* API for the sensor to get the timestamp */
G
guozhanxin 已提交
23
#else
G
guozhanxin 已提交
24
#define  rt_sensor_get_ts()  rt_tick_get()   /* API for the sensor to get the timestamp */
B
bernard 已提交
25
#endif
26

G
guozhanxin 已提交
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 */
G
guozhanxin 已提交
29

G
guozhanxin 已提交
30
#define  RT_SENSOR_MODULE_MAX          (3)       /* The maximum number of members of a sensor module */
G
guozhanxin 已提交
31 32 33

/* Sensor types */

G
guozhanxin 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
#define RT_SENSOR_CLASS_NONE           (0)
#define RT_SENSOR_CLASS_ACCE           (1)  /* Accelerometer     */
#define RT_SENSOR_CLASS_GYRO           (2)  /* Gyroscope         */
#define RT_SENSOR_CLASS_MAG            (3)  /* Magnetometer      */
#define RT_SENSOR_CLASS_TEMP           (4)  /* Temperature       */
#define RT_SENSOR_CLASS_HUMI           (5)  /* Relative Humidity */
#define RT_SENSOR_CLASS_BARO           (6)  /* Barometer         */
#define RT_SENSOR_CLASS_LIGHT          (7)  /* Ambient light     */
#define RT_SENSOR_CLASS_PROXIMITY      (8)  /* Proximity         */
#define RT_SENSOR_CLASS_HR             (9)  /* Heart Rate        */
#define RT_SENSOR_CLASS_TVOC           (10) /* TVOC Level        */
#define RT_SENSOR_CLASS_NOISE          (11) /* Noise Loudness    */
#define RT_SENSOR_CLASS_STEP           (12) /* Step sensor       */
47
#define RT_SENSOR_CLASS_FORCE          (13) /* Force sensor      */
48 49
#define RT_SENSOR_CLASS_DUST           (14) /* Dust sensor       */
#define RT_SENSOR_CLASS_ECO2           (15) /* eCO2 sensor       */
G
guozhanxin 已提交
50

G
guozhanxin 已提交
51
/* Sensor vendor types */
G
guozhanxin 已提交
52

G
guozhanxin 已提交
53 54 55 56 57 58
#define RT_SENSOR_VENDOR_UNKNOWN       (0)
#define RT_SENSOR_VENDOR_STM           (1)  /* STMicroelectronics */
#define RT_SENSOR_VENDOR_BOSCH         (2)  /* Bosch */
#define RT_SENSOR_VENDOR_INVENSENSE    (3)  /* Invensense */
#define RT_SENSOR_VENDOR_SEMTECH       (4)  /* Semtech */
#define RT_SENSOR_VENDOR_GOERTEK       (5)  /* Goertek */
59
#define RT_SENSOR_VENDOR_MIRAMEMS      (6)  /* MiraMEMS */
60
#define RT_SENSOR_VENDOR_DALLAS        (7)  /* Dallas */
61 62 63
#define RT_SENSOR_VENDOR_AOSONG        (8)  /* Aosong */
#define RT_SENSOR_VENDOR_SHARP         (9)  /* Sharp */
#define RT_SENSOR_VENDOR_SENSIRION     (10) /* Sensirion */
G
guozhanxin 已提交
64

G
guozhanxin 已提交
65
/* Sensor unit types */
G
guozhanxin 已提交
66

G
guozhanxin 已提交
67 68 69 70 71 72 73 74 75 76 77
#define  RT_SENSOR_UNIT_NONE           (0)
#define  RT_SENSOR_UNIT_MG             (1)  /* Accelerometer           unit: mG         */
#define  RT_SENSOR_UNIT_MDPS           (2)  /* Gyroscope               unit: mdps       */
#define  RT_SENSOR_UNIT_MGAUSS         (3)  /* Magnetometer            unit: mGauss     */
#define  RT_SENSOR_UNIT_LUX            (4)  /* Ambient light           unit: lux        */
#define  RT_SENSOR_UNIT_CM             (5)  /* Distance                unit: cm         */
#define  RT_SENSOR_UNIT_PA             (6)  /* Barometer               unit: pa         */
#define  RT_SENSOR_UNIT_PERMILLAGE     (7)  /* Relative Humidity       unit: permillage */
#define  RT_SENSOR_UNIT_DCELSIUS       (8)  /* Temperature             unit: dCelsius   */
#define  RT_SENSOR_UNIT_HZ             (9)  /* Frequency               unit: HZ         */
#define  RT_SENSOR_UNIT_ONE            (10) /* Dimensionless quantity  unit: 1          */
78
#define  RT_SENSOR_UNIT_BPM            (11) /* Heart rate              unit: bpm        */
79
#define  RT_SENSOR_UNIT_MM             (12) /* Distance                unit: mm         */
80
#define  RT_SENSOR_UNIT_MN             (13) /* Force                   unit: mN         */
G
guozhanxin 已提交
81 82 83

/* Sensor communication interface types */

G
guozhanxin 已提交
84 85 86 87
#define  RT_SENSOR_INTF_I2C            (1 << 0)
#define  RT_SENSOR_INTF_SPI            (1 << 1)
#define  RT_SENSOR_INTF_UART           (1 << 2)
#define  RT_SENSOR_INTF_ONEWIRE        (1 << 3)
G
guozhanxin 已提交
88

G
guozhanxin 已提交
89
/* Sensor power mode types */
G
guozhanxin 已提交
90

G
guozhanxin 已提交
91 92 93 94 95
#define  RT_SENSOR_POWER_NONE          (0)
#define  RT_SENSOR_POWER_DOWN          (1)  /* power down mode   */
#define  RT_SENSOR_POWER_NORMAL        (2)  /* normal-power mode */
#define  RT_SENSOR_POWER_LOW           (3)  /* low-power mode    */
#define  RT_SENSOR_POWER_HIGH          (4)  /* high-power mode   */
G
guozhanxin 已提交
96 97 98

/* Sensor work mode types */

G
guozhanxin 已提交
99 100 101 102
#define  RT_SENSOR_MODE_NONE           (0)
#define  RT_SENSOR_MODE_POLLING        (1)  /* One shot only read a data */
#define  RT_SENSOR_MODE_INT            (2)  /* TODO: One shot interrupt only read a data */
#define  RT_SENSOR_MODE_FIFO           (3)  /* TODO: One shot interrupt read all fifo data */
G
guozhanxin 已提交
103 104 105

/* Sensor control cmd types */

G
guozhanxin 已提交
106 107 108 109 110 111 112
#define  RT_SENSOR_CTRL_GET_ID         (0)  /* Get device id */
#define  RT_SENSOR_CTRL_GET_INFO       (1)  /* Get sensor info */
#define  RT_SENSOR_CTRL_SET_RANGE      (2)  /* Set the measure range of sensor. unit is info of sensor */
#define  RT_SENSOR_CTRL_SET_ODR        (3)  /* Set output date rate. unit is HZ */
#define  RT_SENSOR_CTRL_SET_MODE       (4)  /* Set sensor's work mode. ex. RT_SENSOR_MODE_POLLING,RT_SENSOR_MODE_INT */
#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 */
#define  RT_SENSOR_CTRL_SELF_TEST      (6)  /* Take a self test */
G
guozhanxin 已提交
113 114

struct rt_sensor_info
B
Bernard Xiong 已提交
115
{
G
guozhanxin 已提交
116 117 118 119 120 121 122 123 124
    rt_uint8_t     type;                    /* The sensor type */
    rt_uint8_t     vendor;                  /* Vendor of sensors */
    const char    *model;                   /* model name of sensor */
    rt_uint8_t     unit;                    /* unit of measurement */
    rt_uint8_t     intf_type;               /* Communication interface type */
    rt_int32_t     range_max;               /* maximum range of this sensor's value. unit is 'unit' */
    rt_int32_t     range_min;               /* minimum range of this sensor's value. unit is 'unit' */
    rt_uint32_t    period_min;              /* Minimum measurement period,unit:ms. zero = not a constant rate */
    rt_uint8_t     fifo_max;
B
Bernard Xiong 已提交
125 126
};

G
guozhanxin 已提交
127
struct rt_sensor_intf
B
Bernard Xiong 已提交
128
{
G
guozhanxin 已提交
129 130 131
    char                       *dev_name;   /* The name of the communication device */
    rt_uint8_t                  type;       /* Communication interface type */
    void                       *user_data;  /* Private data for the sensor. ex. i2c addr,spi cs,control I/O */
B
Bernard Xiong 已提交
132 133
};

G
guozhanxin 已提交
134
struct rt_sensor_config
B
Bernard Xiong 已提交
135
{
G
guozhanxin 已提交
136 137 138 139 140 141
    struct rt_sensor_intf        intf;      /* sensor interface config */
    struct rt_device_pin_mode    irq_pin;   /* Interrupt pin, The purpose of this pin is to notification read data */
    rt_uint8_t                   mode;      /* sensor work mode */
    rt_uint8_t                   power;     /* sensor power mode */
    rt_uint16_t                  odr;       /* sensor out data rate */
    rt_int32_t                   range;     /* sensor range of measurement */
142 143
};

144 145
typedef struct rt_sensor_device *rt_sensor_t;

G
guozhanxin 已提交
146
struct rt_sensor_device
147
{
G
guozhanxin 已提交
148
    struct rt_device             parent;    /* The standard device */
G
guozhanxin 已提交
149

G
guozhanxin 已提交
150 151 152 153 154
    struct rt_sensor_info        info;      /* The sensor info data */
    struct rt_sensor_config      config;    /* The sensor config data */

    void                        *data_buf;  /* The buf of the data received */
    rt_size_t                    data_len;  /* The size of the data received */
G
guozhanxin 已提交
155 156 157

    const struct rt_sensor_ops  *ops;       /* The sensor ops */

G
guozhanxin 已提交
158
    struct rt_sensor_module     *module;    /* The sensor module */
159 160
    
    rt_err_t (*irq_handle)(rt_sensor_t sensor);             /* Called when an interrupt is generated, registered by the driver */
161 162
};

G
guozhanxin 已提交
163
struct rt_sensor_module
164
{
G
guozhanxin 已提交
165
    rt_mutex_t                   lock;                      /* The module lock */
G
guozhanxin 已提交
166

G
guozhanxin 已提交
167 168
    rt_sensor_t                  sen[RT_SENSOR_MODULE_MAX]; /* The module contains a list of sensors */
    rt_uint8_t                   sen_num;                   /* Number of sensors contained in the module */
169 170
};

G
guozhanxin 已提交
171 172
/* 3-axis Data Type */
struct sensor_3_axis
B
Bernard Xiong 已提交
173
{
G
guozhanxin 已提交
174 175 176
    rt_int32_t x;
    rt_int32_t y;
    rt_int32_t z;
B
Bernard Xiong 已提交
177 178
};

G
guozhanxin 已提交
179
struct rt_sensor_data
180
{
G
guozhanxin 已提交
181 182 183
    rt_uint32_t         timestamp;          /* The timestamp when the data was received */
    rt_uint8_t          type;               /* The sensor type of the data */
    union
B
bernard 已提交
184
    {
G
guozhanxin 已提交
185 186 187 188 189 190 191 192
        struct sensor_3_axis acce;          /* Accelerometer.       unit: mG          */
        struct sensor_3_axis gyro;          /* Gyroscope.           unit: mdps        */
        struct sensor_3_axis mag;           /* Magnetometer.        unit: mGauss      */
        rt_int32_t           temp;          /* Temperature.         unit: dCelsius    */
        rt_int32_t           humi;          /* Relative humidity.   unit: permillage  */
        rt_int32_t           baro;          /* Pressure.            unit: pascal (Pa) */
        rt_int32_t           light;         /* Light.               unit: lux         */
        rt_int32_t           proximity;     /* Distance.            unit: centimeters */
193
        rt_int32_t           hr;            /* Heart rate.          unit: bpm         */
G
guozhanxin 已提交
194 195 196
        rt_int32_t           tvoc;          /* TVOC.                unit: permillage  */
        rt_int32_t           noise;         /* Noise Loudness.      unit: HZ          */
        rt_uint32_t          step;          /* Step sensor.         unit: 1           */
197
        rt_int32_t           force;         /* Force sensor.        unit: mN          */
198 199
        rt_int32_t           dust;          /* Dust sensor.         unit: ug/m3       */
        rt_int32_t           eco2;          /* eCO2 sensor.         unit: ppm         */
G
guozhanxin 已提交
200
    } data;
B
Bernard Xiong 已提交
201 202
};

G
guozhanxin 已提交
203
struct rt_sensor_ops
B
Bernard Xiong 已提交
204
{
G
guozhanxin 已提交
205 206
    rt_size_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);
B
Bernard Xiong 已提交
207
};
B
bernard 已提交
208

G
guozhanxin 已提交
209
int rt_hw_sensor_register(rt_sensor_t sensor,
G
guozhanxin 已提交
210 211 212
                          const char              *name,
                          rt_uint32_t              flag,
                          void                    *data);
G
guozhanxin 已提交
213 214 215

#ifdef __cplusplus
}
B
bernard 已提交
216
#endif
G
guozhanxin 已提交
217 218

#endif /* __SENSOR_H__ */