drivers: sensor: adxl367: add trigger support
Add trigger support for the adxl367 Zephyr driver. Signed-off-by: Antoniu Miclaus <antoniu.miclaus@analog.com>
This commit is contained in:
parent
678b29386d
commit
af0b656709
|
@ -8,3 +8,4 @@ zephyr_library()
|
|||
zephyr_library_sources(adxl367.c)
|
||||
zephyr_library_sources(adxl367_spi.c)
|
||||
zephyr_library_sources(adxl367_i2c.c)
|
||||
zephyr_library_sources_ifdef(CONFIG_ADXL367_TRIGGER adxl367_trigger.c)
|
||||
|
|
|
@ -92,4 +92,42 @@ config ADXL367_REFERENCED_INACTIVITY_DETECTION_MODE
|
|||
compared directly to a user set threshold to determine whether
|
||||
motion is present.
|
||||
|
||||
choice ADXL367_TRIGGER_MODE
|
||||
prompt "Trigger mode"
|
||||
default ADXL367_TRIGGER_NONE
|
||||
help
|
||||
Specify the type of triggering used by the driver.
|
||||
|
||||
config ADXL367_TRIGGER_NONE
|
||||
bool "No trigger"
|
||||
|
||||
config ADXL367_TRIGGER_GLOBAL_THREAD
|
||||
bool "Use global thread"
|
||||
depends on GPIO
|
||||
select ADXL367_TRIGGER
|
||||
|
||||
config ADXL367_TRIGGER_OWN_THREAD
|
||||
bool "Use own thread"
|
||||
depends on GPIO
|
||||
select ADXL367_TRIGGER
|
||||
|
||||
endchoice
|
||||
|
||||
config ADXL367_TRIGGER
|
||||
bool
|
||||
|
||||
config ADXL367_THREAD_PRIORITY
|
||||
int "Thread priority"
|
||||
depends on ADXL367_TRIGGER_OWN_THREAD && ADXL367_TRIGGER
|
||||
default 10
|
||||
help
|
||||
Priority of thread used by the driver to handle interrupts.
|
||||
|
||||
config ADXL367_THREAD_STACK_SIZE
|
||||
int "Thread stack size"
|
||||
depends on ADXL367_TRIGGER_OWN_THREAD && ADXL367_TRIGGER
|
||||
default 1024
|
||||
help
|
||||
Stack size of thread used by the driver to handle interrupts.
|
||||
|
||||
endif # ADXL367
|
||||
|
|
|
@ -846,6 +846,9 @@ static const struct sensor_driver_api adxl367_api_funcs = {
|
|||
.attr_set = adxl367_attr_set,
|
||||
.sample_fetch = adxl367_sample_fetch,
|
||||
.channel_get = adxl367_channel_get,
|
||||
#ifdef CONFIG_ADXL367_TRIGGER
|
||||
.trigger_set = adxl367_trigger_set,
|
||||
#endif
|
||||
};
|
||||
|
||||
static int adxl367_probe(const struct device *dev)
|
||||
|
@ -876,7 +879,11 @@ static int adxl367_probe(const struct device *dev)
|
|||
|
||||
data->range = cfg->range;
|
||||
|
||||
#ifdef CONFIG_ADXL367_TRIGGER
|
||||
data->act_proc_mode = ADXL367_LINKED;
|
||||
#else
|
||||
data->act_proc_mode = ADXL367_LOOPED;
|
||||
#endif
|
||||
|
||||
ret = adxl367_self_test(dev);
|
||||
if (ret) {
|
||||
|
@ -931,6 +938,14 @@ static int adxl367_probe(const struct device *dev)
|
|||
return ret;
|
||||
}
|
||||
|
||||
if (IS_ENABLED(CONFIG_ADXL367_TRIGGER)) {
|
||||
ret = adxl367_init_interrupt(dev);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("Failed to initialize interrupt!");
|
||||
return -EIO;
|
||||
}
|
||||
}
|
||||
|
||||
ret = adxl367_set_op_mode(dev, cfg->op_mode);
|
||||
if (ret) {
|
||||
return ret;
|
||||
|
@ -977,6 +992,13 @@ static int adxl367_init(const struct device *dev)
|
|||
CONFIG_SENSOR_INIT_PRIORITY, \
|
||||
&adxl367_api_funcs);
|
||||
|
||||
#ifdef CONFIG_ADXL367_TRIGGER
|
||||
#define ADXL367_CFG_IRQ(inst) \
|
||||
.interrupt = GPIO_DT_SPEC_INST_GET(inst, int1_gpios),
|
||||
#else
|
||||
#define ADXL367_CFG_IRQ(inst)
|
||||
#endif /* CONFIG_ADXL367_TRIGGER */
|
||||
|
||||
#define ADXL367_CONFIG(inst) \
|
||||
.odr = DT_INST_PROP(inst, odr), \
|
||||
.autosleep = false, \
|
||||
|
@ -1011,6 +1033,8 @@ static int adxl367_init(const struct device *dev)
|
|||
.spi = SPI_DT_SPEC_INST_GET(inst, SPI_WORD_SET(8) | \
|
||||
SPI_TRANSFER_MSB, 0), \
|
||||
ADXL367_CONFIG(inst) \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, int1_gpios), \
|
||||
(ADXL367_CFG_IRQ(inst)), ()) \
|
||||
}
|
||||
|
||||
#define ADXL367_DEFINE_SPI(inst) \
|
||||
|
@ -1028,6 +1052,8 @@ static int adxl367_init(const struct device *dev)
|
|||
.bus_init = adxl367_i2c_init, \
|
||||
.i2c = I2C_DT_SPEC_INST_GET(inst), \
|
||||
ADXL367_CONFIG(inst) \
|
||||
COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, int1_gpios), \
|
||||
(ADXL367_CFG_IRQ(inst)), ()) \
|
||||
}
|
||||
|
||||
#define ADXL367_DEFINE_I2C(inst) \
|
||||
|
|
|
@ -177,6 +177,25 @@
|
|||
#define ADXL367_STATUS_FIFO_RDY BIT(1)
|
||||
#define ADXL367_STATUS_DATA_RDY BIT(0)
|
||||
|
||||
/* ADXL367_INTMAP_LOWER */
|
||||
#define ADXL367_INT_LOW BIT(7)
|
||||
#define ADXL367_AWAKE_INT BIT(6)
|
||||
#define ADXL367_INACT_INT BIT(5)
|
||||
#define ADXL367_ACT_INT BIT(4)
|
||||
#define ADXL367_FIFO_OVERRUN BIT(3)
|
||||
#define ADXL367_FIFO_WATERMARK BIT(2)
|
||||
#define ADXL367_FIFO_RDY BIT(1)
|
||||
#define ADXL367_DATA_RDY BIT(0)
|
||||
|
||||
/* ADXL367_INTMAP_UPPER */
|
||||
#define ADXL367_ERR_FUSE BIT(7)
|
||||
#define ADXL367_ERR_USER_REGS BIT(6)
|
||||
#define ADXL367_KPALV_TIMER BIT(4)
|
||||
#define ADXL367_TEMP_ADC_HI BIT(3)
|
||||
#define ADXL367_TEMP_ADC_LOW BIT(2)
|
||||
#define ADXL367_TAP_TWO BIT(1)
|
||||
#define ADXL367_TAP_ONE BIT(0)
|
||||
|
||||
/* Min change = 90mg. Sensitivity = 4LSB / mg */
|
||||
#define ADXL367_SELF_TEST_MIN (90 * 100 / 25)
|
||||
/* Max change = 270mg. Sensitivity = 4LSB / mg */
|
||||
|
@ -280,6 +299,23 @@ struct adxl367_data {
|
|||
struct adxl367_fifo_config fifo_config;
|
||||
enum adxl367_act_proc_mode act_proc_mode;
|
||||
enum adxl367_range range;
|
||||
#ifdef CONFIG_ADXL367_TRIGGER
|
||||
struct gpio_callback gpio_cb;
|
||||
|
||||
sensor_trigger_handler_t th_handler;
|
||||
const struct sensor_trigger *th_trigger;
|
||||
sensor_trigger_handler_t drdy_handler;
|
||||
const struct sensor_trigger *drdy_trigger;
|
||||
const struct device *dev;
|
||||
|
||||
#if defined(CONFIG_ADXL367_TRIGGER_OWN_THREAD)
|
||||
K_KERNEL_STACK_MEMBER(thread_stack, CONFIG_ADXL367_THREAD_STACK_SIZE);
|
||||
struct k_sem gpio_sem;
|
||||
struct k_thread thread;
|
||||
#elif defined(CONFIG_ADXL367_TRIGGER_GLOBAL_THREAD)
|
||||
struct k_work work;
|
||||
#endif
|
||||
#endif /* CONFIG_ADXL367_TRIGGER */
|
||||
};
|
||||
|
||||
struct adxl367_dev_config {
|
||||
|
@ -291,6 +327,10 @@ struct adxl367_dev_config {
|
|||
#endif
|
||||
int (*bus_init)(const struct device *dev);
|
||||
|
||||
#ifdef CONFIG_ADXL367_TRIGGER
|
||||
struct gpio_dt_spec interrupt;
|
||||
#endif
|
||||
|
||||
enum adxl367_odr odr;
|
||||
|
||||
/* Device Settings */
|
||||
|
@ -311,5 +351,10 @@ struct adxl367_dev_config {
|
|||
|
||||
int adxl367_spi_init(const struct device *dev);
|
||||
int adxl367_i2c_init(const struct device *dev);
|
||||
int adxl367_trigger_set(const struct device *dev,
|
||||
const struct sensor_trigger *trig,
|
||||
sensor_trigger_handler_t handler);
|
||||
|
||||
int adxl367_init_interrupt(const struct device *dev);
|
||||
|
||||
#endif /* ZEPHYR_DRIVERS_SENSOR_ADXL367_ADXL367_H_ */
|
||||
|
|
180
drivers/sensor/adxl367/adxl367_trigger.c
Normal file
180
drivers/sensor/adxl367/adxl367_trigger.c
Normal file
|
@ -0,0 +1,180 @@
|
|||
/*
|
||||
* Copyright (c) 2023 Analog Devices Inc.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/sys/util.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
#include "adxl367.h"
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_DECLARE(ADXL367, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
static void adxl367_thread_cb(const struct device *dev)
|
||||
{
|
||||
const struct adxl367_dev_config *cfg = dev->config;
|
||||
struct adxl367_data *drv_data = dev->data;
|
||||
uint8_t status;
|
||||
int ret;
|
||||
|
||||
/* Clear the status */
|
||||
ret = drv_data->hw_tf->read_reg(dev, ADXL367_STATUS, &status);
|
||||
if (ret != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (drv_data->th_handler != NULL) {
|
||||
if (((FIELD_GET(ADXL367_STATUS_INACT, status)) != 0) ||
|
||||
(FIELD_GET(ADXL367_STATUS_ACT, status)) != 0) {
|
||||
drv_data->th_handler(dev, drv_data->th_trigger);
|
||||
}
|
||||
}
|
||||
|
||||
if ((drv_data->drdy_handler != NULL) &&
|
||||
(FIELD_GET(ADXL367_STATUS_DATA_RDY, status) != 0)) {
|
||||
drv_data->drdy_handler(dev, drv_data->drdy_trigger);
|
||||
}
|
||||
|
||||
ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
__ASSERT(ret == 0, "Interrupt configuration failed");
|
||||
}
|
||||
|
||||
static void adxl367_gpio_callback(const struct device *dev,
|
||||
struct gpio_callback *cb, uint32_t pins)
|
||||
{
|
||||
struct adxl367_data *drv_data =
|
||||
CONTAINER_OF(cb, struct adxl367_data, gpio_cb);
|
||||
const struct adxl367_dev_config *cfg = drv_data->dev->config;
|
||||
|
||||
gpio_pin_interrupt_configure_dt(&cfg->interrupt, GPIO_INT_DISABLE);
|
||||
|
||||
#if defined(CONFIG_ADXL367_TRIGGER_OWN_THREAD)
|
||||
k_sem_give(&drv_data->gpio_sem);
|
||||
#elif defined(CONFIG_ADXL367_TRIGGER_GLOBAL_THREAD)
|
||||
k_work_submit(&drv_data->work);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(CONFIG_ADXL367_TRIGGER_OWN_THREAD)
|
||||
static void adxl367_thread(struct adxl367_data *drv_data)
|
||||
{
|
||||
while (true) {
|
||||
k_sem_take(&drv_data->gpio_sem, K_FOREVER);
|
||||
adxl367_thread_cb(drv_data->dev);
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(CONFIG_ADXL367_TRIGGER_GLOBAL_THREAD)
|
||||
static void adxl367_work_cb(struct k_work *work)
|
||||
{
|
||||
struct adxl367_data *drv_data =
|
||||
CONTAINER_OF(work, struct adxl367_data, work);
|
||||
|
||||
adxl367_thread_cb(drv_data->dev);
|
||||
}
|
||||
#endif
|
||||
|
||||
int adxl367_trigger_set(const struct device *dev,
|
||||
const struct sensor_trigger *trig,
|
||||
sensor_trigger_handler_t handler)
|
||||
{
|
||||
const struct adxl367_dev_config *cfg = dev->config;
|
||||
struct adxl367_data *drv_data = dev->data;
|
||||
uint8_t int_mask, int_en, status;
|
||||
int ret;
|
||||
|
||||
ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
|
||||
GPIO_INT_DISABLE);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch (trig->type) {
|
||||
case SENSOR_TRIG_THRESHOLD:
|
||||
drv_data->th_handler = handler;
|
||||
drv_data->th_trigger = trig;
|
||||
int_mask = ADXL367_ACT_INT | ADXL367_INACT_INT;
|
||||
break;
|
||||
case SENSOR_TRIG_DATA_READY:
|
||||
drv_data->drdy_handler = handler;
|
||||
drv_data->drdy_trigger = trig;
|
||||
int_mask = ADXL367_DATA_RDY;
|
||||
break;
|
||||
default:
|
||||
LOG_ERR("Unsupported sensor trigger");
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
if (handler != NULL) {
|
||||
int_en = int_mask;
|
||||
} else {
|
||||
int_en = 0U;
|
||||
}
|
||||
|
||||
ret = drv_data->hw_tf->write_reg_mask(dev, ADXL367_INTMAP1_LOWER, int_mask, int_en);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Clear status */
|
||||
ret = drv_data->hw_tf->read_reg(dev, ADXL367_STATUS, &status);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = gpio_pin_interrupt_configure_dt(&cfg->interrupt,
|
||||
GPIO_INT_EDGE_TO_ACTIVE);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int adxl367_init_interrupt(const struct device *dev)
|
||||
{
|
||||
const struct adxl367_dev_config *cfg = dev->config;
|
||||
struct adxl367_data *drv_data = dev->data;
|
||||
int ret;
|
||||
|
||||
if (!gpio_is_ready_dt(&cfg->interrupt)) {
|
||||
LOG_ERR("GPIO port %s not ready", cfg->interrupt.port->name);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&cfg->interrupt, GPIO_INPUT);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
gpio_init_callback(&drv_data->gpio_cb,
|
||||
adxl367_gpio_callback,
|
||||
BIT(cfg->interrupt.pin));
|
||||
|
||||
ret = gpio_add_callback(cfg->interrupt.port, &drv_data->gpio_cb);
|
||||
if (ret != 0) {
|
||||
LOG_ERR("Failed to set gpio callback!");
|
||||
return ret;
|
||||
}
|
||||
|
||||
drv_data->dev = dev;
|
||||
|
||||
#if defined(CONFIG_ADXL367_TRIGGER_OWN_THREAD)
|
||||
k_sem_init(&drv_data->gpio_sem, 0, K_SEM_MAX_LIMIT);
|
||||
|
||||
k_thread_create(&drv_data->thread, drv_data->thread_stack,
|
||||
CONFIG_ADXL367_THREAD_STACK_SIZE,
|
||||
(k_thread_entry_t)adxl367_thread, drv_data,
|
||||
NULL, NULL, K_PRIO_COOP(CONFIG_ADXL367_THREAD_PRIORITY),
|
||||
0, K_NO_WAIT);
|
||||
#elif defined(CONFIG_ADXL367_TRIGGER_GLOBAL_THREAD)
|
||||
drv_data->work.handler = adxl367_work_cb;
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue