Difference between revisions of "EBC Exercise 10 Flashing an LED"

From eLinux.org
Jump to: navigation, search
(Adding sysfs details)
(Flashing the LEDs: readgpio)
Line 72: Line 72:
 
</pre>
 
</pre>
 
Is it responding correctly?
 
Is it responding correctly?
 +
 +
=== Reading the User Button ===
 +
The Beagle has a couple of push buttons. One reboots the whole board.  Use with care.  One is for you to use, it's to the right of the Reset button, between the two stacks of USB ports.
 +
[[File:BeagleUserButton.png]]
 +
 +
It's attached to gpio port 4.  You can read it via:
 +
<pre>
 +
$ cd /sys/class/gpio
 +
$ ls -F
 +
</pre>
 +
Notice there is no gpio4.  Here's how you can create it, set it to an input port and read its value:
 +
<pre>
 +
$ echo 4 > export
 +
$ ls
 +
$ cd gpio4
 +
$ echo in > direction
 +
$ cat value
 +
</pre>
 +
Try holding down the switch and doing <code>cat value</code> again.  Does the value change?  There's a shell script called '''readgpio'''that will repeatedly read the switch.
 +
<pre>
 +
$ readgpio 4
 +
</pre>
 +
Try pushing the switch.  Does it work?  Hit ctrl-C to stop.  Look at readgpio.  How does it work?
 +
<pre>
 +
$ which readgpio
 +
$ cp /usr/bin/readgpio ~
 +
$ gedit ~/readgpio
 +
</pre>
 +
 
== Resources ==
 
== Resources ==
  

Revision as of 12:40, 20 July 2011


The "Hello World" program is the traditional first program for many classes. Flashing an LED is the embedded equivalent. Here we will explore a few ways to flash and LED on the Beagle and explore General Purpose I/O (gpio) along the way.

gpio via the Shell Command Line and sysfs

The easiest way to do general purpose I/O (gpio) on the Beagle is through a terminal window and a shell prompt. In Linux more everything is treated as a file. Even things that aren't files. In our class we'll use a virtual file system called sysfs. sysfs exposes the drivers for the hardware so you get easily use them.

Try this:

$ cd /sys
$ ls -F
block/  bus/  class/  dev/  devices/  firmware/  fs/  kernel/  module/  power/

Here we see several directories that represent hardware we can control. Explore a bit and see what you find.

Now try:

$ cd /sys/class
$ ls -F
bccat/      hwmon/        mtd/             scsi_disk/     usb_device/
bdi/        i2c-adapter/  net/             scsi_generic/  usbmon/
block/      i2c-dev/      omap-previewer/  scsi_host/     vc/
bluetooth/  input/        omap-resizer/    sound/         video4linux/
bsg/        leds/         pvr/             spi_master/    vtconsole/
display/    mdio_bus/     regulator/       spidev/
firmware/   mem/          rfkill/          thermal/
gpio/       misc/         rtc/             tty/
graphics/   mmc_host/     scsi_device/     ubi/

Explore some. What do you find? In graphics you will see the 3 frame buffers supported by the processor. In sound you'll see the alsa sound devices.

Reading the Keyboard and Mouse

Try this:

$ cd /sys/class/input
$ ls -F
$ evtest event2
Hit ctrl-C to stop

Now move your mouse around, or try you keyboard. My mouse is plugged into the bottom left USB port and event2 responds to it. Where do your keyboard and mouse appear?

Flashing the LEDs

The Beagle has a user0 and user1 LED that you can control. Try this:

$ cd /usr/class/leds
$ ls -F
beagleboard::pmu_stat@  beagleboard::usr0@  beagleboard::usr1@

Here you see the directories for controlling each of the usr LEDs. By default, usr0 flashes a heartbeat patter and usr1 flashes when the micro SD card is accessed. Let's control usr0.

$ cd beagleboard::usr0
$ ls -F
brightness  device@  max_brightness  power/  subsystem@  trigger  uevent

See what's in brightness, max_brightness and trigger by using the cat command. For example:

$ cat trigger
none nand-disk mmc0 [heartbeat]

This shows trigger can have 4 values. The present value is heartbeat. Check the LED, is the beating? You can stop the heartbeat via:

$ echo none > trigger
$ cat trigger
[none] nand-disk mmc0 heartbeat 

Did it stop beating? You can now turn it on and off with:

$ echo 1 > brightness
$ echo 0 > brightness

Is it responding correctly?

Reading the User Button

The Beagle has a couple of push buttons. One reboots the whole board. Use with care. One is for you to use, it's to the right of the Reset button, between the two stacks of USB ports. BeagleUserButton.png

It's attached to gpio port 4. You can read it via:

$ cd /sys/class/gpio
$ ls -F

Notice there is no gpio4. Here's how you can create it, set it to an input port and read its value:

$ echo 4 > export
$ ls
$ cd gpio4
$ echo in > direction
$ cat value

Try holding down the switch and doing cat value again. Does the value change? There's a shell script called readgpiothat will repeatedly read the switch.

$ readgpio 4

Try pushing the switch. Does it work? Hit ctrl-C to stop. Look at readgpio. How does it work?

$ which readgpio
$ cp /usr/bin/readgpio ~
$ gedit ~/readgpio

Resources

  1. Here is wh1ts article on flashing an LED. It is referenced in the readgpio file that comes on the Beagle.
  2. This Make magazine article has a few more details.
  3. Here in a gpio reference for Linux in general. It includes sample 'C' code for flashing at 1 Hz.
  4. Here is a posting in the Beagle Google group about gpio.
  5. Here is some information about gpio from the kernel point of view.