Skip to content

Commit 692ba2e

Browse files
committed
drivers: led: Create led_core.c
In order to develop a sotfware implementation for led blinking, prepare the led API. This move z_impl_led_on and z_impl_led_off code to led_core.c so we could use and update them for blinking a led. Signed-off-by: Alexandre Bailon <abailon@baylibre.com>
1 parent 9b4bebb commit 692ba2e

File tree

3 files changed

+74
-72
lines changed

3 files changed

+74
-72
lines changed

drivers/led/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ zephyr_library_sources_ifdef(CONFIG_PCA9633 pca9633.c)
2525
zephyr_library_sources_ifdef(CONFIG_TLC59108 tlc59108.c)
2626
# zephyr-keep-sorted-stop
2727

28+
zephyr_library_sources(led_core.c)
29+
2830
zephyr_library_sources_ifdef(CONFIG_LED_SHELL led_shell.c)
2931

3032
zephyr_library_sources_ifdef(CONFIG_USERSPACE led_handlers.c)

drivers/led/led_core.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2025 BayLibre SAS
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/drivers/led.h>
8+
9+
int z_impl_led_on(const struct device *dev, uint32_t led)
10+
{
11+
const struct led_driver_api *api = (const struct led_driver_api *)dev->api;
12+
13+
if (api->set_brightness == NULL && api->on == NULL) {
14+
return -ENOSYS;
15+
}
16+
17+
if (api->on == NULL) {
18+
return api->set_brightness(dev, led, LED_BRIGHTNESS_MAX);
19+
}
20+
21+
return api->on(dev, led);
22+
}
23+
24+
int z_impl_led_off(const struct device *dev, uint32_t led)
25+
{
26+
const struct led_driver_api *api = (const struct led_driver_api *)dev->api;
27+
28+
if (api->set_brightness == NULL && api->off == NULL) {
29+
return -ENOSYS;
30+
}
31+
32+
if (api->off == NULL) {
33+
return api->set_brightness(dev, led, 0);
34+
}
35+
36+
return api->off(dev, led);
37+
}
38+
39+
int z_impl_led_set_brightness(const struct device *dev, uint32_t led, uint8_t value)
40+
{
41+
const struct led_driver_api *api = (const struct led_driver_api *)dev->api;
42+
43+
if (api->set_brightness == NULL) {
44+
if (api->on == NULL || api->off == NULL) {
45+
return -ENOSYS;
46+
}
47+
}
48+
49+
if (value > LED_BRIGHTNESS_MAX) {
50+
return -EINVAL;
51+
}
52+
53+
if (api->set_brightness == NULL) {
54+
if (value) {
55+
return api->on(dev, led);
56+
} else {
57+
return api->off(dev, led);
58+
}
59+
}
60+
61+
return api->set_brightness(dev, led, value);
62+
}
63+
64+
int z_impl_led_blink(const struct device *dev, uint32_t led, uint32_t delay_on, uint32_t delay_off)
65+
{
66+
const struct led_driver_api *api = (const struct led_driver_api *)dev->api;
67+
68+
if (api->blink == NULL) {
69+
return -ENOSYS;
70+
}
71+
return api->blink(dev, led, delay_on, delay_off);
72+
}

include/zephyr/drivers/led.h

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,6 @@ __subsystem struct led_driver_api {
143143
__syscall int led_blink(const struct device *dev, uint32_t led,
144144
uint32_t delay_on, uint32_t delay_off);
145145

146-
static inline int z_impl_led_blink(const struct device *dev, uint32_t led,
147-
uint32_t delay_on, uint32_t delay_off)
148-
{
149-
const struct led_driver_api *api =
150-
(const struct led_driver_api *)dev->api;
151-
152-
if (api->blink == NULL) {
153-
return -ENOSYS;
154-
}
155-
return api->blink(dev, led, delay_on, delay_off);
156-
}
157-
158146
/**
159147
* @brief Get LED information
160148
*
@@ -200,34 +188,6 @@ static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
200188
__syscall int led_set_brightness(const struct device *dev, uint32_t led,
201189
uint8_t value);
202190

203-
static inline int z_impl_led_set_brightness(const struct device *dev,
204-
uint32_t led,
205-
uint8_t value)
206-
{
207-
const struct led_driver_api *api =
208-
(const struct led_driver_api *)dev->api;
209-
210-
if (api->set_brightness == NULL) {
211-
if (api->on == NULL || api->off == NULL) {
212-
return -ENOSYS;
213-
}
214-
}
215-
216-
if (value > LED_BRIGHTNESS_MAX) {
217-
return -EINVAL;
218-
}
219-
220-
if (api->set_brightness == NULL) {
221-
if (value) {
222-
return api->on(dev, led);
223-
} else {
224-
return api->off(dev, led);
225-
}
226-
}
227-
228-
return api->set_brightness(dev, led, value);
229-
}
230-
231191
/**
232192
* @brief Write/update a strip of LED channels
233193
*
@@ -327,22 +287,6 @@ static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
327287
*/
328288
__syscall int led_on(const struct device *dev, uint32_t led);
329289

330-
static inline int z_impl_led_on(const struct device *dev, uint32_t led)
331-
{
332-
const struct led_driver_api *api =
333-
(const struct led_driver_api *)dev->api;
334-
335-
if (api->set_brightness == NULL && api->on == NULL) {
336-
return -ENOSYS;
337-
}
338-
339-
if (api->on == NULL) {
340-
return api->set_brightness(dev, led, LED_BRIGHTNESS_MAX);
341-
}
342-
343-
return api->on(dev, led);
344-
}
345-
346290
/**
347291
* @brief Turn off an LED
348292
*
@@ -357,22 +301,6 @@ static inline int z_impl_led_on(const struct device *dev, uint32_t led)
357301
*/
358302
__syscall int led_off(const struct device *dev, uint32_t led);
359303

360-
static inline int z_impl_led_off(const struct device *dev, uint32_t led)
361-
{
362-
const struct led_driver_api *api =
363-
(const struct led_driver_api *)dev->api;
364-
365-
if (api->set_brightness == NULL && api->off == NULL) {
366-
return -ENOSYS;
367-
}
368-
369-
if (api->off == NULL) {
370-
return api->set_brightness(dev, led, 0);
371-
}
372-
373-
return api->off(dev, led);
374-
}
375-
376304
/*
377305
* LED DT helpers.
378306
*/

0 commit comments

Comments
 (0)