Initial commit
This commit is contained in:
5
drivers/sensor/example_sensor/CMakeLists.txt
Normal file
5
drivers/sensor/example_sensor/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
zephyr_library_sources(example_sensor.c)
|
10
drivers/sensor/example_sensor/Kconfig
Normal file
10
drivers/sensor/example_sensor/Kconfig
Normal file
@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config EXAMPLE_SENSOR
|
||||
bool "Example sensor"
|
||||
default y
|
||||
depends on DT_HAS_ZEPHYR_EXAMPLE_SENSOR_ENABLED
|
||||
select GPIO
|
||||
help
|
||||
Enable example sensor
|
86
drivers/sensor/example_sensor/example_sensor.c
Normal file
86
drivers/sensor/example_sensor/example_sensor.c
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT zephyr_example_sensor
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/drivers/sensor.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(example_sensor, CONFIG_SENSOR_LOG_LEVEL);
|
||||
|
||||
struct example_sensor_data {
|
||||
int state;
|
||||
};
|
||||
|
||||
struct example_sensor_config {
|
||||
struct gpio_dt_spec input;
|
||||
};
|
||||
|
||||
static int example_sensor_sample_fetch(const struct device *dev,
|
||||
enum sensor_channel chan)
|
||||
{
|
||||
const struct example_sensor_config *config = dev->config;
|
||||
struct example_sensor_data *data = dev->data;
|
||||
|
||||
data->state = gpio_pin_get_dt(&config->input);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int example_sensor_channel_get(const struct device *dev,
|
||||
enum sensor_channel chan,
|
||||
struct sensor_value *val)
|
||||
{
|
||||
struct example_sensor_data *data = dev->data;
|
||||
|
||||
if (chan != SENSOR_CHAN_PROX) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
val->val1 = data->state;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sensor_driver_api example_sensor_api = {
|
||||
.sample_fetch = &example_sensor_sample_fetch,
|
||||
.channel_get = &example_sensor_channel_get,
|
||||
};
|
||||
|
||||
static int example_sensor_init(const struct device *dev)
|
||||
{
|
||||
const struct example_sensor_config *config = dev->config;
|
||||
|
||||
int ret;
|
||||
|
||||
if (!device_is_ready(config->input.port)) {
|
||||
LOG_ERR("Input GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->input, GPIO_INPUT);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not configure input GPIO (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define EXAMPLE_SENSOR_INIT(i) \
|
||||
static struct example_sensor_data example_sensor_data_##i; \
|
||||
\
|
||||
static const struct example_sensor_config example_sensor_config_##i = {\
|
||||
.input = GPIO_DT_SPEC_INST_GET(i, input_gpios), \
|
||||
}; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(i, example_sensor_init, NULL, \
|
||||
&example_sensor_data_##i, \
|
||||
&example_sensor_config_##i, POST_KERNEL, \
|
||||
CONFIG_SENSOR_INIT_PRIORITY, &example_sensor_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(EXAMPLE_SENSOR_INIT)
|
Reference in New Issue
Block a user