Skip to content
Permalink
Browse files

sensors: ti_hdc driver wait conversion support

TI_HDC Driver now also supports waiting for conversion to finish instead
of waiting for GPIO interrupt.

Signed-off-by: Nikos Oikonomou <nikoikonomou92@gmail.com>
  • Loading branch information...
nikooiko authored and MaureenHelm committed Jun 5, 2019
1 parent 4a38cae commit 080e71c908b8a67c0ceb772e04ed2a4c088b5245
@@ -8,6 +8,6 @@

menuconfig TI_HDC
bool "Texas Instruments Temperature and Humidity Sensor (e.g. HDC1008)"
depends on I2C && HAS_DTS_I2C && GPIO && HAS_DTS_GPIO
depends on I2C && HAS_DTS_I2C
help
Enable driver for TI temperature and humidity sensors.
@@ -18,6 +18,7 @@
#define LOG_LEVEL CONFIG_SENSOR_LOG_LEVEL
LOG_MODULE_REGISTER(TI_HDC);

#if defined(DT_TI_HDC_0_DRDY_GPIOS_CONTROLLER)
static void ti_hdc_gpio_callback(struct device *dev,
struct gpio_callback *cb, u32_t pins)
{
@@ -29,6 +30,7 @@ static void ti_hdc_gpio_callback(struct device *dev,
gpio_pin_disable_callback(dev, DT_TI_HDC_0_DRDY_GPIOS_PIN);
k_sem_give(&drv_data->data_sem);
}
#endif

static int ti_hdc_sample_fetch(struct device *dev, enum sensor_channel chan)
{
@@ -37,8 +39,9 @@ static int ti_hdc_sample_fetch(struct device *dev, enum sensor_channel chan)

__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);

gpio_pin_enable_callback(drv_data->gpio,
DT_TI_HDC_0_DRDY_GPIOS_PIN);
#if defined(DT_TI_HDC_0_DRDY_GPIOS_CONTROLLER)
gpio_pin_enable_callback(drv_data->gpio, DT_TI_HDC_0_DRDY_GPIOS_PIN);
#endif

buf[0] = TI_HDC_REG_TEMP;
if (i2c_write(drv_data->i2c, buf, 1,
@@ -47,7 +50,12 @@ static int ti_hdc_sample_fetch(struct device *dev, enum sensor_channel chan)
return -EIO;
}

#if defined(DT_TI_HDC_0_DRDY_GPIOS_CONTROLLER)
k_sem_take(&drv_data->data_sem, K_FOREVER);
#else
/* wait for the conversion to finish */
k_sleep(HDC_CONVERSION_TIME);
#endif

if (i2c_read(drv_data->i2c, buf, 4, DT_TI_HDC_0_BASE_ADDRESS) < 0) {
LOG_DBG("Failed to read sample data");
@@ -62,8 +70,8 @@ static int ti_hdc_sample_fetch(struct device *dev, enum sensor_channel chan)


static int ti_hdc_channel_get(struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
enum sensor_channel chan,
struct sensor_value *val)
{
struct ti_hdc_data *drv_data = dev->driver_data;
u64_t tmp;
@@ -129,6 +137,7 @@ static int ti_hdc_init(struct device *dev)
return -EINVAL;
}

#if defined(DT_TI_HDC_0_DRDY_GPIOS_CONTROLLER)
k_sem_init(&drv_data->data_sem, 0, UINT_MAX);

/* setup data ready gpio interrupt */
@@ -155,6 +164,9 @@ static int ti_hdc_init(struct device *dev)
LOG_DBG("Failed to set GPIO callback");
return -EIO;
}
#endif

LOG_INF("Initialized device successfully");

return 0;
}
@@ -9,23 +9,27 @@

#include <kernel.h>

#define TI_HDC_I2C_ADDRESS 0x40

#define TI_HDC_REG_TEMP 0x0
#define TI_HDC_REG_HUMIDITY 0x1
#define TI_HDC_REG_MANUFID 0xFE
#define TI_HDC_REG_DEVICEID 0xFF

#define TI_HDC_MANUFID 0x5449
#define TI_HDC_MANUFID 0x5449
#define TI_HDC_DEVICEID 0x1000

/* For 14bit conversion RH needs 6.5ms and Temp 6.35ms */
#define HDC_CONVERSION_TIME 13

struct ti_hdc_data {
struct device *i2c;
struct device *gpio;
struct gpio_callback gpio_cb;
u16_t t_sample;
u16_t rh_sample;

#if defined(DT_TI_HDC_0_DRDY_GPIOS_CONTROLLER)
struct device *gpio;
struct gpio_callback gpio_cb;
struct k_sem data_sem;
#endif /* DT_TI_HDC_0_DRDY_GPIOS_CONTROLLER */
};

#endif
@@ -19,6 +19,6 @@ properties:

drdy-gpios:
type: compound
category: required
category: optional
generation: define, use-prop-name
...

0 comments on commit 080e71c

Please sign in to comment.
You can’t perform that action at this time.