EBC Exercise 10 Flashing an LED
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. This will call be done from the command line of the Beagle, so there is no need for the host computer.
Contents
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 most 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, open a terminal by selecting Applications:Accessories:Terminal
Then type:
$ 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 /sys/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 pattern 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.
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 readgpio that repeatedly reads 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
Reading a gpio pin with an Oscilloscope
You can easily access many of the gpio pins via the Main Expansion Header. Page 107 of the BeagleBoard-xM System Reference Manual has this figure.
Unfortunately the gpio pins don't appear here. It turns out the processor has more internal I/O lines than it has physical pins. Each physical pin can can be connected to up to 8 internal lines. BeagleBoardPinMux does a nice job of explaining it all. The big clue is here BeagleBoardPinMux#Beagle which references Table 20 on page 96 of the Rev C4 (not xm) System Reference Manual.
EXP | Processor | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
---|---|---|---|---|---|---|---|---|---|
1 | VIO_1V8 | ||||||||
2 | DC_5V | ||||||||
3 | AE3 | MMC2_DAT7 | * | * | * | GPIO_139 | * | * | Z |
4 | AB26 | UART2_CTS | McBSP3_DX | GPT9_PWMEVT | X | GPIO_144 | X | X | Z |
5 | F3 | MMC2_DAT6 | * | * | * | GPIO_138 | * | X | Z |
6 | AA25 | UART2_TX | McBSP3_CLKX | GPT11_PWMEVT | X | GPIO_146 | X | X | Z |
7 | AH3 | MMC2_DAT5 | * | * | * | GPIO_137 | * | X | Z |
8 | AE5 | McBSP3_FSX | UART2_RX | X | X | GPIO_143 | * | X | Z |
9 | AE4 | MMC2_DAT4 | * | X | * | GPIO_136 | X | X | Z |
10 | AB25 | UART2_RTS | McBSP3_DR | GPT10_PWMEVT | X | GPIO_145 | X | X | Z |
11 | AF4 | MMC2_DAT3 | McSPI3_CS0 | X | X | GPIO_135 | X | X | Z |
12 | V21 | McBSP1_DX | McSPI4_SIMO | McBSP3_DX | X | GPIO_158 | X | X | Z |
13 | AG4 | MMC2_DAT2 | McSPI3_CS1 | X | X | GPIO_134 | X | X | Z |
14 | W21 | McBSP1_CLKX | X | McBSP3_CLKX | X | GPIO_162 | X | X | Z |
15 | AH4 | MMC2_DAT1 | X | X | X | GPIO_133 | X | X | Z |
16 | K26 | McBSP1_FSX | McSPI4_CS0 | McBSP3_FSX | x | GPIO_161 | X | X | Z |
17 | AH5 | MMC2_DAT0 | McSPI3_SOMI | X | X | GPIO_132 | X | X | Z |
18 | U21 | McBSP1_DR | McSPI4_SOMI | McBSP3_DR | X | GPIO_159 | X | X | Z |
19 | AG5 | MMC2_CMD | McSPI3_SIMO | X | X | GPIO_131 | X | X | Z |
20 | Y21 | McBSP1_CLKR | McSPI4_CLK | X | X | GPIO_156 | X | X | Z |
21 | AE2 | MMC2_CLKO | McSPI3_CLK | X | X | GPIO_130 | X | X | Z |
22 | AA21 | McBSP1_FSR | X | * | Z | GPIO_157 | X | X | Z |
23 | AE15 | I2C2_SDA | X | X | X | GPIO_183 | X | X | Z |
24 | AF15 | I2C2_SCL | X | X | X | GPIO_168 | X | X | Z |
25 | 25 | REGEN | |||||||
26 | 26 | Nreset | |||||||
27 | 27 | GND | |||||||
28 | 28 | GND |
Note that gpio130 appears on pin 21 of the Expansion Header. Also note that pins 27 and 28 are ground. Attach your scope probe to these. Now, let's put a signal on the pin.
$ cd /sys/class/gpio $ ls -F export gpiochip128@ gpiochip192@ gpiochip64@ unexport gpiochip0@ gpiochip160@ gpiochip32@ gpiochip96@
Notice there is no folder for gpio130. Create it with:
$ echo 130 > export $ ls -F export gpiochip0@ gpiochip160@ gpiochip32@ gpiochip96@ gpio130@ gpiochip128@ gpiochip192@ gpiochip64@ unexport
Go to your home directory on your Beagle and get togglepgio.
$ cd $ wget http://www.rose-hulman.edu/~yoder/Beagle/exercises/togglegpio $ chmod +x togglegpio $ gedit togglegpio
Can you tell what the program is doing? Try running it:
$ togglegpio 130 0.05
The first argument tells which gpio port to toggle, the second tells how long to delay between toggling. In this example 0.05 s is 50 ms, which should give a period around 100ms. Measure the signal on an oscilloscope.
Assignment: gpio from the shell
Measuring a gpio pin on an Oscilloscope
Answer the following questions about gpio measurements.
- What's the min and max voltage?
- What period is it?
- How close is it to 100ms?
- Why do they differ?
- Run htop and see how much processor you are using.
- Try different values for the sleep time (2nd argument). What's the shortest period you can get? Make a table of the values you try and the corresponding period and processor usage.
- How stable is the period?
- Try launching something like mplayer. How stable is the period?
- Try cleaning up togglegpio and removing unneeded lines. Does it impact the period?
- togglegpio uses bash (first line in file). Try using sh. Is the period shorter?
- What's the shortest period you can get?
Toggling the LEDs
Modify togglegpio (call it toggleLED) to toggle the LEDs. Can you get the LED to appear to dim by changing the duty cycle of the toggling?
User Button to gpio 130
Write a shell script that reads the User Button and outputs it value on gpio pin 130.
Count the User Button Presses
Write a shell script that displays a count of the number of times the User Button has been pressed.
Copy gpio 130 to gpio 131
Write a shell script that copies the value of gpio pin 130 to gpio pin 131. How much CPU time does it take? What's the delay from the time the input changes until the output changes? How constant is the delay?
Resources
- Here is wh1ts article on flashing an LED. It is referenced in the readgpio file that comes on the Beagle.
- This Make magazine article has a few more details.
- Here in a gpio reference for Linux in general. It includes sample 'C' code for flashing at 1 Hz.
- Here is a posting in the Beagle Google group about gpio.
- Here is some information about gpio from the kernel point of view.
- Here is some info on a GPIO Event Driver
- Here is info on how to set edge to falling and poll() the pin.