2016-05-24 17:09:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-05-24 17:09:22 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <device.h>
|
|
|
|
#include <i2c.h>
|
|
|
|
#include <misc/__assert.h>
|
|
|
|
#include <misc/util.h>
|
2016-11-10 21:21:34 +08:00
|
|
|
#include <kernel.h>
|
2016-05-24 17:09:22 +08:00
|
|
|
#include <sensor.h>
|
|
|
|
|
2016-10-15 20:19:56 +08:00
|
|
|
#include "hts221.h"
|
2016-05-24 17:09:22 +08:00
|
|
|
|
|
|
|
int hts221_trigger_set(struct device *dev,
|
|
|
|
const struct sensor_trigger *trig,
|
|
|
|
sensor_trigger_handler_t handler)
|
|
|
|
{
|
|
|
|
struct hts221_data *drv_data = dev->driver_data;
|
|
|
|
|
2016-07-28 07:56:08 +08:00
|
|
|
__ASSERT_NO_MSG(trig->type == SENSOR_TRIG_DATA_READY);
|
2016-05-24 17:09:22 +08:00
|
|
|
|
|
|
|
gpio_pin_disable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
|
|
|
|
|
|
|
|
drv_data->data_ready_handler = handler;
|
|
|
|
if (handler == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
drv_data->data_ready_trigger = *trig;
|
|
|
|
|
|
|
|
gpio_pin_enable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hts221_gpio_callback(struct device *dev,
|
2017-04-21 23:03:20 +08:00
|
|
|
struct gpio_callback *cb, u32_t pins)
|
2016-05-24 17:09:22 +08:00
|
|
|
{
|
|
|
|
struct hts221_data *drv_data =
|
|
|
|
CONTAINER_OF(cb, struct hts221_data, gpio_cb);
|
|
|
|
|
|
|
|
ARG_UNUSED(pins);
|
|
|
|
|
|
|
|
gpio_pin_disable_callback(dev, CONFIG_HTS221_GPIO_PIN_NUM);
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
#if defined(CONFIG_HTS221_TRIGGER_OWN_THREAD)
|
2016-11-10 21:21:34 +08:00
|
|
|
k_sem_give(&drv_data->gpio_sem);
|
2016-11-10 23:05:53 +08:00
|
|
|
#elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_THREAD)
|
2016-11-10 21:21:34 +08:00
|
|
|
k_work_submit(&drv_data->work);
|
2016-05-24 17:09:22 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
static void hts221_thread_cb(void *arg)
|
2016-05-24 17:09:22 +08:00
|
|
|
{
|
|
|
|
struct device *dev = arg;
|
|
|
|
struct hts221_data *drv_data = dev->driver_data;
|
|
|
|
|
|
|
|
if (drv_data->data_ready_handler != NULL) {
|
|
|
|
drv_data->data_ready_handler(dev,
|
|
|
|
&drv_data->data_ready_trigger);
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_pin_enable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
|
|
|
|
}
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
#ifdef CONFIG_HTS221_TRIGGER_OWN_THREAD
|
|
|
|
static void hts221_thread(int dev_ptr, int unused)
|
2016-05-24 17:09:22 +08:00
|
|
|
{
|
|
|
|
struct device *dev = INT_TO_POINTER(dev_ptr);
|
|
|
|
struct hts221_data *drv_data = dev->driver_data;
|
|
|
|
|
|
|
|
ARG_UNUSED(unused);
|
|
|
|
|
|
|
|
while (1) {
|
2016-11-10 21:21:34 +08:00
|
|
|
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
|
2016-11-10 23:05:53 +08:00
|
|
|
hts221_thread_cb(dev);
|
2016-05-24 17:09:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
#ifdef CONFIG_HTS221_TRIGGER_GLOBAL_THREAD
|
2016-11-10 21:21:34 +08:00
|
|
|
static void hts221_work_cb(struct k_work *work)
|
2016-05-24 17:09:22 +08:00
|
|
|
{
|
|
|
|
struct hts221_data *drv_data =
|
|
|
|
CONTAINER_OF(work, struct hts221_data, work);
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
hts221_thread_cb(drv_data->dev);
|
2016-05-24 17:09:22 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int hts221_init_interrupt(struct device *dev)
|
|
|
|
{
|
|
|
|
struct hts221_data *drv_data = dev->driver_data;
|
|
|
|
|
|
|
|
/* setup data ready gpio interrupt */
|
|
|
|
drv_data->gpio = device_get_binding(CONFIG_HTS221_GPIO_DEV_NAME);
|
|
|
|
if (drv_data->gpio == NULL) {
|
|
|
|
SYS_LOG_ERR("Cannot get pointer to %s device.",
|
|
|
|
CONFIG_HTS221_GPIO_DEV_NAME);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gpio_pin_configure(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM,
|
|
|
|
GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE |
|
|
|
|
GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE);
|
|
|
|
|
|
|
|
gpio_init_callback(&drv_data->gpio_cb,
|
|
|
|
hts221_gpio_callback,
|
|
|
|
BIT(CONFIG_HTS221_GPIO_PIN_NUM));
|
|
|
|
|
|
|
|
if (gpio_add_callback(drv_data->gpio, &drv_data->gpio_cb) < 0) {
|
|
|
|
SYS_LOG_ERR("Could not set gpio callback.");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable data-ready interrupt */
|
|
|
|
if (i2c_reg_write_byte(drv_data->i2c, HTS221_I2C_ADDR,
|
|
|
|
HTS221_REG_CTRL3, HTS221_DRDY_EN) < 0) {
|
|
|
|
SYS_LOG_ERR("Could not enable data-ready interrupt.");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2016-11-10 23:05:53 +08:00
|
|
|
#if defined(CONFIG_HTS221_TRIGGER_OWN_THREAD)
|
2016-11-10 21:21:34 +08:00
|
|
|
k_sem_init(&drv_data->gpio_sem, 0, UINT_MAX);
|
2016-05-24 17:09:22 +08:00
|
|
|
|
2017-05-10 02:59:40 +08:00
|
|
|
k_thread_create(&drv_data->thread, drv_data->thread_stack,
|
|
|
|
CONFIG_HTS221_THREAD_STACK_SIZE,
|
|
|
|
(k_thread_entry_t)hts221_thread, POINTER_TO_INT(dev),
|
|
|
|
0, NULL, K_PRIO_COOP(CONFIG_HTS221_THREAD_PRIORITY),
|
|
|
|
0, 0);
|
2016-11-10 23:05:53 +08:00
|
|
|
#elif defined(CONFIG_HTS221_TRIGGER_GLOBAL_THREAD)
|
2016-05-24 17:09:22 +08:00
|
|
|
drv_data->work.handler = hts221_work_cb;
|
|
|
|
drv_data->dev = dev;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
gpio_pin_enable_callback(drv_data->gpio, CONFIG_HTS221_GPIO_PIN_NUM);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|