Initial commit
This commit is contained in:
8
drivers/CMakeLists.txt
Normal file
8
drivers/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
# Out-of-tree drivers for custom classes
|
||||
add_subdirectory_ifdef(CONFIG_BLINK blink)
|
||||
|
||||
# Out-of-tree drivers for existing driver classes
|
||||
add_subdirectory_ifdef(CONFIG_SENSOR sensor)
|
7
drivers/Kconfig
Normal file
7
drivers/Kconfig
Normal file
@ -0,0 +1,7 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menu "Drivers"
|
||||
rsource "blink/Kconfig"
|
||||
rsource "sensor/Kconfig"
|
||||
endmenu
|
5
drivers/blink/CMakeLists.txt
Normal file
5
drivers/blink/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
# Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
zephyr_library()
|
||||
zephyr_library_sources_ifdef(CONFIG_BLINK_GPIO_LED gpio_led.c)
|
23
drivers/blink/Kconfig
Normal file
23
drivers/blink/Kconfig
Normal file
@ -0,0 +1,23 @@
|
||||
# Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
menuconfig BLINK
|
||||
bool "Blink device drivers"
|
||||
help
|
||||
This option enables the blink custom driver class.
|
||||
|
||||
if BLINK
|
||||
|
||||
config BLINK_INIT_PRIORITY
|
||||
int "Blink device drivers init priority"
|
||||
default KERNEL_INIT_PRIORITY_DEVICE
|
||||
help
|
||||
Blink device drivers init priority.
|
||||
|
||||
module = BLINK
|
||||
module-str = blink
|
||||
source "subsys/logging/Kconfig.template.log_config"
|
||||
|
||||
rsource "Kconfig.gpio_led"
|
||||
|
||||
endif # BLINK
|
11
drivers/blink/Kconfig.gpio_led
Normal file
11
drivers/blink/Kconfig.gpio_led
Normal file
@ -0,0 +1,11 @@
|
||||
# Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
config BLINK_GPIO_LED
|
||||
bool "GPIO-controlled LED blink driver"
|
||||
default y
|
||||
depends on DT_HAS_BLINK_GPIO_LED_ENABLED
|
||||
select GPIO
|
||||
help
|
||||
Enable this option to use the GPIO-controlled LED blink driver. This
|
||||
demonstrates how to implement a driver for a custom driver class.
|
101
drivers/blink/gpio_led.c
Normal file
101
drivers/blink/gpio_led.c
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Nordic Semiconductor ASA
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#define DT_DRV_COMPAT blink_gpio_led
|
||||
|
||||
#include <zephyr/device.h>
|
||||
|
||||
#include <zephyr/devicetree.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include <app/drivers/blink.h>
|
||||
|
||||
LOG_MODULE_REGISTER(blink_gpio_led, CONFIG_BLINK_LOG_LEVEL);
|
||||
|
||||
struct blink_gpio_led_data {
|
||||
struct k_timer timer;
|
||||
};
|
||||
|
||||
struct blink_gpio_led_config {
|
||||
struct gpio_dt_spec led;
|
||||
unsigned int period_ms;
|
||||
};
|
||||
|
||||
static void blink_gpio_led_on_timer_expire(struct k_timer *timer)
|
||||
{
|
||||
const struct device *dev = k_timer_user_data_get(timer);
|
||||
const struct blink_gpio_led_config *config = dev->config;
|
||||
int ret;
|
||||
|
||||
ret = gpio_pin_toggle_dt(&config->led);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not toggle LED GPIO (%d)", ret);
|
||||
}
|
||||
}
|
||||
|
||||
static int blink_gpio_led_set_period_ms(const struct device *dev,
|
||||
unsigned int period_ms)
|
||||
{
|
||||
const struct blink_gpio_led_config *config = dev->config;
|
||||
struct blink_gpio_led_data *data = dev->data;
|
||||
|
||||
if (period_ms == 0) {
|
||||
k_timer_stop(&data->timer);
|
||||
return gpio_pin_set_dt(&config->led, 0);
|
||||
}
|
||||
|
||||
k_timer_start(&data->timer, K_MSEC(period_ms), K_MSEC(period_ms));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct blink_driver_api blink_gpio_led_api = {
|
||||
.set_period_ms = &blink_gpio_led_set_period_ms,
|
||||
};
|
||||
|
||||
static int blink_gpio_led_init(const struct device *dev)
|
||||
{
|
||||
const struct blink_gpio_led_config *config = dev->config;
|
||||
struct blink_gpio_led_data *data = dev->data;
|
||||
int ret;
|
||||
|
||||
if (!gpio_is_ready_dt(&config->led)) {
|
||||
LOG_ERR("LED GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
ret = gpio_pin_configure_dt(&config->led, GPIO_OUTPUT_INACTIVE);
|
||||
if (ret < 0) {
|
||||
LOG_ERR("Could not configure LED GPIO (%d)", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
k_timer_init(&data->timer, blink_gpio_led_on_timer_expire, NULL);
|
||||
k_timer_user_data_set(&data->timer, (void *)dev);
|
||||
|
||||
if (config->period_ms > 0) {
|
||||
k_timer_start(&data->timer, K_MSEC(config->period_ms),
|
||||
K_MSEC(config->period_ms));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define BLINK_GPIO_LED_DEFINE(inst) \
|
||||
static struct blink_gpio_led_data data##inst; \
|
||||
\
|
||||
static const struct blink_gpio_led_config config##inst = { \
|
||||
.led = GPIO_DT_SPEC_INST_GET(inst, led_gpios), \
|
||||
.period_ms = DT_INST_PROP_OR(inst, blink_period_ms, 0U), \
|
||||
}; \
|
||||
\
|
||||
DEVICE_DT_INST_DEFINE(inst, blink_gpio_led_init, NULL, &data##inst, \
|
||||
&config##inst, POST_KERNEL, \
|
||||
CONFIG_BLINK_INIT_PRIORITY, \
|
||||
&blink_gpio_led_api);
|
||||
|
||||
DT_INST_FOREACH_STATUS_OKAY(BLINK_GPIO_LED_DEFINE)
|
4
drivers/sensor/CMakeLists.txt
Normal file
4
drivers/sensor/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
add_subdirectory_ifdef(CONFIG_EXAMPLE_SENSOR example_sensor)
|
6
drivers/sensor/Kconfig
Normal file
6
drivers/sensor/Kconfig
Normal file
@ -0,0 +1,6 @@
|
||||
# Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
if SENSOR
|
||||
rsource "example_sensor/Kconfig"
|
||||
endif # SENSOR
|
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