sensors: add TH02 temperature sensor (Grove)
Support for the Temperature&Humidity Sensor (High-Accuracy &Mini) v1.0 sensor, see: http://wiki.seeed.cc/Grove-TemptureAndHumidity_Sensor-High-Accuracy_AndMini-v1.0/ Change-Id: I439eafc863116f21e3b3385175d1935e60a5e9b1 Signed-off-by: Anas Nashif <anas.nashif@intel.com>
This commit is contained in:
parent
d14e4522eb
commit
915eac4625
6 changed files with 253 additions and 0 deletions
|
@ -96,6 +96,8 @@ source "drivers/sensor/Kconfig.sht3xd"
|
|||
|
||||
source "drivers/sensor/Kconfig.sx9500"
|
||||
|
||||
source "drivers/sensor/th02/Kconfig"
|
||||
|
||||
source "drivers/sensor/Kconfig.tmp007"
|
||||
|
||||
source "drivers/sensor/Kconfig.tmp112"
|
||||
|
|
|
@ -37,6 +37,7 @@ obj-$(CONFIG_SHT3XD) += sensor_sht3xd.o
|
|||
obj-$(CONFIG_SHT3XD_TRIGGER) += sensor_sht3xd_trigger.o
|
||||
obj-$(CONFIG_SX9500) += sensor_sx9500.o
|
||||
obj-$(CONFIG_SX9500_TRIGGER) += sensor_sx9500_trigger.o
|
||||
obj-$(CONFIG_TH02) += th02/
|
||||
obj-$(CONFIG_TMP007) += sensor_tmp007.o
|
||||
obj-$(CONFIG_TMP007_TRIGGER) += sensor_tmp007_trigger.o
|
||||
obj-$(CONFIG_TMP112) += sensor_tmp112.o
|
||||
|
|
41
drivers/sensor/th02/Kconfig
Normal file
41
drivers/sensor/th02/Kconfig
Normal file
|
@ -0,0 +1,41 @@
|
|||
#
|
||||
# Copyright (c) 2016 Intel Corporation
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
menuconfig TH02
|
||||
bool
|
||||
prompt "TH02 Temperature Sensor"
|
||||
default n
|
||||
depends on SENSOR && I2C && NANO_TIMERS && NANO_TIMEOUTS
|
||||
help
|
||||
Enable driver for the TH02 temperature sensor.
|
||||
|
||||
if TH02
|
||||
config TH02_NAME
|
||||
string
|
||||
prompt "Driver name"
|
||||
default "TH02"
|
||||
help
|
||||
Device name with which the TH02 sensor is identified.
|
||||
|
||||
config TH02_I2C_MASTER_DEV_NAME
|
||||
string
|
||||
prompt "I2C Master"
|
||||
default "I2C_0"
|
||||
help
|
||||
The device name of the I2C master device to which the TH02
|
||||
chip is connected.
|
||||
|
||||
endif
|
1
drivers/sensor/th02/Makefile
Normal file
1
drivers/sensor/th02/Makefile
Normal file
|
@ -0,0 +1 @@
|
|||
obj-y += th02.o
|
161
drivers/sensor/th02/th02.c
Normal file
161
drivers/sensor/th02/th02.c
Normal file
|
@ -0,0 +1,161 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <nanokernel.h>
|
||||
#include <device.h>
|
||||
#include <i2c.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <misc/util.h>
|
||||
#include <sensor.h>
|
||||
#include <misc/__assert.h>
|
||||
|
||||
#include "th02.h"
|
||||
|
||||
uint16_t read16(struct device *dev, uint8_t d)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
|
||||
if (i2c_burst_read(dev, TH02_I2C_DEV_ID, d, (uint8_t *)buf, 2) < 0) {
|
||||
SYS_LOG_ERR("Error reading register.");
|
||||
}
|
||||
return (buf[0] << 8 | buf[1]);
|
||||
}
|
||||
|
||||
uint8_t read8(struct device *dev, uint8_t d)
|
||||
{
|
||||
uint8_t buf;
|
||||
|
||||
if (i2c_reg_read_byte(dev, TH02_I2C_DEV_ID, d, &buf) < 0) {
|
||||
SYS_LOG_ERR("Error reading register.");
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
int is_ready(struct device *dev)
|
||||
{
|
||||
|
||||
uint8_t status;
|
||||
|
||||
if (i2c_reg_read_byte(dev, TH02_I2C_DEV_ID,
|
||||
TH02_REG_STATUS, &status) < 0) {
|
||||
SYS_LOG_ERR("error reading status register");
|
||||
}
|
||||
|
||||
if (status & TH02_STATUS_RDY_MASK) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t get_humi(struct device *dev)
|
||||
{
|
||||
uint16_t humidity = 0;
|
||||
|
||||
if (i2c_reg_write_byte(dev, TH02_I2C_DEV_ID,
|
||||
TH02_REG_CONFIG, TH02_CMD_MEASURE_HUMI) < 0) {
|
||||
SYS_LOG_ERR("Error writing register");
|
||||
return 0;
|
||||
}
|
||||
while (!is_ready(dev)) {
|
||||
}
|
||||
;
|
||||
|
||||
humidity = read8(dev, TH02_REG_DATA_H) << 8;
|
||||
humidity |= read8(dev, TH02_REG_DATA_L);
|
||||
humidity >>= 4;
|
||||
|
||||
return humidity;
|
||||
}
|
||||
|
||||
uint16_t get_temp(struct device *dev)
|
||||
{
|
||||
uint16_t temperature = 0;
|
||||
|
||||
if (i2c_reg_write_byte(dev, TH02_I2C_DEV_ID,
|
||||
TH02_REG_CONFIG, TH02_CMD_MEASURE_TEMP) < 0) {
|
||||
SYS_LOG_ERR("Error writing register");
|
||||
return 0;
|
||||
}
|
||||
while (!is_ready(dev)) {
|
||||
}
|
||||
;
|
||||
|
||||
temperature = read8(dev, TH02_REG_DATA_H) << 8;
|
||||
temperature |= read8(dev, TH02_REG_DATA_L);
|
||||
temperature >>= 2;
|
||||
|
||||
return temperature;
|
||||
}
|
||||
|
||||
static int th02_sample_fetch(struct device *dev, enum sensor_channel chan)
|
||||
{
|
||||
struct th02_data *drv_data = dev->driver_data;
|
||||
|
||||
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL || chan == SENSOR_CHAN_TEMP);
|
||||
|
||||
drv_data->t_sample = get_temp(drv_data->i2c);
|
||||
SYS_LOG_INF("temp: %u", drv_data->t_sample);
|
||||
drv_data->rh_sample = get_humi(drv_data->i2c);
|
||||
SYS_LOG_INF("rh: %u", drv_data->rh_sample);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int th02_channel_get(struct device *dev, enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct th02_data *drv_data = dev->driver_data;
|
||||
|
||||
__ASSERT_NO_MSG(chan == SENSOR_CHAN_TEMP ||
|
||||
chan == SENSOR_CHAN_HUMIDITY);
|
||||
|
||||
if (chan == SENSOR_CHAN_TEMP) {
|
||||
val->type = SENSOR_VALUE_TYPE_DOUBLE;
|
||||
val->dval = (double)drv_data->t_sample / 32.0 - 50.0;
|
||||
} else {
|
||||
val->type = SENSOR_VALUE_TYPE_DOUBLE;
|
||||
val->dval = (double)drv_data->rh_sample / 16.0 - 24.0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct sensor_driver_api th02_driver_api = {
|
||||
.sample_fetch = th02_sample_fetch,
|
||||
.channel_get = th02_channel_get,
|
||||
};
|
||||
|
||||
int th02_init(struct device *dev)
|
||||
{
|
||||
struct th02_data *drv_data = dev->driver_data;
|
||||
|
||||
drv_data->i2c = device_get_binding(CONFIG_TH02_I2C_MASTER_DEV_NAME);
|
||||
if (drv_data->i2c == NULL) {
|
||||
SYS_LOG_ERR("Failed to get pointer to %s device!",
|
||||
CONFIG_TH02_I2C_MASTER_DEV_NAME);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
dev->driver_api = &th02_driver_api;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct th02_data th02_driver;
|
||||
|
||||
DEVICE_INIT(th02, CONFIG_TH02_NAME, th02_init, &th02_driver,
|
||||
NULL, SECONDARY, CONFIG_SENSOR_INIT_PRIORITY);
|
47
drivers/sensor/th02/th02.h
Normal file
47
drivers/sensor/th02/th02.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _SENSOR_TH02
|
||||
#define _SENSOR_TH02
|
||||
|
||||
#include <device.h>
|
||||
#include <misc/util.h>
|
||||
|
||||
#define TH02_I2C_DEV_ID 0x40
|
||||
#define TH02_REG_STATUS 0x00
|
||||
#define TH02_REG_DATA_H 0x01
|
||||
#define TH02_REG_DATA_L 0x02
|
||||
#define TH02_REG_CONFIG 0x03
|
||||
#define TH02_REG_ID 0x11
|
||||
|
||||
#define TH02_STATUS_RDY_MASK 0x01
|
||||
|
||||
#define TH02_CMD_MEASURE_HUMI 0x01
|
||||
#define TH02_CMD_MEASURE_TEMP 0x11
|
||||
|
||||
#define TH02_WR_REG_MODE 0xC0
|
||||
#define TH02_RD_REG_MODE 0x80
|
||||
|
||||
struct th02_data {
|
||||
struct device *i2c;
|
||||
uint16_t t_sample;
|
||||
uint16_t rh_sample;
|
||||
};
|
||||
|
||||
#define SYS_LOG_DOMAIN "TH02"
|
||||
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_SENSOR_LEVEL
|
||||
#include <misc/sys_log.h>
|
||||
#endif /* _SENSOR_TH02_ */
|
Loading…
Add table
Add a link
Reference in a new issue