Skip to content

Commit

Permalink
sensors: ti_hdc driver wait conversion support
Browse files Browse the repository at this point in the history
TI_HDC Driver now also supports waiting for conversion to finish instead
of waiting for GPIO interrupt.

Signed-off-by: Nikos Oikonomou <[email protected]>
  • Loading branch information
nikooiko authored and MaureenHelm committed Jun 11, 2019
1 parent 4a38cae commit 080e71c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion drivers/sensor/ti_hdc/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
20 changes: 16 additions & 4 deletions drivers/sensor/ti_hdc/ti_hdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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,
Expand All @@ -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");
Expand All @@ -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;
Expand Down Expand Up @@ -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 */
Expand All @@ -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;
}
Expand Down
14 changes: 9 additions & 5 deletions drivers/sensor/ti_hdc/ti_hdc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion dts/bindings/sensor/ti,hdc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.