GPIO LEDS: Difference between revisions

From ArmadeusWiki
(New page: How to use leds-gpio driver to manage states of connected leds of your Armadeus board. ==Introduction== You can manage a led connected to a GPIO pin. The managing is similar with the sta...)
 
 
(23 intermediate revisions by 6 users not shown)
Line 1: Line 1:
How to use leds-gpio driver to manage states of connected leds of your Armadeus board.
How to use ''gpio-leds'' driver to manage states of the LEDs connected to your Armadeus Dev board.


==Introduction==
==Introduction==


You can manage a led connected to a GPIO pin. The managing is similar with the standard GPIO sysfs driver, but you have some new features like triggers.
You can manage a LED connected to a GPIO pin with a sysfs interface very similar to the standard [[GPIOlib|GPIO sysfs driver]], but you will have some new features like triggers (e.g. "heartbeat" trigger will make LED blink like a heart at the rate of the CPU load) . Here are the GPIO used for the user LED on each APF board:
* APF9328: PORT A / bit 2
* APF27: GPIO_PORTF | 14
* APF28: PINID_GPMI_RDY1 (Bank 0 - pin 21)
* APF51: GPIO_PORTA | 2
* APF6: GPIO7 | 12
* OPOS6UL: GPIO3 | 4


==Configuration==
==Configuration==
{{Note|Following configuration instructions are now done by default in BSP > 5.3, so you can skip them if you use a recent armadeus software}}
You need to enable the leds-gpio driver in your kernel and some triggers like the "heartbeat".


First, you need to enable the leds-gpio driver in your kernel.
* Configure Linux kernel:
 
<pre class="host">
$ make linux-menuconfig
</pre>
<pre class="config">
<pre class="config">
Device Drivers  --->
Device Drivers  --->
     --- LED support
     --- LED support
         [*] LED Class Support
         [*] LED Class Support
              *** LED drivers ***
         <*> LED Support for GPIO connected LEDs
         <*> LED Support for GPIO connected LEDs
               [*] Platform device bindings for GPIO LEDs
               [*] Platform device bindings for GPIO LEDs
              *** LED Triggers ***
        [*]  LED Trigger support
        <*>    LED Timer Trigger
        <*>    LED Heartbeat Trigger
        <*>    LED backlight Trigger
        <*>    LED Default ON Trigger
</pre>
</pre>


Then, in your ''apf27-dev.c'', you need to define your LED <b>before</b> the variable ''platform_devices[]''.
Then, in your ''apfXX-dev.c'', you would need to define your LED <b>before</b> the variable ''platform_devices[]''. This code is already implemented for the APF27, APF28 and APF51 so the source code hereafter (for the APF27) is only present as a reference sample to understand how to activate a GPIO LED driver.


<source lang="c">
<source lang="c">
#include <linux/leds.h>
/* GPIO LED */
/* GPIO LED */
#if 1
#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
static struct gpio_led apf27dev_led[] = {
/* PORTF_14 used as leds-gpio (GPIO used as LED) */
static struct gpio_led apf27_gpio_leds[] = {
{
{
.name = "portf14",
.name = "apfdev:green:user",
.default_trigger = "none",
.default_trigger = "heartbeat",
.gpio = (GPIO_PORTF | 14),
.gpio = (GPIO_PORTF | 14),
//.active_low = 1,
.active_low = 1,
//.retain_state_suspended = 1,
//.default_state = LEDS_GPIO_DEFSTATE_ON,
},
},
};
};


static struct gpio_led_platform_data apf27_gpio_leds_platform_data = {
static struct gpio_led_platform_data apf27dev_led_data = {
.leds = apf27_gpio_leds,
.num_leds = ARRAY_SIZE(apf27dev_led),
.num_leds = ARRAY_SIZE(apf27_gpio_leds),
.leds = apf27dev_led
};
};


static struct platform_device apf27_gpio_leds_device = {
static struct platform_device apf27dev_led_dev = {
.name = "leds-gpio",
.name = "leds-gpio",
.id = 0,
.id = -1,
.dev = {
.dev = {
.platform_data = &apf27_gpio_leds_platform_data,
.platform_data = &apf27dev_led_data,
},
},
};
};
# define LEDS_GPIO &apf27_gpio_leds_device,
#endif /* CONFIG_LEDS_GPIO */
#else
# define LEDS_GPIO
#endif
</source>
</source>


Add the button to get it recognized by the card.
Add the LED to get it managed by the kernel.


<source lang="c">
<source lang="c">
static struct platform_device *platform_devices[] __initdata = {
static struct platform_device *platform_devices[] __initdata = {
#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
&apf27dev_led_dev,
#endif
ALSA_SOUND
ALSA_SOUND
LEDS_GPIO
 
};
};
</source>
</source>


Then rebuild and update your bard with the new kernel.
Upon the next kernel boot you should see the LED flash like a heartbeat (if you have activated the "heartbeat" trigger)
==Usage==
* In following instructions replace $LED with "''apfdev\:green\:user''", except on [[OPOS6ULDev]] where you will use "''User''":
<pre class="apf">
<pre class="apf">
# ls /sys/class/leds/portf14/
# ls /sys/class/leds/$LED/
brightness      max_brightness  subsystem      uevent
brightness      max_brightness  subsystem      uevent
device          power          trigger
device          power          trigger
</pre>
</pre>
You can change the trigger behaviors. By default, Heartbeat is selected:
* "heatbeat": led blinks like a heart and blink frequency will change according o the CPU activity.
* "nand-disk": the led will blink each time nand access occur (try with ''sync'' command to see it blinking).
<pre class="apf">
# cat /sys/class/leds/$LED/trigger
none nand-disk mmc0 timer [heartbeat] backlight gpio default-on
# echo none > /sys/class/leds/$LED/trigger
</pre>
Switch on and off the LED
<pre class="apf">
# cat /sys/class/leds/$LED/max_brightness > /sys/class/leds/$LED/brightness
# echo 0 > /sys/class/leds/$LED/brightness
</pre>
It is possible to switch LED state using the [[APF28Dev]]/[[OPOS6ULDev]] User button. This button is seen as gpio17 (as seen under APF28Dev [[datasheet]]) under Linux; gpio43 on [[OPOS6ULDev]].
Configure the LED trigger as gpio :
<pre class="apf">
# echo "gpio" > /sys/class/leds/$LED/trigger
</pre>
New config files are available :
<pre class="apf">
# ls /sys/class/leds/$LED/
brightness          gpio                power              uevent
desired_brightness  inverted            subsystem
device              max_brightness      trigger
</pre>
Then the trigger gpio can be configured with ''gpio'' file (replace 17 with 43 on [[OPOS6ULDev]]) :
<pre class="apf">
# echo 17 > /sys/class/leds/$LED/gpio
</pre>
Then pushing the user switch will now commute the LED state.
==Links==
* http://www.kernel.org/doc/Documentation/leds/leds-class.txt

Latest revision as of 18:16, 7 September 2017

How to use gpio-leds driver to manage states of the LEDs connected to your Armadeus Dev board.

Introduction

You can manage a LED connected to a GPIO pin with a sysfs interface very similar to the standard GPIO sysfs driver, but you will have some new features like triggers (e.g. "heartbeat" trigger will make LED blink like a heart at the rate of the CPU load) . Here are the GPIO used for the user LED on each APF board:

  • APF9328: PORT A / bit 2
  • APF27: GPIO_PORTF | 14
  • APF28: PINID_GPMI_RDY1 (Bank 0 - pin 21)
  • APF51: GPIO_PORTA | 2
  • APF6: GPIO7 | 12
  • OPOS6UL: GPIO3 | 4

Configuration

Note Note: Following configuration instructions are now done by default in BSP > 5.3, so you can skip them if you use a recent armadeus software

You need to enable the leds-gpio driver in your kernel and some triggers like the "heartbeat".

  • Configure Linux kernel:
$ make linux-menuconfig
Device Drivers  --->
     --- LED support
         [*] LED Class Support
              *** LED drivers *** 
         <*> LED Support for GPIO connected LEDs
              [*] Platform device bindings for GPIO LEDs
              *** LED Triggers ***
         [*]   LED Trigger support
         <*>     LED Timer Trigger
         <*>     LED Heartbeat Trigger
         <*>     LED backlight Trigger
         <*>     LED Default ON Trigger

Then, in your apfXX-dev.c, you would need to define your LED before the variable platform_devices[]. This code is already implemented for the APF27, APF28 and APF51 so the source code hereafter (for the APF27) is only present as a reference sample to understand how to activate a GPIO LED driver.

#include <linux/leds.h>

/* GPIO LED */
#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
static struct gpio_led apf27dev_led[] = {
	{
		.name = "apfdev:green:user",
		.default_trigger = "heartbeat",
		.gpio = (GPIO_PORTF | 14),
		.active_low = 1,
	},
};

static struct gpio_led_platform_data apf27dev_led_data = {
	.num_leds	= ARRAY_SIZE(apf27dev_led),
	.leds		= apf27dev_led
};

static struct platform_device apf27dev_led_dev = {
	.name		= "leds-gpio",
	.id		= -1,
	.dev		= {
		.platform_data	= &apf27dev_led_data,
	},
};
#endif /* CONFIG_LEDS_GPIO */

Add the LED to get it managed by the kernel.

static struct platform_device *platform_devices[] __initdata = {
#if defined(CONFIG_LEDS_GPIO) || defined(CONFIG_LEDS_GPIO_MODULE)
	&apf27dev_led_dev,
#endif
	ALSA_SOUND

};

Then rebuild and update your bard with the new kernel. Upon the next kernel boot you should see the LED flash like a heartbeat (if you have activated the "heartbeat" trigger)

Usage

  • In following instructions replace $LED with "apfdev\:green\:user", except on OPOS6ULDev where you will use "User":
# ls /sys/class/leds/$LED/
brightness      max_brightness  subsystem       uevent
device          power           trigger

You can change the trigger behaviors. By default, Heartbeat is selected:

  • "heatbeat": led blinks like a heart and blink frequency will change according o the CPU activity.
  • "nand-disk": the led will blink each time nand access occur (try with sync command to see it blinking).
# cat /sys/class/leds/$LED/trigger 
none nand-disk mmc0 timer [heartbeat] backlight gpio default-on 

# echo none > /sys/class/leds/$LED/trigger 

Switch on and off the LED

# cat /sys/class/leds/$LED/max_brightness > /sys/class/leds/$LED/brightness 

# echo 0 > /sys/class/leds/$LED/brightness 

It is possible to switch LED state using the APF28Dev/OPOS6ULDev User button. This button is seen as gpio17 (as seen under APF28Dev datasheet) under Linux; gpio43 on OPOS6ULDev. Configure the LED trigger as gpio :

# echo "gpio" > /sys/class/leds/$LED/trigger

New config files are available :

# ls /sys/class/leds/$LED/
brightness          gpio                power               uevent
desired_brightness  inverted            subsystem
device              max_brightness      trigger

Then the trigger gpio can be configured with gpio file (replace 17 with 43 on OPOS6ULDev) :

# echo 17 > /sys/class/leds/$LED/gpio

Then pushing the user switch will now commute the LED state.

Links