Using gpio

3291 views
Skip to first unread message

aero386

unread,
Feb 12, 2009, 7:17:00 PM2/12/09
to Beagle Board

So I found the below code on
http://blog.makezine.com/archive/2009/02/blinking_leds_with_the_beagle_board.html


#!/bin/sh
#
# Blink the onboard LED

GPIO=$1

cleanup() { # Release the GPIO port
echo $GPIO > /sys/class/gpio/unexport
exit
}

# Open the GPIO port
#
echo $GPIO > /sys/class/gpio/export

trap cleanup SIGINT # call cleanup on Ctrl-C

# Blink forever
while [ "1" = "1" ]; do
echo "high" > /sys/class/gpio/gpio$GPIO/direction
sleep 1
echo "low" > /sys/class/gpio/gpio$GPIO/direction
sleep 1
done

cleanup # call the cleanup routine


...it does not work on the beagle image I am running "2.6.28". I do
not have the specified folders sys/class/gpio/gpio$GPIO/direction in
the image. I was wondering if there was a simple fix or something. the
only folders I have are

root@beagleboard:/# ls
bin dev home linuxrc mnt sbin tmp var
boot etc lib media proc sys usr
root@beagleboard:/# cd sys
root@beagleboard:/sys# cd class
root@beagleboard:/sys/class# ls
bdi i2c-adapter mmc_host scsi_generic usb_device
bluetooth i2c-dev mtd scsi_host usb_endpoint
display ieee80211 net sound usb_host
firmware input regulator spi_master usbmon
gpio leds rtc thermal vc
graphics mem scsi_device tty vtconsole
hwmon misc scsi_disk ubi
root@beagleboard:/sys/class# cd gpio
root@beagleboard:/sys/class/gpio# ls
export gpiochip128 gpiochip192 gpiochip64 unexport
gpiochip0 gpiochip160 gpiochip32 gpiochip96
root@beagleboard:/sys/class/gpio# cd gpiochip160
root@beagleboard:/sys/class/gpio/gpiochip160# ls
base label ngpio power subsystem uevent
root@beagleboard:/sys/class/gpio/gpiochip160#

So anyways I was just wondering what exactly that was all about.

Tomi Valkeinen

unread,
Feb 13, 2009, 2:12:17 AM2/13/09
to Beagle Board
Hi,

Check Documentation/gpio.txt from kernel sources. It explains how the
sysfs interface works.

Tomi

Boireau Laurent

unread,
Feb 13, 2009, 4:14:32 AM2/13/09
to beagl...@googlegroups.com
Try the following under minicom, after logging as root to Angstrom.
Check if you have the following directory (that's a special Led driver patch : some versions of Angstrom demo have it, others don't). It is described here : http://article.gmane.org/gmane.linux.ports.arm.omap/8611 and loaded at boot (see log and /etc/init.d).
> cd /sys/class/leds/beagleboard::usr1
>ls
> more trigger
> echo none > trigger
That shoud break the connection between LED and mmc
Now :
> echo 1 > brightness
> echo 0 > brightness
Done !

If you don't have it, just use basic GPIO
> cd /sys/class/gpio
> echo 150 > export (150 is usr1 LED, 149 is usr0 ... You can also access expansion port GPIO that way, say 168 for pin 24)
That should create directory /sys/class/gpio/gpio150, with all the access you want.
>cd /sys/class/gpio/gpio150
>echo "high" > direction
>echo "low" > direction
Or alternatively :
>echo "out">direction (set port as output)
>echo "1">value
>echo "0" value
Done !
If you want then to unmount the device, use unexport :
>cd /sys/class/gpio/
>echo 150 > unexport


While you're at it, you can also play with the user button :
>cd /sys/class/gpio
> echo "7" > export
> cd gpio7
>echo "in" > direction (set port as input)
> more value (should return 0 if usr button is not pressed, 1 if pressed ...).

A c program would do much the same, opening and writing (or reading) to the same files.

We definitely should set up some kind of beginners site with such simple or more complex exemples, with shell scripts and small C programs, demonstrating "hello world" capabilites, and basic interaction with LEDs, buttons, timers, interrupts, serial, SPI, I2C, PWM, interfacing a webcam, using the NEON or the DSP ... That would make starting applications (say robotics ...) with beagle much faster !! I do prefer learning by example rather than reinventing the wheel by parsing hundreds of pages of doc and googling around, even if the latter can't always be avoided ;-)
Have fun !!

Laurent



-----Message d'origine-----
De : beagl...@googlegroups.com [mailto:beagl...@googlegroups.com] De la part de Tomi Valkeinen
Envoyé : vendredi 13 février 2009 08:12
À : Beagle Board
Objet : [beagleboard] Re: Using gpio

bjepson

unread,
Feb 13, 2009, 8:48:26 AM2/13/09
to Beagle Board
On Feb 13, 4:14 am, "Boireau Laurent" <laurent.boir...@cnes.fr> wrote:

> We definitely should set up some kind of beginners site with such simple or more
> complex exemples, with shell scripts and small C programs, demonstrating "hello
> world" capabilites, and basic interaction with LEDs, buttons, timers, interrupts,
> serial, SPI, I2C, PWM, interfacing a webcam, using the NEON or the DSP ... That
> would make starting applications (say robotics ...) with beagle much faster !! I do
> prefer learning by example rather than reinventing the wheel by parsing hundreds
> of pages of doc and googling around, even if the latter can't always be avoided ;-)

Laurent,

Thanks for explaining the GPIO features in more detail. I wrote the
GPIO article and code over at Makezine, but I haven't had much luck
learning anything beyond the basics of what I showed in my article.

I tried to learn how to communicate over I2C from Antti Seppänen's
BeagleBot page at http://www.hervanta.com/stuff/Beaglebot, but a lot
of that went over my head.

I think the tutorials you describe would be very helpful; I think many
would benefit from something that explains what needs to be done to
prepare a kernel to use these interfaces and how to write and compile
userspace code for this. I think it would be helpful to see how to
wire things up, as well, including some information on connecting to
3v and 5v devices (Spark Fun sells a logic level shifting board that
may come in handy: http://www.sparkfun.com/commerce/product_info.php?products_id=8745).

Thanks,

Brian

aero386

unread,
Feb 13, 2009, 10:14:30 AM2/13/09
to Beagle Board
Thanks So much Laurent and Brian. This worked great

>If you don't have it, just use basic GPIO
> cd /sys/class/gpio
> echo 150 > export (150 is usr1 LED, 149 is usr0 ... You can also >access expansion port GPIO that way, say 168 for pin 24)

>That should create directory /sys/class/gpio/gpio150, with all the access >you want.
>cd /sys/class/gpio/gpio150
>echo "high" > direction
>echo "low" > direction
Or alternatively :
>echo "out">direction (set port as output)
>echo "1">value
>echo "0" value

>Done !
>If you want then to unmount the device, use unexport :

>cd /sys/class/gpio/
>echo 150 > unexport

>While you're at it, you can also play with the user button :

>cd /sys/class/gpio
> echo "7" > export
> cd gpio7
>echo "in" > direction (set port as input)
> more value (should return 0 if usr button is not pressed, 1 if pressed ...).

---------------------------------------------------------------------------
I modified Brian's code from that website to turn on the led for the
angstrom 2.6.28 image that I have so I will just put this here to
recap
...anything between <> is what you should type do not type <>

choose a file folder

type <vi Blink_USR0>

then in vi enter in the following code...

-------------------------------------------------------------------------
#
# Blink the onboard LED USR0
# For Angstrom Version 2.6.28

cleanup() { # Release the GPIO port
echo 150 > /sys/class/gpio/unexport
exit
}

# Open the GPIO port
#
echo 150 > /sys/class/gpio/export

trap cleanup SIGINT # call cleanup on Ctrl-C
# Blink forever
while [ "1" = "1" ]; do
echo "high" > /sys/class/gpio/gpio150/direction
sleep 1
echo "low" > /sys/class/gpio/gpio150/direction
sleep 1
done

cleanup # call the cleanup routine

---------------------------------------------------------------------

Then type <chmod 755 Blink_USR0>
then type <./Blink_USR0>
the led will blink
press control c to exit

Thanks again for the Help!!! That was very useful.

aero386

unread,
Feb 13, 2009, 11:03:39 AM2/13/09
to Beagle Board
Does anybody know how to get this code to work in c? I am using the
code sourcery G++ compiler.

Koen Kooi

unread,
Feb 13, 2009, 11:05:43 AM2/13/09
to beagl...@googlegroups.com

Op 13 feb 2009, om 17:03 heeft aero386 het volgende geschreven:

>
> Does anybody know how to get this code to work in c? I am using the
> code sourcery G++ compiler.

Don't use that, angstrom is not compiled with a CSL compiler, so
you're facing lots of compatibility issues when using harry potter
toolchains (apart from bugs like -Os, NEON, right shifts and non-qemu
platforms not working)

regards,

Koen

PGP.sig

aero386

unread,
Feb 13, 2009, 12:11:19 PM2/13/09
to Beagle Board
Do you have a recommendation for a compiler? Before you responded that
was the one I should put onto the board. I don't mind using a
different one I just don't know what one to use.

Koen Kooi

unread,
Feb 13, 2009, 12:56:56 PM2/13/09
to beagl...@googlegroups.com

I recommended using the angstrom one, which isn't CSL for sure.

regards,

Koen

PGP.sig

Robert P. J. Day

unread,
Feb 13, 2009, 1:07:53 PM2/13/09
to beagl...@googlegroups.com

if that's the case, i suspect a number of web sites should update
their recommendations. if you google on something like "angstrom
beagleboard compiler", you find a number of sites like this:

http://garrenblog.blogspot.com/2008/10/adventures-in-beagleboard-land-compiler.html

which read, "The compiler used with the Beagleboard is the one from
Codesourcery." so if people should be using a different one, a link
to that one should probably be more prominently displayed everywhere.

rday
--

========================================================================
Robert P. J. Day
Linux Consulting, Training and Annoying Kernel Pedantry:
Have classroom, will lecture.

http://crashcourse.ca Waterloo, Ontario, CANADA
========================================================================

Koen Kooi

unread,
Feb 13, 2009, 1:25:45 PM2/13/09
to beagl...@googlegroups.com

Op 13 feb 2009, om 19:07 heeft Robert P. J. Day het volgende geschreven:

>
> On Fri, 13 Feb 2009, Koen Kooi wrote:
>
>>
>> Op 13 feb 2009, om 18:11 heeft aero386 het volgende geschreven:
>>
>>> Do you have a recommendation for a compiler? Before you responded
>>> that was the one I should put onto the board. I don't mind using a
>>> different one I just don't know what one to use.
>>
>> I recommended using the angstrom one, which isn't CSL for sure.
>
> if that's the case, i suspect a number of web sites should update
> their recommendations. if you google on something like "angstrom
> beagleboard compiler", you find a number of sites like this:
>
> http://garrenblog.blogspot.com/2008/10/adventures-in-beagleboard-land-compiler.html
>
> which read, "The compiler used with the Beagleboard is the one from
> Codesourcery." so if people should be using a different one, a link
> to that one should probably be more prominently displayed everywhere.

Just use the toolchain OE builds for you or install a native one from
the feeds on the beagle itself, you can't go wrong that way. I can't
stop people from posting nonsense to wikis and blogs, no do I want to.

regards,

Koen

PGP.sig

aero386

unread,
Feb 13, 2009, 1:44:09 PM2/13/09
to Beagle Board
> Just use the toolchain OE builds for you or install a native one from  
> the feeds on the beagle itself, you can't go wrong that way. I can't  
> stop people from posting nonsense to wikis and blogs, no do I want to.

I still have not gotten OE to work Yet. I am just using the angstrom
2.6.28 from

http://www.angstrom-distribution.org/demo/beagleboard/

...LOL Oh you know what I might be using the right compiler, I assumed
that g++ stood for code sourcery g++. I followed this....

Op 13 jan 2009, om 18:35 heeft skate...@bellsouth.net het volgende
geschreven:

> Sorry for making so many dumb requests but...I was wondering if koen
> had made an angstrom image with a decent gui c++ compiler that had a
> library with easy access to the gpio and usb ports and such? And if so
> where can I download it, and what is the compiler called in Angstrom?

"opkg install task-native-sdk" in any image and the compiler is
called
'g++"

regards,

Koen

Statement that you had made. So Do I have the right compiler? This is
the one I am using. I think I might be mistaken.

And my streak as an idiot continues!!!!!!!!!!!!!forever hahahahfhahh

thanks

Koen Kooi

unread,
Feb 13, 2009, 1:52:25 PM2/13/09
to beagl...@googlegroups.com

Op 13 feb 2009, om 19:44 heeft aero386 het volgende geschreven:

>
>> Just use the toolchain OE builds for you or install a native one from
>> the feeds on the beagle itself, you can't go wrong that way. I can't
>> stop people from posting nonsense to wikis and blogs, no do I want
>> to.
>
> I still have not gotten OE to work Yet. I am just using the angstrom
> 2.6.28 from
>
> http://www.angstrom-distribution.org/demo/beagleboard/
>
> ...LOL Oh you know what I might be using the right compiler, I assumed
> that g++ stood for code sourcery g++. I followed this....
>
> Op 13 jan 2009, om 18:35 heeft skate...@bellsouth.net het volgende
> geschreven:
>
>> Sorry for making so many dumb requests but...I was wondering if koen
>> had made an angstrom image with a decent gui c++ compiler that had a
>> library with easy access to the gpio and usb ports and such? And if
>> so
>> where can I download it, and what is the compiler called in
>> Angstrom?
>
> "opkg install task-native-sdk" in any image and the compiler is
> called
> 'g++"
>
> regards,
>
> Koen
>
> Statement that you had made. So Do I have the right compiler?


Yes

regards,

Koen

PGP.sig

aero386

unread,
Feb 13, 2009, 1:55:07 PM2/13/09
to Beagle Board
oh and if I do have the right G++, is there some documentation on it
that you know of so I can figure out what general and special
functions it has?

Thanks for all the help...

Ohh and I have a data plan on my cell phone and it provides ethernet
to the beagle it is awesome...but I still have not figured out how to
get it to work in the image with the desktop, only working for command
line.

aero386

unread,
Feb 13, 2009, 4:33:04 PM2/13/09
to Beagle Board
Well thanks to everyone above I have put together a C program to blink
the LED...unfortunately the code seems much more confusing than it has
to be. Let me know if there is an easier way to do this. Another issue
is that the port pin must be initialized before the program is run or
it will not work. Let me know if there is a fix for this. I took most
of this code from...

http://www.avrfreaks.net/wiki/index.php/Documentation:Linux/GPIO#Getting_access_to_a_GPIO

and modified it a bit to work. Please re-post with fixes if possible.
For Angstrom image 2.6.28
------------------------------------------------------------------------------------
type what is inside of <> do not type <>
open command prompt and init port pin by typeing

<echo 150 > /sys/class/gpio/export>

Then enter in all of the code below, or skip to end for shortcut

--------------------------------------------------------------------------------------
// Blink pin 3 on port B at 1 Hz
// Just add an LED and see the light! ;)
//
//Created by Dingo_aus, 7 January 2009
//email: dingo_aus [at] internode <dot> on /dot/ net
//
//Created in AVR32 Studio (version 2.0.2) running on Ubuntu 8.04

//Modified by Jeff Lally 13 February 2009 for beagle board Angstrom
2.6.28
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

FILE *fp;

int main(int argc, char** argv)
{

printf("\n**********************************\n"
"* Welcome to PIN Blink program *\n"
"* ....blinking USR0 *\n"
"* ....rate of 1 Hz............ *\n"
"**********************************\n");

//create a variable to store whether we are sending a '1' or a '0'
char set_value[4];
//Integer to keep track of whether we want on or off
int toggle = 0;

//Using sysfs we need to write "150" to /sys/class/gpio/export
//This will create the folder /sys/class/gpio/gpio150
if ((fp = fopen("/sys/class/gpio/export", "ab")) == NULL)
{
printf("Cannot open export file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(fp);
//Write our value of "150" to the file
strcpy(set_value,"150");
fwrite(&set_value, sizeof(char), 2, fp);
fclose(fp);

printf("...export file accessed, new pin now accessible\n");

//SET DIRECTION
//Open the LED's sysfs file in binary for reading and writing, store
file pointer in fp
if ((fp = fopen("/sys/class/gpio/gpio150/direction", "rb+")) == NULL)
{
printf("Cannot open direction file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(fp);
//Write our value of "out" to the file
strcpy(set_value,"out");
fwrite(&set_value, sizeof(char), 3, fp);
fclose(fp);
printf("...direction set to output\n");

//SET VALUE
//Open the LED's sysfs file in binary for reading and writing, store
file pointer in fp
if ((fp = fopen("/sys/class/gpio/gpio150/value", "rb+")) == NULL)
{
printf("Cannot open value file.\n");
exit(1);
}
//Set pointer to begining of the file
rewind(fp);
//Write our value of "1" to the file
strcpy(set_value,"1");
fwrite(&set_value, sizeof(char), 1, fp);
fclose(fp);
printf("...value set to 1...\n");

//Run an infinite loop - will require Ctrl-C to exit this program
while(1)
{
//Set it so we know the starting value in case something above
doesn't leave it as 1
strcpy(set_value,"1");

if ((fp = fopen("/sys/class/gpio/gpio150/value", "rb+")) == NULL)
{
printf("Cannot open value file.\n");
exit(1);
}
toggle = !toggle;
if(toggle)
{
//Set pointer to begining of the file
rewind(fp);
//Write our value of "1" to the file
strcpy(set_value,"1");
fwrite(&set_value, sizeof(char), 1, fp);
fclose(fp);
printf("...value set to 1...\n");
}
else
{
//Set pointer to begining of the file
rewind(fp);
//Write our value of "0" to the file
strcpy(set_value,"0");
fwrite(&set_value, sizeof(char), 1, fp);
fclose(fp);
printf("...value set to 0...\n");
}
//Pause for one second
sleep(1);
}
return 0;
}
------------------------------------------------------------------------------------
To make it easier just copy to into a text editor, save to a thumb
drive, plug thumb drive into usb port of beagle board copy to
destination compile then run.

Thanks again, and let me know how to make it better. Thanks


aero386

unread,
Feb 13, 2009, 4:36:28 PM2/13/09
to Beagle Board
Sorry that was a bit confusing

<echo 150 > /sys/class/gpio/export>

Type the > after 150

Lars Poulsen

unread,
Jul 11, 2012, 11:42:53 AM7/11/12
to beagl...@googlegroups.com


On 11/07/2012, at 14.26, Francois <majes...@gmail.com> wrote:

I wrote a small lib for myself to use the GPIO pins from C++ in a much
easier way, wrote a few examples to drive LCD displays as well 
including a nokia 6100 lcd using SPI



Nice work. Similar to what I have done myself but quite a bit more polished :-)

http://www.youtube.com/watch?v=0JYtD60L9o4&feature=plcp 

On Thursday, July 5, 2012 8:44:51 PM UTC+12, Darryl wrote:
Sorry for resurrecting an ancient thread, but a few places link to here, and so I thought I'd mention a couple of things (NOTE: the following pertains to the beaglebone, but I suspect that they might apply to the beagleboard, too):
  • You can't control the beaglebone LEDs using GPIO (w/the default Angstrom distribution), because an LED driver is being used.  Instead, you control the LEDs via something like:
# Turn off LED 1:
echo 0 > /sys/class/leds/beaglebone::usr1/brightness
# Turn on LED 1:
echo 255 > /sys/class/leds/beaglebone::usr1/brightness

The four LEDs on the beaglebone are:

/sys/class/leds/beaglebone::usr0/brightness
/sys/class/leds/beaglebone::usr1/brightness
/sys/class/leds/beaglebone::usr2/brightness
/sys/class/leds/beaglebone::usr3/brightness

HOWEVER, the usr0 LED is currently used by the heartbeat, and so you shouldn't touch it, unless you know what you are doing.

Yes, the "brightness" value does go from 0-255, but the beaglebone LEDs are purely binary -- there's no brightness control.  As a result, any non-zero brightness value will turn on the LED.  Because of this, good programming practice dictates that the "on" value should be 255.

-- To join: http://beagleboard.org/discuss
To unsubscribe from this group, send email to:
beagleboard...@googlegroups.com
Frequently asked questions: http://beagleboard.org/faq
Reply all
Reply to author
Forward
0 new messages