Managing GPIO with C++

176 views
Skip to first unread message

Dror Lugasi

unread,
Mar 12, 2017, 8:47:14 AM3/12/17
to BeagleBoard
Hi guys!

I am writing a program with C++ for the BBB that has some serial ports and Ethernet communication, and i want to control the GPIO with it.

At this moment i am able to activate / read / write / and choose in or out for the digital GPIO, and also read the analog input.
I am doing everything with the function "system" that runs a terminal command.

I am doing this only cause all the tutorials i saw about GPIO are from the terminal, sending 'echo' and 'cat' to the GPIO's files in the 
/sys/class/gpio path's..


is this a reasonable way? i don't need the GPIO switching to be lightning fast.
I saw that there are some libraries (like BlackLib for example) for controlling the GPIO but my question is: does they do exactly that? 
or do they reach the hardware from a better/faster way??

same goes to all other features like PWM, I2C etc...

Thanks for your attention! 
Dror.

Dennis Lee Bieber

unread,
Mar 12, 2017, 10:45:27 AM3/12/17
to beagl...@googlegroups.com
On Sun, 12 Mar 2017 05:47:14 -0700 (PDT), Dror Lugasi
<lugas...@gmail.com> declaimed the
following:

>
>I am doing this only cause all the tutorials i saw about GPIO are from the
>terminal, sending 'echo' and 'cat' to the GPIO's files in the
>/sys/class/gpio path's..
>

The key words are "GPIO's files"!

They appear as /files/, so you can OPEN them as files (I'm not going to
try to map to C++ I/O, so the following is pseudo-C):

gp1fh = fopen("/sys/class/gpio...", "r");
vlu = fread(gp1fh, ...);
fclose(gp1fh);
--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

William Hermans

unread,
Mar 12, 2017, 7:38:41 PM3/12/17
to beagl...@googlegroups.com
You need only learn how to read, and write from / to files using C++. Once you have that figured out, you just need to learn what files to read from, and write to for the desired effect.

One thing to understand, is that the only real advantage of C++ over C in this case. Is that C++ has "real" string objects. Instead of C's character arrays. So some may argue that C++ is "safer", which to some extent may be true. But most of your code will be C. . . and if you  pay attention to what you're doing. Either language will work fine.

Anyway, if you search the internet with keywords such as "beaglebone GPIO in C", then ignore all the mention of libraries. You'll get a good idea of how this is done. But you can view the command "cat" as a read, and "echo" as a write to a file. So you could adapt command line examples into C or C++ just as easily. If you're familiar with either language.

Here is a real C++ example for GPIO on the beaglebone through, using the fs object: http://stackoverflow.com/questions/21198933/how-to-control-beaglebone-gpio-pins It's not as clean as I like for C, but it's true C++.

William Hermans

unread,
Mar 12, 2017, 7:48:32 PM3/12/17
to beagl...@googlegroups.com
err, do also note that the first part of the example given by the person who answered the question probably is no longer needed. In fact that application as is would probably fail on modern beaglebone Debian images. So omit:

   fs.open("/sys/kernel/debug/omap_mux/gpmc_ad4");
   fs << "7";
   fs.close();

As the above code snippet is no longer required. I'm fairly certain for that step nothing is required to replace it. As universal IO is enabled by default on all modern 
Debian images, and with shell scripts all I've ever had to do was export the given GPIO pin, to start manipulating it. But if you have any problems with the example
code in the link i gave above, I'd be able to help sort it out.
Generally though if you google error messages, you should be able to figure things out on your own.

Jacek Radzikowski

unread,
Mar 12, 2017, 7:48:51 PM3/12/17
to beagleboard
Dror,

a while ago I wrote for myself a small class library which implements
wrappers for gpio, spi, and i2c, and provides simple and user friendly
interface to the application. Take a look at it, and see if it will
work for you: https://github.com/piranha32/IOoo

Jacek.
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to beagleboard...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/beagleboard/4efd585d-cf3d-473f-a940-61922d27d5f3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Given a choice between two theories, take the one which is funnier

William Hermans

unread,
Mar 12, 2017, 7:56:46 PM3/12/17
to beagl...@googlegroups.com

On Sun, Mar 12, 2017 at 4:48 PM, Jacek Radzikowski <jacek.ra...@gmail.com> wrote:
Dror,

a while ago I wrote for myself a small class library which implements
wrappers for gpio, spi, and i2c, and provides simple and user friendly
interface to the application. Take a look at it, and see if it will
work for you:  https://github.com/piranha32/IOoo

The problem with using someones library when accessing the hardware modules through sysfs. Is that one doesn't learn anything in the process. So, if you run into a problem, you've no idea how to correct it. A good example would be when the beaglebone images moved from a 3.8.x kernel to 4.x kernels. For many people this rendered libraries broken, with no idea how to correct the problem. When the problem was a simple pathing issue.

Jacek Radzikowski

unread,
Mar 12, 2017, 7:59:05 PM3/12/17
to beagleboard
The library does not use sysfs, but mmap. This was one of the most
important reasons why I spent time to write it.

Jacek.
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to beagleboard...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/beagleboard/CALHSORqpWWmOD-4Gae%3Dv%2BqZu8KEEXtTWraaYq6%3DrkAVw9%3DThZQ%40mail.gmail.com.

William Hermans

unread,
Mar 12, 2017, 8:03:09 PM3/12/17
to beagl...@googlegroups.com

On Sun, Mar 12, 2017 at 4:58 PM, Jacek Radzikowski <jacek.ra...@gmail.com> wrote:
The library does not use sysfs, but mmap. This was one of the most
important reasons why I spent time to write it.

Oh, ok right, I remember that git now. Pretty cool library. However, I'd still argue if one is attempting to learn the system, it may not be the best thing to use. But later if someone is trying to learn how to use mmap() it's probably a really good read. That, and using devmem.c as an example too.

Dror Lugasi

unread,
Mar 13, 2017, 3:20:21 AM3/13/17
to BeagleBoard
Thanks for answering guys! 

Jacek >> 
Thank you so much for the library! it looks like a very hard work was invested in it!
but i tend to go with williams's approach.. i want to know how to do it my self also as a part of learning the BBB
i am planning on making more complicated programs than just read and write some GPIO.

I saw in some places that there are the FS and MMAP way and that the MMAP is a lot faster!
If you could tell me just how to approach this and so i could write some basic functions like read write and set as in or out
that would be great!

i also want to get the best performance as possible, but i am not that knowledgeable about deep embedded programming, i know that the beagle bone has 2 PRU's and they can be accessed to make the calculations and use all the peripherals (UART, PWM, I2C etc..)
so is there a way to access the PRU with c++??


William >> 
Thanks for the example! right now i am using the same terminal commands with the "system" function
cat - to read 
echo - to write

does using a file stream will be faster then this? if so i will change the code so it will use a file instead.


Thanks a lot guys! :)
Reply all
Reply to author
Forward
0 new messages