EBC Exercise 10 Flashing an LED

From eLinux.org
Revision as of 09:44, 3 September 2018 by Yoder (talk | contribs) (Challenge: Removed)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

thumb‎ Embedded Linux Class by Mark A. Yoder

3.8 Kernel

This page is for the Bone (Black or White) running the 3.8 Kernel. See EBC_Exercise_10_Flashing_an_LED_-_xM_WhiteBone for the White Bone or xM running 3.2.

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 an LED on the Beagle and explore General Purpose I/O (gpio) along the way. These calls will be done from the command line of the Beagle, so there is no need for the host computer.

gpio via the Shell Command Line and sysfs

(Note: Draw the new diagrams with this.)

The easiest way to do general purpose I/O (gpio) on the Beagle is through a terminal window and a shell prompt. In Linux, almost 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 can easily use them.

Try this, open a terminal and type:

beagle$ cd /sys
beagle$ 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:

beagle$ cd /sys/class
beagle$ ls -F
backlight/  firmware/     lcd/       mtd/           scsi_disk/   ubi/
bdi/        gpio/         leds/      net/           scsi_host/   udc/
block/      graphics/     mbox/      power_supply/  sound/       uio/
bluetooth/  hwmon/        mdio_bus/  regulator/     spi_master/  usbmon/
bsg/        i2c-adapter/  mem/       rfkill/        spidev/      vc/
dma/        i2c-dev/      misc/      rtc/           thermal/     vtconsole/
drm/        input/        mmc_host/  scsi_device/   tty/         watchdog/

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

Flashing the user LEDs

The Beagle Black has four user LEDS, usr0 - usr3, that you can control. Try this:

beagle$ cd /sys/class/leds
beagle$ ls -1F

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

beagle$ cd beagleboard\:green\:usr0
beagle$ 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:

beagle$ cat trigger
none nand-disk mmc0 mmc1 timer oneshot [heartbeat] backlight gpio cpu0 default-on transient

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

beagle$ echo none > trigger
beagle$ cat trigger
[none] nand-disk mmc0 mmc1 timer oneshot heartbeat backlight gpio cpu0 default-on transient 

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

beagle$ echo 1 > brightness
beagle$ echo 0 > brightness

Is it responding correctly?

The Bone has more trigger options. Try:

beagle$ cat trigger
[none] nand-disk mmc0 mmc1 timer oneshot heartbeat backlight gpio cpu0 default-on transient 
beagle$ echo timer > trigger
beagle$ ls -F
brightness  delay_on  max_brightness  subsystem@  uevent
delay_off   device@   power/          trigger
beagle$ echo 100 > delay_on
beagle$ echo 900 > delay_off

What does this do?

Adding your own LED

It's not hard to use the gpio pins to control your own LED. All you need is an LED and a 220Ω resistor. Here's a picture of how it's wired. We are just doing the LED at the top of the breadboard for now.

Bone gpio.JPG BoneGPIO.png

So how do you know where to connect it? The BeagleBone System Reference Manual has the details. Figure 45 on page 74 shows:


There at two expansion headers, P8 and P9. Look at the bottom of the left header and you'll see it's labeled P9. Table 11 on page 78 shows the pinout for P9.


On the first photo above you can see that pin 1 (Ground) is wired to the - bus and pin 3 (3.3V) is wired to the + bus. The 220Ω resistor is wired to the - bus and the other end is attached to the negative lead of the LED. The positive lead is attached to pin 12 which, as shown in Table 11, is attached to gpio1_28. The gpio pins are in banks of 32 each, so to find the gpio number to use on the Beagle, use 1*32+28=60. Here's how you turn it on

beagle$ cd /sys/class/gpio
beagle$ ls -F
export  gpiochip0@  gpiochip32@  gpiochip64@  gpiochip96@  unexport

Presently no gpio pins are visible. You need to tell it which pin to export

beagle$ echo 60 > export
beagle$ ls -F
export  gpio60@  gpiochip0@  gpiochip32@  gpiochip64@  gpiochip96@  unexport

Notice gpio60 has appeared. All we need to do is tell it which direction and then turn it on.

beagle$ cd gpio60
beagle$ echo out > direction
beagle$ echo 1 > value

Your LED should be on! When you are done you can unexport the pin and it will disappear.

beagle$ cd ..
beagle$ echo 60 > unexport

Reading a switch

Now that you have an LED working, wiring in a switch is easy. The picture above shows a push button switch wired in at the bottom of the P9 header. Attach the + bus to one pole on the switch with a wire. The other end of the switch is attached to pin 42 which is gpio0_7.

Based on what you saw above, show how to read the switch.

Once you have the switch and LED working you can use the following scripts to play with them.

beagle$ cd ~/exercises/gpio
beagle$ ./togglegpio.sh 60 0.1

The LED should be blinking on and off.

beagle$ ./readgpio.sh 7

Push the pushbutton and see what happens.

How can this work without a pulldown resistor? It turns out the Bone has an internal pulldown (and up) resistor that can be software enabled. We'll discuss how when we cover device trees.

thumb‎ Embedded Linux Class by Mark A. Yoder