diff --git a/APP_Framework/Applications/framework_init.c b/APP_Framework/Applications/framework_init.c index 6ba1d6978..603af2fac 100644 --- a/APP_Framework/Applications/framework_init.c +++ b/APP_Framework/Applications/framework_init.c @@ -32,6 +32,7 @@ extern int Zg09Co2Init(void); extern int As830Ch4Init(void); extern int Tb600bIaq10IaqInit(void); extern int Tb600bTvoc10TvocInit(void); +extern int Tb600bWqHcho1osInit(void); typedef int (*InitFunc)(void); struct InitDesc @@ -104,6 +105,10 @@ static struct InitDesc sensor_desc[] = { "tvoc_tb600b_tvoc10", Tb600bTvoc10TvocInit }, #endif +#ifdef SENSOR_TB600B_WQ_HCHO1OS + { "tvoc_tb600b_wq_hcho1os", Tb600bWqHcho1osInit }, +#endif + { "NULL", NULL }, }; diff --git a/APP_Framework/Applications/sensor_app/Kconfig b/APP_Framework/Applications/sensor_app/Kconfig index 9dafba604..67474d37c 100755 --- a/APP_Framework/Applications/sensor_app/Kconfig +++ b/APP_Framework/Applications/sensor_app/Kconfig @@ -5,6 +5,16 @@ menu "sensor app" default n if APPLICATION_SENSOR + menuconfig APPLICATION_SENSOR_HCHO + bool "Using sensor HCHO apps" + default n + + if APPLICATION_SENSOR_HCHO + config APPLICATION_SENSOR_HCHO_TB600B_WQ_HCHO1OS + bool "Using sensor TB600B_WQ_HCHO1OS apps" + default n + endif + menuconfig APPLICATION_SENSOR_TVOC bool "Using sensor TVOC apps" default n diff --git a/APP_Framework/Applications/sensor_app/Makefile b/APP_Framework/Applications/sensor_app/Makefile index d457b729a..7a067414b 100644 --- a/APP_Framework/Applications/sensor_app/Makefile +++ b/APP_Framework/Applications/sensor_app/Makefile @@ -1,5 +1,9 @@ SRC_FILES := +ifeq ($(CONFIG_APPLICATION_SENSOR_HCHO_TB600B_WQ_HCHO1OS), y) + SRC_FILES += hcho_tb600b_wq_hcho1os.c +endif + ifeq ($(CONFIG_APPLICATION_SENSOR_TVOC_TB600B_TVOC10), y) SRC_FILES += tvoc_tb600b_tvoc10.c endif diff --git a/APP_Framework/Applications/sensor_app/hcho_tb600b_wq_hcho1os.c b/APP_Framework/Applications/sensor_app/hcho_tb600b_wq_hcho1os.c new file mode 100644 index 000000000..09e2b7858 --- /dev/null +++ b/APP_Framework/Applications/sensor_app/hcho_tb600b_wq_hcho1os.c @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** + * @file hcho_tb600b_wq_hcho1os.c + * @brief hcho example + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2021.12.15 + */ + +#include +#include + + +/** + * @description: Read a hcho + * @return 0 + */ +void HchoTb600bHcho1os(void) +{ + struct SensorQuantity *hcho = SensorQuantityFind(SENSOR_QUANTITY_TB600B_HCHO, SENSOR_QUANTITY_HCHO); + SensorQuantityOpen(hcho); + int32_t result = 0; + + result = SensorQuantityRead(hcho); + + printf("tvoc concentration is : %dppb\n", result); + SensorQuantityClose(hcho); +} \ No newline at end of file diff --git a/APP_Framework/Framework/sensor/Kconfig b/APP_Framework/Framework/sensor/Kconfig index f65b6cc68..69ea74471 100644 --- a/APP_Framework/Framework/sensor/Kconfig +++ b/APP_Framework/Framework/sensor/Kconfig @@ -4,6 +4,13 @@ menuconfig SUPPORT_SENSOR_FRAMEWORK select TRANSFORM_LAYER_ATTRIUBUTE if SUPPORT_SENSOR_FRAMEWORK + menuconfig SENSOR_HCHO + bool "Using HCHO sensor device" + default n + if SENSOR_HCHO + source "$APP_DIR/Framework/sensor/hcho/Kconfig" + endif + menuconfig SENSOR_TVOC bool "Using TVOC sensor device" default n diff --git a/APP_Framework/Framework/sensor/Makefile b/APP_Framework/Framework/sensor/Makefile index c1764df68..9f19136de 100644 --- a/APP_Framework/Framework/sensor/Makefile +++ b/APP_Framework/Framework/sensor/Makefile @@ -1,5 +1,9 @@ SRC_FILES := sensor.c +ifeq ($(CONFIG_SENSOR_HCHO),y) + SRC_DIR += hcho +endif + ifeq ($(CONFIG_SENSOR_TVOC),y) SRC_DIR += tvoc endif diff --git a/APP_Framework/Framework/sensor/hcho/Kconfig b/APP_Framework/Framework/sensor/hcho/Kconfig new file mode 100644 index 000000000..0b5ad82dd --- /dev/null +++ b/APP_Framework/Framework/sensor/hcho/Kconfig @@ -0,0 +1,43 @@ + +config SENSOR_TB600B_WQ_HCHO1OS + bool "Using TB600B WQ_HCHO1OS" + default n + + if SENSOR_TB600B_WQ_HCHO1OS + config SENSOR_DEVICE_TB600B_WQ_HCHO1OS + string "tb600b wq_hcho1os sensor name" + default "tb600b_wq_hcho1os_1" + + config SENSOR_QUANTITY_TB600B_HCHO + string "tb600b wq_hcho1os quantity name" + default "hcho_1" + + if ADD_XIUOS_FETURES + config SENSOR_TB600B_WQ_HCHO1OS_DRIVER_EXTUART + bool "Using extra uart to support tb600b wq_hcho1os" + default y + + config SENSOR_DEVICE_TB600B_WQ_HCHO1OS_DEV + string "tb600b wq_hcho1os device uart path" + default "/dev/uart2_dev2" + depends on !SENSOR_TB600B_WQ_HCHO1OS_DRIVER_EXTUART + + if SENSOR_TB600B_WQ_HCHO1OS_DRIVER_EXTUART + config SENSOR_DEVICE_TB600B_WQ_HCHO1OS_DEV + string "tb600b wq_hcho1os device extra uart path" + default "/dev/extuart_dev5" + + config SENSOR_DEVICE_TB600B_WQ_HCHO1OS_DEV_EXT_PORT + int "if TB600B_WQ_HCHO1OS device using extuart, choose port" + default "5" + endif + endif + + if ADD_NUTTX_FETURES + + endif + + if ADD_RTTHREAD_FETURES + + endif + endif diff --git a/APP_Framework/Framework/sensor/hcho/Makefile b/APP_Framework/Framework/sensor/hcho/Makefile new file mode 100644 index 000000000..c30211f6c --- /dev/null +++ b/APP_Framework/Framework/sensor/hcho/Makefile @@ -0,0 +1,5 @@ +ifeq ($(CONFIG_SENSOR_TB600B_WQ_HCHO1OS),y) + SRC_DIR += tb600b_wq_hcho1os +endif + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/sensor/hcho/tb600b_wq_hcho1os/Makefile b/APP_Framework/Framework/sensor/hcho/tb600b_wq_hcho1os/Makefile new file mode 100644 index 000000000..3dec9399d --- /dev/null +++ b/APP_Framework/Framework/sensor/hcho/tb600b_wq_hcho1os/Makefile @@ -0,0 +1,3 @@ +SRC_FILES := tb600b_wq_hcho1os.c + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Framework/sensor/hcho/tb600b_wq_hcho1os/tb600b_wq_hcho1os.c b/APP_Framework/Framework/sensor/hcho/tb600b_wq_hcho1os/tb600b_wq_hcho1os.c new file mode 100644 index 000000000..96ede2fc9 --- /dev/null +++ b/APP_Framework/Framework/sensor/hcho/tb600b_wq_hcho1os/tb600b_wq_hcho1os.c @@ -0,0 +1,222 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** + * @file wq_hcho1os.c + * @brief wq_hcho1os driver base sensor + * @version 1.0 + * @author AIIT XUOS Lab + * @date 2021.12.15 + */ + +#include + +static struct SensorDevice wq_hcho1os; +static uint8_t ReadInstruction[9]; + +static struct SensorProductInfo info = +{ + SENSOR_ABILITY_HCHO, + "AQS", + "TB600B_WQ_HCHO1OS", +}; + +/** + * @description: Open TB600B WQ_HCHO1OS sensor device + * @param sdev - sensor device pointer + * @return success: 1 , failure: other + */ +static int SensorDeviceOpen(struct SensorDevice *sdev) +{ + int result = 0; + + sdev->fd = PrivOpen(SENSOR_DEVICE_TB600B_WQ_HCHO1OS_DEV, O_RDWR); + if (sdev->fd < 0) { + printf("open %s error\n", SENSOR_DEVICE_TB600B_WQ_HCHO1OS_DEV); + return -1; + } + + struct SerialDataCfg cfg; + cfg.serial_baud_rate = BAUD_RATE_9600; + cfg.serial_data_bits = DATA_BITS_8; + cfg.serial_stop_bits = STOP_BITS_1; + cfg.serial_buffer_size = 128; + cfg.serial_parity_mode = PARITY_NONE; + cfg.serial_bit_order = 0; + cfg.serial_invert_mode = 0; +#ifdef SENSOR_TB600B_WQ_HCHO1OS_DRIVER_EXTUART + cfg.ext_uart_no = SENSOR_DEVICE_TB600B_WQ_HCHO1OS_DEV_EXT_PORT; + cfg.port_configure = PORT_CFG_INIT; +#endif + + struct PrivIoctlCfg ioctl_cfg; + ioctl_cfg.ioctl_driver_type = SERIAL_TYPE; + ioctl_cfg.args = &cfg; + result = PrivIoctl(sdev->fd, OPE_INT, &ioctl_cfg); + + return result; +} + +/** + * @description: Read sensor device + * @param sdev - sensor device pointer + * @param len - the length of the read data + * @return get data length + */ +static int SensorDeviceRead(struct SensorDevice *sdev, size_t len) +{ + uint8_t tmp = 0; + uint8_t idx = 0; + + /* this instruction will read gas with temperature and humidity,return 9 datas*/ + ReadInstruction[0] = 0xFF; + ReadInstruction[1] = 0x01; + ReadInstruction[2] = 0x86; + ReadInstruction[3] = 0x00; + ReadInstruction[4] = 0x00; + ReadInstruction[5] = 0x00; + ReadInstruction[6] = 0x00; + ReadInstruction[7] = 0x00; + ReadInstruction[8] = 0x79; + + PrivWrite(sdev->fd, ReadInstruction, 9); + + for(idx = 0; idx < 9; idx++) + { + if (PrivRead(sdev->fd, &tmp, 1) == 1) { + sdev->buffer[idx] = tmp; + } + } + + return idx; +} +/** + * @description: Read sensor device + * @param sdev - sensor device pointer + * @param len - the length of the read data + * @return get data length + */ +static int SensorDeviceWrite(struct SensorDevice *sdev, const void *buf, size_t len) +{ + return PrivWrite(sdev->fd, buf, len); + +} + +static struct SensorDone done = +{ + SensorDeviceOpen, + NULL, + SensorDeviceRead, + SensorDeviceWrite, + NULL, +}; + +/** + * @description: Init TB600B WQ_HCHO1OS sensor and register + * @return void + */ +static void SensorDeviceTb600bHcho1osInit(void) +{ + wq_hcho1os.name = SENSOR_DEVICE_TB600B_WQ_HCHO1OS; + wq_hcho1os.info = &info; + wq_hcho1os.done = &done; + + SensorDeviceRegister(&wq_hcho1os); +} + +static struct SensorQuantity wq_hcho1os_hcho; + +/* check data*/ +static uint8_t getCheckSum(uint8_t *packet) +{ + uint8_t i; + uint8_t checksum = 0; + for( i = 1; i < 12; i++) + { + checksum += packet[i]; + } + checksum = ~checksum + 1; + + return checksum; +} + +/** + * @description: Analysis TB600B WQ_HCHO1OS result + * @param quant - sensor quantity pointer + * @return quantity value + */ +static int32_t QuantityRead(struct SensorQuantity *quant) +{ + if (!quant) + return -1; + + uint32_t len = 0; + uint8_t checksum = 0; + uint16_t ppb, ugm3; + + if (quant->sdev->done->read != NULL) { + if(quant->sdev->status == SENSOR_DEVICE_PASSIVE) { + len = quant->sdev->done->read(quant->sdev, 9); + if (len == 0) + { + printf("error read data length = 0.\n"); + return -1; + } + checksum = getCheckSum(quant->sdev->buffer); + if(checksum == quant->sdev->buffer[8]) + { + ugm3 = (uint16_t)quant->sdev->buffer[2] * 256 + (uint16_t)quant->sdev->buffer[3]; + ppb = (uint16_t)quant->sdev->buffer[6] * 256 + (uint16_t)quant->sdev->buffer[7]; + + printf("Formaldehyde concentration is : %dug/m³(%dppb)\n", ugm3, ppb); + return ppb; + } + else + { + printf("This reading is wrong\n"); + return SENSOR_QUANTITY_VALUE_ERROR; + } + } + if (quant->sdev->status == SENSOR_DEVICE_ACTIVE) + { + printf("Please set passive mode.\n"); + } + }else{ + printf("%s don't have read done.\n", quant->name); + } + + return -1; +} + +/** + * @description: Init TB600B WQ_HCHO1OS quantity and register + * @return 0 + */ +int Tb600bWqHcho1osInit(void) +{ + SensorDeviceTb600bHcho1osInit(); + + wq_hcho1os_hcho.name = SENSOR_QUANTITY_TB600B_HCHO; + wq_hcho1os_hcho.type = SENSOR_QUANTITY_HCHO; + wq_hcho1os_hcho.value.decimal_places = 0; + wq_hcho1os_hcho.value.max_std = SENSOR_QUANTITY_VALUE_ERROR; + wq_hcho1os_hcho.value.min_std = SENSOR_QUANTITY_VALUE_ERROR; + wq_hcho1os_hcho.value.last_value = SENSOR_QUANTITY_VALUE_ERROR; + wq_hcho1os_hcho.value.max_value = SENSOR_QUANTITY_VALUE_ERROR; + wq_hcho1os_hcho.value.min_value = SENSOR_QUANTITY_VALUE_ERROR; + wq_hcho1os_hcho.sdev = &wq_hcho1os; + wq_hcho1os_hcho.ReadValue = QuantityRead; + + SensorQuantityRegister(&wq_hcho1os_hcho); + + return 0; +} \ No newline at end of file diff --git a/APP_Framework/Framework/sensor/sensor.h b/APP_Framework/Framework/sensor/sensor.h index 1da734c7c..fad97fdce 100644 --- a/APP_Framework/Framework/sensor/sensor.h +++ b/APP_Framework/Framework/sensor/sensor.h @@ -54,6 +54,7 @@ extern "C" { #define SENSOR_ABILITY_CH4 ((uint32_t)(1 << SENSOR_QUANTITY_CH4)) #define SENSOR_ABILITY_IAQ ((uint32_t)(1 << SENSOR_QUANTITY_IAQ)) #define SENSOR_ABILITY_TVOC ((uint32_t)(1 << SENSOR_QUANTITY_TVOC)) +#define SENSOR_ABILITY_HCHO ((uint32_t)(1 << SENSOR_QUANTITY_HCHO)) struct SensorProductInfo { uint32_t ability; /* Bitwise OR of sensor ability */