2021-04-20 17:49:32 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT gpio_leds
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief GPIO driven LEDs
|
|
|
|
*/
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/led.h>
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
#include <zephyr/device.h>
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 15:58:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2021-04-20 17:49:32 +08:00
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2021-04-20 17:49:32 +08:00
|
|
|
LOG_MODULE_REGISTER(led_gpio, CONFIG_LED_LOG_LEVEL);
|
|
|
|
|
|
|
|
struct led_gpio_config {
|
|
|
|
size_t num_leds;
|
|
|
|
const struct gpio_dt_spec *led;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int led_gpio_set_brightness(const struct device *dev, uint32_t led, uint8_t value)
|
|
|
|
{
|
|
|
|
|
|
|
|
const struct led_gpio_config *config = dev->config;
|
|
|
|
const struct gpio_dt_spec *led_gpio;
|
|
|
|
|
|
|
|
if ((led >= config->num_leds) || (value > 100)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
led_gpio = &config->led[led];
|
|
|
|
|
2021-06-23 18:53:50 +08:00
|
|
|
return gpio_pin_set_dt(led_gpio, value > 0);
|
2021-04-20 17:49:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int led_gpio_on(const struct device *dev, uint32_t led)
|
|
|
|
{
|
|
|
|
return led_gpio_set_brightness(dev, led, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int led_gpio_off(const struct device *dev, uint32_t led)
|
|
|
|
{
|
|
|
|
return led_gpio_set_brightness(dev, led, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int led_gpio_init(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct led_gpio_config *config = dev->config;
|
|
|
|
int err = 0;
|
|
|
|
|
|
|
|
if (!config->num_leds) {
|
|
|
|
LOG_ERR("%s: no LEDs found (DT child nodes missing)", dev->name);
|
|
|
|
err = -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; (i < config->num_leds) && !err; i++) {
|
|
|
|
const struct gpio_dt_spec *led = &config->led[i];
|
|
|
|
|
|
|
|
if (device_is_ready(led->port)) {
|
2021-04-28 01:38:28 +08:00
|
|
|
err = gpio_pin_configure_dt(led, GPIO_OUTPUT_INACTIVE);
|
2021-04-20 17:49:32 +08:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
LOG_ERR("Cannot configure GPIO (err %d)", err);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG_ERR("%s: GPIO device not ready", dev->name);
|
|
|
|
err = -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct led_driver_api led_gpio_api = {
|
|
|
|
.on = led_gpio_on,
|
|
|
|
.off = led_gpio_off,
|
|
|
|
.set_brightness = led_gpio_set_brightness,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define LED_GPIO_DEVICE(i) \
|
|
|
|
\
|
2022-07-06 16:57:53 +08:00
|
|
|
static const struct gpio_dt_spec gpio_dt_spec_##i[] = { \
|
|
|
|
DT_INST_FOREACH_CHILD_SEP_VARGS(i, GPIO_DT_SPEC_GET, (,), gpios) \
|
2021-04-20 17:49:32 +08:00
|
|
|
}; \
|
|
|
|
\
|
|
|
|
static const struct led_gpio_config led_gpio_config_##i = { \
|
|
|
|
.num_leds = ARRAY_SIZE(gpio_dt_spec_##i), \
|
|
|
|
.led = gpio_dt_spec_##i, \
|
|
|
|
}; \
|
|
|
|
\
|
2021-04-28 17:14:04 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(i, &led_gpio_init, NULL, \
|
2021-04-20 17:49:32 +08:00
|
|
|
NULL, &led_gpio_config_##i, \
|
|
|
|
POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \
|
|
|
|
&led_gpio_api);
|
|
|
|
|
|
|
|
DT_INST_FOREACH_STATUS_OKAY(LED_GPIO_DEVICE)
|