This is the area to clarify hardware specification if there's anything unclear from the datasheet. If the specification is software related, please ask in the related software section.

UPBoard onboard LEDs access from c users code

Lubo
Lubo New Member Posts: 41 ✭✭

I am able to set On/Off UPBoard onboard LEDs from text terminal , however I am not able to find how to change their status from my c code ( I am using mraa lib to access 40pin io header for example, I am assuming something similar ) . Is there any information, which pin are they mapped to... or the way how to set their on/off state from c level ?

Comments

  • Kurt
    Kurt New Member Posts: 146 ✭✭

    My guess is this should go into the software sections...

    But if you look at the Wiki: https://wiki.up-community.org/Pinout_UP2

    There is a section on the LEDS

    The UP Squared includes 4 LEDs (yellow, green, red and blue) on the underside of the board (underneath Ethernet dual port), which are controlled by the pin control FPGA on the board. As root, you can use the following commands to control the LEDs:

    Turn on the Green LED

    echo 1 > /sys/class/leds/upboard:green:/brightness

    Turn off the Green LED

    echo 0 > /sys/class/leds/upboard:green:/brightness

    For other LEDS, replace "green" with "red","blue" or "yellow" in the commands above.

    Currently I don't see any code in MRAA that handles these. I thought they might have been defined as GPIO pins, but I don't think so. You can however write code that opens up those specific files and do a write to them. But as mentioned you need to have root privileges.

  • Lubo
    Lubo New Member Posts: 41 ✭✭

    Thank you for answer,

    Turn on the Green LED

    echo 1 > /sys/class/leds/upboard:green:/brightness

    Turn off the Green LED

    echo 0 > /sys/class/leds/upboard:green:/brightness

    For other LEDS, replace "green" with "red","blue" or "yellow" in the commands above.

    Yes, this is exactly how I am able to control them from terminal , of course with right privileges assigned to user ....

    Currently I don't see any code in MRAA that handles these. I thought they might have been defined as GPIO pins, but I don't think so. You can however write code that opens up those specific files and do a write to them.

    To use files is option... but I would prefer something like "direct" way as GPIO pins from c code ... that is my question .... so still open....

  • Kurt
    Kurt New Member Posts: 146 ✭✭

    I understand.

    Note: MRAA does the same stuff. On a few systems like edison, there is some support for memory mapped IO that works for some/all GPIO pins, otherwise the mraa_gpio_* functions also simply work through the /sys filenames, starting at: /sys/class/gpio

    FYI - I hacked up a simple test program that blinks the 3 LEDS on my UP board. Note: I have to run this app with sudo...

    #include<sys/stat.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<stdint.h>
    #include<stdio.h>
    #include<string.h>
    #include<time.h>
    #include <errno.h>
    
    typedef enum {GREEN_LED = 0, YELLOW_LED = 1, RED_LED = 2} Leds;
    
    extern bool set_led(Leds led, uint8_t value);
    
    int main(int argc, char** argv)
    {
        // Pretty simple setup.
        printf("\n\n*** Start testing leds ***\n");
        //usleep(250000L);
        while(1) {
            for (int i = 0; i < 8; i++) {
                set_led(GREEN_LED, (i&1)? 1 : 0);
                set_led(YELLOW_LED, (i&2)? 1 : 0);
                set_led(RED_LED, (i&4)? 1 : 0);
                usleep(250000L);    // sleep 250ms
            }
        }
    }
    
    bool set_led(Leds led, uint8_t value) {
        int led_handle = -1;
        char value_string[4];
    
        // Open file
        switch (led) {
            case GREEN_LED:
                led_handle = open("/sys/class/leds/upboard:green:/brightness", O_WRONLY);
                break;
            case YELLOW_LED:
                led_handle = open("/sys/class/leds/upboard:yellow:/brightness", O_WRONLY);
                break;
            case RED_LED:
                led_handle = open("/sys/class/leds/upboard:red:/brightness", O_WRONLY);
                break;
        }
        if (led_handle == -1) {
            printf("set_led(%d, %d) failed to open file: %d\n\r", (uint8_t)led, value, errno);
            return false;
        }
        // Write file
        int length = snprintf(value_string, sizeof(value_string), "%d", value);
        if (write(led_handle, value_string, length * sizeof(char)) == -1) {
            printf("set_led(%d, %d) failed to write to file: %d\n\r", (uint8_t)led, value, errno);
            close(led_handle);
            return false;
        }
        close(led_handle);
        //printf("set_led(%d, %d) succeed\n\r", (uint8_t)led, value);
        return true;
    }
    
  • Kurt
    Kurt New Member Posts: 146 ✭✭

    Thought I would mention, that I was able to get the program to work for a standard user without sudo, in the same way that I was able to do so for gpio calls.

    That is I created a new user group leds, and added myself to it:

    sudo addgroup leds
    sudo adduser kurt leds
    

    Obviously you would change the name kurt to whatever user names you need...

    I then created a udev rules file:

    kurt@kurt-UP-CHT01:~$ ls -l /etc/udev/rules.d/50-leds.rules
    -rw-r--r-- 1 root root 244 Jan 13 10:41 /etc/udev/rules.d/50-leds.rules
    kurt@kurt-UP-CHT01:~$ cat /etc/udev/rules.d/50-leds.rules
    SUBSYSTEM=="leds*", PROGRAM="/bin/sh -c '\
            chown -R root:leds /sys/class/leds && chmod -R 770 /sys/class/leds;\
            chown -R root:leds /sys/devices/platform/up-pinctrl/leds && chmod -R 770 /sys/devices/platform/up-pinctrl/leds;\
    '"
    kurt@kurt-UP-CHT01:~$
    

    And rebooted.

  • Kurt
    Kurt New Member Posts: 146 ✭✭

    Another quick update. I tried this also on my UP2 board that has ubuntu installed (one that supports the IO pins and the like...

    I added the blue led, added calls to MRAA that finds out which board I have and now blinks 3 or 4 leds depending on which board. Test app also catches ctrl+c to end the program and turn off all leds.

    Note: the devices /sys/class/leds are links to somewhere in the /sys/devices/platform/* trees and are different between my two boards so my udev rule was updated to hopefully make it work on both without needing sudo...

    SUBSYSTEM=="leds*", PROGRAM="/bin/sh -c '\
            chown -R root:leds /sys/class/leds && chmod -R 770 /sys/class/leds;\
            chown -R root:leds /sys/devices/platform/up-pinctrl/leds && chmod -R 770 /sys/devices/platform/up-pinctrl/leds;\
            chown -R root:leds /sys/devices/platform/AANT0F01:00/upboard-led.* && chmod -R 770 /sys/devices/platform/AANT0F01:00/upboard-led.*;\
    '"
    

    Note: Did not test on ubilinux, so not sure if any changes would be needed.

    I put the test program and udev rule file up in my Raspberry Pi project.
    https://github.com/KurtE/Raspberry_Pi/tree/master/testUPLeds

  • Kurt
    Kurt New Member Posts: 146 ✭✭

    I thought I would answer this one more time a different way.

    Turns out MRAA does have LED objects. Notes on installing MRAA.

    If you use the instructions on how to install MRAA that is up on the UP wiki: https://wiki.up-community.org/MRAA/UPM
    The command: udo apt-get install mraa upm
    Fails to find mraa or upm.

    Or if you install MRAA using the instructions shown up on the main github for MRAA: https://github.com/intel-iot-devkit/mraa the version installed here did not appear to have the LED objects in the current version...

    So I installed MRAA from sources and build using the instructions that you can find in the main github readme file for the mraa project.

    I then made a version of my blinking of the different LEDS on the UP2... Should work as well on UP, but I did not try it. Also note: I am still using the udev rules from above, so can run without using sudo...

    Code for MRAAA version:

    #include <fcntl.h>
    #include <unistd.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <signal.h>
    #include <errno.h>
    
    #include "mraa.h"
    
    volatile uint8_t g_continue = true;
    
    //====================================================================================================
    // SignalHandler - Try to free up things like servos if we abort.
    //====================================================================================================
    void SignalHandler(int sig){
        printf("Caught signal %d\n", sig);
    
        if (!g_continue) {
            printf("Second signal Abort\n");
            exit(1);
        }
        // Set global telling main to abort and return
        g_continue = false;
    }
    
    
    int main(int argc, char** argv)
    {
        bool has_blue_led = false;
        uint8_t exit_loop_count = 8;
    
      // Contexts for the up to 4 leds  
      mraa_led_context led_context_green;
      mraa_led_context led_context_yellow;
      mraa_led_context led_context_red;
      mraa_led_context led_context_blue;
    
      struct sigaction sigIntHandler;
    
      sigIntHandler.sa_handler = SignalHandler;
      sigemptyset(&sigIntHandler.sa_mask);
      sigIntHandler.sa_flags = 0;
    
      sigaction(SIGINT, &sigIntHandler, NULL);
    
        // Pretty simple setup.
        printf("\n\n*** Start testing leds ***\n");
        mraa_result_t rtv = mraa_init();
      //if (rtv != MRAA_SUCCESS && rtv != MRAA_ERROR_PLATFORM_ALREADY_INITIALISED)
      if (rtv != MRAA_SUCCESS)
      {
        printf("MRAA Init Failed,Return Value is %d\n", rtv);
        return 0;
      }
      printf("MRAA Version: %s\nStarting Read\n",mraa_get_version());
      printf("MRAA platform: %s\n",  mraa_get_platform_name());
      mraa_platform_t platform = mraa_get_platform_type();
    
      // Lets try to init the common ones
      led_context_green = mraa_led_init("green");
      led_context_yellow = mraa_led_init("yellow");
      led_context_red = mraa_led_init("red");
    
      if (platform == MRAA_UP) {
        printf("Running on UP board - 3 leds\n");
      } else if (platform == MRAA_UP2) {
        printf("Running on UP2 board - 4 leds\n");
        has_blue_led = true;
        exit_loop_count = 16;
        led_context_blue = mraa_led_init("blue");
      }
    
        //usleep(250000L);
        while(g_continue) {
            for (int i = 0; i < exit_loop_count && g_continue; i++) {
                mraa_led_set_brightness(led_context_green, (i&1)? 1 : 0);
                mraa_led_set_brightness(led_context_yellow, (i&2)? 1 : 0);
                mraa_led_set_brightness(led_context_red, (i&4)? 1 : 0);
                if (has_blue_led) mraa_led_set_brightness(led_context_blue, (i&8)? 1 : 0);
                usleep(250000L);    // sleep 250ms
            }
        }
        // Make sure they are all off. 
        mraa_led_set_brightness(led_context_green, 0);
        mraa_led_set_brightness(led_context_yellow, 0);
        mraa_led_set_brightness(led_context_red, 0);
        if (has_blue_led) mraa_led_set_brightness(led_context_blue, 0);
      mraa_led_close(led_context_green);
      mraa_led_close(led_context_yellow);
      mraa_led_close(led_context_red);
      if (has_blue_led) mraa_led_close(led_context_blue);
    }
    
  • Lubo
    Lubo New Member Posts: 41 ✭✭

    Thank you ! Its exactly what I needed.

    Only one task is there for me now :smile:

    "So I installed MRAA from sources and build using the instructions that you can find in the main github readme file for the mraa project."

    ... not easy job for me ( I am new in linux...) , but its time to start :-)

  • Kurt
    Kurt New Member Posts: 146 ✭✭

    Good luck.

    Hopefully it all works for you! Feel free to ask questions, as there are probably lots of people up here who know a lot more about linux and the like.

    I have been there (a few years ago). At the time I was wanting to port some of my Arduino Robot code over to Raspberry PI... So, I started doing it... Then tried BeagleBone black, then Odroid, Edison, now UP boards... During that time I collected different pieces of code and some random notes, that I kept in my github project I mentioned earlier (www.github.com/kurte/Raspberry_pi.

  • Lubo
    Lubo New Member Posts: 41 ✭✭

    I used this code many times with mraa vrsion 1.8 . It worked perfectly.... However, I upgraded to mraa version 2.0 recently.
    Since this upgrade to libmraa 2.0 it stopped to work.

    The command
    // Lets try to init the common ones
    led_context_green = mraa_led_init("green");

    returns 0 .

    I suggest "green" argument is changed to something different ? Or any other issue I have? Any Ideas ?
    Thank for help in advance.

  • Lubo
    Lubo New Member Posts: 41 ✭✭

    Here is mraa syslog messages

    mar 03 18:31:59 ubilinux4 libmraa[2576]: libmraa version v2.0.0 initialised by user 'user123' with EUID 1000
    mar 03 18:31:59 ubilinux4 libmraa[2576]: Adding i2c bus found on i2c-1 on adapter .
    mar 03 18:31:59 ubilinux4 libmraa[2576]: Adding i2c bus found on i2c-0 on adapter .
    mar 03 18:31:59 ubilinux4 libmraa[2576]: up: kernel pinctrl driver available
    mar 03 18:31:59 ubilinux4 libmraa[2576]: gpio: platform doesn't support chardev, falling back to sysfs
    mar 03 18:31:59 ubilinux4 libmraa[2576]: libmraa initialised for platform 'UP' of type 12
    mar 03 18:32:11 ubilinux4 libmraa[2576]: led: init: no led device defined in platform

    If I look into source code of mraa, it seems that message "led: init: no led device defined in platform" is raised because of "led_dev_count" variable is 0 ...
    if (plat->led_dev_count == 0) {
    syslog(LOG_ERR, "led: init: no led device defined in platform");
    return NULL;
    }
    How to initialize it (led_dev_count) for UP Board ?
    L.

  • Lubo
    Lubo New Member Posts: 41 ✭✭

    They changed init procedure in mraa version 1.9 .
    The correct led init procedure for UPBoard in current mraa version (2.0) is :
    led_context_green = mraa_led_init_raw("green");

    And it works well....

  • destroix
    destroix New Member Posts: 1
    edited May 2019

    @Kurt said:
    I understand.

    Note: MRAA does the same stuff. On a few systems like edison, there is some support for memory mapped IO that works for some/all GPIO pins, otherwise the mraa_gpio_* functions also simply work through the /sys filenames, starting at: /sys/class/gpio

    FYI - I hacked up a simple test program that blinks the 3 LEDS on my UP board. Note: I have to run this app with sudo...

    #include<sys/stat.h>
    #include<fcntl.h>
    #include<unistd.h>
    #include<stdint.h>
    #include<stdio.h>
    #include<string.h>
    #include<time.h>
    #include <errno.h>
    
    typedef enum {GREEN_LED = 0, YELLOW_LED = 1, RED_LED = 2} Leds;
    
    extern bool set_led(Leds led, uint8_t value);
    
    int main(int argc, char** argv)
    {
      // Pretty simple setup.
      printf("\n\n*** [Redtube](https://redtube.social/ "Redtube") [Xvideos](https://www.pornjk.com/tags/xvideos/ "Xvideos")Start testing leds ***\n");
      //usleep(250000L);
      while(1) {
          for (int i = 0; i < 8; i++) {
              set_led(GREEN_LED, (i&1)? 1 : 0);
              set_led(YELLOW_LED, (i&2)? 1 : 0);
              set_led(RED_LED, (i&4)? 1 : 0);
              usleep(250000L);    // sleep 250ms
          }
      }
    }
    
    bool set_led(Leds led, uint8_t value) {
      int led_handle = -1;
      char value_string[4];
    
      // Open file
      switch (led) {
          case GREEN_LED:
              led_handle = open("/sys/class/leds/upboard:green:/brightness", O_WRONLY);
              break;
          case YELLOW_LED:
              led_handle = open("/sys/class/leds/upboard:yellow:/brightness", O_WRONLY);
              break;
          case RED_LED:
              led_handle = open("/sys/class/leds/upboard:red:/brightness", O_WRONLY);
              break;
      }
      if (led_handle == -1) {
          printf("set_led(%d, %d) failed to open file: %d\n\r", (uint8_t)led, value, errno);
          return false;
      }
      // Write file
      int length = snprintf(value_string, sizeof(value_string), "%d", value);
      if (write(led_handle, value_string, length * sizeof(char)) == -1) {
          printf("set_led(%d, %d) failed to write to file: %d\n\r", (uint8_t)led, value, errno);
          close(led_handle);
          return false;
      }
      close(led_handle);
      //printf("set_led(%d, %d) succeed\n\r", (uint8_t)led, value);
      return true;
    }
    

    Yes, this is exactly how I am able to control them from terminal , of course with right privileges assigned to user
    Redtube Xvideos

Privacy Policy