drivers: sensors: sm351lt: Add new driver

Adds sm351lt magnetoresitive sensor driver.

Signed-off-by: Jamie McCrae <jamie.mccrae@lairdconnect.com>
This commit is contained in:
Jamie McCrae 2020-08-07 15:29:52 +01:00 committed by Maureen Helm
parent c789ea87ef
commit 4cf1832fa8
15 changed files with 415 additions and 3 deletions

View file

@ -69,8 +69,9 @@
mag {
compatible = "gpio-keys";
mag0: mag_0 {
compatible = "honeywell,sm351lt";
gpios = <&gpio1 14 0>;
label = "Magnetoresitive Sensor";
label = "SM351LT_0";
};
};

View file

@ -13,3 +13,4 @@ supported:
- pwm
- watchdog
- i2c
- sm351lt

View file

@ -24,6 +24,9 @@ CONFIG_LIS2DH_TRIGGER_GLOBAL_THREAD=y
CONFIG_LIS2DH_OPER_MODE_LOW_POWER=y
CONFIG_SI7055=y
# enable magnetoresistive driver
CONFIG_SM351LT=y
# enable console
CONFIG_CONSOLE=y
CONFIG_UART_CONSOLE=y

View file

@ -63,6 +63,7 @@ add_subdirectory_ifdef(CONFIG_SHT3XD sht3xd)
add_subdirectory_ifdef(CONFIG_SI7006 si7006)
add_subdirectory_ifdef(CONFIG_SI7055 si7055)
add_subdirectory_ifdef(CONFIG_SI7060 si7060)
add_subdirectory_ifdef(CONFIG_SM351LT sm351lt)
add_subdirectory_ifdef(CONFIG_STTS751 stts751)
add_subdirectory_ifdef(CONFIG_SX9500 sx9500)
add_subdirectory_ifdef(CONFIG_TH02 th02)

View file

@ -165,6 +165,8 @@ source "drivers/sensor/si7055/Kconfig"
source "drivers/sensor/si7060/Kconfig"
source "drivers/sensor/sm351lt/Kconfig"
source "drivers/sensor/stts751/Kconfig"
source "drivers/sensor/sx9500/Kconfig"

View file

@ -0,0 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_SM351LT sm351lt.c)

View file

@ -0,0 +1,50 @@
# SM351LT Magnetoresitive Sensor configuration options
# Copyright (c) 2020, Laird Connectivity
# SPDX-License-Identifier: Apache-2.0
menuconfig SM351LT
bool "SM351LT Magnetoresistive Sensor"
depends on (GPIO)
help
Enable GPIO-based driver for SM351LT magnetoresistive
sensor.
if SM351LT
choice SM351LT_TRIGGER_MODE
prompt "Trigger mode"
help
Specify the type of triggering to be used by the driver.
config SM351LT_TRIGGER_NONE
bool "No trigger"
config SM351LT_TRIGGER_GLOBAL_THREAD
bool "Use global thread"
select SM351LT_TRIGGER
config SM351LT_TRIGGER_OWN_THREAD
bool "Use own thread"
select SM351LT_TRIGGER
endchoice
config SM351LT_TRIGGER
bool
config SM351LT_THREAD_PRIORITY
int "Thread priority"
depends on SM351LT_TRIGGER_OWN_THREAD
default 10
help
Priority of thread used by the driver to handle interrupts.
config SM351LT_THREAD_STACK_SIZE
int "Thread stack size"
depends on SM351LT_TRIGGER_OWN_THREAD
default 512
help
Stack size of thread used by the driver to handle interrupts.
endif # SM351LT

View file

@ -0,0 +1,268 @@
/*
* Copyright (c) 2020, Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT honeywell_sm351lt
#include "sm351lt.h"
#include <init.h>
#include <sys/byteorder.h>
#include <logging/log.h>
#include <stdio.h>
#include <sys/util.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>
#include <string.h>
LOG_MODULE_REGISTER(SM351LT, CONFIG_SENSOR_LOG_LEVEL);
#if CONFIG_SM351LT_TRIGGER
static int sm351lt_trigger_set(struct device *dev,
const struct sensor_trigger *trig,
sensor_trigger_handler_t handler)
{
const struct sm351lt_config *const config = dev->config;
struct sm351lt_data *data = dev->data;
int ret = -ENOTSUP;
if (trig->chan == SENSOR_CHAN_PROX) {
data->changed_handler = handler;
ret = gpio_pin_interrupt_configure(data->bus, config->gpio_pin,
(handler ? data->trigger_type
: GPIO_INT_DISABLE));
if (ret < 0) {
return ret;
}
if (handler) {
ret = gpio_add_callback(data->bus, &data->gpio_cb);
} else {
ret = gpio_remove_callback(data->bus, &data->gpio_cb);
}
}
return ret;
}
static void sm351lt_gpio_callback(struct device *dev,
struct gpio_callback *cb, uint32_t pins)
{
struct sm351lt_data *data =
CONTAINER_OF(cb, struct sm351lt_data, gpio_cb);
#if defined(CONFIG_SM351LT_TRIGGER_OWN_THREAD)
k_sem_give(&data->gpio_sem);
#elif defined(CONFIG_SM351LT_TRIGGER_GLOBAL_THREAD)
k_work_submit(&data->work);
#endif
}
static void sm351lt_thread_cb(void *arg)
{
struct device *dev = arg;
struct sm351lt_data *data = dev->data;
struct sensor_trigger mag_trigger = {
.type = SENSOR_TRIG_NEAR_FAR,
.chan = SENSOR_CHAN_PROX,
};
if (likely(data->changed_handler != NULL)) {
data->changed_handler(dev, &mag_trigger);
}
return;
}
#if defined(CONFIG_SM351LT_TRIGGER_OWN_THREAD)
static void sm351lt_thread(void *arg1, void *unused2, void *unused3)
{
struct device *dev = arg1;
struct sm351lt_data *data = dev->data;
ARG_UNUSED(unused2);
ARG_UNUSED(unused3);
while (1) {
k_sem_take(&data->gpio_sem, K_FOREVER);
sm351lt_thread_cb(dev);
}
}
#endif
#if defined(CONFIG_SM351LT_TRIGGER_GLOBAL_THREAD)
static void sm351lt_work_cb(struct k_work *work)
{
struct sm351lt_data *data =
CONTAINER_OF(work, struct sm351lt_data, work);
sm351lt_thread_cb(data->dev);
}
#endif
#endif
static int sm351lt_sample_fetch(struct device *dev,
enum sensor_channel chan)
{
const struct sm351lt_config *config = dev->config;
struct sm351lt_data *data = dev->data;
if (chan != SENSOR_CHAN_ALL && chan != SENSOR_CHAN_PROX) {
return -ENOTSUP;
}
data->sample_status = gpio_pin_get(data->bus, config->gpio_pin);
return 0;
}
static int sm351lt_channel_get(struct device *dev,
enum sensor_channel chan,
struct sensor_value *val)
{
struct sm351lt_data *data = dev->data;
if (chan == SENSOR_CHAN_PROX) {
val->val1 = data->sample_status;
val->val2 = 0;
} else {
return -ENOTSUP;
}
return 0;
}
#if CONFIG_SM351LT_TRIGGER
static int sm351lt_attr_set(struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr,
const struct sensor_value *val)
{
struct sm351lt_data *data = dev->data;
if (chan == SENSOR_CHAN_PROX) {
if (attr == SENSOR_ATTR_SM351LT_TRIGGER_TYPE) {
/* Interrupt triggering type */
data->trigger_type = val->val1;
} else {
return -ENOTSUP;
}
} else {
return -ENOTSUP;
}
return 0;
}
static int sm351lt_attr_get(struct device *dev, enum sensor_channel chan,
enum sensor_attribute attr,
struct sensor_value *val)
{
struct sm351lt_data *data = dev->data;
if (chan == SENSOR_CHAN_PROX) {
if (attr == SENSOR_ATTR_SM351LT_TRIGGER_TYPE) {
/* Interrupt triggering type */
val->val1 = data->trigger_type;
val->val2 = 0;
} else {
return -ENOTSUP;
}
} else {
return -ENOTSUP;
}
return 0;
}
#endif
static const struct sensor_driver_api sm351lt_api_funcs = {
.sample_fetch = sm351lt_sample_fetch,
.channel_get = sm351lt_channel_get,
#if CONFIG_SM351LT_TRIGGER
.attr_set = sm351lt_attr_set,
.attr_get = sm351lt_attr_get,
.trigger_set = sm351lt_trigger_set,
#endif
};
static int sm351lt_init(struct device *dev)
{
const struct sm351lt_config *const config = dev->config;
struct sm351lt_data *data = dev->data;
uint32_t ret;
data->bus = device_get_binding(config->bus_name);
if (!data->bus) {
LOG_ERR("gpio bus not found: %s", config->bus_name);
return -ENODEV;
}
ret = gpio_pin_configure(data->bus, config->gpio_pin,
(config->gpio_flags | GPIO_INPUT));
if (ret) {
LOG_ERR("failed to configure gpio: %d", ret);
return ret;
}
#if defined(CONFIG_SM351LT_TRIGGER)
#if defined(CONFIG_SM351LT_TRIGGER_OWN_THREAD)
k_sem_init(&data->gpio_sem, 0, UINT_MAX);
k_thread_create(&data->thread, data->thread_stack,
CONFIG_SM351LT_THREAD_STACK_SIZE,
(k_thread_entry_t)sm351lt_thread, dev, NULL,
NULL, K_PRIO_COOP(CONFIG_SM351LT_THREAD_PRIORITY),
0, K_NO_WAIT);
#if defined(CONFIG_THREAD_NAME) && defined(CONFIG_THREAD_MAX_NAME_LEN)
/* Sets up thread name as the device name */
k_thread_name_set(&data->thread, dev->name);
#endif
#elif defined(CONFIG_SM351LT_TRIGGER_GLOBAL_THREAD)
data->work.handler = sm351lt_work_cb;
data->dev = dev;
#endif
data->trigger_type = GPIO_INT_DISABLE;
ret = gpio_pin_interrupt_configure(data->bus, config->gpio_pin,
GPIO_INT_DISABLE);
if (ret) {
LOG_ERR("failed to configure gpio interrupt: %d", ret);
return ret;
}
/* Setup callback struct but do not add it yet */
gpio_init_callback(&data->gpio_cb, sm351lt_gpio_callback,
BIT(config->gpio_pin));
#endif
return 0;
}
/* Instantiation macros for each individual device. */
#define SM351LT_DEFINE(inst) \
static struct sm351lt_data sm351lt_data_##inst; \
static const struct sm351lt_config sm351lt_config_##inst = { \
.bus_name = DT_INST_GPIO_LABEL(inst, gpios), \
.gpio_pin = DT_INST_GPIO_PIN(inst, gpios), \
.gpio_flags = DT_INST_GPIO_FLAGS(inst, gpios), \
}; \
\
DEVICE_AND_API_INIT(sm351lt_##inst, \
DT_INST_LABEL(inst), \
sm351lt_init, \
&sm351lt_data_##inst, \
&sm351lt_config_##inst, \
POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&sm351lt_api_funcs);
/* Main instantiation macro for every configured device in DTS. */
DT_INST_FOREACH_STATUS_OKAY(SM351LT_DEFINE)

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2020, Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_SENSOR_SM351LT_SM351LT_H_
#define ZEPHYR_DRIVERS_SENSOR_SM351LT_SM351LT_H_
#include <stdint.h>
#include <drivers/gpio.h>
#include <drivers/sensor.h>
#define SENSOR_ATTR_SM351LT_TRIGGER_TYPE SENSOR_ATTR_PRIV_START
struct sm351lt_config {
const char *bus_name;
gpio_pin_t gpio_pin;
gpio_dt_flags_t gpio_flags;
};
struct sm351lt_data {
struct device *bus;
bool sample_status;
#ifdef CONFIG_SM351LT_TRIGGER
struct gpio_callback gpio_cb;
uint32_t trigger_type;
sensor_trigger_handler_t changed_handler;
#if defined(CONFIG_SM351LT_TRIGGER_OWN_THREAD)
K_THREAD_STACK_MEMBER(thread_stack, CONFIG_SM351LT_THREAD_STACK_SIZE);
struct k_thread thread;
struct k_sem gpio_sem;
#elif defined(CONFIG_SM351LT_TRIGGER_GLOBAL_THREAD)
struct k_work work;
struct device *dev;
#endif
#endif /* CONFIG_SM351LT_TRIGGER */
};
#endif /* ZEPHYR_DRIVERS_SENSOR_SM351LT_SM351LT_H_ */

View file

@ -0,0 +1,18 @@
# Copyright (c) 2020, Laird Connectivity
# SPDX-License-Identifier: Apache-2.0
description: SM351LT magnetoresistive sensor
compatible: "honeywell,sm351lt"
include: base.yaml
properties:
gpios:
type: phandle-array
required: true
description: GPIO connected to the sensor
label:
required: true
type: string
description: Driver label

View file

@ -22,6 +22,8 @@
#gpio-cells = <0x2>;
label = "TEST_GPIO_1";
status = "okay";
#include "gpio.dtsi"
};
test_i2c: i2c@11112222 {

View file

@ -0,0 +1,13 @@
/*
* Copyright (c) 2020, Laird Connectivity
*
* SPDX-License-Identifier: Apache-2.0
*
* Application overlay for gpio devices
*/
test_gpio_sm351lt: sm351lt0 {
compatible = "honeywell,sm351lt";
gpios = <&test_gpio 0 0>;
label = "SM351LT0";
};

View file

@ -32,6 +32,7 @@ CONFIG_SHT3XD=y
CONFIG_SI7006=y
CONFIG_SI7055=y
CONFIG_SI7060=y
CONFIG_SM351LT=y
CONFIG_SX9500=y
CONFIG_TEMP_NRF5=y
CONFIG_TH02=y

View file

@ -1,3 +1,4 @@
CONFIG_MAX_THREAD_BYTES=4
CONFIG_TEST=y
CONFIG_TEST_USERSPACE=y
CONFIG_I2C=y
@ -28,6 +29,8 @@ CONFIG_MPU6050=y
CONFIG_MPU6050_TRIGGER_OWN_THREAD=y
CONFIG_SHT3XD=y
CONFIG_SHT3XD_TRIGGER_OWN_THREAD=y
CONFIG_SM351LT=y
CONFIG_SM351LT_TRIGGER_OWN_THREAD=y
CONFIG_SX9500=y
CONFIG_SX9500_TRIGGER_OWN_THREAD=y
CONFIG_TMP007=y

View file

@ -27,7 +27,7 @@ tests:
min_ram: 32
platform_exclude: frdm_kw41z
tags: drivers
depends_on: adc spi
depends_on: gpio adc spi
sensors.build.a_h:
build_only: true
extra_args: CONF_FILE=sensors_a_h.conf
@ -42,7 +42,7 @@ tests:
min_ram: 32
platform_exclude: frdm_kw41z
tags: drivers
depends_on: adc spi
depends_on: gpio adc spi
sensors.build.stmemsc:
build_only: true
extra_args: CONF_FILE=sensors_stmemsc.conf