11 changed files with 363 additions and 0 deletions
@ -0,0 +1,45 @@ |
|||
/*
|
|||
* 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 iaq_tb600b_iaq10.c |
|||
* @brief iaq10 example |
|||
* @version 1.0 |
|||
* @author AIIT XUOS Lab |
|||
* @date 2021.12.14 |
|||
*/ |
|||
|
|||
#include <user_api.h> |
|||
#include <sensor.h> |
|||
|
|||
// struct iaq_data {
|
|||
// uint16_t gas;
|
|||
// uint8_t TH;
|
|||
// uint8_t TL;
|
|||
// uint8_t RhH;
|
|||
// uint8_t RhL;
|
|||
// };
|
|||
/**
|
|||
* @description: Read a iaq |
|||
* @return 0 |
|||
*/ |
|||
void IaqTb600bIaq10(void) |
|||
{ |
|||
struct SensorQuantity *iaq = SensorQuantityFind(SENSOR_QUANTITY_TB600B_IAQ, SENSOR_QUANTITY_IAQ); |
|||
SensorQuantityOpen(iaq); |
|||
int32_t result = 0; |
|||
|
|||
result = SensorQuantityRead(iaq); |
|||
|
|||
printf("Gas concentration is : %dppb\n", result); |
|||
SensorQuantityClose(iaq); |
|||
} |
@ -0,0 +1,43 @@ |
|||
|
|||
config SENSOR_TB600B_IAQ10 |
|||
bool "Using TB600B IAQ10" |
|||
default n |
|||
|
|||
if SENSOR_TB600B_IAQ10 |
|||
config SENSOR_DEVICE_TB600B_IAQ10 |
|||
string "tb600b iaq10 sensor name" |
|||
default "tb600b_iaq10_1" |
|||
|
|||
config SENSOR_QUANTITY_TB600B_IAQ |
|||
string "tb600b iaq10 quantity name" |
|||
default "iaq_1" |
|||
|
|||
if ADD_XIUOS_FETURES |
|||
config SENSOR_TB600B_IAQ10_DRIVER_EXTUART |
|||
bool "Using extra uart to support tb600b iaq10" |
|||
default y |
|||
|
|||
config SENSOR_DEVICE_TB600B_IAQ10_DEV |
|||
string "tb600b iaq10 device uart path" |
|||
default "/dev/uart2_dev2" |
|||
depends on !SENSOR_TB600B_IAQ10_DRIVER_EXTUART |
|||
|
|||
if SENSOR_TB600B_IAQ10_DRIVER_EXTUART |
|||
config SENSOR_DEVICE_TB600B_IAQ10_DEV |
|||
string "tb600b iaq10 device extra uart path" |
|||
default "/dev/extuart_dev5" |
|||
|
|||
config SENSOR_DEVICE_TB600B_IAQ10_DEV_EXT_PORT |
|||
int "if TB600B_IAQ10 device using extuart, choose port" |
|||
default "5" |
|||
endif |
|||
endif |
|||
|
|||
if ADD_NUTTX_FETURES |
|||
|
|||
endif |
|||
|
|||
if ADD_RTTHREAD_FETURES |
|||
|
|||
endif |
|||
endif |
@ -0,0 +1,5 @@ |
|||
ifeq ($(CONFIG_SENSOR_TB600B_IAQ10),y) |
|||
SRC_DIR += tb600b_iaq10 |
|||
endif |
|||
|
|||
include $(KERNEL_ROOT)/compiler.mk |
@ -0,0 +1,3 @@ |
|||
SRC_FILES := tb600b_iaq10.c |
|||
|
|||
include $(KERNEL_ROOT)/compiler.mk |
@ -0,0 +1,235 @@ |
|||
/*
|
|||
* 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 tb600b_iaq10.c |
|||
* @brief tb600b_iaq10 driver base sensor |
|||
* @version 1.0 |
|||
* @author AIIT XUOS Lab |
|||
* @date 2021.12.14 |
|||
*/ |
|||
|
|||
#include <sensor.h> |
|||
|
|||
static struct SensorDevice tb600b_iaq10; |
|||
static uint8_t ReadInstruction[9]; |
|||
|
|||
struct iaq_data { |
|||
uint16_t gas; |
|||
uint8_t TH; |
|||
uint8_t TL; |
|||
uint8_t RhH; |
|||
uint8_t RhL; |
|||
}; |
|||
|
|||
static struct SensorProductInfo info = |
|||
{ |
|||
SENSOR_ABILITY_IAQ, |
|||
"AQS", |
|||
"TB600B_IAQ10", |
|||
}; |
|||
|
|||
/**
|
|||
* @description: Open AS830 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_IAQ10_DEV, O_RDWR); |
|||
if (sdev->fd < 0) { |
|||
printf("open %s error\n", SENSOR_DEVICE_TB600B_IAQ10_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_IAQ10_DRIVER_EXTUART |
|||
cfg.ext_uart_no = SENSOR_DEVICE_TB600B_IAQ10_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; |
|||
int32_t ret = 0; |
|||
|
|||
/* this instruction will read gas with temperature and humidity,return 13 datas*/ |
|||
ReadInstruction[0] = 0xFF; |
|||
ReadInstruction[1] = 0x01; |
|||
ReadInstruction[2] = 0x87; |
|||
ReadInstruction[3] = 0x00; |
|||
ReadInstruction[4] = 0x00; |
|||
ReadInstruction[5] = 0x00; |
|||
ReadInstruction[6] = 0x00; |
|||
ReadInstruction[7] = 0x00; |
|||
ReadInstruction[8] = 0x78; |
|||
|
|||
PrivWrite(sdev->fd, ReadInstruction, 9); |
|||
|
|||
for(idx = 0; idx < 13; 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 AS830 sensor and register |
|||
* @return void |
|||
*/ |
|||
static void SensorDeviceTb600bIaq10Init(void) |
|||
{ |
|||
tb600b_iaq10.name = SENSOR_DEVICE_TB600B_IAQ10; |
|||
tb600b_iaq10.info = &info; |
|||
tb600b_iaq10.done = &done; |
|||
|
|||
SensorDeviceRegister(&tb600b_iaq10); |
|||
} |
|||
|
|||
static struct SensorQuantity tb600b_iaq10_iaq; |
|||
|
|||
/* 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 AS830 CH4 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; |
|||
uint8_t TH, TL, RhH, RhL; |
|||
uint16_t gas; |
|||
struct iaq_data result; |
|||
|
|||
if (quant->sdev->done->read != NULL) { |
|||
if(quant->sdev->status == SENSOR_DEVICE_PASSIVE) { |
|||
len = quant->sdev->done->read(quant->sdev, 13); |
|||
if (len == 0) |
|||
{ |
|||
printf("error read data length = 0.\n"); |
|||
return -1; |
|||
} |
|||
checksum = getCheckSum(quant->sdev->buffer); |
|||
if(checksum == quant->sdev->buffer[12]) |
|||
{ |
|||
result.gas = (uint16_t)quant->sdev->buffer[6] * 256 + (uint16)quant->sdev->buffer[7]; |
|||
result.TH = ((int)((quant->sdev->buffer[8] << 8)|quant->sdev->buffer[9]))/100; |
|||
result.TL = ((int)((quant->sdev->buffer[8] << 8)|quant->sdev->buffer[9]))%100; |
|||
result.RhH = ((unsigned int)((quant->sdev->buffer[10] << 8)|quant->sdev->buffer[11]))/100; |
|||
result.RhL = ((unsigned int)((quant->sdev->buffer[10] << 8)|quant->sdev->buffer[11]))%100; |
|||
printf("Gas concentration is : %dppb\nThe temperature is : %d.%d℃\nThe humidity is : %d.%drh%%\n", result.gas, result.TH, result.TL, result.RhH, result.RhL); |
|||
return result.gas; |
|||
} |
|||
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 AS830 CH4 quantity and register |
|||
* @return 0 |
|||
*/ |
|||
int Tb600bIaq10IaqInit(void) |
|||
{ |
|||
SensorDeviceTb600bIaq10Init(); |
|||
|
|||
tb600b_iaq10_iaq.name = SENSOR_QUANTITY_TB600B_IAQ; |
|||
tb600b_iaq10_iaq.type = SENSOR_QUANTITY_IAQ; |
|||
tb600b_iaq10_iaq.value.decimal_places = 0; |
|||
tb600b_iaq10_iaq.value.max_std = SENSOR_QUANTITY_VALUE_ERROR; |
|||
tb600b_iaq10_iaq.value.min_std = SENSOR_QUANTITY_VALUE_ERROR; |
|||
tb600b_iaq10_iaq.value.last_value = SENSOR_QUANTITY_VALUE_ERROR; |
|||
tb600b_iaq10_iaq.value.max_value = SENSOR_QUANTITY_VALUE_ERROR; |
|||
tb600b_iaq10_iaq.value.min_value = SENSOR_QUANTITY_VALUE_ERROR; |
|||
tb600b_iaq10_iaq.sdev = &tb600b_iaq10; |
|||
tb600b_iaq10_iaq.ReadValue = QuantityRead; |
|||
|
|||
SensorQuantityRegister(&tb600b_iaq10_iaq); |
|||
|
|||
return 0; |
|||
} |
Loading…
Reference in new issue