drivers: sensor: Add TMP116
Added driver for TI's TMP116. This driver does not use the Alert functionality. Signed-off-by: Ioannis Konstantelias <ikonstadel@gmail.com>
This commit is contained in:
parent
83d4dc8b5e
commit
556cf41021
8 changed files with 209 additions and 0 deletions
|
@ -55,6 +55,7 @@ add_subdirectory_ifdef(CONFIG_SX9500 sx9500)
|
|||
add_subdirectory_ifdef(CONFIG_TH02 th02)
|
||||
add_subdirectory_ifdef(CONFIG_TMP007 tmp007)
|
||||
add_subdirectory_ifdef(CONFIG_TMP112 tmp112)
|
||||
add_subdirectory_ifdef(CONFIG_TMP116 tmp116)
|
||||
add_subdirectory_ifdef(CONFIG_VL53L0X vl53l0x)
|
||||
|
||||
zephyr_sources_ifdef(CONFIG_USERSPACE sensor_handlers.c)
|
||||
|
|
|
@ -135,6 +135,8 @@ source "drivers/sensor/tmp007/Kconfig"
|
|||
|
||||
source "drivers/sensor/tmp112/Kconfig"
|
||||
|
||||
source "drivers/sensor/tmp116/Kconfig"
|
||||
|
||||
source "drivers/sensor/vl53l0x/Kconfig"
|
||||
|
||||
endif # SENSOR
|
||||
|
|
5
drivers/sensor/tmp116/CMakeLists.txt
Normal file
5
drivers/sensor/tmp116/CMakeLists.txt
Normal file
|
@ -0,0 +1,5 @@
|
|||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
|
||||
zephyr_library_sources_ifdef(CONFIG_TMP116 tmp116.c)
|
13
drivers/sensor/tmp116/Kconfig
Normal file
13
drivers/sensor/tmp116/Kconfig
Normal file
|
@ -0,0 +1,13 @@
|
|||
# Kconfig - TMP116 temperature sensor configuration options
|
||||
|
||||
#
|
||||
# Copyright (c) 2019 Centaur Analytics, Inc
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
menuconfig TMP116
|
||||
bool "TMP116 Temperature Sensor"
|
||||
depends on I2C && HAS_DTS_I2C
|
||||
help
|
||||
Enable driver for TMP116 temperature sensor.
|
145
drivers/sensor/tmp116/tmp116.c
Normal file
145
drivers/sensor/tmp116/tmp116.c
Normal file
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Centaur Analytics, Inc
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <device.h>
|
||||
#include <drivers/i2c.h>
|
||||
#include <drivers/sensor.h>
|
||||
#include <misc/util.h>
|
||||
#include <misc/byteorder.h>
|
||||
#include <misc/__assert.h>
|
||||
#include <logging/log.h>
|
||||
#include <kernel.h>
|
||||
|
||||
#include "tmp116.h"
|
||||
|
||||
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
|
||||
LOG_MODULE_REGISTER(TMP116);
|
||||
|
||||
static int tmp116_reg_read(struct device *dev, u8_t reg, u16_t *val)
|
||||
{
|
||||
struct tmp116_data *drv_data = dev->driver_data;
|
||||
const struct tmp116_dev_config *cfg = dev->config->config_info;
|
||||
|
||||
if (i2c_burst_read(drv_data->i2c, cfg->i2c_addr, reg, (u8_t *)val, 2)
|
||||
< 0) {
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
*val = sys_be16_to_cpu(*val);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check the Device ID
|
||||
*
|
||||
* @param[in] dev Pointer to the device structure
|
||||
*
|
||||
* @retval 0 On success
|
||||
* @retval -EIO Otherwise
|
||||
*/
|
||||
static inline int tmp116_device_id_check(struct device *dev)
|
||||
{
|
||||
u16_t value;
|
||||
|
||||
if (tmp116_reg_read(dev, TMP116_REG_DEVICE_ID, &value) != 0) {
|
||||
LOG_ERR("%s: Failed to get Device ID register!",
|
||||
DT_INST_0_TI_TMP116_LABEL);
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
if (value != TMP116_DEVICE_ID) {
|
||||
LOG_ERR("%s: Failed to match the device IDs!",
|
||||
DT_INST_0_TI_TMP116_LABEL);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tmp116_sample_fetch(struct device *dev, enum sensor_channel chan)
|
||||
{
|
||||
struct tmp116_data *drv_data = dev->driver_data;
|
||||
u16_t value;
|
||||
int rc;
|
||||
|
||||
__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL ||
|
||||
chan == SENSOR_CHAN_AMBIENT_TEMP);
|
||||
|
||||
/* clear sensor values */
|
||||
drv_data->sample = 0U;
|
||||
|
||||
/* Get the most recent temperature measurement */
|
||||
rc = tmp116_reg_read(dev, TMP116_REG_TEMP, &value);
|
||||
if (rc < 0) {
|
||||
LOG_ERR("%s: Failed to read from TEMP register!",
|
||||
DT_INST_0_TI_TMP116_LABEL);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* store measurements to the driver */
|
||||
drv_data->sample = (s16_t)value;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tmp116_channel_get(struct device *dev, enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct tmp116_data *drv_data = dev->driver_data;
|
||||
s32_t tmp;
|
||||
|
||||
if (chan != SENSOR_CHAN_AMBIENT_TEMP) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
/*
|
||||
* See datasheet "Temperature Results and Limits" section for more
|
||||
* details on processing sample data.
|
||||
*/
|
||||
tmp = (s32_t)drv_data->sample * TMP116_RESOLUTION;
|
||||
val->val1 = tmp / 10000000; /* Tens of uCelsius */
|
||||
val->val2 = tmp % 10000000;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api tmp116_driver_api = {
|
||||
.sample_fetch = tmp116_sample_fetch,
|
||||
.channel_get = tmp116_channel_get
|
||||
};
|
||||
|
||||
static int tmp116_init(struct device *dev)
|
||||
{
|
||||
struct tmp116_data *drv_data = dev->driver_data;
|
||||
int rc;
|
||||
|
||||
/* Bind to the I2C bus that the sensor is connected */
|
||||
drv_data->i2c = device_get_binding(DT_INST_0_TI_TMP116_BUS_NAME);
|
||||
if (!drv_data->i2c) {
|
||||
LOG_ERR("Cannot bind to %s device!",
|
||||
DT_INST_0_TI_TMP116_BUS_NAME);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* Check the Device ID */
|
||||
rc = tmp116_device_id_check(dev);
|
||||
if (rc < 0) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct tmp116_data tmp116_data;
|
||||
|
||||
static const struct tmp116_dev_config tmp116_config = {
|
||||
.i2c_addr = DT_INST_0_TI_TMP116_BASE_ADDRESS,
|
||||
};
|
||||
|
||||
DEVICE_AND_API_INIT(hdc1080, DT_INST_0_TI_TMP116_LABEL, tmp116_init,
|
||||
&tmp116_data, &tmp116_config, POST_KERNEL,
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &tmp116_driver_api);
|
35
drivers/sensor/tmp116/tmp116.h
Normal file
35
drivers/sensor/tmp116/tmp116.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Centaur Analytics, Inc
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_
|
||||
#define ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_
|
||||
|
||||
#define TMP116_REG_TEMP 0x0
|
||||
#define TMP116_REG_CFGR 0x1
|
||||
#define TMP116_REG_HIGH_LIM 0x2
|
||||
#define TMP116_REG_LOW_LIM 0x3
|
||||
#define TMP116_REG_EEPROM_UL 0x4
|
||||
#define TMP116_REG_EEPROM1 0x5
|
||||
#define TMP116_REG_EEPROM2 0x6
|
||||
#define TMP116_REG_EEPROM3 0x7
|
||||
#define TMP116_REG_EEPROM4 0x8
|
||||
#define TMP116_REG_DEVICE_ID 0xF
|
||||
|
||||
#define TMP116_RESOLUTION 78125 /* in tens of uCelsius*/
|
||||
#define TMP116_RESOLUTION_DIV 10000000
|
||||
|
||||
#define TMP116_DEVICE_ID 0x1116
|
||||
|
||||
struct tmp116_data {
|
||||
struct device *i2c;
|
||||
u16_t sample;
|
||||
};
|
||||
|
||||
struct tmp116_dev_config {
|
||||
u16_t i2c_addr;
|
||||
};
|
||||
|
||||
#endif /* ZEPHYR_DRIVERS_SENSOR_TMP116_TMP116_H_ */
|
|
@ -258,6 +258,13 @@
|
|||
#define DT_INST_0_TI_HDC_BASE_ADDRESS 0
|
||||
#endif
|
||||
|
||||
#ifndef DT_INST_0_TI_TMP116_LABEL
|
||||
#define DT_INST_0_TI_TMP116_LABEL ""
|
||||
#define DT_INST_0_TI_TMP116_BASE_ADDRESS 0
|
||||
#define DT_INST_0_TI_TMP116_BUS_NAME ""
|
||||
#define DT_TI_TMP116_BUS_I2C 1
|
||||
#endif
|
||||
|
||||
#endif /* CONFIG_HAS_DTS_I2C */
|
||||
|
||||
#ifndef DT_ADXL372_DEV_NAME
|
||||
|
|
|
@ -32,4 +32,5 @@ CONFIG_TH02=y
|
|||
CONFIG_TI_HDC=y
|
||||
CONFIG_TMP007=y
|
||||
CONFIG_TMP112=y
|
||||
CONFIG_TMP116=y
|
||||
CONFIG_VL53L0X=y
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue