-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Add leds_gpio driver and modify blinky application #9511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Mani-Sadhasivam
wants to merge
3
commits into
zephyrproject-rtos:master
from
Mani-Sadhasivam:gpio_led
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # | ||
| # Copyright (c) 2018 Manivannan Sadhasivam | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| config LED_GPIO | ||
| bool "LED GPIO driver" | ||
| depends on GPIO | ||
| help | ||
| Enable software driven LED using GPIO pin. | ||
|
|
||
| # ---------- LED 0 ---------- | ||
|
|
||
| config LED_GPIO_0 | ||
| bool "Enable LED GPIO 0" | ||
| depends on LED_GPIO | ||
| help | ||
| Enable LED GPIO 0. | ||
|
|
||
| # ---------- LED 1 ---------- | ||
|
|
||
| config LED_GPIO_1 | ||
| bool "Enable LED GPIO 1" | ||
| depends on LED_GPIO | ||
| help | ||
| Enable LED GPIO 1. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright (c) 2018 Manivannan Sadhasivam | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * @file | ||
| * @brief LEDs driver for GPIOs | ||
| * | ||
| * This driver controls LED devices attached to GPIO pins which | ||
| * are declared under leds node in board devicetree. The driver | ||
| * takes care of configuring the pin to output state before using it. | ||
| * | ||
| * TODO: Implement Software Blinking | ||
| */ | ||
|
|
||
| #include <device.h> | ||
| #include <gpio.h> | ||
| #include <led.h> | ||
| #include <misc/util.h> | ||
| #include <zephyr.h> | ||
|
|
||
| #define SYS_LOG_LEVEL CONFIG_SYS_LOG_LED_LEVEL | ||
| #include <logging/sys_log.h> | ||
|
|
||
| #include "led_context.h" | ||
|
|
||
| enum led_gpio_modes { | ||
| LED_OFF, | ||
| LED_ON, | ||
| }; | ||
|
|
||
| struct led_gpio_cfg { | ||
| char *gpio_port; | ||
| u8_t gpio_pin; | ||
| u8_t gpio_polarity; | ||
| }; | ||
|
|
||
| struct led_gpio_data { | ||
| struct device *gpio; | ||
| struct led_data dev_data; | ||
| }; | ||
|
|
||
| static inline int led_gpio_on(struct device *dev, u32_t led) | ||
| { | ||
| const struct led_gpio_cfg *cfg = dev->config->config_info; | ||
| struct led_gpio_data *data = dev->driver_data; | ||
|
|
||
| /* Make sure that the LED is valid */ | ||
| if (led != cfg->gpio_pin) { | ||
| return -EINVAL; | ||
MaureenHelm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /* | ||
| * Check for active high or active low defined in DT. For | ||
| * simplicity, non zero cases are considered as active high | ||
| * and zeroes are active low. | ||
| */ | ||
| gpio_pin_write(data->gpio, led, | ||
| (cfg->gpio_polarity & GPIO_INT_ACTIVE_HIGH) != 0); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static inline int led_gpio_off(struct device *dev, u32_t led) | ||
| { | ||
| const struct led_gpio_cfg *cfg = dev->config->config_info; | ||
| struct led_gpio_data *data = dev->driver_data; | ||
|
|
||
| /* Make sure that the LED is valid */ | ||
| if (led != cfg->gpio_pin) { | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| /* | ||
| * Check for active high or active low defined in DT. For | ||
| * simplicity, non zero cases are considered as active high | ||
| * and zeroes are active low. | ||
| */ | ||
| gpio_pin_write(data->gpio, led, | ||
| (cfg->gpio_polarity & GPIO_INT_ACTIVE_HIGH) == 0); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static int led_gpio_init(struct device *dev) | ||
| { | ||
| const struct led_gpio_cfg *cfg = dev->config->config_info; | ||
| struct led_gpio_data *data = dev->driver_data; | ||
| struct led_data *dev_data = &data->dev_data; | ||
|
|
||
| data->gpio = device_get_binding(cfg->gpio_port); | ||
| if (!data->gpio) { | ||
| SYS_LOG_DBG("Failed to get GPIO device"); | ||
| return -EINVAL; | ||
| } | ||
|
|
||
| /* Set LED pin as output */ | ||
| gpio_pin_configure(data->gpio, cfg->gpio_pin, GPIO_DIR_OUT); | ||
|
|
||
| /* Blinking and Brightness are not supported yet */ | ||
| dev_data->min_period = 0; | ||
| dev_data->max_period = 0; | ||
| dev_data->min_brightness = 0; | ||
| dev_data->max_brightness = 0; | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static const struct led_driver_api led_gpio_api = { | ||
| .on = led_gpio_on, | ||
| .off = led_gpio_off, | ||
| }; | ||
|
|
||
| #define DEFINE_LED_GPIO(_num) \ | ||
| \ | ||
| static struct led_gpio_data led_gpio_data_##_num; \ | ||
| \ | ||
| static const struct led_gpio_cfg led_gpio_cfg_##_num = { \ | ||
| .gpio_port = LED##_num##_GPIO_CONTROLLER, \ | ||
| .gpio_pin = LED##_num##_GPIO_PIN, \ | ||
| .gpio_polarity = LED##_num##_GPIO_FLAGS, \ | ||
| }; \ | ||
| \ | ||
| DEVICE_AND_API_INIT(led_gpio_##_num, LED##_num##_LABEL, \ | ||
| led_gpio_init, \ | ||
| &led_gpio_data_##_num, \ | ||
| &led_gpio_cfg_##_num, \ | ||
| POST_KERNEL, CONFIG_LED_INIT_PRIORITY, \ | ||
| &led_gpio_api) | ||
|
|
||
| #ifdef CONFIG_LED_GPIO_0 | ||
| DEFINE_LED_GPIO(0); | ||
| #endif | ||
|
|
||
| #ifdef CONFIG_LED_GPIO_1 | ||
| DEFINE_LED_GPIO(1); | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| CONFIG_GPIO=y | ||
| CONFIG_SERIAL=n | ||
| CONFIG_LED=y | ||
| CONFIG_LED_GPIO=y | ||
| CONFIG_LED_GPIO_0=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is 'led' argument actually required ?
We should be able to retrieve it from *dev.
And I think this is the interest of the API, so user doesn't care of the PIN
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ledargument is there only for the purpose of LED API and yes, we can retrieve it from *dev. Anyway, passing theledargument gives us the possibility of checking the correct pin in led_on/led_off as below:Will include the above check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mani-Sadhasivam ,
In the other hand not having 'led' as arguments makes this kind of issue impossible.
I still prefer this is removed from the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erwango No. This only holds true for
leds-gpiodriver, for other drivers there is no GPIO information incfg.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the argument should stay and the driver should check it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check following PR to see if it wouldn't provide similar service and spare the argument?
#12056
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how #12056 relates. I also agree that since this is being done as an implementation for an API that is designed to support multiple LEDs the argument must stay. However, see review summary.