Skip to content

Commit 4c95949

Browse files
committed
drivers: led: gpio: Add support of software blink
This updates the led_gpio driver to use software blinking. Signed-off-by: Alexandre Bailon <abailon@baylibre.com>
1 parent c5be998 commit 4c95949

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

drivers/led/led_gpio.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <zephyr/device.h>
1717
#include <zephyr/kernel.h>
1818

19+
#include "led_blink.h"
20+
1921
#include <zephyr/logging/log.h>
2022
LOG_MODULE_REGISTER(led_gpio, CONFIG_LED_LOG_LEVEL);
2123

@@ -24,9 +26,28 @@ struct led_gpio_config {
2426
const struct gpio_dt_spec *led;
2527
};
2628

27-
static int led_gpio_set_brightness(const struct device *dev, uint32_t led, uint8_t value)
29+
struct led_gpio_data {
30+
#ifdef CONFIG_LED_BLINK_SOFTWARE
31+
struct led_blink_software_data *blink_data;
32+
#endif
33+
};
34+
35+
#ifdef CONFIG_LED_BLINK_SOFTWARE
36+
struct led_blink_software_data *led_gpio_blink_data(const struct device *dev, uint32_t led)
2837
{
38+
const struct led_gpio_config *config = dev->config;
39+
struct led_gpio_data *data = dev->data;
40+
41+
if (led >= config->num_leds) {
42+
return NULL;
43+
}
44+
45+
return &data->blink_data[led];
46+
}
47+
#endif
2948

49+
static int led_gpio_set_brightness(const struct device *dev, uint32_t led, uint8_t value)
50+
{
3051
const struct led_gpio_config *config = dev->config;
3152
const struct gpio_dt_spec *led_gpio;
3253

@@ -69,6 +90,9 @@ static int led_gpio_init(const struct device *dev)
6990

7091
static DEVICE_API(led, led_gpio_api) = {
7192
.set_brightness = led_gpio_set_brightness,
93+
#ifdef CONFIG_LED_BLINK_SOFTWARE
94+
.get_blink_data = led_gpio_blink_data,
95+
#endif
7296
};
7397

7498
#define LED_GPIO_DEVICE(i) \
@@ -78,12 +102,17 @@ static const struct gpio_dt_spec gpio_dt_spec_##i[] = { \
78102
}; \
79103
\
80104
static const struct led_gpio_config led_gpio_config_##i = { \
81-
.num_leds = ARRAY_SIZE(gpio_dt_spec_##i), \
105+
.num_leds = ARRAY_SIZE(gpio_dt_spec_##i), \
82106
.led = gpio_dt_spec_##i, \
83107
}; \
84108
\
109+
static struct led_gpio_data led_gpio_data_##i = { \
110+
LED_BLINK_SOFTWARE_DATA(i, blink_data) \
111+
}; \
112+
\
85113
DEVICE_DT_INST_DEFINE(i, &led_gpio_init, NULL, \
86-
NULL, &led_gpio_config_##i, \
114+
(&led_gpio_data_##i), \
115+
&led_gpio_config_##i, \
87116
POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \
88117
&led_gpio_api);
89118

0 commit comments

Comments
 (0)