add sensor framework

This commit is contained in:
Liu_Weichao
2021-06-16 18:35:33 +08:00
parent b6b06aa538
commit fc505837b9
31 changed files with 222 additions and 778 deletions

View File

@@ -0,0 +1,40 @@
menuconfig SENSOR_SENSORDEVICE
bool "Using sensor devices"
default y
if SENSOR_SENSORDEVICE
menuconfig SENSOR_CO2
bool "Using CO2 sensor device"
default n
if SENSOR_CO2
source "$APP_DIR/Framework/sensor/co2/Kconfig"
endif
menuconfig SENSOR_PM
bool "Using PM sensor device"
default n
if SENSOR_PM
source "$APP_DIR/Framework/sensor/pm/Kconfig"
endif
menuconfig SENSOR_VOICE
bool "Using voice sensor device"
default n
if SENSOR_VOICE
source "$APP_DIR/Framework/sensor/voice/Kconfig"
endif
menuconfig SENSOR_TEMPERATURE
bool "Using temperature sensor device"
default n
if SENSOR_TEMPERATURE
source "$APP_DIR/Framework/sensor/temperature/Kconfig"
endif
menuconfig SENSOR_HUMIDITY
bool "Using humidity sensor device"
default n
if SENSOR_HUMIDITY
source "$APP_DIR/Framework/sensor/humidity/Kconfig"
endif
endif

View File

@@ -1,3 +1,23 @@
SRC_FILES := sensor.c
include $(KERNEL_ROOT)/compiler.mk
ifeq ($(CONFIG_SENSOR_CO2),y)
SRC_DIR += co2
endif
ifeq ($(CONFIG_SENSOR_PM),y)
SRC_DIR += pm
endif
ifeq ($(CONFIG_SENSOR_VOICE),y)
SRC_DIR += voice
endif
ifeq ($(CONFIG_SENSOR_TEMPERATURE),y)
SRC_DIR += temperature
endif
ifeq ($(CONFIG_SENSOR_HUMIDITY),y)
SRC_DIR += humidity
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,22 @@
config SENSOR_ZG09
bool "Using zg09"
default n
if SENSOR_ZG09
config SENSOR_DEVICE_ZG09
string "zg09 sensor name"
default "zg09_1"
config SENSOR_QUANTITY_ZG09_CO2
string "zg09 quantity name"
default "co2_1"
config SENSOR_DEVICE_ZG09_DEV
string "zg09 device name"
default "/dev/uart2_dev2"
config SENSOR_DEVICE_ZG09_DEV_EXT_PORT
int "if ZG09 device using extuart, choose port"
default "4"
endif

View File

@@ -0,0 +1,5 @@
ifeq ($(CONFIG_SENSOR_ZG09),y)
SRC_DIR += zg09
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,3 @@
SRC_FILES := zg09.c
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,227 @@
/*
* 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 zg09.c
* @brief ZG09 CO2 driver base perception
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021.04.22
*/
#include <sensor.h>
static uint8_t zg09_set_passive[8]={0xFE, 0x06, 0x00, 0x05, 0x00, 0x00, 0x8D, 0xC4};
static uint8_t zg09_set_active[8]={0xFE, 0x06, 0x00, 0x05, 0x00, 0x01, 0x4C, 0x04};
static uint8_t zg09_read_instruction[8]={0xFE, 0x03, 0x00, 0x0B, 0x00, 0x01, 0xE1, 0xC7};
static struct SensorDevice zg09;
static struct SensorProductInfo info =
{
SENSOR_ABILITY_CO2,
"ZyAura",
"ZG09",
};
/**
* @description: Open ZG09 sensor device
* @param sdev - sensor device pointer
* @return success: 1 , failure: other
*/
static int SensorDeviceOpen(struct SensorDevice *sdev)
{
int result = 1;
sdev->fd = PrivOpen(SENSOR_DEVICE_ZG09_DEV, O_RDWR);
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;
cfg.ext_uart_no = SENSOR_DEVICE_ZG09_DEV_EXT_PORT;
cfg.port_configure = PORT_CFG_INIT;
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
ioctl_cfg.args = &cfg;
result = PrivIoctl(sdev->fd, OPE_INT, &ioctl_cfg);
sdev->done->ioctl(sdev, SENSOR_DEVICE_PASSIVE);
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;
int timeout = 0;
while (1) {
PrivRead(sdev->fd, &tmp, 1);
if ((tmp == 0xFE) || (timeout >= 1000))
break;
UserTaskDelay(10);
++timeout;
}
if(tmp != 0xFE)
return -1;
uint8_t idx = 0;
sdev->buffer[idx++] = tmp;
while ((idx < len) && (timeout < 1000)) {
if (PrivRead(sdev->fd, &tmp, 1) == 1) {
timeout = 0;
sdev->buffer[idx++] = tmp;
}
++timeout;
}
return idx;
}
/**
* @description: set sensor work mode
* @param sdev - sensor device pointer
* @param cmd - mode command
* @return success: 1 , failure: -1
*/
static int SensorDeviceIoctl(struct SensorDevice *sdev, int cmd)
{
switch (cmd)
{
case SENSOR_DEVICE_PASSIVE:
PrivWrite(sdev->fd, zg09_set_passive, 8);
sdev->done->read(sdev, 8);
if (memcmp(sdev->buffer, zg09_set_passive, 8) == 0) {
sdev->status = SENSOR_DEVICE_PASSIVE;
return 1;
}
break;
case SENSOR_DEVICE_ACTIVE:
PrivWrite(sdev->fd, zg09_set_active, 8);
sdev->done->read(sdev, 8);
if (memcmp(sdev->buffer, zg09_set_active, 8) == 0) {
sdev->status = SENSOR_DEVICE_ACTIVE;
return 1;
}
break;
default:
printf("This device does not have this command!\n");
break;
}
return -1;
}
static struct SensorDone done =
{
SensorDeviceOpen,
NULL,
SensorDeviceRead,
NULL,
SensorDeviceIoctl,
};
/**
* @description: Init ZG09 sensor and register
* @return void
*/
static void SensorDeviceZg09Init(void)
{
zg09.name = SENSOR_DEVICE_ZG09;
zg09.info = &info;
zg09.done = &done;
SensorDeviceRegister(&zg09);
}
static struct SensorQuantity zg09_co2;
/**
* @description: Analysis ZG09 CO2 result
* @param quant - sensor quantity pointer
* @return quantity value
*/
static int32_t QuantityRead(struct SensorQuantity *quant)
{
if (!quant)
return -1;
uint32_t result;
if (quant->sdev->done->read != NULL) {
if(quant->sdev->status == SENSOR_DEVICE_PASSIVE) {
PrivWrite(quant->sdev->fd, zg09_read_instruction, 8);
quant->sdev->done->read(quant->sdev, 7);
if(Crc16(quant->sdev->buffer, 5) == ((uint32_t)quant->sdev->buffer[6] << 8) + ((uint32_t)quant->sdev->buffer[5])) {
result = (uint32_t)quant->sdev->buffer[3] * 256 + (uint32_t)quant->sdev->buffer[4];
if (result > quant->value.max_value)
quant->value.max_value = result;
else if (result < quant->value.min_value)
quant->value.min_value = result;
quant->value.last_value = result;
return result;
}else{
printf("This reading is wrong\n");
result = SENSOR_QUANTITY_VALUE_ERROR;
return result;
}
}
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 ZG09 CO2 quantity and register
* @return 0
*/
int Zg09Co2Init(void)
{
SensorDeviceZg09Init();
zg09_co2.name = SENSOR_QUANTITY_ZG09_CO2;
zg09_co2.type = SENSOR_QUANTITY_CO2;
zg09_co2.value.decimal_places = 0;
zg09_co2.value.max_std = 1000;
zg09_co2.value.min_std = 350;
zg09_co2.value.last_value = SENSOR_QUANTITY_VALUE_ERROR;
zg09_co2.value.max_value = SENSOR_QUANTITY_VALUE_ERROR;
zg09_co2.value.min_value = SENSOR_QUANTITY_VALUE_ERROR;
zg09_co2.sdev = &zg09;
zg09_co2.ReadValue = QuantityRead;
SensorQuantityRegister(&zg09_co2);
return 0;
}

View File

@@ -0,0 +1,18 @@
config PERCEPTION_HS300X
bool "Using HS300x"
default n
if PERCEPTION_HS300X
config SENSOR_DEVICE_HS300X
string "HS300x sensor name"
default "hs300x_1"
config SENSOR_QUANTITY_HS300X_HUMIDITY
string "HS300x quantity name"
default "humidity_1"
config SENSOR_DEVICE_HS300X_DEV
string "HS300x device name"
default "/dev/i2c1_dev0"
endif

View File

@@ -0,0 +1,5 @@
ifeq ($(CONFIG_SENSOR_HS300X),y)
SRC_DIR += hs300x_humi
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,3 @@
SRC_FILES := hs300x_humi.c
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,141 @@
/*
* 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 hs300x_humi.c
* @brief HS300x humidity driver base perception
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021.04.22
*/
#include <sensor.h>
static struct SensorDevice hs300x;
static struct SensorProductInfo info =
{
(SENSOR_ABILITY_HUMI | SENSOR_ABILITY_TEMP),
"Renesas",
"HS300x",
};
/**
* @description: Open HS300x sensor device
* @param sdev - sensor device pointer
* @return 1
*/
static int SensorDeviceOpen(struct SensorDevice *sdev)
{
sdev->fd = PrivOpen(SENSOR_DEVICE_HS300X_DEV, O_RDWR);
return 1;
}
/**
* @description: Read sensor device
* @param sdev - sensor device pointer
* @param len - the length of the read data
* @return success: 1 , failure: -1
*/
static int SensorDeviceRead(struct SensorDevice *sdev, size_t len)
{
if (PrivWrite(sdev->fd, NULL, 0) != 1)
return -1;
UserTaskDelay(50);
if (PrivRead(sdev->fd, sdev->buffer, len) != 1)
return -1;
return 1;
}
static struct SensorDone done =
{
SensorDeviceOpen,
NULL,
SensorDeviceRead,
NULL,
NULL,
};
/**
* @description: Init HS300x sensor and register
* @return void
*/
static void SensorDeviceHs300xInit(void)
{
hs300x.name = SENSOR_DEVICE_HS300X;
hs300x.info = &info;
hs300x.done = &done;
hs300x.status = SENSOR_DEVICE_PASSIVE;
SensorDeviceRegister(&hs300x);
}
static struct SensorQuantity hs300x_humidity;
/**
* @description: Analysis HS300x humidity result
* @param quant - sensor quantity pointer
* @return quantity value
*/
static int32_t ReadHumidity(struct SensorQuantity *quant)
{
if (!quant)
return -1;
float result = 0.0;
if (quant->sdev->done->read != NULL) {
if (quant->sdev->status == SENSOR_DEVICE_PASSIVE) {
quant->sdev->done->read(quant->sdev, 4);
quant->sdev->done->read(quant->sdev, 4); /* It takes two reads to get the data right */
result = ((quant->sdev->buffer[0] << 8 | quant->sdev->buffer[1] ) & 0x3fff) * 100.0 / ( (1 << 14) - 1);
return (int32_t)(result * 10);
}
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 HS300x humidity quantity and register
* @return 0
*/
int Hs300xHumidityInit(void)
{
SensorDeviceHs300xInit();
hs300x_humidity.name = SENSOR_QUANTITY_HS300X_HUMIDITY;
hs300x_humidity.type = SENSOR_QUANTITY_HUMI;
hs300x_humidity.value.decimal_places = 1;
hs300x_humidity.value.max_std = 1000;
hs300x_humidity.value.min_std = 0;
hs300x_humidity.value.last_value = SENSOR_QUANTITY_VALUE_ERROR;
hs300x_humidity.value.max_value = SENSOR_QUANTITY_VALUE_ERROR;
hs300x_humidity.value.min_value = SENSOR_QUANTITY_VALUE_ERROR;
hs300x_humidity.sdev = &hs300x;
hs300x_humidity.ReadValue = ReadHumidity;
SensorQuantityRegister(&hs300x_humidity);
return 0;
}

View File

@@ -0,0 +1,30 @@
config SENSOR_PS5308
bool "Using PS5308"
default n
if SENSOR_PS5308
config SENSOR_DEVICE_PS5308
string "PS5308 sensor name"
default "ps5308_1"
config SENSOR_QUANTITY_PS5308_PM1_0
string "PS5308 quantity PM1.0 name"
default "pm1_0_1"
config SENSOR_QUANTITY_PS5308_PM2_5
string "PS5308 quantity PM2.5 name"
default "pm2_5_1"
config SENSOR_QUANTITY_PS5308_PM10
string "PS5308 quantity PM10 name"
default "pm10_1"
config SENSOR_DEVICE_PS5308_DEV
string "PS5308 device name"
default "/dev/uart2_dev2"
config SENSOR_DEVICE_PS5308_DEV_EXT_PORT
int "if PS5308 device using extuart, choose port"
default "4"
endif

View File

@@ -0,0 +1,5 @@
ifeq ($(CONFIG_SENSOR_PS5308),y)
SRC_DIR += ps5308
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,3 @@
SRC_FILES := ps5308.c
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,359 @@
/*
* 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 ps5308.c
* @brief PS5308 PM1.0, PM2.5, PM10, driver base perception
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021.04.22
*/
#include <sensor.h>
static struct SensorDevice ps5308;
static int32_t active_task_id;
static int buff_lock;
static struct SensorProductInfo info =
{
SENSOR_ABILITY_PM,
"Pulsensor",
"PS5308",
};
/**
* @description: Read sensor task
* @param sdev - sensor device pointer
*/
static void ReadTask(struct SensorDevice *sdev)
{
while (1) {
UserMutexObtain(buff_lock, WAITING_FOREVER);
sdev->done->read(sdev, 32);
UserMutexAbandon(buff_lock);
UserTaskDelay(750);
}
}
/**
* @description: Open PS5308 sensor device
* @param sdev - sensor device pointer
* @return success: 1 , failure: other
*/
static int SensorDeviceOpen(struct SensorDevice *sdev)
{
int result = 1;
buff_lock = UserMutexCreate();
sdev->fd = open(SENSOR_DEVICE_PS5308_DEV, O_RDWR);
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;
cfg.ext_uart_no = SENSOR_DEVICE_PS5308_DEV_EXT_PORT;
cfg.port_configure = PORT_CFG_INIT;
result = ioctl(sdev->fd, OPE_INT, &cfg);
UtaskType active_task;
const char name[NAME_NUM_MAX] = "ps5308_task";
strncpy(active_task.name, name, strlen(name));
active_task.func_entry = ReadTask;
active_task.func_param = sdev;
active_task.prio = KTASK_PRIORITY_MAX/2;
active_task.stack_size = 1024;
active_task_id = UserTaskCreate(active_task);
result = UserTaskStartup(active_task_id);
return result;
}
/**
* @description: Close PS5308 sensor device
* @param sdev - sensor device pointer
* @return 1
*/
static int SensorDeviceClose(struct SensorDevice *sdev)
{
UserTaskDelete(active_task_id);
UserMutexDelete(buff_lock);
return 1;
}
/**
* @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;
int timeout = 0;
while (1) {
read(sdev->fd, &tmp, 1);
if ((tmp == 0x44) || (timeout >= 1000))
break;
UserTaskDelay(10);
++timeout;
}
if (tmp != 0x44)
return -1;
uint8_t idx = 0;
sdev->buffer[idx++] = tmp;
while ((idx < len) && (timeout < 1000)) {
if (read(sdev->fd, &tmp, 1) == 1) {
timeout = 0;
sdev->buffer[idx++] = tmp;
}
++timeout;
}
return idx;
}
static struct SensorDone SensorDeviceDone =
{
SensorDeviceOpen,
SensorDeviceClose,
SensorDeviceRead,
NULL,
NULL,
};
/**
* @description: Init PS5308 sensor and register
* @return void
*/
static void Ps5308Init(void)
{
ps5308.name = SENSOR_DEVICE_PS5308;
ps5308.info = &info;
ps5308.done = &SensorDeviceDone;
ps5308.status = SENSOR_DEVICE_ACTIVE;
SensorDeviceRegister(&ps5308);
}
#ifdef SENSOR_QUANTITY_PS5308_PM1_0
static struct SensorQuantity ps5308_pm1_0;
/**
* @description: Analysis PS5308 PM1.0 result
* @param quant - sensor quantity pointer
* @return quantity value
*/
static int32_t ReadPm1_0(struct SensorQuantity *quant)
{
if (!quant)
return -1;
uint32_t result;
if (quant->sdev->done->read != NULL) {
uint16_t checksum = 0;
UserMutexObtain(buff_lock, WAITING_FOREVER);
for (uint8_t i = 0; i < 30; i++)
checksum += quant->sdev->buffer[i];
if (checksum == (((uint16_t)quant->sdev->buffer[30] << 8) + ((uint16_t)quant->sdev->buffer[31]))) {
result = ((uint16_t)quant->sdev->buffer[4] << 8) + (uint16_t)quant->sdev->buffer[5];
if (result > quant->value.max_value)
quant->value.max_value = result;
else if (result < quant->value.min_value)
quant->value.min_value = result;
quant->value.last_value = result;
return result;
}else{
printf("This reading is wrong\n");
result = SENSOR_QUANTITY_VALUE_ERROR;
return result;
}
}else{
printf("%s don't have read done.\n", quant->name);
}
return -1;
}
/**
* @description: Init PS5308 PM1.0 quantity and register
* @return 0
*/
int Ps5308Pm1_0Init(void)
{
Ps5308Init();
ps5308_pm1_0.name = SENSOR_QUANTITY_PS5308_PM1_0;
ps5308_pm1_0.type = SENSOR_QUANTITY_PM;
ps5308_pm1_0.value.decimal_places = 0;
ps5308_pm1_0.value.max_std = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm1_0.value.min_std = 0;
ps5308_pm1_0.value.last_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm1_0.value.max_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm1_0.value.min_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm1_0.sdev = &ps5308;
ps5308_pm1_0.ReadValue = ReadPm1_0;
SensorQuantityRegister(&ps5308_pm1_0);
return 0;
}
#endif
#ifdef SENSOR_QUANTITY_PS5308_PM2_5
static struct SensorQuantity ps5308_pm2_5;
/**
* @description: Analysis PS5308 PM2.5 result
* @param quant - sensor quantity pointer
* @return quantity value
*/
static int32_t ReadPm2_5(struct SensorQuantity *quant)
{
if (!quant)
return -1;
uint32_t result;
if (quant->sdev->done->read != NULL) {
uint16_t checksum = 0;
UserMutexObtain(buff_lock, WAITING_FOREVER);
for (uint i = 0; i < 30; i++)
checksum += quant->sdev->buffer[i];
if (checksum == (((uint16_t)quant->sdev->buffer[30] << 8) + ((uint16_t)quant->sdev->buffer[31]))) {
result = ((uint16_t)quant->sdev->buffer[6] << 8) + (uint16_t)quant->sdev->buffer[7];
if (result > quant->value.max_value)
quant->value.max_value = result;
else if (result < quant->value.min_value)
quant->value.min_value = result;
quant->value.last_value = result;
return result;
}else{
printf("This reading is wrong\n");
result = SENSOR_QUANTITY_VALUE_ERROR;
return result;
}
}else{
printf("%s don't have read done.\n", quant->name);
}
return -1;
}
/**
* @description: Init PS5308 PM2.5 quantity and register
* @return 0
*/
int Ps5308Pm2_5Init(void)
{
Ps5308Init();
ps5308_pm2_5.name = SENSOR_QUANTITY_PS5308_PM2_5;
ps5308_pm2_5.type = SENSOR_QUANTITY_PM;
ps5308_pm2_5.value.decimal_places = 0;
ps5308_pm2_5.value.max_std = 35;
ps5308_pm2_5.value.min_std = 0;
ps5308_pm2_5.value.last_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm2_5.value.max_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm2_5.value.min_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm2_5.sdev = &ps5308;
ps5308_pm2_5.ReadValue = ReadPm2_5;
SensorQuantityRegister(&ps5308_pm2_5);
return 0;
}
#endif
#ifdef SENSOR_QUANTITY_PS5308_PM10
static struct SensorQuantity ps5308_pm10;
/**
* @description: Analysis PS5308 PM10 result
* @param quant - sensor quantity pointer
* @return quantity value
*/
static int32_t ReadPm10(struct SensorQuantity *quant)
{
if (!quant)
return -1;
uint32_t result;
if (quant->sdev->done->read != NULL) {
uint16_t checksum = 0;
UserMutexObtain(buff_lock, WAITING_FOREVER);
for (uint i = 0; i < 30; i++)
checksum += quant->sdev->buffer[i];
if (checksum == (((uint16_t)quant->sdev->buffer[30] << 8) + ((uint16_t)quant->sdev->buffer[31]))) {
result = ((uint16_t)quant->sdev->buffer[8] << 8) + (uint16_t)quant->sdev->buffer[9];
if (result > quant->value.max_value)
quant->value.max_value = result;
else if (result < quant->value.min_value)
quant->value.min_value = result;
quant->value.last_value = result;
return result;
}else{
printf("This reading is wrong\n");
result = SENSOR_QUANTITY_VALUE_ERROR;
return result;
}
}else{
printf("%s don't have read done.\n", quant->name);
}
return -1;
}
/**
* @description: Init PS5308 PM10 quantity and register
* @return 0
*/
int Ps5308Pm10Init(void)
{
Ps5308Init();
ps5308_pm10.name = SENSOR_QUANTITY_PS5308_PM10;
ps5308_pm10.type = SENSOR_QUANTITY_PM;
ps5308_pm10.value.decimal_places = 0;
ps5308_pm10.value.max_std = 75;
ps5308_pm10.value.min_std = 0;
ps5308_pm10.value.last_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm10.value.max_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm10.value.min_value = SENSOR_QUANTITY_VALUE_ERROR;
ps5308_pm10.sdev = &ps5308;
ps5308_pm10.ReadValue = ReadPm10;
SensorQuantityRegister(&ps5308_pm10);
return 0;
}
#endif

View File

@@ -0,0 +1,18 @@
config SENSOR_HS300X
bool "Using HS300x"
default n
if SENSOR_HS300X
config SENSOR_DEVICE_HS300X
string "HS300x sensor name"
default "hs300x_1"
config SENSOR_QUANTITY_HS300X_TEMPERATURE
string "HS300x quantity name"
default "temperature_1"
config SENSOR_DEVICE_HS300X_DEV
string "HS300x device name"
default "/dev/i2c1_dev0"
endif

View File

@@ -0,0 +1,5 @@
ifeq ($(CONFIG_SENSOR_HS300X),y)
SRC_DIR += hs300x_temp
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,3 @@
SRC_FILES := hs300x_temp.c
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,141 @@
/*
* 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 hs300x_temp.c
* @brief HS300x temperature driver base perception
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021.04.22
*/
#include <sensor.h>
static struct SensorDevice hs300x;
static struct SensorProductInfo info =
{
(SENSOR_ABILITY_HUMI | SENSOR_ABILITY_TEMP),
"Renesas",
"HS300x",
};
/**
* @description: Open HS300x sensor device
* @param sdev - sensor device pointer
* @return 1
*/
static int SensorDeviceOpen(struct SensorDevice *sdev)
{
sdev->fd = PrivOpen(SENSOR_DEVICE_HS300X_DEV, O_RDWR);
return 1;
}
/**
* @description: Read sensor device
* @param sdev - sensor device pointer
* @param len - the length of the read data
* @return success: 1 , failure: -1
*/
static int SensorDeviceRead(struct SensorDevice *sdev, size_t len)
{
if (PrivWrite(sdev->fd, NULL, 0) != 1)
return -1;
UserTaskDelay(50);
if (PrivRead(sdev->fd, sdev->buffer, len) != 1)
return -1;
return 1;
}
static struct SensorDone done =
{
SensorDeviceOpen,
NULL,
SensorDeviceRead,
NULL,
NULL,
};
/**
* @description: Init HS300x sensor and register
* @return void
*/
static void SensorDeviceHs300xInit(void)
{
hs300x.name = SENSOR_DEVICE_HS300X;
hs300x.info = &info;
hs300x.done = &done;
hs300x.status = SENSOR_DEVICE_PASSIVE;
SensorDeviceRegister(&hs300x);
}
static struct SensorQuantity hs300x_temperature;
/**
* @description: Analysis HS300x temperature result
* @param quant - sensor quantity pointer
* @return quantity value
*/
static int32_t ReadTemperature(struct SensorQuantity *quant)
{
if (!quant)
return -1;
float result;
if (quant->sdev->done->read != NULL) {
if (quant->sdev->status == SENSOR_DEVICE_PASSIVE) {
quant->sdev->done->read(quant->sdev, 4);
quant->sdev->done->read(quant->sdev, 4); /* It takes two reads to get the data right */
result = ((quant->sdev->buffer[2] << 8 | quant->sdev->buffer[3]) >> 2) * 165.0 /( (1 << 14) - 1) - 40.0;
return (int32_t)(result * 10);
}
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 HS300x temperature quantity and register
* @return 0
*/
int Hs300xTemperatureInit(void)
{
SensorDeviceHs300xInit();
hs300x_temperature.name = SENSOR_QUANTITY_HS300X_TEMPERATURE;
hs300x_temperature.type = SENSOR_QUANTITY_TEMP;
hs300x_temperature.value.decimal_places = 1;
hs300x_temperature.value.max_std = 1000;
hs300x_temperature.value.min_std = 0;
hs300x_temperature.value.last_value = SENSOR_QUANTITY_VALUE_ERROR;
hs300x_temperature.value.max_value = SENSOR_QUANTITY_VALUE_ERROR;
hs300x_temperature.value.min_value = SENSOR_QUANTITY_VALUE_ERROR;
hs300x_temperature.sdev = &hs300x;
hs300x_temperature.ReadValue = ReadTemperature;
SensorQuantityRegister(&hs300x_temperature);
return 0;
}

View File

@@ -0,0 +1,22 @@
config SENSOR_D124
bool "Using D124"
default n
if SENSOR_D124
config SENSOR_DEVICE_D124
string "D124 sensor name"
default "d124_1"
config SENSOR_QUANTITY_D124_VOICE
string "D124 quantity PM1.0 name"
default "voice_1"
config SENSOR_DEVICE_D124_DEV
string "D124 device name"
default "/dev/extuart_dev4"
config SENSOR_DEVICE_D124_DEV_EXT_PORT
int "if D124 device using extuart, choose port"
default "4"
endif

View File

@@ -0,0 +1,5 @@
ifeq ($(CONFIG_SENSOR_D124),y)
SRC_DIR += d124
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -0,0 +1,3 @@
SRC_FILES := d124.c
include $(KERNEL_ROOT)/compiler.mk

View File

@@ -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 d124.c
* @brief D124 voice driver base perception
* @version 1.0
* @author AIIT XUOS Lab
* @date 2021.04.22
*/
#include <sensor.h>
static struct SensorDevice d124;
static int32_t active_task_id;
static int buff_lock;
static struct SensorProductInfo info =
{
SENSOR_ABILITY_VOICE,
"龙戈电子",
"D124",
};
/**
* @description: Read sensor task
* @param sdev - sensor device pointer
*/
static void ReadTask(struct SensorDevice *sdev)
{
while (1) {
UserMutexObtain(buff_lock, WAITING_FOREVER);
sdev->done->read(sdev, 5);
UserMutexAbandon(buff_lock);
UserTaskDelay(750);
}
}
/**
* @description: Open D124 voice device
* @param sdev - sensor device pointer
* @return success: 1 , failure: other
*/
static int SensorDeviceOpen(struct SensorDevice *sdev)
{
int result = 1;
buff_lock = UserMutexCreate();
sdev->fd = PrivOpen(SENSOR_DEVICE_D124_DEV, O_RDWR);
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 = 64;
cfg.serial_parity_mode = PARITY_NONE;
cfg.serial_bit_order = 0;
cfg.serial_invert_mode = 0;
cfg.ext_uart_no = SENSOR_DEVICE_D124_DEV_EXT_PORT;
cfg.port_configure = PORT_CFG_INIT;
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
ioctl_cfg.args = &cfg;
result = PrivIoctl(sdev->fd, OPE_INT, &ioctl_cfg);
UtaskType active_task;
const char name[NAME_NUM_MAX] = "d124_task";
strncpy(active_task.name, name, strlen(name));
active_task.func_entry = ReadTask;
active_task.func_param = sdev;
active_task.prio = KTASK_PRIORITY_MAX/2;
active_task.stack_size = 2048;
active_task_id = UserTaskCreate(active_task);
result = UserTaskStartup(active_task_id);
return result;
}
/**
* @description: Close D124 sensor device
* @param sdev - sensor device pointer
* @return 1
*/
static int SensorDeviceClose(struct SensorDevice *sdev)
{
UserTaskDelete(active_task_id);
UserMutexDelete(buff_lock);
return 1;
}
/**
* @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;
int timeout = 0;
while (1) {
PrivRead(sdev->fd, &tmp, 1);
if ((tmp == 0xAA) || (timeout >= 1000))
break;
UserTaskDelay(10);
++timeout;
}
if(tmp != 0xAA)
return -1;
uint8_t idx = 0;
sdev->buffer[idx++] = tmp;
while ((idx < len) && (timeout < 1000)) {
if (PrivRead(sdev->fd, &tmp, 1) == 1) {
timeout = 0;
sdev->buffer[idx++] = tmp;
}
++timeout;
}
return idx;
}
static struct SensorDone done =
{
SensorDeviceOpen,
SensorDeviceClose,
SensorDeviceRead,
NULL,
NULL,
};
/**
* @description: Init D124 sensor and register
* @return void
*/
static void D124Init(void)
{
d124.name = SENSOR_DEVICE_D124;
d124.info = &info;
d124.done = &done;
d124.status = SENSOR_DEVICE_ACTIVE;
SensorDeviceRegister(&d124);
}
static struct SensorQuantity d124_voice;
/**
* @description: Analysis D124 voice result
* @param quant - sensor quantity pointer
* @return quantity value
*/
static int32_t ReadVoice(struct SensorQuantity *quant)
{
if (!quant)
return -1;
uint32_t result;
if (quant->sdev->done->read != NULL) {
UserMutexObtain(buff_lock, WAITING_FOREVER);
if (quant->sdev->buffer[3] == quant->sdev->buffer[1] + quant->sdev->buffer[2]) {
result = ((uint16_t)quant->sdev->buffer[1] << 8) + (uint16_t)quant->sdev->buffer[2];
if (result > quant->value.max_value)
quant->value.max_value = result;
else if (result < quant->value.min_value)
quant->value.min_value = result;
quant->value.last_value = result;
return result;
}else{
printf("This reading is wrong\n");
result = SENSOR_QUANTITY_VALUE_ERROR;
return result;
}
}else{
printf("%s don't have read done.\n", quant->name);
}
return -1;
}
/**
* @description: Init D124 voice quantity and register
* @return 0
*/
int D124VoiceInit(void)
{
D124Init();
d124_voice.name = SENSOR_QUANTITY_D124_VOICE;
d124_voice.type = SENSOR_QUANTITY_VOICE;
d124_voice.value.decimal_places = 1;
d124_voice.value.max_std = 600;
d124_voice.value.min_std = 0;
d124_voice.value.last_value = SENSOR_QUANTITY_VALUE_ERROR;
d124_voice.value.max_value = SENSOR_QUANTITY_VALUE_ERROR;
d124_voice.value.min_value = SENSOR_QUANTITY_VALUE_ERROR;
d124_voice.sdev = &d124;
d124_voice.ReadValue = ReadVoice;
SensorQuantityRegister(&d124_voice);
return 0;
}