EBC Exercise 11 gpio Polling and Interrupts

From eLinux.org
Revision as of 18:19, 21 July 2011 by Yoder (Talk | contribs) (Initial page)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


In the previous exercise (EBC Exercise 02 Flashing an LED) we saw how to interact with the general purpose I/O pins via the command shell and the sysfs files. These are rather easy ways to work with gpio; however they tend to be slow and require a lot of the CPU. In this exercise we'll explore using sysfs via C. We'll also see how using interrupts can greatly reduce the CPU usage and increase our max output speed.

gpio via C and sysfs

Since /sys/class/gpio/gpio130/value is just a file, we can read and write it from a C program just as easily as from the shell. Here is a nice example of how it's done. I've cleaned it up a bit and put a copy called togglegpio.c here. Get a copy and look it over. It' presently hardwired to use gpio130. Later you will get to make it work with any port.

  1. Compile and run it. Does the correct waveform appear on the oscilloscope?
  2. Use htop to measure the CPU usage.
  3. Try different periods. Make a chart of the input period vs. the measured period vs. CPU usage.

This program is really interrupt based. When the usleep command is run, the process suspends until the correct time has elapsed. Other processes are allowed to run. When the time is up the CPU is interrupted and our process is allowed to continue.

gpio via Interrupts

Assignments

Modify togglegpio.c to specify the port at run time

Modify togglegpio.c to take the gpio number. Clean things up a bit and use the structure of gpio-int-test.c.

Presently one parameter specifies both the on and off time. Modify the code so the on and off times are controlled separately.