Beaglebone Green GPIO

In the past I have been using the Beaglebone Black Wireless, but due to unavailability of boards in quantity I have switched to Beaglebone Green Wireless.

My GPIO is not working and as I am not an expert I need some device.

Using C++ I read and write to files in the /sys/class/gpio - an example would be to gpio45, but in the BBG none of these files exist. How do I get the files setup so I can use them,

Sorry I am not an expert .

How do I set up the gpio in C++ using these commands…

cd /sys/class/gpio/
echo 49 > export
cd gpio49
cat out > direction
echo 1 > value //led should light up
echo 0 > value //led should go off

Any help would be appreciated as I am really struggling to do something that I believe many of the users have already implemented.

This is what I have tried to no avail…

void PIN_IO::Set_Pin(int iNumber, bool blnInput)
{

int exportfd = open("/sys/class/gpio/export", O_WRONLY);
if (exportfd < 0)
{
	printf("Cannot open GPIO to export it\n");
	exit(1);
}

system("echo 44 > /sys/class/gpio/export");
system("chmod 777 /sys/class/gpio/gpio44");
system("chmod 777 /sys/class/gpio/gpio44/direction");
system("chmod 777 /sys/class/gpio/gpio44/edge");
system("chmod 777 /sys/class/gpio/gpio44/value");

}

1 Like

Hello,

What are your preprocessor directives? And…why do you not want to use GPIO in C instead? If you wanted to use C or Python3, easier methods are available. I searched online just now. I came across many libs but one that may prove useful in your C++ case:

/*
Example of using libsoc for GPIO programming.
Toggles three LEDs in a "Cylon" pattern until the pushbutton is
pressed, and then exits. Will run on Raspberry Pi or Toradex Colibri
platforms.
This version reads the switch status using interrupts and a callback.
*/

#include <stdio.h>
#include <unistd.h>
#include "libsoc_gpio.h"
#include "libsoc_debug.h"

// Uncomment the appropriate lines below for the hardware it is
// running on.

// Raspberry Pi
//const int led1   = 24; // Red LED
//const int led2   = 25; // Green LED
//const int led3   =  5; // Yellow LED
//const int button =  6; // Pushbutton

// Toradex Colibri
const int led1   = 52; // Red LED
const int led2   = 53; // Green LED
const int led3   = 63; // Yellow LED
const int button = 93; // Pushbutton

// Delay (in microseconds)
const int delay = 100000;

// Exit flag
bool exitLoop = false;


// Callback invoked when button is pressed. Set flag to exit.
int buttonCallback(void* arg)
{
    printf("Button pressed\n");
    exitLoop = true;
    return 0;
}


// Main program
int main(void)
{
    // Uncomment the next line to enable debug output if desired.
    //libsoc_set_debug(1);

    // Request gpios. May need to use LS_SHARED with older versions
    // of libsoc.
    gpio *gpio_led1 = libsoc_gpio_request(led1, LS_GPIO_SHARED);
    gpio *gpio_led2 = libsoc_gpio_request(led2, LS_GPIO_SHARED);
    gpio *gpio_led3 = libsoc_gpio_request(led3, LS_GPIO_SHARED);
    gpio *gpio_button = libsoc_gpio_request(button, LS_GPIO_SHARED);

    // Ensure gpios were successfully requested.
    if (gpio_led1 == NULL) {
        printf("Failed gpio_led1 request.\n");
        exit(EXIT_FAILURE);
    }
    if (gpio_led2 == NULL) {
        printf("Failed gpio_led2 request.\n");
        exit(EXIT_FAILURE);
    }
    if (gpio_led3 == NULL) {
        printf("Failed gpio_led3 request.\n");
        exit(EXIT_FAILURE);
    }
    if (gpio_button == NULL) {
        printf("Failed gpio_button request.\n");
        exit(EXIT_FAILURE);
    }
  
    // Set directions.
    libsoc_gpio_set_direction(gpio_led1, OUTPUT);
    libsoc_gpio_set_direction(gpio_led2, OUTPUT);
    libsoc_gpio_set_direction(gpio_led3, OUTPUT);
    libsoc_gpio_set_direction(gpio_button, INPUT);
  
    // Check directions.
    if (libsoc_gpio_get_direction(gpio_led1) != OUTPUT) {
        printf("Failed to set direction of gpio_led1\n");
        exit(EXIT_FAILURE);
    }
    if (libsoc_gpio_get_direction(gpio_led2) != OUTPUT) {
        printf("Failed to set direction of gpio_led2\n");
        exit(EXIT_FAILURE);
    }
    if (libsoc_gpio_get_direction(gpio_led3) != OUTPUT) {
        printf("Failed to set direction of gpio_led3\n");
        exit(EXIT_FAILURE);
    }
    if (libsoc_gpio_get_direction(gpio_button) != INPUT) {
        printf("Failed to set direction of gpio_button\n");
        exit(EXIT_FAILURE);
    }

    // Set input edge to FALLING.
    libsoc_gpio_set_edge(gpio_button, FALLING);

    // Set up callback when button status changes.
    libsoc_gpio_callback_interrupt(gpio_button, &buttonCallback, (void *) 0);

    while (!exitLoop) {

        // Turn LEDs on and off in desired sequence.
        libsoc_gpio_set_level(gpio_led1, HIGH);
        libsoc_gpio_set_level(gpio_led2, LOW);
        libsoc_gpio_set_level(gpio_led3, LOW);
        usleep(delay);
 
        libsoc_gpio_set_level(gpio_led1, LOW);
        libsoc_gpio_set_level(gpio_led2, HIGH);
        libsoc_gpio_set_level(gpio_led3, LOW);
        usleep(delay);

        libsoc_gpio_set_level(gpio_led1, LOW);
        libsoc_gpio_set_level(gpio_led2, LOW);
        libsoc_gpio_set_level(gpio_led3, HIGH);
        usleep(delay);

        libsoc_gpio_set_level(gpio_led1, LOW);
        libsoc_gpio_set_level(gpio_led2, HIGH);
        libsoc_gpio_set_level(gpio_led3, LOW);
        usleep(delay);
    }

    // Cancel the callback as it is no longer needed.
    libsoc_gpio_callback_interrupt_cancel(gpio_button);

    // Turn all LEDs off.
    libsoc_gpio_set_level(gpio_led1, LOW);
    libsoc_gpio_set_level(gpio_led2, LOW);
    libsoc_gpio_set_level(gpio_led3, LOW);
  
    // Clean up.
    libsoc_gpio_free(gpio_led1);
    libsoc_gpio_free(gpio_led2);
    libsoc_gpio_free(gpio_led3);
    libsoc_gpio_free(gpio_button);
  
    return EXIT_SUCCESS;
}

I am not sure about the two libraries above but I think you can port this source to the BBB if needed:

#include "libsoc_gpio.h"
#include "libsoc_debug.h"

I have not used this source before today. I may not use it but it is a starting point. I do not use libsoc_gpio.h in any form so far.

Seth

P.S. It might get you on track! The source and other ideas for GPIO can be found here: blogs/demo2.cpp at master · tranter/blogs · GitHub

Hi Silver2row,

Thank you for your guidance, it did help me a lot. I did not use the library but was able to see the process.

My problem is that I was trying to run the program from the user debian and not from the root. Once I logged in as root and moved my program to the root folder I was able to access the /sys/class/gpio/export file and so could continue.

Your help is really appreciated.

I have posted my code below…

void PIN_IO::Set_Pin(int iNumber, bool blnInput)
{

writeToSysfs<unsigned int>(SYSFS_GPIO_DIR "/export", iNumber);							//Need to export the file

PinNumber = iNumber;
PinName = "gpio" + to_string(iNumber);
PinPath = GPIO_PATH + PinName + "/";

string strPath = PinPath + "direction";
writeToSysfs<const char*>(strPath.c_str(), blnInput ? "in" : "out");

}

template int PIN_IO::writeToSysfs(const char* path, T value) {
std::ofstream stream(path);
if (!stream) {
std::cerr << "OPERATION FAILED: Unable to write to sysfs path: " << path
<< std::endl;
return -1;
}

stream << value;
stream.close();

return 0;

}

1 Like

Hello @BryanJ ,

Also, if you choose to not use the root account/user, one can also change permissions of the groups.

Seth

1 Like