From 613f89a37275d9bc190d0d523379e8e97560a9bb Mon Sep 17 00:00:00 2001 From: xfwangqiang <164877907@qq.com> Date: Tue, 16 Mar 2021 01:15:58 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=A3=80=E6=B5=8Brt=5Fsensor=5Fdevice?= =?UTF-8?q?=E4=B8=8B=E7=9A=84ops=E5=92=8Cops=E4=B8=AD=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E6=8C=87=E9=92=88=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=9B=A0?= =?UTF-8?q?=E6=9C=AA=E5=AE=9A=E4=B9=89ops=E4=B8=AD=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E8=80=8C=E9=80=A0=E6=88=90=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/sensors/sensor.c | 90 +++++++++++++++++++---------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/components/drivers/sensors/sensor.c b/components/drivers/sensors/sensor.c index ccc1fca34e..345edbde89 100644 --- a/components/drivers/sensors/sensor.c +++ b/components/drivers/sensors/sensor.c @@ -147,7 +147,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY) { - if (sensor->ops->control != RT_NULL) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) { /* If polling mode is supported, configure it to polling mode */ sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING); @@ -156,7 +156,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) } else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX) { - if (sensor->ops->control != RT_NULL) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) { /* If interrupt mode is supported, configure it to interrupt mode */ sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT); @@ -167,7 +167,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) } else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX) { - if (sensor->ops->control != RT_NULL) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) { /* If fifo mode is supported, configure it to fifo mode */ sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO); @@ -183,7 +183,14 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) } /* Configure power mode to normal mode */ - if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + { + if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK) + { + sensor->config.power = RT_SENSOR_POWER_NORMAL; + } + } + else { sensor->config.power = RT_SENSOR_POWER_NORMAL; } @@ -211,7 +218,14 @@ static rt_err_t rt_sensor_close(rt_device_t dev) } /* Configure power mode to power down mode */ - if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + { + if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK) + { + sensor->config.power = RT_SENSOR_POWER_DOWN; + } + } + else { sensor->config.power = RT_SENSOR_POWER_DOWN; } @@ -279,7 +293,7 @@ static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_siz sensor->data_len = 0; result = len; } - else + else if ((sensor->ops != RT_NULL) && (sensor->ops->fetch_data != RT_NULL)) { /* If the buffer is empty read the data */ result = sensor->ops->fetch_data(sensor, buf, len); @@ -309,7 +323,10 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args) case RT_SENSOR_CTRL_GET_ID: if (args) { - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args); + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + { + result = sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args); + } } break; case RT_SENSOR_CTRL_GET_INFO: @@ -319,46 +336,57 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args) } break; case RT_SENSOR_CTRL_SET_RANGE: - - /* Configuration measurement range */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_RANGE, args); - if (result == RT_EOK) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) { - sensor->config.range = (rt_int32_t)args; - LOG_D("set range %d", sensor->config.range); - } + /* Configuration measurement range */ + result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_RANGE, args); + if (result == RT_EOK) + { + sensor->config.range = (rt_int32_t)args; + LOG_D("set range %d", sensor->config.range); + } + } break; case RT_SENSOR_CTRL_SET_ODR: - - /* Configuration data output rate */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_ODR, args); - if (result == RT_EOK) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) { - sensor->config.odr = (rt_uint32_t)args & 0xFFFF; - LOG_D("set odr %d", sensor->config.odr); + /* Configuration data output rate */ + result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_ODR, args); + if (result == RT_EOK) + { + sensor->config.odr = (rt_uint32_t)args & 0xFFFF; + LOG_D("set odr %d", sensor->config.odr); + } } break; case RT_SENSOR_CTRL_SET_POWER: - - /* Configuration sensor power mode */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, args); - if (result == RT_EOK) + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) { - sensor->config.power = (rt_uint32_t)args & 0xFF; - LOG_D("set power mode code:", sensor->config.power); + /* Configuration sensor power mode */ + result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, args); + if (result == RT_EOK) + { + sensor->config.power = (rt_uint32_t)args & 0xFF; + LOG_D("set power mode code:", sensor->config.power); + } } break; case RT_SENSOR_CTRL_SELF_TEST: - - /* Device self-test */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SELF_TEST, args); + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + { + /* Device self-test */ + result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SELF_TEST, args); + } break; default: if (cmd > RT_SENSOR_CTRL_USER_CMD_START) { - /* Custom commands */ - result = sensor->ops->control(sensor, cmd, args); + if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + { + /* Custom commands */ + result = sensor->ops->control(sensor, cmd, args); + } } else { From 58bb9f5b2e152168427025040f3c6c498ce564a5 Mon Sep 17 00:00:00 2001 From: wangqiang Date: Wed, 17 Mar 2021 13:40:44 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BC=98=E5=8C=96ops=E7=9A=84=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/sensors/sensor.c | 154 ++++++++++++++-------------- 1 file changed, 79 insertions(+), 75 deletions(-) diff --git a/components/drivers/sensors/sensor.c b/components/drivers/sensors/sensor.c index 345edbde89..2a1f3a19af 100644 --- a/components/drivers/sensors/sensor.c +++ b/components/drivers/sensors/sensor.c @@ -121,12 +121,30 @@ static rt_err_t rt_sensor_irq_init(rt_sensor_t sensor) return 0; } +// local rt_sensor_ops + +static rt_size_t local_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len) +{ + LOG_D("Undefined fetch_data"); + return 0; +} +static rt_err_t local_control(struct rt_sensor_device *sensor, int cmd, void *arg) +{ + LOG_D("Undefined control"); + return RT_ERROR; +} +static struct rt_sensor_ops local_ops = { + .fetch_data = local_fetch_data, + .control = local_control +}; + /* RT-Thread Device Interface */ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) { rt_sensor_t sensor = (rt_sensor_t)dev; RT_ASSERT(dev != RT_NULL); rt_err_t res = RT_EOK; + rt_err_t (*local_ctrl)(struct rt_sensor_device *sensor, int cmd, void *arg) = local_control; if (sensor->module) { @@ -144,37 +162,36 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) goto __exit; } } + if (sensor->ops->control != RT_NULL) + { + local_ctrl = sensor->ops->control; + } + sensor->config.mode = RT_SENSOR_MODE_POLLING; if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY) { - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) - { - /* If polling mode is supported, configure it to polling mode */ - sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING); - } - sensor->config.mode = RT_SENSOR_MODE_POLLING; + /* If polling mode is supported, configure it to polling mode */ + local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING); } else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX) { - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + /* If interrupt mode is supported, configure it to interrupt mode */ + if (RT_EOK== local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT)) { - /* If interrupt mode is supported, configure it to interrupt mode */ - sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT); + /* Initialization sensor interrupt */ + rt_sensor_irq_init(sensor); + sensor->config.mode = RT_SENSOR_MODE_INT; } - /* Initialization sensor interrupt */ - rt_sensor_irq_init(sensor); - sensor->config.mode = RT_SENSOR_MODE_INT; } else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX) { - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + /* If fifo mode is supported, configure it to fifo mode */ + if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO)) { - /* If fifo mode is supported, configure it to fifo mode */ - sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO); + /* Initialization sensor interrupt */ + rt_sensor_irq_init(sensor); + sensor->config.mode = RT_SENSOR_MODE_FIFO; } - /* Initialization sensor interrupt */ - rt_sensor_irq_init(sensor); - sensor->config.mode = RT_SENSOR_MODE_FIFO; } else { @@ -183,14 +200,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) } /* Configure power mode to normal mode */ - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) - { - if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK) - { - sensor->config.power = RT_SENSOR_POWER_NORMAL; - } - } - else + if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL)) { sensor->config.power = RT_SENSOR_POWER_NORMAL; } @@ -209,6 +219,7 @@ static rt_err_t rt_sensor_close(rt_device_t dev) { rt_sensor_t sensor = (rt_sensor_t)dev; int i; + rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control; RT_ASSERT(dev != RT_NULL); @@ -216,16 +227,13 @@ static rt_err_t rt_sensor_close(rt_device_t dev) { rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER); } + if (sensor->ops->control != RT_NULL) + { + local_ctrl = sensor->ops->control; + } /* Configure power mode to power down mode */ - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) - { - if (sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK) - { - sensor->config.power = RT_SENSOR_POWER_DOWN; - } - } - else + if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN)) { sensor->config.power = RT_SENSOR_POWER_DOWN; } @@ -293,10 +301,13 @@ static rt_size_t rt_sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_siz sensor->data_len = 0; result = len; } - else if ((sensor->ops != RT_NULL) && (sensor->ops->fetch_data != RT_NULL)) + else { /* If the buffer is empty read the data */ - result = sensor->ops->fetch_data(sensor, buf, len); + if (sensor->ops->fetch_data != RT_NULL) + { + result = sensor->ops->fetch_data(sensor, buf, len); + } } if (sensor->module) @@ -312,21 +323,23 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args) rt_sensor_t sensor = (rt_sensor_t)dev; rt_err_t result = RT_EOK; RT_ASSERT(dev != RT_NULL); + rt_err_t (*local_ctrl)(struct rt_sensor_device * sensor, int cmd, void *arg) = local_control; if (sensor->module) { rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER); } + if (sensor->ops->control != RT_NULL) + { + local_ctrl = sensor->ops->control; + } switch (cmd) { case RT_SENSOR_CTRL_GET_ID: if (args) { - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) - { - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_GET_ID, args); - } + result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args); } break; case RT_SENSOR_CTRL_GET_INFO: @@ -336,57 +349,42 @@ static rt_err_t rt_sensor_control(rt_device_t dev, int cmd, void *args) } break; case RT_SENSOR_CTRL_SET_RANGE: - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + /* Configuration measurement range */ + result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_RANGE, args); + if (result == RT_EOK) { - /* Configuration measurement range */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_RANGE, args); - if (result == RT_EOK) - { - sensor->config.range = (rt_int32_t)args; - LOG_D("set range %d", sensor->config.range); - } - } + sensor->config.range = (rt_int32_t)args; + LOG_D("set range %d", sensor->config.range); + } break; case RT_SENSOR_CTRL_SET_ODR: - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + /* Configuration data output rate */ + result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ODR, args); + if (result == RT_EOK) { - /* Configuration data output rate */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_ODR, args); - if (result == RT_EOK) - { - sensor->config.odr = (rt_uint32_t)args & 0xFFFF; - LOG_D("set odr %d", sensor->config.odr); - } + sensor->config.odr = (rt_uint32_t)args & 0xFFFF; + LOG_D("set odr %d", sensor->config.odr); } break; case RT_SENSOR_CTRL_SET_POWER: - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) + /* Configuration sensor power mode */ + result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, args); + if (result == RT_EOK) { - /* Configuration sensor power mode */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SET_POWER, args); - if (result == RT_EOK) - { - sensor->config.power = (rt_uint32_t)args & 0xFF; - LOG_D("set power mode code:", sensor->config.power); - } + sensor->config.power = (rt_uint32_t)args & 0xFF; + LOG_D("set power mode code:", sensor->config.power); } break; case RT_SENSOR_CTRL_SELF_TEST: - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) - { - /* Device self-test */ - result = sensor->ops->control(sensor, RT_SENSOR_CTRL_SELF_TEST, args); - } + /* Device self-test */ + result = local_ctrl(sensor, RT_SENSOR_CTRL_SELF_TEST, args); break; default: if (cmd > RT_SENSOR_CTRL_USER_CMD_START) { - if ((sensor->ops != RT_NULL) && (sensor->ops->control != RT_NULL)) - { - /* Custom commands */ - result = sensor->ops->control(sensor, cmd, args); - } + /* Custom commands */ + result = local_ctrl(sensor, cmd, args); } else { @@ -415,6 +413,7 @@ const static struct rt_device_ops rt_sensor_ops = }; #endif + /* * sensor register */ @@ -429,6 +428,11 @@ int rt_hw_sensor_register(rt_sensor_t sensor, char *sensor_name = RT_NULL, *device_name = RT_NULL; + if (sensor->ops == RT_NULL) + { + sensor->ops = &local_ops; + } + /* Add a type name for the sensor device */ sensor_name = sensor_name_str[sensor->info.type]; device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name)); From da0e3ed35db999bfcd89e0fedc8e2039ed9339be Mon Sep 17 00:00:00 2001 From: wangqiang Date: Wed, 17 Mar 2021 15:20:54 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=B0=86sensor.h=E7=9A=84=E5=A4=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=94=BE=E7=BD=AE=E4=BA=86=E4=B8=80=E4=BB=BD?= =?UTF-8?q?=E5=9C=A8driver/include=E4=B8=8B=E9=9D=A2=EF=BC=8Crtdevice.h?= =?UTF-8?q?=E7=9A=84=E5=A4=B4=E6=96=87=E4=BB=B6=E4=B9=9F=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E4=BA=86sensor.h=EF=BC=8C=E7=AE=80=E5=8C=96=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=B1=82=E8=B0=83=E7=94=A8sensor=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/sensor.h | 238 ++++++++++++++++++++ components/drivers/include/rtdevice.h | 39 ++-- 2 files changed, 260 insertions(+), 17 deletions(-) create mode 100644 components/drivers/include/drivers/sensor.h diff --git a/components/drivers/include/drivers/sensor.h b/components/drivers/include/drivers/sensor.h new file mode 100644 index 0000000000..f54d4e8799 --- /dev/null +++ b/components/drivers/include/drivers/sensor.h @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2019-01-31 flybreak first version + */ + +#ifndef __SENSOR_H__ +#define __SENSOR_H__ + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef RT_USING_RTC +#define rt_sensor_get_ts() time(RT_NULL) /* API for the sensor to get the timestamp */ +#else +#define rt_sensor_get_ts() rt_tick_get() /* API for the sensor to get the timestamp */ +#endif + +#define RT_PIN_NONE 0xFFFF /* RT PIN NONE */ +#define RT_DEVICE_FLAG_FIFO_RX 0x200 /* Flag to use when the sensor is open by fifo mode */ + +#define RT_SENSOR_MODULE_MAX (3) /* The maximum number of members of a sensor module */ + +/* Sensor types */ + +#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 */ +#define RT_SENSOR_CLASS_FORCE (13) /* Force sensor */ +#define RT_SENSOR_CLASS_DUST (14) /* Dust sensor */ +#define RT_SENSOR_CLASS_ECO2 (15) /* eCO2 sensor */ +#define RT_SENSOR_CLASS_GNSS (16) /* GPS/GNSS sensor */ +#define RT_SENSOR_CLASS_TOF (17) /* TOF sensor */ + +/* Sensor vendor types */ + +#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 */ +#define RT_SENSOR_VENDOR_MIRAMEMS (6) /* MiraMEMS */ +#define RT_SENSOR_VENDOR_DALLAS (7) /* Dallas */ +#define RT_SENSOR_VENDOR_ASAIR (8) /* Aosong */ +#define RT_SENSOR_VENDOR_SHARP (9) /* Sharp */ +#define RT_SENSOR_VENDOR_SENSIRION (10) /* Sensirion */ +#define RT_SENSOR_VENDOR_TI (11) /* Texas Instruments */ +#define RT_SENSOR_VENDOR_PLANTOWER (12) /* Plantower */ +#define RT_SENSOR_VENDOR_AMS (13) /* ams AG */ +#define RT_SENSOR_VENDOR_MAXIM (14) /* Maxim Integrated */ + + +/* Sensor unit types */ + +#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 */ +#define RT_SENSOR_UNIT_BPM (11) /* Heart rate unit: bpm */ +#define RT_SENSOR_UNIT_MM (12) /* Distance unit: mm */ +#define RT_SENSOR_UNIT_MN (13) /* Force unit: mN */ +#define RT_SENSOR_UNIT_PPM (14) /* Concentration unit: ppm */ +#define RT_SENSOR_UNIT_PPB (15) /* Concentration unit: ppb */ +#define RT_SENSOR_UNIT_DMS (16) /* Coordinates unit: DMS */ +#define RT_SENSOR_UNIT_DD (17) /* Coordinates unit: DD */ + +/* Sensor communication interface types */ + +#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) + +/* Sensor power mode types */ + +#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 */ + +/* Sensor work mode types */ + +#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 */ + +/* Sensor control cmd types */ + +#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 */ + +#define RT_SENSOR_CTRL_USER_CMD_START 0x100 /* User commands should be greater than 0x100 */ + +struct rt_sensor_info +{ + 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; +}; + +struct rt_sensor_intf +{ + 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 */ +}; + +struct rt_sensor_config +{ + 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 */ +}; + +typedef struct rt_sensor_device *rt_sensor_t; + +struct rt_sensor_device +{ + struct rt_device parent; /* The standard device */ + + 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 */ + + const struct rt_sensor_ops *ops; /* The sensor ops */ + + struct rt_sensor_module *module; /* The sensor module */ + + rt_err_t (*irq_handle)(rt_sensor_t sensor); /* Called when an interrupt is generated, registered by the driver */ +}; + +struct rt_sensor_module +{ + rt_mutex_t lock; /* The module lock */ + + 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 */ +}; + +/* 3-axis Data Type */ +struct sensor_3_axis +{ + rt_int32_t x; + rt_int32_t y; + rt_int32_t z; +}; + +struct coordinates +{ + double longitude; + double latitude; +}; + +struct rt_sensor_data +{ + rt_uint32_t timestamp; /* The timestamp when the data was received */ + rt_uint8_t type; /* The sensor type of the data */ + union + { + struct sensor_3_axis acce; /* Accelerometer. unit: mG */ + struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */ + struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */ + struct coordinates coord; /* Coordinates unit: degrees */ + 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 */ + rt_int32_t hr; /* Heart rate. unit: bpm */ + rt_int32_t tvoc; /* TVOC. unit: permillage */ + rt_int32_t noise; /* Noise Loudness. unit: HZ */ + rt_uint32_t step; /* Step sensor. unit: 1 */ + rt_int32_t force; /* Force sensor. unit: mN */ + rt_uint32_t dust; /* Dust sensor. unit: ug/m3 */ + rt_uint32_t eco2; /* eCO2 sensor. unit: ppm */ + } data; +}; + +struct rt_sensor_ops +{ + 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); +}; + +int rt_hw_sensor_register(rt_sensor_t sensor, + const char *name, + rt_uint32_t flag, + void *data); + +#ifdef __cplusplus +} +#endif + +#endif /* __SENSOR_H__ */ diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index 82868e5aef..aeb3fa5223 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -78,70 +78,75 @@ extern "C" { #include "drivers/mmcsd_core.h" #include "drivers/sd.h" #include "drivers/sdio.h" -#endif +#endif /* RT_USING_SDIO */ + +#ifdef RT_USING_SENSOR +#include "drivers/sensor.h" +#endif /* RT_USING_SENSOR */ #ifdef RT_USING_WDT #include "drivers/watchdog.h" -#endif +#endif /* RT_USING_WDT */ #ifdef RT_USING_PIN #include "drivers/pin.h" -#endif +#endif /* RT_USING_PIN */ #ifdef RT_USING_CAN #include "drivers/can.h" -#endif +#endif /* RT_USING_CAN */ #ifdef RT_USING_HWTIMER #include "drivers/hwtimer.h" -#endif +#endif /* RT_USING_HWTIMER */ #ifdef RT_USING_AUDIO #include "drivers/audio.h" -#endif +#endif /* RT_USING_AUDIO */ #ifdef RT_USING_CPUTIME #include "drivers/cputime.h" -#endif +#endif /* RT_USING_CPUTIME */ #ifdef RT_USING_ADC #include "drivers/adc.h" -#endif +#endif /* RT_USING_ADC */ #ifdef RT_USING_DAC #include "drivers/dac.h" -#endif +#endif /* RT_USING_DAC */ #ifdef RT_USING_PWM #include "drivers/rt_drv_pwm.h" -#endif +#endif /* RT_USING_PWM */ #ifdef RT_USING_PM #include "drivers/pm.h" -#endif +#endif /* RT_USING_PM */ #ifdef RT_USING_WIFI #include "drivers/wlan.h" -#endif +#endif /* RT_USING_WIFI */ #ifdef MTD_USING_NOR #include "drivers/mtdnor.h" -#endif +#endif /* MTD_USING_NOR */ + #ifdef MTD_USING_NAND #include "drivers/mtdnand.h" -#endif +#endif /* MTD_USING_NAND */ #ifdef RT_USING_HWCRYPTO #include "drivers/crypto.h" -#endif +#endif /* RT_USING_HWCRYPTO */ #ifdef RT_USING_PULSE_ENCODER #include "drivers/pulse_encoder.h" -#endif +#endif /* RT_USING_PULSE_ENCODER */ #ifdef RT_USING_INPUT_CAPTURE #include "drivers/rt_inputcapture.h" -#endif +#endif /* RT_USING_INPUT_CAPTURE */ #ifdef __cplusplus } From 3487f2790ab0cbb3e2c0bbe1f7c62e004a8d1d0a Mon Sep 17 00:00:00 2001 From: wangqiang Date: Wed, 17 Mar 2021 17:42:43 +0800 Subject: [PATCH 4/5] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96pin.h,=20sensor.h,?= =?UTF-8?q?=20rtdevice.h=EF=BC=8C=E9=81=BF=E5=85=8D=E5=8F=8D=E5=90=91?= =?UTF-8?q?=E5=8C=85=E5=90=AB=202.=20=E4=BC=98=E5=8C=96rt=5Fsensor=5Fclose?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=8Cpolling=E6=A8=A1=E5=BC=8F=E4=B8=8B?= =?UTF-8?q?=E4=B8=8D=E9=9C=80=E8=A6=81=E6=93=8D=E4=BD=9C=E5=92=8Crt=5Fpin?= =?UTF-8?q?=5Firq=5Fenable=E5=87=BD=E6=95=B0=203.=20=E4=BC=98=E5=8C=96rt?= =?UTF-8?q?=5Fdevice=5Fregister=E5=92=8Csensor=5Fpolling=E7=9A=84LOG?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=204.=20=E4=BF=AE=E5=A4=8Dsensor=5Fpolling?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=89=93=E5=BC=80=E8=AE=BE=E5=A4=87=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=A0=87=E5=BF=97=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/include/drivers/pin.h | 1 - components/drivers/include/drivers/sensor.h | 2 +- components/drivers/include/rtdevice.h | 7 +- components/drivers/sensors/sensor.c | 13 +- components/drivers/sensors/sensor.h | 224 -------------------- components/drivers/sensors/sensor_cmd.c | 8 +- 6 files changed, 18 insertions(+), 237 deletions(-) diff --git a/components/drivers/include/drivers/pin.h b/components/drivers/include/drivers/pin.h index 5ba1e6c8ea..74a18a7f0f 100644 --- a/components/drivers/include/drivers/pin.h +++ b/components/drivers/include/drivers/pin.h @@ -13,7 +13,6 @@ #define PIN_H__ #include -#include #ifdef __cplusplus extern "C" { diff --git a/components/drivers/include/drivers/sensor.h b/components/drivers/include/drivers/sensor.h index f54d4e8799..d071893eed 100644 --- a/components/drivers/include/drivers/sensor.h +++ b/components/drivers/include/drivers/sensor.h @@ -12,7 +12,7 @@ #define __SENSOR_H__ #include -#include +#include "pin.h" #ifdef __cplusplus extern "C" { diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h index aeb3fa5223..fbd3c9e7b2 100644 --- a/components/drivers/include/rtdevice.h +++ b/components/drivers/include/rtdevice.h @@ -80,9 +80,6 @@ extern "C" { #include "drivers/sdio.h" #endif /* RT_USING_SDIO */ -#ifdef RT_USING_SENSOR -#include "drivers/sensor.h" -#endif /* RT_USING_SENSOR */ #ifdef RT_USING_WDT #include "drivers/watchdog.h" @@ -92,6 +89,10 @@ extern "C" { #include "drivers/pin.h" #endif /* RT_USING_PIN */ +#ifdef RT_USING_SENSOR +#include "drivers/sensor.h" +#endif /* RT_USING_SENSOR */ + #ifdef RT_USING_CAN #include "drivers/can.h" #endif /* RT_USING_CAN */ diff --git a/components/drivers/sensors/sensor.c b/components/drivers/sensors/sensor.c index 2a1f3a19af..1736677aee 100644 --- a/components/drivers/sensors/sensor.c +++ b/components/drivers/sensors/sensor.c @@ -256,10 +256,13 @@ static rt_err_t rt_sensor_close(rt_device_t dev) } } } - /* Sensor disable interrupt */ - if (sensor->config.irq_pin.pin != RT_PIN_NONE) + if (sensor->config.mode != RT_SENSOR_MODE_POLLING) { - rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE); + /* Sensor disable interrupt */ + if (sensor->config.irq_pin.pin != RT_PIN_NONE) + { + rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE); + } } __exit: @@ -476,12 +479,12 @@ int rt_hw_sensor_register(rt_sensor_t sensor, result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE); if (result != RT_EOK) { + LOG_E("rt_sensor[%s] register err code: %d", device_name, result); rt_free(device_name); - LOG_E("rt_sensor register err code: %d", result); return result; } rt_free(device_name); - LOG_I("rt_sensor init success"); + LOG_I("rt_sensor[%s] init success", device_name); return RT_EOK; } diff --git a/components/drivers/sensors/sensor.h b/components/drivers/sensors/sensor.h index f54d4e8799..761ee49b4c 100755 --- a/components/drivers/sensors/sensor.h +++ b/components/drivers/sensors/sensor.h @@ -8,231 +8,7 @@ * 2019-01-31 flybreak first version */ -#ifndef __SENSOR_H__ -#define __SENSOR_H__ #include #include -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef RT_USING_RTC -#define rt_sensor_get_ts() time(RT_NULL) /* API for the sensor to get the timestamp */ -#else -#define rt_sensor_get_ts() rt_tick_get() /* API for the sensor to get the timestamp */ -#endif - -#define RT_PIN_NONE 0xFFFF /* RT PIN NONE */ -#define RT_DEVICE_FLAG_FIFO_RX 0x200 /* Flag to use when the sensor is open by fifo mode */ - -#define RT_SENSOR_MODULE_MAX (3) /* The maximum number of members of a sensor module */ - -/* Sensor types */ - -#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 */ -#define RT_SENSOR_CLASS_FORCE (13) /* Force sensor */ -#define RT_SENSOR_CLASS_DUST (14) /* Dust sensor */ -#define RT_SENSOR_CLASS_ECO2 (15) /* eCO2 sensor */ -#define RT_SENSOR_CLASS_GNSS (16) /* GPS/GNSS sensor */ -#define RT_SENSOR_CLASS_TOF (17) /* TOF sensor */ - -/* Sensor vendor types */ - -#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 */ -#define RT_SENSOR_VENDOR_MIRAMEMS (6) /* MiraMEMS */ -#define RT_SENSOR_VENDOR_DALLAS (7) /* Dallas */ -#define RT_SENSOR_VENDOR_ASAIR (8) /* Aosong */ -#define RT_SENSOR_VENDOR_SHARP (9) /* Sharp */ -#define RT_SENSOR_VENDOR_SENSIRION (10) /* Sensirion */ -#define RT_SENSOR_VENDOR_TI (11) /* Texas Instruments */ -#define RT_SENSOR_VENDOR_PLANTOWER (12) /* Plantower */ -#define RT_SENSOR_VENDOR_AMS (13) /* ams AG */ -#define RT_SENSOR_VENDOR_MAXIM (14) /* Maxim Integrated */ - - -/* Sensor unit types */ - -#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 */ -#define RT_SENSOR_UNIT_BPM (11) /* Heart rate unit: bpm */ -#define RT_SENSOR_UNIT_MM (12) /* Distance unit: mm */ -#define RT_SENSOR_UNIT_MN (13) /* Force unit: mN */ -#define RT_SENSOR_UNIT_PPM (14) /* Concentration unit: ppm */ -#define RT_SENSOR_UNIT_PPB (15) /* Concentration unit: ppb */ -#define RT_SENSOR_UNIT_DMS (16) /* Coordinates unit: DMS */ -#define RT_SENSOR_UNIT_DD (17) /* Coordinates unit: DD */ - -/* Sensor communication interface types */ - -#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) - -/* Sensor power mode types */ - -#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 */ - -/* Sensor work mode types */ - -#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 */ - -/* Sensor control cmd types */ - -#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 */ - -#define RT_SENSOR_CTRL_USER_CMD_START 0x100 /* User commands should be greater than 0x100 */ - -struct rt_sensor_info -{ - 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; -}; - -struct rt_sensor_intf -{ - 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 */ -}; - -struct rt_sensor_config -{ - 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 */ -}; - -typedef struct rt_sensor_device *rt_sensor_t; - -struct rt_sensor_device -{ - struct rt_device parent; /* The standard device */ - - 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 */ - - const struct rt_sensor_ops *ops; /* The sensor ops */ - - struct rt_sensor_module *module; /* The sensor module */ - - rt_err_t (*irq_handle)(rt_sensor_t sensor); /* Called when an interrupt is generated, registered by the driver */ -}; - -struct rt_sensor_module -{ - rt_mutex_t lock; /* The module lock */ - - 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 */ -}; - -/* 3-axis Data Type */ -struct sensor_3_axis -{ - rt_int32_t x; - rt_int32_t y; - rt_int32_t z; -}; - -struct coordinates -{ - double longitude; - double latitude; -}; - -struct rt_sensor_data -{ - rt_uint32_t timestamp; /* The timestamp when the data was received */ - rt_uint8_t type; /* The sensor type of the data */ - union - { - struct sensor_3_axis acce; /* Accelerometer. unit: mG */ - struct sensor_3_axis gyro; /* Gyroscope. unit: mdps */ - struct sensor_3_axis mag; /* Magnetometer. unit: mGauss */ - struct coordinates coord; /* Coordinates unit: degrees */ - 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 */ - rt_int32_t hr; /* Heart rate. unit: bpm */ - rt_int32_t tvoc; /* TVOC. unit: permillage */ - rt_int32_t noise; /* Noise Loudness. unit: HZ */ - rt_uint32_t step; /* Step sensor. unit: 1 */ - rt_int32_t force; /* Force sensor. unit: mN */ - rt_uint32_t dust; /* Dust sensor. unit: ug/m3 */ - rt_uint32_t eco2; /* eCO2 sensor. unit: ppm */ - } data; -}; - -struct rt_sensor_ops -{ - 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); -}; - -int rt_hw_sensor_register(rt_sensor_t sensor, - const char *name, - rt_uint32_t flag, - void *data); - -#ifdef __cplusplus -} -#endif - -#endif /* __SENSOR_H__ */ diff --git a/components/drivers/sensors/sensor_cmd.c b/components/drivers/sensors/sensor_cmd.c index 5c2d22640c..0f0ab66d63 100755 --- a/components/drivers/sensors/sensor_cmd.c +++ b/components/drivers/sensors/sensor_cmd.c @@ -231,6 +231,7 @@ static void sensor_polling(int argc, char **argv) struct rt_sensor_data data; rt_size_t res, i; rt_int32_t delay; + rt_err_t result; dev = rt_device_find(argv[1]); if (dev == RT_NULL) @@ -244,9 +245,10 @@ static void sensor_polling(int argc, char **argv) sensor = (rt_sensor_t)dev; delay = sensor->info.period_min > 100 ? sensor->info.period_min : 100; - if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) + result = rt_device_open(dev, RT_DEVICE_FLAG_RDONLY); + if (result != RT_EOK) { - LOG_E("open device failed!"); + LOG_E("open device failed! error code : %d", result); return; } rt_device_control(dev, RT_SENSOR_CTRL_SET_ODR, (void *)100); @@ -451,7 +453,7 @@ static void sensor(int argc, char **argv) dev = rt_device_find(argv[2]); if (dev == RT_NULL) { - LOG_E("Can't find device:%s", argv[1]); + LOG_E("Can't find device:%s", argv[2]); return; } if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK) From c72227680daf0bd8880afa4d10d93ab574abf7b2 Mon Sep 17 00:00:00 2001 From: wangqiang Date: Mon, 22 Mar 2021 10:11:42 +0800 Subject: [PATCH 5/5] Fixed formatting issues --- components/drivers/sensors/sensor.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/drivers/sensors/sensor.c b/components/drivers/sensors/sensor.c index 1736677aee..6d584e568e 100644 --- a/components/drivers/sensors/sensor.c +++ b/components/drivers/sensors/sensor.c @@ -176,7 +176,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX) { /* If interrupt mode is supported, configure it to interrupt mode */ - if (RT_EOK== local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT)) + if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT) == RT_EOK) { /* Initialization sensor interrupt */ rt_sensor_irq_init(sensor); @@ -186,7 +186,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX) { /* If fifo mode is supported, configure it to fifo mode */ - if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO)) + if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO) == RT_EOK) { /* Initialization sensor interrupt */ rt_sensor_irq_init(sensor); @@ -200,7 +200,7 @@ static rt_err_t rt_sensor_open(rt_device_t dev, rt_uint16_t oflag) } /* Configure power mode to normal mode */ - if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL)) + if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK) { sensor->config.power = RT_SENSOR_POWER_NORMAL; } @@ -233,7 +233,7 @@ static rt_err_t rt_sensor_close(rt_device_t dev) } /* Configure power mode to power down mode */ - if (RT_EOK == local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN)) + if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK) { sensor->config.power = RT_SENSOR_POWER_DOWN; }