User avatar
gordon@drogon.net
Posts: 1956
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Can we control the on-board leds

Wed Jun 19, 2013 2:08 pm

If you want to control the on-board 'ACT' LED, then here is a quick way:

Get wiringPi:

Code: Select all

  cd
  git clone git://git.drogon.net/wiringPi
  cd wiringPi
  ./build
  cd examples
  make okLed
  sudo ./okLed
And see what happens :)

-Gordon
--
Gordons projects: https://projects.drogon.net/

amallaya
Posts: 1
Joined: Sun Aug 04, 2013 2:05 pm

Re: Can we control the on-board leds

Sun Aug 04, 2013 2:09 pm

Thanks for the info @BrianW
It is fun :D

dingo35
Posts: 12
Joined: Sat Oct 26, 2013 8:09 am

Re: Can we control the on-board leds

Sat Oct 26, 2013 8:21 am

I succesfully managed to clear the bright colored LED's on my Raspberry Pi, except for the red power led.

Clearing or setting the green led is already documented, so I won't cover that here.

Clearing the led's that are flickering at ethernet access can be done by modifying a few lines of kernel code.
In drivers/net/usb/smsc95xx.h you will find the code:

Code: Select all

#define LED_GPIO_CFG      (0x24)
#define LED_GPIO_CFG_SPD_LED    (0x01000000)
#define LED_GPIO_CFG_LNK_LED    (0x00100000)
#define LED_GPIO_CFG_FDX_LED    (0x00010000)
If you modify this to:

Code: Select all

#define LED_GPIO_CFG      (0x24)
#define LED_GPIO_CFG_SPD_LED    (0x00000000)
#define LED_GPIO_CFG_LNK_LED    (0x00000000)
#define LED_GPIO_CFG_FDX_LED    (0x00000000)
and recompile the kernel following these excellent instructions:
http://elinux.org/RPi_Kernel_Compilation, you will find your new kernel-image will not program the gpio pins of the LAN9512 chip to drive the colored leds.

I have also tried to reprogram the gpio pins from user space (with and without using libusb), but somehow it doesn't work; I find that, even after recompiling the kernel so it uses smsc95xx as a kernel module, and unloading this module, the device 0424:ec00 can be opened, even claimed, but writing the LED configuration with the usb_control_msg returns error -32 EBUSY, signalling that somehow the chip isn't accepting that control message.

If people are interested I can post my experimental code.

unkzo
Posts: 49
Joined: Mon May 06, 2013 11:38 pm

Re: Can we control the on-board leds

Thu Nov 28, 2013 11:34 pm

Hi guys, now we can control all of ethernet leds from userspace: http://everpi.tsar.in/2013/11/patch-par ... rrypi.html

dingo35
Posts: 12
Joined: Sat Oct 26, 2013 8:09 am

Re: Can we control the on-board leds

Fri Nov 29, 2013 7:41 am

This is not really taking control from userspace, since the kernel has to be recompiled also (first reason to recompile is to enable sms95xx support as a module, since per default in Raspbian it is compiled in the kernel itself; second reason to recompile is for the module to include the patch... ) but it looks like a nice patch!

With a solution from userspace I mean a program that writes the correct values to the low level registers to control the leds without patching the kernel.

But progress is made, thanks for this!

cmk20
Posts: 4
Joined: Sun Nov 10, 2013 2:01 am

Re: Can we control the on-board leds

Sun Jun 29, 2014 11:23 pm

hi i'm new to this subject and I have my raspi booted of a usb drive but I want the act light to flash when it accesses the usb disk but it only blinks when it accesses the sd card, can I change this?

User avatar
mikerr
Posts: 2470
Joined: Thu Jan 12, 2012 12:46 pm
Location: Up north , UK
Contact: Website

Re: Can we control the on-board leds

Wed Jul 02, 2014 11:33 am

You would need to add a trigger for usb filesystem access (no idea how though ;) )

I was surprised to find I had lots of triggers already setup instead of just SD (mmc0):

Code: Select all

cat /sys/class/leds/led0/trigger
none [mmc0] timer oneshot heartbeat backlight gpio cpu0 default-on rfkill0 phy0rx phy0tx phy0assoc phy0radio
I did find this (for openWRT routers, but still relevant):
http://wiki.openwrt.org/doc/uci/system#leds
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
ragnarjensen
Posts: 318
Joined: Wed May 15, 2013 6:13 pm
Location: Stockholm, Sweden
Contact: Website

Re: Can we control the on-board leds

Wed Jul 02, 2014 7:38 pm

I wrote a program to do that.
Only tested in Raspbian.

Code: Select all

/*
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to <http://unlicense.org/>
 *
 **************************************************************************
 *
 * A disk activity light for the Raspberry Pi.
 * Blinks the ACT led on all mass storage I/O, i.e. not only the SD card. 
 * Based on hddled.c - http://members.optusnet.com.au/foonly/whirlpool/code/hddled.c -
 * 
 *
 * To compile:
 *   gcc -Wall -O3 -o actledPi actledPi.c
 *
 * Options:
 * -d, --detach               Detach from terminal (become a daemon)
 * -r, --refresh=VALUE        Refresh interval (default: 20 ms)
 *
 */


#define VMSTAT "/proc/vmstat"
#define ACTLED "/sys/class/leds/led0/brightness"
#define TRGCTL "/sys/class/leds/led0/trigger"
#define LOW     0
#define HIGH    1

#define _GNU_SOURCE

#include <argp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

static unsigned int o_refresh = 20; /* milliseconds */
static int o_detach = 0;

static volatile sig_atomic_t running = 1;
static char *line = NULL;
static size_t len = 0;

FILE *actled = NULL;
FILE *trigger_ctl = NULL;

/* Reread the vmstat file */
int activity(FILE *vmstat) {
        static unsigned int prev_pgpgin, prev_pgpgout;
        unsigned int pgpgin, pgpgout;
        int found_pgpgin, found_pgpgout;
        int result;

        /* Reload the vmstat file */
        result = TEMP_FAILURE_RETRY(fseek(vmstat, 0L, SEEK_SET));
        if (result) {
                perror("Could not rewind " VMSTAT);
                return result;
        }

        /* Clear glibc's buffer */
        result = TEMP_FAILURE_RETRY(fflush(vmstat));
        if (result) {
                perror("Could not flush input stream");
                return result;
        }

        /* Extract the I/O stats */
        found_pgpgin = found_pgpgout = 0;
        while (getline(&line, &len, vmstat) != -1 && errno != EINTR) {
                if (sscanf(line, "pgpgin %u", &pgpgin))
                        found_pgpgin++;
                else if (sscanf(line, "pgpgout %u", &pgpgout))
                        found_pgpgout++;
                if (found_pgpgin && found_pgpgout)
                        break;
        }
        if (!found_pgpgin || !found_pgpgout) {
                fprintf(stderr, "Could not find required lines in " VMSTAT);
                return -1;
        }

        /* Anything changed? */
        result =
                (prev_pgpgin  != pgpgin) ||
                (prev_pgpgout != pgpgout);
        prev_pgpgin = pgpgin;
        prev_pgpgout = pgpgout;

        return result;
}

/* Update the LED */
void led(int on) {
        static int current = 1; /* Ensure the LED turns off on first call */
        if (current == on)
                return;

                if (on) {
                        fputs("255\n", actled);
                } else {
                        fputs("0\n", actled);
                }
                fflush(actled);
        current = on;
}

/* Signal handler -- break out of the main loop */
void shutdown(int sig) {
        running = 0;
}

/* Argp parser function */
error_t parse_options(int key, char *arg, struct argp_state *state) {
        switch (key) {
        case 'd':
                o_detach = 1;
                break;
        case 'r':
                o_refresh = strtol(arg, NULL, 10);
                if (o_refresh < 10)
                        argp_failure(state, EXIT_FAILURE, 0,
                                "refresh interval must be at least 10");
                break;
        }
        return 0;
}

int main(int argc, char **argv) {
        struct argp_option options[] = {
                { "detach",  'd',      NULL, 0, "Detach from terminal" },
                { "refresh", 'r',   "VALUE", 0, "Refresh interval (default: 20 ms)" },
                { 0 },
        };
        struct argp parser = {
                NULL, parse_options, NULL,
                "Show disk activity on all disks.",
                NULL, NULL, NULL
        };
        int status = EXIT_FAILURE;
        FILE *vmstat = NULL;
        struct timespec delay;

        /* Parse the command-line */
        parser.options = options;
        if (argp_parse(&parser, argc, argv, ARGP_NO_ARGS, NULL, NULL))
                goto out;

        delay.tv_sec = o_refresh / 1000;
        delay.tv_nsec = 1000000 * (o_refresh % 1000);


        /* Open the vmstat file */
        vmstat = fopen(VMSTAT, "r");
        if (!vmstat) {
                perror("Could not open " VMSTAT " for reading");
                goto out;
        }

        /* Change the trigger on the OK/Act LED to "none" */

        trigger_ctl = fopen(TRGCTL, "rw");
        if (!trigger_ctl) {
                perror("Unable to change LED trigger");
                goto out;
        }
        fputs ("none\n", trigger_ctl);
        fclose (trigger_ctl);


        /* Open the actled file */
        actled = fopen(ACTLED, "w");
        if (!actled) {
                perror("Could not open " ACTLED " for writing");
                goto out;
        }

        /* Ensure the LED is off */
        led(LOW);

        if (activity(vmstat) < 0)
                goto out;

        /* Detach from terminal? */
        if (o_detach) {
                pid_t child = fork();
                if (child < 0) {
                        perror("Could not detach from terminal");
                        goto out;
                }
                if (child) {
                        /* I am the parent */
                        status = EXIT_SUCCESS;
                        goto out;
                }
        }

        /* We catch these signals so we can clean up */
        {
                struct sigaction action;
                memset(&action, 0, sizeof(action));
                action.sa_handler = shutdown;
                sigemptyset(&action.sa_mask);
                action.sa_flags = 0; /* We block on usleep; don't use SA_RESTART */
                sigaction(SIGHUP, &action, NULL);
                sigaction(SIGINT, &action, NULL);
                sigaction(SIGTERM, &action, NULL);
        }

        /* Loop until signal received */
        while (running) {
                int a;
                if (nanosleep(&delay, NULL) < 0)
                        break;
                a = activity(vmstat);
                if (a < 0)
                        break;
                led(a);
        }

        /* Ensure the LED is off */
        led(LOW);

        status = EXIT_SUCCESS;

out:
        if (line) free(line);
        if (vmstat) fclose(vmstat);
        if (actled) {
                fclose(actled);
                trigger_ctl = fopen(TRGCTL, "rw");
                fputs("mmc0\n", trigger_ctl);
                fclose(trigger_ctl);
        }
        return status;
}
--
Ragnar

cmk20
Posts: 4
Joined: Sun Nov 10, 2013 2:01 am

Re: Can we control the on-board leds

Wed Jul 02, 2014 10:52 pm

ragnarjensen wrote:I wrote a program to do that.
Only tested in Raspbian.

Code: Select all

/*
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to <http://unlicense.org/>
 *
 **************************************************************************
 *
 * A disk activity light for the Raspberry Pi.
 * Blinks the ACT led on all mass storage I/O, i.e. not only the SD card. 
 * Based on hddled.c - http://members.optusnet.com.au/foonly/whirlpool/code/hddled.c -
 * 
 *
 * To compile:
 *   gcc -Wall -O3 -o actledPi actledPi.c
 *
 * Options:
 * -d, --detach               Detach from terminal (become a daemon)
 * -r, --refresh=VALUE        Refresh interval (default: 20 ms)
 *
 */


#define VMSTAT "/proc/vmstat"
#define ACTLED "/sys/class/leds/led0/brightness"
#define TRGCTL "/sys/class/leds/led0/trigger"
#define LOW     0
#define HIGH    1

#define _GNU_SOURCE

#include <argp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

static unsigned int o_refresh = 20; /* milliseconds */
static int o_detach = 0;

static volatile sig_atomic_t running = 1;
static char *line = NULL;
static size_t len = 0;

FILE *actled = NULL;
FILE *trigger_ctl = NULL;

/* Reread the vmstat file */
int activity(FILE *vmstat) {
        static unsigned int prev_pgpgin, prev_pgpgout;
        unsigned int pgpgin, pgpgout;
        int found_pgpgin, found_pgpgout;
        int result;

        /* Reload the vmstat file */
        result = TEMP_FAILURE_RETRY(fseek(vmstat, 0L, SEEK_SET));
        if (result) {
                perror("Could not rewind " VMSTAT);
                return result;
        }

        /* Clear glibc's buffer */
        result = TEMP_FAILURE_RETRY(fflush(vmstat));
        if (result) {
                perror("Could not flush input stream");
                return result;
        }

        /* Extract the I/O stats */
        found_pgpgin = found_pgpgout = 0;
        while (getline(&line, &len, vmstat) != -1 && errno != EINTR) {
                if (sscanf(line, "pgpgin %u", &pgpgin))
                        found_pgpgin++;
                else if (sscanf(line, "pgpgout %u", &pgpgout))
                        found_pgpgout++;
                if (found_pgpgin && found_pgpgout)
                        break;
        }
        if (!found_pgpgin || !found_pgpgout) {
                fprintf(stderr, "Could not find required lines in " VMSTAT);
                return -1;
        }

        /* Anything changed? */
        result =
                (prev_pgpgin  != pgpgin) ||
                (prev_pgpgout != pgpgout);
        prev_pgpgin = pgpgin;
        prev_pgpgout = pgpgout;

        return result;
}

/* Update the LED */
void led(int on) {
        static int current = 1; /* Ensure the LED turns off on first call */
        if (current == on)
                return;

                if (on) {
                        fputs("255\n", actled);
                } else {
                        fputs("0\n", actled);
                }
                fflush(actled);
        current = on;
}

/* Signal handler -- break out of the main loop */
void shutdown(int sig) {
        running = 0;
}

/* Argp parser function */
error_t parse_options(int key, char *arg, struct argp_state *state) {
        switch (key) {
        case 'd':
                o_detach = 1;
                break;
        case 'r':
                o_refresh = strtol(arg, NULL, 10);
                if (o_refresh < 10)
                        argp_failure(state, EXIT_FAILURE, 0,
                                "refresh interval must be at least 10");
                break;
        }
        return 0;
}

int main(int argc, char **argv) {
        struct argp_option options[] = {
                { "detach",  'd',      NULL, 0, "Detach from terminal" },
                { "refresh", 'r',   "VALUE", 0, "Refresh interval (default: 20 ms)" },
                { 0 },
        };
        struct argp parser = {
                NULL, parse_options, NULL,
                "Show disk activity on all disks.",
                NULL, NULL, NULL
        };
        int status = EXIT_FAILURE;
        FILE *vmstat = NULL;
        struct timespec delay;

        /* Parse the command-line */
        parser.options = options;
        if (argp_parse(&parser, argc, argv, ARGP_NO_ARGS, NULL, NULL))
                goto out;

        delay.tv_sec = o_refresh / 1000;
        delay.tv_nsec = 1000000 * (o_refresh % 1000);


        /* Open the vmstat file */
        vmstat = fopen(VMSTAT, "r");
        if (!vmstat) {
                perror("Could not open " VMSTAT " for reading");
                goto out;
        }

        /* Change the trigger on the OK/Act LED to "none" */

        trigger_ctl = fopen(TRGCTL, "rw");
        if (!trigger_ctl) {
                perror("Unable to change LED trigger");
                goto out;
        }
        fputs ("none\n", trigger_ctl);
        fclose (trigger_ctl);


        /* Open the actled file */
        actled = fopen(ACTLED, "w");
        if (!actled) {
                perror("Could not open " ACTLED " for writing");
                goto out;
        }

        /* Ensure the LED is off */
        led(LOW);

        if (activity(vmstat) < 0)
                goto out;

        /* Detach from terminal? */
        if (o_detach) {
                pid_t child = fork();
                if (child < 0) {
                        perror("Could not detach from terminal");
                        goto out;
                }
                if (child) {
                        /* I am the parent */
                        status = EXIT_SUCCESS;
                        goto out;
                }
        }

        /* We catch these signals so we can clean up */
        {
                struct sigaction action;
                memset(&action, 0, sizeof(action));
                action.sa_handler = shutdown;
                sigemptyset(&action.sa_mask);
                action.sa_flags = 0; /* We block on usleep; don't use SA_RESTART */
                sigaction(SIGHUP, &action, NULL);
                sigaction(SIGINT, &action, NULL);
                sigaction(SIGTERM, &action, NULL);
        }

        /* Loop until signal received */
        while (running) {
                int a;
                if (nanosleep(&delay, NULL) < 0)
                        break;
                a = activity(vmstat);
                if (a < 0)
                        break;
                led(a);
        }

        /* Ensure the LED is off */
        led(LOW);

        status = EXIT_SUCCESS;

out:
        if (line) free(line);
        if (vmstat) fclose(vmstat);
        if (actled) {
                fclose(actled);
                trigger_ctl = fopen(TRGCTL, "rw");
                fputs("mmc0\n", trigger_ctl);
                fclose(trigger_ctl);
        }
        return status;
}
--
Ragnar
Cool but how do you set this up?

User avatar
ragnarjensen
Posts: 318
Joined: Wed May 15, 2013 6:13 pm
Location: Stockholm, Sweden
Contact: Website

Re: Can we control the on-board leds

Thu Jul 03, 2014 8:22 am

Save as actledPi.c
Then build it:

Code: Select all

gcc -Wall -O3 -o actledPi actledPi.c
Put the binary somewhere in the PATH, I put it in /usr/local/bin:

Code: Select all

sudo mv actledPi /usr/local/bin
and run it:

Code: Select all

sudo actledPi -d
Put the line

Code: Select all

/usr/local/bin/actledPi -d
in /etc/rc.local, just before the last line, that says "exit 0", to have it start automatically at boot.
--
Ragnar

cmk20
Posts: 4
Joined: Sun Nov 10, 2013 2:01 am

Re: Can we control the on-board leds

Thu Jul 03, 2014 12:08 pm

Thanks all try it!

cmk20
Posts: 4
Joined: Sun Nov 10, 2013 2:01 am

Re: Can we control the on-board leds

Thu Jul 03, 2014 9:27 pm

yay it works my pi had problems compiling it because the text editor i was using put hidden Unicode symbols in it i used a simpler one and it compiled just fine...?

User avatar
tonyhansen
Posts: 13
Joined: Sat Jan 03, 2015 6:20 am

Re: Can we control the on-board leds

Sat Jan 03, 2015 6:29 am

With the Raspberry Pi model B+, a program I had that used GPIO16 to drive the STATUS OK LED on the Raspberry Pi model B no longer works. Given that GPIO16 is now exposed on pin36 of the 40-pin header, I'm not too surprised. However, this does beg the question: besides writing to /sys/class/leds/led0/brightness, how can I drive the STATUS OK LED on the B+ using the GPIO interface?

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9480
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Can we control the on-board leds

Sat Jan 03, 2015 5:11 pm

True! On a A+ and B+ the status ACT LED is now controlled by GPIO47, and its actually controlling a FET, so on/off is now inverted, with high being ON!

In fact you can now also sense the status of the PWR LED, that is the brownout detector driving it, by using GPIO35 to read it out, but if you change GPIO35 to an output you can actually control the PWR LED now too, same way as the ACT LED.

User avatar
DougieLawson
Posts: 29359
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Can we control the on-board leds

Sat Jan 03, 2015 5:14 pm

tonyhansen wrote:With the Raspberry Pi model B+, a program I had that used GPIO16 to drive the STATUS OK LED on the Raspberry Pi model B no longer works. Given that GPIO16 is now exposed on pin36 of the 40-pin header, I'm not too surprised. However, this does beg the question: besides writing to /sys/class/leds/led0/brightness, how can I drive the STATUS OK LED on the B+ using the GPIO interface?
Change your code to use GPIO47.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

User avatar
tonyhansen
Posts: 13
Joined: Sat Jan 03, 2015 6:20 am

Re: Can we control the on-board leds

Sat Jan 03, 2015 11:25 pm

mahjongg, thank you for the complete response, along with the additional information on controlling the other status LED.

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9480
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Can we control the on-board leds

Sat Jan 03, 2015 11:47 pm

I'm very happy to help.

User avatar
Hitechcomputergeek
Posts: 28
Joined: Wed Nov 27, 2013 4:29 am

Re: Can we control the on-board leds

Mon May 11, 2015 7:19 pm

Geekmason wrote:I'm not sure, but I it looks like this thread (or rather its intrepid participants) may be able to solve a riddle I have concerning the onboard LEDs. See my questions here: http://www.raspberrypi.org/phpBB3/viewt ... 83#p366483

I was hoping to "recreate" an ACT(ivity) LED off one of the GPIO pins so I can mount it to the exterior of a custom case. Doable?
Not sure about this, but could you set the trigger for the other GPIO pin to mmc0 using /sys/idk/trigger?

User avatar
ConnersJM
Posts: 2
Joined: Thu May 28, 2015 10:41 pm
Location: San Jose, CA

Re: Can we control the on-board leds

Thu May 28, 2015 10:49 pm

This is excellent information. I managed to add 2 additional LED instantiations in the board support kernel file to control the additional LEDs that we're using for our system. Much less hassle than instantiating the GPIO in our control program.

Code: Select all

static struct gpio_led bcm2708_leds[] = {
    [0] = {
           .gpio = 16,
           .name = "led0",
           .default_trigger = "mmc0",
           .active_low = 1,
           },
    [1] = {
          .gpio = 2,
          .name = "led1",
          .default_trigger = "cpu0",
          .active_low = 1,
          },
    [2] = {
         .gpio = 12,
         .name = "led2",
         .default_trigger = "cpu0",
         .active_low = 1,
         },
};

static struct gpio_led_platform_data bcm2708_led_pdata = {
	.num_leds = ARRAY_SIZE(bcm2708_leds),
	.leds = bcm2708_leds,
};

static struct platform_device bcm2708_led_device = {
	.name = "leds-gpio",
	.id = -1,
	.dev = {
		.platform_data = &bcm2708_led_pdata,
		},
};

static void __init bcm2708_init_led(void)
{
    bcm2708_leds[0].gpio = disk_led_gpio;
    bcm2708_leds[0].active_low = disk_led_active_low;
    bcm2708_leds[1].gpio = 2;
    bcm2708_leds[1].active_low = 1;
    bcm2708_leds[2].gpio = 12;
    bcm2708_leds[2].active_low = 1;
    platform_device_register(&bcm2708_led_device);
}
For the trial-run I set them to cpu0 to show activity, but I'll change them to trigger on gpio. Then they're controlled by changing the brightness between 0 and 1<=x<=255. I don't see any difference in brightness when I tested, but I suppose they could be PWMed to control brightness. Not sure if this was the intent. Does anyone know?

-Jeremy

rern
Posts: 2
Joined: Tue Sep 06, 2016 6:14 am

Re: Can we control the on-board leds

Tue Sep 06, 2016 8:12 am

@ragnarjensen
After hours of seacrhing for GPIO LED for USB drive, your code is the only one I've found that do exactly I want. Please advice how to make it works on ArchLinux.

actledPi and hddledPi on another post, Custom Case indicator LEDs, run successfully on *Raspbian(OSMC) but not on **ArchLinux(RuneAudio).
  • To run actledPi on GPIO connected LED:
    On Pi3, dtoverlay=pi3-act-led,gpio=n must be appended to config.txt .
* compiling failed on error: usb.h, $ sudo apt-get install libusb-dev fixed it.
** ArchLinux compiling complained about missing braces at ... if (current == on) ...

(Edited: Sep 07, 2016)
Result after tried both actledPi and hddledPi.

User avatar
DavidS
Posts: 3009
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Can we control the on-board leds

Sat Sep 10, 2016 8:19 pm

simplesi wrote:Are any of the on-board LEDS controllable by us users?

Simon
You could try something like:

Code: Select all



.LedInit:
 LDR R1,GPIOBase
 LDR R0,#1 SHL #21
 STR R0,[R1,#16]
 MOV R15,R14

.LedOn
 LDR R1,GPIOBase
 LDR R0,#1 SHL #15    ;Turning on the GPIO controling the LED.
 STR R0,[R1,#32]      ;Write the GPIO Set Register.
 MOV R15,R14          ;Return to caller.

.LedOff
 LDR R1,GPIOBase
 LDR R0,#1 SHL #15    ;We are changing the the bit for the LED GPIO.
 STR R0,[R1,#44]      ;Write to the clear GPIO Clear location.
 MOV R15,R14          ;Return to caller.



.GPIOBase
 EQUW &3F200000
Assuming you are running on the ARM, and on a RPi B+/2B/3B. A bit different if you are running on the VideoCore IV, though same basic concept.
ARM BASIC: For the love of Simplicity, Fast Interpreted BASIC, and Assembly Language.
Always KISS Keep It Simple Silly.

User avatar
DougieLawson
Posts: 29359
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Can we control the on-board leds

Sun Sep 11, 2016 10:07 am

DavidS wrote:
simplesi wrote:Are any of the on-board LEDS controllable by us users?

Simon
You could try something like:
...
Assuming you are running on the ARM, and on a RPi B+/2B/3B. A bit different if you are running on the VideoCore IV, though same basic concept.
You're replying to a post from Thu 26 Jul 2012 @ 21:19 by someone who hasn't visited the forum since Thu 05 Feb 2015 @ 12:46.

Good solid necro there.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

User avatar
DavidS
Posts: 3009
Joined: Thu Dec 15, 2011 6:39 am
Location: USA
Contact: Website

Re: Can we control the on-board leds

Sun Sep 11, 2016 1:41 pm

DougieLawson wrote:
DavidS wrote:
simplesi wrote:Are any of the on-board LEDS controllable by us users?

Simon
You could try something like:
...
Assuming you are running on the ARM, and on a RPi B+/2B/3B. A bit different if you are running on the VideoCore IV, though same basic concept.
You're replying to a post from Thu 26 Jul 2012 @ 21:19 by someone who hasn't visited the forum since Thu 05 Feb 2015 @ 12:46.

Good solid necro there.
Ooops
ARM BASIC: For the love of Simplicity, Fast Interpreted BASIC, and Assembly Language.
Always KISS Keep It Simple Silly.

Heater
Posts: 7602
Joined: Tue Jul 17, 2012 3:02 pm

Re: Can we control the on-board leds

Sun Sep 11, 2016 2:06 pm

I don't see what is wrong with posting solutions to problems that were asked a long time ago.

In this case I can well imagine someone new here having the same question. Having googled it, as people always suggest they should, they may end up here. At which point a recent post with a new or better solution, or a post pointing out that perhaps the old solution don't work anymore due to whatever hardware software changes, is not a bad thing.

In short questions don't become invalid just because they are old or the original poster has disappeared.

User avatar
DougieLawson
Posts: 29359
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Can we control the on-board leds

Sun Sep 11, 2016 5:49 pm

Heater wrote: In short questions don't become invalid just because they are old or the original poster has disappeared.
Yes they do, because the RPi3B hardware is just a little bit different to the RPi1B and code that works on a 3B won't run on a 1B.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 4 guests